Ticket #37179: patch.diff
| File patch.diff, 3.0 KB (added by , 10 years ago) |
|---|
-
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 { 961 961 * @return array Index entity 962 962 */ 963 963 public function get_index( $request ) { 964 $routes = $this->get_data_for_routes( $this->get_routes(), $request['context'] ); 965 964 966 // General site data. 965 967 $available = array( 966 968 'name' => get_option( 'blogname' ), … … class WP_REST_Server { 969 971 'home' => home_url(), 970 972 'namespaces' => array_keys( $this->namespaces ), 971 973 'authentication' => array(), 972 'routes' => $ this->get_data_for_routes( $this->get_routes(), $request['context'] ),974 'routes' => $routes, 973 975 ); 974 976 975 977 $response = new WP_REST_Response( $available ); 976 978 977 979 $response->add_link( 'help', 'http://v2.wp-api.org/' ); 978 980 981 $this->add_index_links($response, $routes); 982 979 983 /** 980 984 * Filters the API root index data. 981 985 * … … class WP_REST_Server { 990 994 return apply_filters( 'rest_index', $response ); 991 995 } 992 996 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 993 1041 /** 994 1042 * Retrieves the index for a namespace. 995 1043 * … … class WP_REST_Server { 1019 1067 // Link to the root index. 1020 1068 $response->add_link( 'up', rest_url( '/' ) ); 1021 1069 1070 $this->add_index_links($response, $data['routes']); 1071 1022 1072 /** 1023 1073 * Filters the namespace index data. 1024 1074 * … … class WP_REST_Server { 1094 1144 'namespace' => '', 1095 1145 'methods' => array(), 1096 1146 'endpoints' => array(), 1147 'link_name' => null, 1097 1148 ); 1098 1149 1099 1150 if ( isset( $this->route_options[ $route ] ) ) { … … class WP_REST_Server { 1103 1154 $data['namespace'] = $options['namespace']; 1104 1155 } 1105 1156 1157 if ( isset( $options['link_name'] ) ) { 1158 $data['link_name'] = $options['link_name']; 1159 } 1160 1106 1161 if ( isset( $options['schema'] ) && 'help' === $context ) { 1107 1162 $data['schema'] = call_user_func( $options['schema'] ); 1108 1163 }