Make WordPress Core

Ticket #37179: patch.diff

File patch.diff, 3.0 KB (added by maennchen, 10 years ago)

Prototype Patch

  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git src/wp-includes/rest-api/class-wp-rest-server.php src/wp-includes/rest-api/class-wp-rest-server.php
    index 12e4086..b2e4740 100644
    class WP_REST_Server { 
    961961         * @return array Index entity
    962962         */
    963963        public function get_index( $request ) {
     964                $routes = $this->get_data_for_routes( $this->get_routes(), $request['context'] );
     965
    964966                // General site data.
    965967                $available = array(
    966968                        'name'           => get_option( 'blogname' ),
    class WP_REST_Server { 
    969971                        'home'           => home_url(),
    970972                        'namespaces'     => array_keys( $this->namespaces ),
    971973                        'authentication' => array(),
    972                         'routes'         => $this->get_data_for_routes( $this->get_routes(), $request['context'] ),
     974                        'routes'         => $routes,
    973975                );
    974976
    975977                $response = new WP_REST_Response( $available );
    976978
    977979                $response->add_link( 'help', 'http://v2.wp-api.org/' );
    978980
     981                $this->add_index_links($response, $routes);
     982
    979983                /**
    980984                 * Filters the API root index data.
    981985                 *
    class WP_REST_Server { 
    990994                return apply_filters( 'rest_index', $response );
    991995        }
    992996
     997        protected function add_index_links(WP_REST_Response $response, array $routes)
     998        {
     999                foreach($routes as $route_name => $route) {
     1000                        if($route['link_name'] === null) {
     1001                                continue;
     1002                        }
     1003
     1004                        $link = $this->get_rest_api_endpoint_link($route);
     1005
     1006                        $response->add_link( $route['link_name'], $link['href'], $link);
     1007                }
     1008        }
     1009
     1010        public function get_rest_api_endpoint_link(array $route, $method = 'GET')
     1011        {
     1012                $endpoints = $route['endpoints'];
     1013                $method_endpoint = null;
     1014                $link = array(
     1015                        'href' => $route['_links']['self']
     1016                );
     1017                foreach($endpoints as $endpoint) {
     1018                        if(in_array($method, $endpoint['methods'], true)) {
     1019                                $method_endpoint = $endpoint;
     1020                                break;
     1021                        }
     1022                }
     1023                if($method_endpoint === null) {
     1024                        return $link;
     1025                }
     1026                $query_params = array();
     1027                foreach(array_keys($method_endpoint['args']) as $arg_name) {
     1028                        $query_params[] = $arg_name;
     1029                }
     1030
     1031                if(count($query_params) === 0) {
     1032                        return $link;
     1033                }
     1034                $link['templated'] = true;
     1035                $link['href'] .= sprintf('{?%s}',
     1036                        implode(',', $query_params)
     1037                );
     1038                return $link;
     1039        }
     1040
    9931041        /**
    9941042         * Retrieves the index for a namespace.
    9951043         *
    class WP_REST_Server { 
    10191067                // Link to the root index.
    10201068                $response->add_link( 'up', rest_url( '/' ) );
    10211069
     1070                $this->add_index_links($response, $data['routes']);
     1071
    10221072                /**
    10231073                 * Filters the namespace index data.
    10241074                 *
    class WP_REST_Server { 
    10941144                        'namespace' => '',
    10951145                        'methods' => array(),
    10961146                        'endpoints' => array(),
     1147                        'link_name' => null,
    10971148                );
    10981149
    10991150                if ( isset( $this->route_options[ $route ] ) ) {
    class WP_REST_Server { 
    11031154                                $data['namespace'] = $options['namespace'];
    11041155                        }
    11051156
     1157                        if ( isset( $options['link_name'] ) ) {
     1158                                $data['link_name'] = $options['link_name'];
     1159                        }
     1160
    11061161                        if ( isset( $options['schema'] ) && 'help' === $context ) {
    11071162                                $data['schema'] = call_user_func( $options['schema'] );
    11081163                        }