Changeset 39021
- Timestamp:
- 10/30/2016 04:19:22 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
r38832 r39021 1 1 <?php 2 3 2 /** 3 * REST API: WP_REST_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core base controller for managing and interacting with REST API items. 12 * 13 * @since 4.7.0 14 */ 4 15 abstract class WP_REST_Controller { 5 16 … … 7 18 * The namespace of this controller's route. 8 19 * 20 * @since 4.7.0 21 * @access protected 9 22 * @var string 10 23 */ … … 14 27 * The base of this controller's route. 15 28 * 29 * @since 4.7.0 30 * @access protected 16 31 * @var string 17 32 */ … … 19 34 20 35 /** 21 * Register the routes for the objects of the controller. 36 * Registers the routes for the objects of the controller. 37 * 38 * @since 4.7.0 39 * @access public 22 40 */ 23 41 public function register_routes() { 24 _doing_it_wrong( 'WP_REST_Controller::register_routes', __( 'The register_routes() method must be overridden' ), 'WPAPI-2.0' ); 25 } 26 27 /** 28 * Check if a given request has access to get items. 29 * 30 * @param WP_REST_Request $request Full data about the request. 31 * @return WP_Error|boolean 42 _doing_it_wrong( 'WP_REST_Controller::register_routes', __( 'The register_routes() method must be overridden' ), '4.7' ); 43 } 44 45 /** 46 * Checks if a given request has access to get items. 47 * 48 * @since 4.7.0 49 * @access public 50 * 51 * @param WP_REST_Request $request Full data about the request. 52 * @return WP_Error|bool True if the request has read access, WP_Error object otherwise. 32 53 */ 33 54 public function get_items_permissions_check( $request ) { … … 36 57 37 58 /** 38 * Get a collection of items. 39 * 40 * @param WP_REST_Request $request Full data about the request. 41 * @return WP_Error|WP_REST_Response 59 * Retrieves a collection of items. 60 * 61 * @since 4.7.0 62 * @access public 63 * 64 * @param WP_REST_Request $request Full data about the request. 65 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 42 66 */ 43 67 public function get_items( $request ) { … … 46 70 47 71 /** 48 * Check if a given request has access to get a specific item. 49 * 50 * @param WP_REST_Request $request Full data about the request. 51 * @return WP_Error|boolean 72 * Checks if a given request has access to get a specific item. 73 * 74 * @since 4.7.0 75 * @access public 76 * 77 * @param WP_REST_Request $request Full data about the request. 78 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. 52 79 */ 53 80 public function get_item_permissions_check( $request ) { … … 56 83 57 84 /** 58 * Get one item from the collection. 59 * 60 * @param WP_REST_Request $request Full data about the request. 61 * @return WP_Error|WP_REST_Response 85 * Retrieves one item from the collection. 86 * 87 * @since 4.7.0 88 * @access public 89 * 90 * @param WP_REST_Request $request Full data about the request. 91 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 62 92 */ 63 93 public function get_item( $request ) { … … 66 96 67 97 /** 68 * Check if a given request has access to create items. 69 * 70 * @param WP_REST_Request $request Full data about the request. 71 * @return WP_Error|boolean 98 * Checks if a given request has access to create items. 99 * 100 * @since 4.7.0 101 * @access public 102 * 103 * @param WP_REST_Request $request Full data about the request. 104 * @return WP_Error|bool True if the request has access to create items, WP_Error object otherwise. 72 105 */ 73 106 public function create_item_permissions_check( $request ) { … … 76 109 77 110 /** 78 * Create one item from the collection. 79 * 80 * @param WP_REST_Request $request Full data about the request. 81 * @return WP_Error|WP_REST_Response 111 * Creates one item from the collection. 112 * 113 * @since 4.7.0 114 * @access public 115 * 116 * @param WP_REST_Request $request Full data about the request. 117 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 82 118 */ 83 119 public function create_item( $request ) { … … 86 122 87 123 /** 88 * Check if a given request has access to update a specific item. 89 * 90 * @param WP_REST_Request $request Full data about the request. 91 * @return WP_Error|boolean 124 * Checks if a given request has access to update a specific item. 125 * 126 * @since 4.7.0 127 * @access public 128 * 129 * @param WP_REST_Request $request Full data about the request. 130 * @return WP_Error|bool True if the request has access to update the item, WP_Error object otherwise. 92 131 */ 93 132 public function update_item_permissions_check( $request ) { … … 96 135 97 136 /** 98 * Update one item from the collection. 99 * 100 * @param WP_REST_Request $request Full data about the request. 101 * @return WP_Error|WP_REST_Response 137 * Updates one item from the collection. 138 * 139 * @since 4.7.0 140 * @access public 141 * 142 * @param WP_REST_Request $request Full data about the request. 143 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 102 144 */ 103 145 public function update_item( $request ) { … … 106 148 107 149 /** 108 * Check if a given request has access to delete a specific item. 109 * 110 * @param WP_REST_Request $request Full data about the request. 111 * @return WP_Error|boolean 150 * Checks if a given request has access to delete a specific item. 151 * 152 * @since 4.7.0 153 * @access public 154 * 155 * @param WP_REST_Request $request Full data about the request. 156 * @return WP_Error|bool True if the request has access to delete the item, WP_Error object otherwise. 112 157 */ 113 158 public function delete_item_permissions_check( $request ) { … … 116 161 117 162 /** 118 * Delete one item from the collection. 119 * 120 * @param WP_REST_Request $request Full data about the request. 121 * @return WP_Error|WP_REST_Response 163 * Deletes one item from the collection. 164 * 165 * @since 4.7.0 166 * @access public 167 * 168 * @param WP_REST_Request $request Full data about the request. 169 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 122 170 */ 123 171 public function delete_item( $request ) { … … 126 174 127 175 /** 128 * Prepare the item for create or update operation. 176 * Prepares one item for create or update operation. 177 * 178 * @since 4.7.0 179 * @access public 129 180 * 130 181 * @param WP_REST_Request $request Request object. 131 * @return WP_Error|object $prepared_item182 * @return WP_Error|object The prepared item, or WP_Error object on failure. 132 183 */ 133 184 protected function prepare_item_for_database( $request ) { … … 136 187 137 188 /** 138 * Prepare the item for the REST response. 139 * 140 * @param mixed $item WordPress representation of the item. 189 * Prepares the item for the REST response. 190 * 191 * @since 4.7.0 192 * @access public 193 * 194 * @param mixed $item WordPress representation of the item. 141 195 * @param WP_REST_Request $request Request object. 142 * @return WP_Error|WP_REST_Response $response196 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 143 197 */ 144 198 public function prepare_item_for_response( $item, $request ) { … … 147 201 148 202 /** 149 * Prepare a response for inserting into a collection. 203 * Prepares a response for insertion into a collection. 204 * 205 * @since 4.7.0 206 * @access public 150 207 * 151 208 * @param WP_REST_Response $response Response object. 152 * @return array Response data, ready for insertion into collection data.209 * @return array|mixed Response data, ready for insertion into collection data. 153 210 */ 154 211 public function prepare_response_for_collection( $response ) { … … 157 214 } 158 215 159 $data = (array) $response->get_data();216 $data = (array) $response->get_data(); 160 217 $server = rest_get_server(); 161 218 … … 174 231 175 232 /** 176 * Filter a response based on the context defined in the schema. 177 * 178 * @param array $data 179 * @param string $context 180 * @return array 233 * Filters a response based on the context defined in the schema. 234 * 235 * @since 4.7.0 236 * @access public 237 * 238 * @param array $data Response data to fiter. 239 * @param string $context Context defined in the schema. 240 * @return array Filtered response. 181 241 */ 182 242 public function filter_response_by_context( $data, $context ) { 183 243 184 244 $schema = $this->get_item_schema(); 245 185 246 foreach ( $data as $key => $value ) { 186 247 if ( empty( $schema['properties'][ $key ] ) || empty( $schema['properties'][ $key ]['context'] ) ) { … … 198 259 continue; 199 260 } 261 200 262 if ( ! in_array( $context, $details['context'], true ) ) { 201 263 if ( isset( $data[ $key ][ $attribute ] ) ) { … … 211 273 212 274 /** 213 * Get the item's schema, conforming to JSON Schema. 214 * 215 * @return array 275 * Retrieves the item's schema, conforming to JSON Schema. 276 * 277 * @since 4.7.0 278 * @access public 279 * 280 * @return array Item schema data. 216 281 */ 217 282 public function get_item_schema() { … … 220 285 221 286 /** 222 * Get the item's schema for display / public consumption purposes. 223 * 224 * @return array 287 * Retrieves the item's schema for display / public consumption purposes. 288 * 289 * @since 4.7.0 290 * @access public 291 * 292 * @return array Public item schema data. 225 293 */ 226 294 public function get_public_item_schema() { … … 236 304 237 305 /** 238 * Get the query params for collections. 239 * 240 * @return array 306 * Retrieves the query params for the collections. 307 * 308 * @since 4.7.0 309 * @access public 310 * 311 * @return array Query parameters for the collection. 241 312 */ 242 313 public function get_collection_params() { … … 270 341 271 342 /** 272 * Get the magical context param. 273 * 274 * Ensures consistent description between endpoints, and populates enum from schema. 275 * 276 * @param array $args 277 * @return array 343 * Retrieves the magical context param. 344 * 345 * Ensures consistent descriptions between endpoints, and populates enum from schema. 346 * 347 * @since 4.7.0 348 * @access public 349 * 350 * @param array $args Optional. Additional arguments for context parameter. Default empty array. 351 * @return array Context parameter details. 278 352 */ 279 353 public function get_context_param( $args = array() ) { … … 284 358 'validate_callback' => 'rest_validate_request_arg', 285 359 ); 360 286 361 $schema = $this->get_item_schema(); 362 287 363 if ( empty( $schema['properties'] ) ) { 288 364 return array_merge( $param_details, $args ); 289 365 } 366 290 367 $contexts = array(); 368 291 369 foreach ( $schema['properties'] as $attributes ) { 292 370 if ( ! empty( $attributes['context'] ) ) { … … 294 372 } 295 373 } 374 296 375 if ( ! empty( $contexts ) ) { 297 376 $param_details['enum'] = array_unique( $contexts ); 298 377 rsort( $param_details['enum'] ); 299 378 } 379 300 380 return array_merge( $param_details, $args ); 301 381 } 302 382 303 383 /** 304 * Add the values from additional fields to a data object. 305 * 306 * @param array $object 307 * @param WP_REST_Request $request 308 * @return array modified object with additional fields. 384 * Adds the values from additional fields to a data object. 385 * 386 * @since 4.7.0 387 * @access protected 388 * 389 * @param array $object Data object. 390 * @param WP_REST_Request $request Full details about the request. 391 * @return array Modified data object with additional fields. 309 392 */ 310 393 protected function add_additional_fields_to_object( $object, $request ) { … … 325 408 326 409 /** 327 * Update the values of additional fields added to a data object. 328 * 329 * @param array $object 330 * @param WP_REST_Request $request 410 * Updates the values of additional fields added to a data object. 411 * 412 * @since 4.7.0 413 * @access protected 414 * 415 * @param array $object Data Object. 416 * @param WP_REST_Request $request Full details about the request. 331 417 * @return bool|WP_Error True on success, WP_Error object if a field cannot be updated. 332 418 */ … … 345 431 346 432 $result = call_user_func( $field_options['update_callback'], $request[ $field_name ], $object, $field_name, $request, $this->get_object_type() ); 433 347 434 if ( is_wp_error( $result ) ) { 348 435 return $result; … … 354 441 355 442 /** 356 * Add the schema from additional fields to anschema array.443 * Adds the schema from additional fields to a schema array. 357 444 * 358 445 * The type of object is inferred from the passed schema. 446 * 447 * @since 4.7.0 448 * @access protected 359 449 * 360 450 * @param array $schema Schema array. … … 366 456 } 367 457 368 /** 369 * Can't use $this->get_object_type otherwise we cause an inf loop. 370 */ 458 // Can't use $this->get_object_type otherwise we cause an inf loop. 371 459 $object_type = $schema['title']; 372 460 … … 385 473 386 474 /** 387 * Get all the registered additional fields for a given object-type. 388 * 389 * @param string $object_type 390 * @return array 475 * Retrieves all of the registered additional fields for a given object-type. 476 * 477 * @since 4.7.0 478 * @access protected 479 * 480 * @param string $object_type Optional. The object type. 481 * @return array Registered additional fields (if any), empty array if none or if the object type could 482 * not be inferred. 391 483 */ 392 484 protected function get_additional_fields( $object_type = null ) { … … 410 502 411 503 /** 412 * Get the object type this controller is responsible for managing. 413 * 414 * @return string 504 * Retrieves the object type this controller is responsible for managing. 505 * 506 * @since 4.7.0 507 * @access protected 508 * 509 * @return string Object type for the controller. 415 510 */ 416 511 protected function get_object_type() { … … 425 520 426 521 /** 427 * Get an array of endpoint arguments from the item schema for the controller. 428 * 429 * @param string $method HTTP method of the request. The arguments 430 * for `CREATABLE` requests are checked for required 431 * values and may fall-back to a given default, this 432 * is not done on `EDITABLE` requests. Default is 433 * WP_REST_Server::CREATABLE. 434 * @return array $endpoint_args 522 * Retrieves an array of endpoint arguments from the item schema for the controller. 523 * 524 * @since 4.7.0 525 * @access public 526 * 527 * @param string $method Optional. HTTP method of the request. The arguments for `CREATABLE` requests are 528 * checked for required values and may fall-back to a given default, this is not done 529 * on `EDITABLE` requests. Default WP_REST_Server::CREATABLE. 530 * @return array Endpoint arguments. 435 531 */ 436 532 public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { 437 533 438 $schema 439 $schema_properties 440 $endpoint_args = array();534 $schema = $this->get_item_schema(); 535 $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); 536 $endpoint_args = array(); 441 537 442 538 foreach ( $schema_properties as $field_id => $params ) { … … 493 589 * post that is used in the REST API. 494 590 * 591 * @since 4.7.0 592 * @access public 593 * 495 594 * @see get_post() 496 595 * @global WP_Query $wp_query 497 596 * 498 * @param int|WP_Post $post Post ID or post object. Defaults to global $post.499 * @return WP_Post|null A `WP_Post` object when successful.597 * @param int|WP_Post $post Post ID or object. Defaults to global `$post` object. 598 * @return WP_Post|null A WP_Post object when successful, otherwise null. 500 599 */ 501 600 public function get_post( $post ) { … … 503 602 504 603 /** 505 * Filter the post.604 * Filters the post in the context of a REST request. 506 605 * 507 * Allows plugins to filter the post object as returned by `\WP_REST_Controller::get_post()`.606 * Allows plugins to filter the post object as returned by WP_REST_Controller::get_post(). 508 607 * 509 * @param WP_Post|null $post_obj The post object as returned by `get_post()`. 510 * @param int|WP_Post $post The original value used to obtain the post object. 608 * @since 4.7.0 609 * 610 * @param WP_Post|null $post_obj The post object as returned by get_post(). 611 * @param int|WP_Post $post The original value used to obtain the post object. 511 612 */ 512 613 $post = apply_filters( 'rest_the_post', $post_obj, $post ); … … 516 617 517 618 /** 518 * Sanitize the slug value. 519 * 520 * @internal We can't use {@see sanitize_title} directly, as the second 619 * Sanitizes the slug value. 620 * 621 * @since 4.7.0 622 * @access public 623 * 624 * @internal We can't use sanitize_title() directly, as the second 521 625 * parameter is the fallback title, which would end up being set to the 522 626 * request object. 627 * 523 628 * @see https://github.com/WP-API/WP-API/issues/1585 524 629 *
Note: See TracChangeset
for help on using the changeset viewer.