Make WordPress Core

Ticket #49116: 49116.diff

File 49116.diff, 3.7 KB (added by dshanske, 5 years ago)
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index fd5d9f32f2..cc3e9828a2 100644
    a b add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); 
    284284add_action( 'wp_head', 'wp_resource_hints', 2 );
    285285add_action( 'wp_head', 'feed_links', 2 );
    286286add_action( 'wp_head', 'feed_links_extra', 3 );
     287add_action( 'wp_head', 'rest_add_resource_link', 4 );
    287288add_action( 'wp_head', 'rsd_link' );
    288289add_action( 'wp_head', 'wlwmanifest_link' );
    289290add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index dc55af08f3..a0e952a0cb 100644
    a b function rest_filter_response_by_context( $data, $schema, $context ) { 
    17761776
    17771777        return $data;
    17781778}
     1779
     1780
     1781/**
     1782 * Outputs a header link to a REST resource
     1783 *
     1784 * @since 5.5.0
     1785 *
     1786 * @param string       $route Route.
     1787 */
     1788function rest_head_link( $route ) {
     1789        if ( ! empty( $route ) ) {
     1790                printf( '<link rel="alternate" type="%s" href="%s" />', esc_attr( 'application/json' ), esc_url( rest_url( $route ) ) );
     1791        }
     1792}
     1793
     1794/**
     1795 * Return the rest route for a post.
     1796 *
     1797 * @since 5.5.0
     1798 *
     1799 * @param int|WP_Post       $post Post ID or Object.
     1800 * @return string           Route.
     1801 */
     1802function rest_get_route_for_post( $post = null ) {
     1803        $post = get_post( $post );
     1804
     1805        $post_type = get_post_type_object( $post->post_type );
     1806        if ( ! $post_type ) {
     1807                return '';
     1808        }
     1809
     1810        $route = '';
     1811       
     1812        // The only two controllers that we can detect are the Attachments and Posts controllers.
     1813        if ( ! empty( $post_type->rest_controller_class ) && in_array( $post_type->rest_controller_class, array( 'WP_REST_Attachments_Controller', 'WP_REST_Posts_Controller' ), true ) ) {
     1814                $namespace = 'wp/v2';
     1815                $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
     1816                $route = sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
     1817        }
     1818
     1819        /**
     1820         * Filters the route based on information in the post object.
     1821         *
     1822         * @since 5.5.0
     1823         *
     1824         * @param string $route Route.
     1825         * @param WP_Post $post Post Object.
     1826         */
     1827        return apply_filters( 'rest_route_for_post', $route, $post );
     1828}
     1829
     1830/**
     1831 * Return the rest route for a term.
     1832 *
     1833 * @since 5.5.0
     1834 *
     1835 * @param WP_Term       $term Term.
     1836 * @return string           Route.
     1837 */
     1838function rest_get_route_for_term( $term ) {
     1839        if ( ! $term instanceOf WP_Term ) {
     1840                return '';
     1841        }
     1842       
     1843        $taxonomy = get_taxonomy( $term->taxonomy );
     1844        if ( ! $taxonomy ) {
     1845                return '';
     1846        }
     1847
     1848        $route = '';
     1849        error_log( wp_json_encode( $taxonomy ) );
     1850        // The only controller that works is the Terms controller.
     1851        if ( ! empty( $taxonomy->rest_controller_class ) && 'WP_REST_Terms_Controller' === $taxonomy->rest_controller_class ) {
     1852                $namespace = 'wp/v2';
     1853                $rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxnomomy->name;
     1854                $route = sprintf( '%s/%s/%d', $namespace, $rest_base, $term->term_id );
     1855        }
     1856
     1857        /**
     1858         * Filters the route based on information in the term object.
     1859         *
     1860         * @since 5.5.0
     1861         *
     1862         * @param string $route Route.
     1863         * @param WP_Term $term Term Object.
     1864         */
     1865        return apply_filters( 'rest_route_for_term', $route, $term );
     1866}
     1867
     1868/**
     1869 * Add a link to the current REST API resource.
     1870 *
     1871 * @since 5.5.0
     1872 *
     1873 */
     1874function rest_add_resource_link() {
     1875        if ( is_attachment() || is_singular() ) {
     1876                rest_head_link( rest_get_route_for_post( get_post() ) );
     1877        } elseif ( is_category() || is_tag() || is_tax() ) {
     1878                rest_head_link( rest_get_route_for_term( get_queried_object() ) );
     1879        } elseif ( is_author() ) {
     1880                rest_head_link( 'wp/v2/users/' . get_the_author_meta( 'ID' )  );
     1881        }
     1882}