| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Revisions_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to access revisions via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Controller 16 */ 17 class WP_REST_Revisions_Controller extends WP_REST_Controller { 18 19 /** 20 * Parent post type. 21 * 22 * @since 4.7.0 23 * @var string 24 */ 25 private $parent_post_type; 26 27 /** 28 * Instance of a revision meta fields object. 29 * 30 * @since 6.4.0 31 * @var WP_REST_Post_Meta_Fields 32 */ 33 protected $meta; 34 35 /** 36 * Parent controller. 37 * 38 * @since 4.7.0 39 * @var WP_REST_Controller 40 */ 41 private $parent_controller; 42 43 /** 44 * The base of the parent controller's route. 45 * 46 * @since 4.7.0 47 * @var string 48 */ 49 private $parent_base; 50 51 /** 52 * Constructor. 53 * 54 * @since 4.7.0 55 * 56 * @param string $parent_post_type Post type of the parent. 57 */ 58 public function __construct( $parent_post_type ) { 59 $this->parent_post_type = $parent_post_type; 60 $post_type_object = get_post_type_object( $parent_post_type ); 61 $parent_controller = $post_type_object->get_rest_controller(); 62 63 if ( ! $parent_controller ) { 64 $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); 65 } 66 67 $this->parent_controller = $parent_controller; 68 $this->rest_base = 'revisions'; 69 $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 70 $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; 71 $this->meta = new WP_REST_Post_Meta_Fields( $parent_post_type ); 72 } 73 74 /** 75 * Registers the routes for revisions based on post types supporting revisions. 76 * 77 * @since 4.7.0 78 * 79 * @see register_rest_route() 80 */ 81 public function register_routes() { 82 83 register_rest_route( 84 $this->namespace, 85 '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base, 86 array( 87 'args' => array( 88 'parent' => array( 89 'description' => __( 'The ID for the parent of the revision.' ), 90 'type' => 'integer', 91 ), 92 ), 93 array( 94 'methods' => WP_REST_Server::READABLE, 95 'callback' => array( $this, 'get_items' ), 96 'permission_callback' => array( $this, 'get_items_permissions_check' ), 97 'args' => $this->get_collection_params(), 98 ), 99 'schema' => array( $this, 'get_public_item_schema' ), 100 ) 101 ); 102 103 register_rest_route( 104 $this->namespace, 105 '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', 106 array( 107 'args' => array( 108 'parent' => array( 109 'description' => __( 'The ID for the parent of the revision.' ), 110 'type' => 'integer', 111 ), 112 'id' => array( 113 'description' => __( 'Unique identifier for the revision.' ), 114 'type' => 'integer', 115 ), 116 ), 117 array( 118 'methods' => WP_REST_Server::READABLE, 119 'callback' => array( $this, 'get_item' ), 120 'permission_callback' => array( $this, 'get_item_permissions_check' ), 121 'args' => array( 122 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 123 ), 124 ), 125 array( 126 'methods' => WP_REST_Server::DELETABLE, 127 'callback' => array( $this, 'delete_item' ), 128 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 129 'args' => array( 130 'force' => array( 131 'type' => 'boolean', 132 'default' => false, 133 'description' => __( 'Required to be true, as revisions do not support trashing.' ), 134 ), 135 ), 136 ), 137 'schema' => array( $this, 'get_public_item_schema' ), 138 ) 139 ); 140 } 141 142 /** 143 * Get the parent post, if the ID is valid. 144 * 145 * @since 4.7.2 146 * 147 * @param int $parent_post_id Supplied ID. 148 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. 149 */ 150 protected function get_parent( $parent_post_id ) { 151 $error = new WP_Error( 152 'rest_post_invalid_parent', 153 __( 'Invalid post parent ID.' ), 154 array( 'status' => 404 ) 155 ); 156 157 if ( (int) $parent_post_id <= 0 ) { 158 return $error; 159 } 160 161 $parent_post = get_post( (int) $parent_post_id ); 162 163 if ( empty( $parent_post ) || empty( $parent_post->ID ) 164 || $this->parent_post_type !== $parent_post->post_type 165 ) { 166 return $error; 167 } 168 169 return $parent_post; 170 } 171 172 /** 173 * Checks if a given request has access to get revisions. 174 * 175 * @since 4.7.0 176 * 177 * @param WP_REST_Request $request Full details about the request. 178 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 179 */ 180 public function get_items_permissions_check( $request ) { 181 $parent = $this->get_parent( $request['parent'] ); 182 if ( is_wp_error( $parent ) ) { 183 return $parent; 184 } 185 186 if ( ! current_user_can( 'edit_post', $parent->ID ) ) { 187 return new WP_Error( 188 'rest_cannot_read', 189 __( 'Sorry, you are not allowed to view revisions of this post.' ), 190 array( 'status' => rest_authorization_required_code() ) 191 ); 192 } 193 194 return true; 195 } 196 197 /** 198 * Get the revision, if the ID is valid. 199 * 200 * @since 4.7.2 201 * 202 * @param int $id Supplied ID. 203 * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. 204 */ 205 protected function get_revision( $id ) { 206 $error = new WP_Error( 207 'rest_post_invalid_id', 208 __( 'Invalid revision ID.' ), 209 array( 'status' => 404 ) 210 ); 211 212 if ( (int) $id <= 0 ) { 213 return $error; 214 } 215 216 $revision = get_post( (int) $id ); 217 if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) { 218 return $error; 219 } 220 221 return $revision; 222 } 223 224 /** 225 * Gets a collection of revisions. 226 * 227 * @since 4.7.0 228 * 229 * @see WP_REST_Posts_Controller::get_items() 230 * 231 * @param WP_REST_Request $request Full details about the request. 232 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 233 */ 234 public function get_items( $request ) { 235 $parent = $this->get_parent( $request['parent'] ); 236 if ( is_wp_error( $parent ) ) { 237 return $parent; 238 } 239 240 // Ensure a search string is set in case the orderby is set to 'relevance'. 241 if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { 242 return new WP_Error( 243 'rest_no_search_term_defined', 244 __( 'You need to define a search term to order by relevance.' ), 245 array( 'status' => 400 ) 246 ); 247 } 248 249 // Ensure an include parameter is set in case the orderby is set to 'include'. 250 if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { 251 return new WP_Error( 252 'rest_orderby_include_missing_include', 253 __( 'You need to define an include parameter to order by include.' ), 254 array( 'status' => 400 ) 255 ); 256 } 257 258 $is_head_request = $request->is_method( 'HEAD' ); 259 260 if ( wp_revisions_enabled( $parent ) ) { 261 $registered = $this->get_collection_params(); 262 $args = array( 263 'post_parent' => $parent->ID, 264 'post_type' => 'revision', 265 'post_status' => 'inherit', 266 'posts_per_page' => -1, 267 'orderby' => 'date ID', 268 'order' => 'DESC', 269 'suppress_filters' => true, 270 ); 271 272 $parameter_mappings = array( 273 'exclude' => 'post__not_in', 274 'include' => 'post__in', 275 'offset' => 'offset', 276 'order' => 'order', 277 'orderby' => 'orderby', 278 'page' => 'paged', 279 'per_page' => 'posts_per_page', 280 'search' => 's', 281 ); 282 283 foreach ( $parameter_mappings as $api_param => $wp_param ) { 284 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { 285 $args[ $wp_param ] = $request[ $api_param ]; 286 } 287 } 288 289 // For backward-compatibility, 'date' needs to resolve to 'date ID'. 290 if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) { 291 $args['orderby'] = 'date ID'; 292 } 293 294 if ( $is_head_request ) { 295 // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination. 296 $args['fields'] = 'ids'; 297 // Disable priming post meta for HEAD requests to improve performance. 298 $args['update_post_term_cache'] = false; 299 $args['update_post_meta_cache'] = false; 300 } 301 302 /** 303 * Filters WP_Query arguments when querying revisions via the REST API. 304 * 305 * Serves the same purpose as the {@see 'rest_{$this->post_type}_query'} filter in 306 * WP_REST_Posts_Controller, but for the standalone WP_REST_Revisions_Controller. 307 * 308 * @since 5.0.0 309 * 310 * @param array $args Array of arguments for WP_Query. 311 * @param WP_REST_Request $request The REST API request. 312 */ 313 $args = apply_filters( 'rest_revision_query', $args, $request ); 314 if ( ! is_array( $args ) ) { 315 $args = array(); 316 } 317 $query_args = $this->prepare_items_query( $args, $request ); 318 319 $revisions_query = new WP_Query(); 320 $revisions = $revisions_query->query( $query_args ); 321 $offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0; 322 $page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0; 323 $total_revisions = $revisions_query->found_posts; 324 325 if ( $total_revisions < 1 ) { 326 // Out-of-bounds, run the query without pagination/offset to get the total count. 327 unset( $query_args['paged'], $query_args['offset'] ); 328 329 $count_query = new WP_Query(); 330 $query_args['fields'] = 'ids'; 331 $query_args['posts_per_page'] = 1; 332 $query_args['update_post_meta_cache'] = false; 333 $query_args['update_post_term_cache'] = false; 334 335 $count_query->query( $query_args ); 336 $total_revisions = $count_query->found_posts; 337 } 338 339 if ( $revisions_query->query_vars['posts_per_page'] > 0 ) { 340 $max_pages = (int) ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] ); 341 } else { 342 $max_pages = $total_revisions > 0 ? 1 : 0; 343 } 344 345 if ( $total_revisions > 0 ) { 346 if ( $offset >= $total_revisions ) { 347 return new WP_Error( 348 'rest_revision_invalid_offset_number', 349 __( 'The offset number requested is larger than or equal to the number of available revisions.' ), 350 array( 'status' => 400 ) 351 ); 352 } elseif ( ! $offset && $page > $max_pages ) { 353 return new WP_Error( 354 'rest_revision_invalid_page_number', 355 __( 'The page number requested is larger than the number of pages available.' ), 356 array( 'status' => 400 ) 357 ); 358 } 359 } 360 } else { 361 $revisions = array(); 362 $total_revisions = 0; 363 $max_pages = 0; 364 $page = (int) $request['page']; 365 } 366 367 if ( ! $is_head_request ) { 368 $response = array(); 369 370 foreach ( $revisions as $revision ) { 371 $data = $this->prepare_item_for_response( $revision, $request ); 372 $response[] = $this->prepare_response_for_collection( $data ); 373 } 374 375 $response = rest_ensure_response( $response ); 376 } else { 377 $response = new WP_REST_Response( array() ); 378 } 379 380 $response->header( 'X-WP-Total', (int) $total_revisions ); 381 $response->header( 'X-WP-TotalPages', (int) $max_pages ); 382 383 $request_params = $request->get_query_params(); 384 $base_path = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ); 385 $base = add_query_arg( urlencode_deep( $request_params ), $base_path ); 386 387 if ( $page > 1 ) { 388 $prev_page = $page - 1; 389 390 if ( $prev_page > $max_pages ) { 391 $prev_page = $max_pages; 392 } 393 394 $prev_link = add_query_arg( 'page', $prev_page, $base ); 395 $response->link_header( 'prev', $prev_link ); 396 } 397 if ( $max_pages > $page ) { 398 $next_page = $page + 1; 399 $next_link = add_query_arg( 'page', $next_page, $base ); 400 401 $response->link_header( 'next', $next_link ); 402 } 403 404 return $response; 405 } 406 407 /** 408 * Checks if a given request has access to get a specific revision. 409 * 410 * @since 4.7.0 411 * 412 * @param WP_REST_Request $request Full details about the request. 413 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. 414 */ 415 public function get_item_permissions_check( $request ) { 416 return $this->get_items_permissions_check( $request ); 417 } 418 419 /** 420 * Retrieves one revision from the collection. 421 * 422 * @since 4.7.0 423 * @since 6.5.0 Added a condition to check that parent id matches revision parent id. 424 * 425 * @param WP_REST_Request $request Full details about the request. 426 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 427 */ 428 public function get_item( $request ) { 429 $parent = $this->get_parent( $request['parent'] ); 430 if ( is_wp_error( $parent ) ) { 431 return $parent; 432 } 433 434 $revision = $this->get_revision( $request['id'] ); 435 if ( is_wp_error( $revision ) ) { 436 return $revision; 437 } 438 439 if ( (int) $parent->ID !== (int) $revision->post_parent ) { 440 return new WP_Error( 441 'rest_revision_parent_id_mismatch', 442 /* translators: %d: A post id. */ 443 sprintf( __( 'The revision does not belong to the specified parent with id of "%d"' ), $parent->ID ), 444 array( 'status' => 404 ) 445 ); 446 } 447 448 $response = $this->prepare_item_for_response( $revision, $request ); 449 return rest_ensure_response( $response ); 450 } 451 452 /** 453 * Checks if a given request has access to delete a revision. 454 * 455 * @since 4.7.0 456 * 457 * @param WP_REST_Request $request Full details about the request. 458 * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. 459 */ 460 public function delete_item_permissions_check( $request ) { 461 $parent = $this->get_parent( $request['parent'] ); 462 if ( is_wp_error( $parent ) ) { 463 return $parent; 464 } 465 466 if ( ! current_user_can( 'delete_post', $parent->ID ) ) { 467 return new WP_Error( 468 'rest_cannot_delete', 469 __( 'Sorry, you are not allowed to delete revisions of this post.' ), 470 array( 'status' => rest_authorization_required_code() ) 471 ); 472 } 473 474 $revision = $this->get_revision( $request['id'] ); 475 if ( is_wp_error( $revision ) ) { 476 return $revision; 477 } 478 479 $response = $this->get_items_permissions_check( $request ); 480 if ( ! $response || is_wp_error( $response ) ) { 481 return $response; 482 } 483 484 if ( ! current_user_can( 'delete_post', $revision->ID ) ) { 485 return new WP_Error( 486 'rest_cannot_delete', 487 __( 'Sorry, you are not allowed to delete this revision.' ), 488 array( 'status' => rest_authorization_required_code() ) 489 ); 490 } 491 492 return true; 493 } 494 495 /** 496 * Deletes a single revision. 497 * 498 * @since 4.7.0 499 * 500 * @param WP_REST_Request $request Full details about the request. 501 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 502 */ 503 public function delete_item( $request ) { 504 $revision = $this->get_revision( $request['id'] ); 505 if ( is_wp_error( $revision ) ) { 506 return $revision; 507 } 508 509 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 510 511 // We don't support trashing for revisions. 512 if ( ! $force ) { 513 return new WP_Error( 514 'rest_trash_not_supported', 515 /* translators: %s: force=true */ 516 sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ), 517 array( 'status' => 501 ) 518 ); 519 } 520 521 $previous = $this->prepare_item_for_response( $revision, $request ); 522 523 $result = wp_delete_post( $request['id'], true ); 524 525 /** 526 * Fires after a revision is deleted via the REST API. 527 * 528 * @since 4.7.0 529 * 530 * @param WP_Post|false|null $result The revision object (if it was deleted or moved to the Trash successfully) 531 * or false or null (failure). If the revision was moved to the Trash, $result represents 532 * its new state; if it was deleted, $result represents its state before deletion. 533 * @param WP_REST_Request $request The request sent to the API. 534 */ 535 do_action( 'rest_delete_revision', $result, $request ); 536 537 if ( ! $result ) { 538 return new WP_Error( 539 'rest_cannot_delete', 540 __( 'The post cannot be deleted.' ), 541 array( 'status' => 500 ) 542 ); 543 } 544 545 $response = new WP_REST_Response(); 546 $response->set_data( 547 array( 548 'deleted' => true, 549 'previous' => $previous->get_data(), 550 ) 551 ); 552 return $response; 553 } 554 555 /** 556 * Determines the allowed query_vars for a get_items() response and prepares 557 * them for WP_Query. 558 * 559 * @since 5.0.0 560 * 561 * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array. 562 * @param WP_REST_Request $request Optional. Full details about the request. 563 * @return array Items query arguments. 564 */ 565 protected function prepare_items_query( $prepared_args = array(), $request = null ) { 566 $query_args = array(); 567 if ( ! is_array( $prepared_args ) ) { 568 $prepared_args = array(); 569 } 570 571 foreach ( $prepared_args as $key => $value ) { 572 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ 573 $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 574 } 575 576 // Map to proper WP_Query orderby param. 577 if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) { 578 $orderby_mappings = array( 579 'id' => 'ID', 580 'include' => 'post__in', 581 'slug' => 'post_name', 582 'include_slugs' => 'post_name__in', 583 ); 584 585 if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) { 586 $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ]; 587 } 588 } 589 590 return $query_args; 591 } 592 593 /** 594 * Prepares the revision for the REST response. 595 * 596 * @since 4.7.0 597 * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. 598 * @since 7.1.0 The global post is now restored to its previous value before returning. 599 * 600 * @global WP_Post|null $post Global post object. 601 * 602 * @param WP_Post $item Post revision object. 603 * @param WP_REST_Request $request Request object. 604 * @return WP_REST_Response Response object. 605 */ 606 public function prepare_item_for_response( $item, $request ) { 607 /* 608 * Save the previous global post so it can be restored before returning. 609 * Preparing the revision sets up the global post and post data, which 610 * must not leak into the rest of the request (e.g. the autosaves endpoint 611 * is preloaded in the block editor, where a leaked global post can cause 612 * the editor to be initialized with the wrong post). 613 * 614 * Note that $post is intentionally not declared as a global here. It must 615 * remain local to this method so that a filter which reassigns the global 616 * post while the response is being prepared (for example on 'the_content') 617 * cannot change which post the remaining fields are read from. 618 */ 619 $previous_post = isset( $GLOBALS['post'] ) && $GLOBALS['post'] instanceof WP_Post ? $GLOBALS['post'] : null; 620 621 // Restores the more descriptive, specific name for use within this method. 622 $post = $item; 623 624 $GLOBALS['post'] = $post; 625 626 setup_postdata( $post ); 627 628 // Don't prepare the response body for HEAD requests. 629 if ( $request->is_method( 'HEAD' ) ) { 630 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */ 631 $response = apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request ); 632 633 $this->restore_post_data( $previous_post ); 634 635 return $response; 636 } 637 638 $fields = $this->get_fields_for_response( $request ); 639 $data = array(); 640 641 if ( in_array( 'author', $fields, true ) ) { 642 $data['author'] = (int) $post->post_author; 643 } 644 645 if ( in_array( 'date', $fields, true ) ) { 646 $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); 647 } 648 649 if ( in_array( 'date_gmt', $fields, true ) ) { 650 $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); 651 } 652 653 if ( in_array( 'id', $fields, true ) ) { 654 $data['id'] = $post->ID; 655 } 656 657 if ( in_array( 'modified', $fields, true ) ) { 658 $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); 659 } 660 661 if ( in_array( 'modified_gmt', $fields, true ) ) { 662 $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); 663 } 664 665 if ( in_array( 'parent', $fields, true ) ) { 666 $data['parent'] = (int) $post->post_parent; 667 } 668 669 if ( in_array( 'slug', $fields, true ) ) { 670 $data['slug'] = $post->post_name; 671 } 672 673 if ( rest_is_field_included( 'guid', $fields ) ) { 674 $data['guid'] = array(); 675 } 676 if ( rest_is_field_included( 'guid.rendered', $fields ) ) { 677 /** This filter is documented in wp-includes/post-template.php */ 678 $data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID ); 679 } 680 if ( rest_is_field_included( 'guid.raw', $fields ) ) { 681 $data['guid']['raw'] = $post->guid; 682 } 683 684 if ( rest_is_field_included( 'title', $fields ) ) { 685 $data['title'] = array(); 686 } 687 if ( rest_is_field_included( 'title.raw', $fields ) ) { 688 $data['title']['raw'] = $post->post_title; 689 } 690 if ( rest_is_field_included( 'title.rendered', $fields ) ) { 691 $data['title']['rendered'] = get_the_title( $post->ID ); 692 } 693 694 if ( rest_is_field_included( 'content', $fields ) ) { 695 $data['content'] = array(); 696 } 697 if ( rest_is_field_included( 'content.raw', $fields ) ) { 698 $data['content']['raw'] = $post->post_content; 699 } 700 if ( rest_is_field_included( 'content.rendered', $fields ) ) { 701 /** This filter is documented in wp-includes/post-template.php */ 702 $data['content']['rendered'] = apply_filters( 'the_content', $post->post_content ); 703 } 704 705 if ( rest_is_field_included( 'excerpt', $fields ) ) { 706 $data['excerpt'] = array(); 707 } 708 if ( rest_is_field_included( 'excerpt.raw', $fields ) ) { 709 $data['excerpt']['raw'] = $post->post_excerpt; 710 } 711 if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) { 712 $data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post ); 713 } 714 715 if ( rest_is_field_included( 'meta', $fields ) ) { 716 $data['meta'] = $this->meta->get_value( $post->ID, $request ); 717 } 718 719 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 720 $data = $this->add_additional_fields_to_object( $data, $request ); 721 $data = $this->filter_response_by_context( $data, $context ); 722 $response = rest_ensure_response( $data ); 723 724 if ( ! empty( $data['parent'] ) ) { 725 $response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) ); 726 } 727 728 /** 729 * Filters a revision returned from the REST API. 730 * 731 * Allows modification of the revision right before it is returned. 732 * 733 * @since 4.7.0 734 * 735 * @param WP_REST_Response $response The response object. 736 * @param WP_Post $post The original revision object. 737 * @param WP_REST_Request $request Request used to generate the response. 738 */ 739 $response = apply_filters( 'rest_prepare_revision', $response, $post, $request ); 740 741 $this->restore_post_data( $previous_post ); 742 743 return $response; 744 } 745 746 /** 747 * Restores the global post to its previous value after preparing a revision. 748 * 749 * Preparing a revision overwrites the global post and post data via 750 * setup_postdata(). This restores the global post that was in place 751 * beforehand so the change does not leak into the rest of the request. 752 * 753 * Only the global post is guaranteed to be restored. When there was no 754 * previous global post and the main query has no post either, which is the 755 * usual state during a REST request, wp_reset_postdata() has nothing to 756 * restore from, so the remaining globals set by setup_postdata() (such as 757 * $id, $authordata and $pages) are left describing the revision. Clearing 758 * those would mean unsetting each one by hand, which is beyond what is 759 * needed to keep the global post from leaking. 760 * 761 * @since 7.1.0 762 * 763 * @param WP_Post|null $previous_post The global post to restore, or null if there was none. 764 */ 765 private function restore_post_data( ?WP_Post $previous_post ): void { 766 if ( $previous_post ) { 767 $GLOBALS['post'] = $previous_post; 768 setup_postdata( $previous_post ); 769 return; 770 } 771 772 /* 773 * There was no global post to restore, so clear the revision's post data. 774 * This runs before clearing the global post because wp_reset_postdata() 775 * repopulates it from the main query whenever that query has a post. Note 776 * that it is a no-op when the main query has no post, in which case only 777 * the global post below is cleared. 778 */ 779 wp_reset_postdata(); 780 781 /* 782 * Assigned rather than unset so that any `global $post` binding made before 783 * this request keeps pointing at the global. Unsetting removes the entry from 784 * the symbol table, which detaches those bindings, and a later write through 785 * one of them would no longer be visible to get_post(). 786 */ 787 $GLOBALS['post'] = null; 788 } 789 790 /** 791 * Checks the post_date_gmt or modified_gmt and prepare any post or 792 * modified date for single post output. 793 * 794 * @since 4.7.0 795 * 796 * @param string $date_gmt GMT publication time. 797 * @param string|null $date Optional. Local publication time. Default null. 798 * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null. 799 */ 800 protected function prepare_date_response( $date_gmt, $date = null ) { 801 if ( '0000-00-00 00:00:00' === $date_gmt ) { 802 return null; 803 } 804 805 if ( isset( $date ) ) { 806 return mysql_to_rfc3339( $date ); 807 } 808 809 return mysql_to_rfc3339( $date_gmt ); 810 } 811 812 /** 813 * Retrieves the revision's schema, conforming to JSON Schema. 814 * 815 * @since 4.7.0 816 * 817 * @return array Item schema data. 818 */ 819 public function get_item_schema() { 820 if ( $this->schema ) { 821 return $this->add_additional_fields_schema( $this->schema ); 822 } 823 824 $schema = array( 825 '$schema' => 'http://json-schema.org/draft-04/schema#', 826 'title' => "{$this->parent_post_type}-revision", 827 'type' => 'object', 828 // Base properties for every Revision. 829 'properties' => array( 830 'author' => array( 831 'description' => __( 'The ID for the author of the revision.' ), 832 'type' => 'integer', 833 'context' => array( 'view', 'edit', 'embed' ), 834 ), 835 'date' => array( 836 'description' => __( "The date the revision was published, in the site's timezone." ), 837 'type' => 'string', 838 'format' => 'date-time', 839 'context' => array( 'view', 'edit', 'embed' ), 840 ), 841 'date_gmt' => array( 842 'description' => __( 'The date the revision was published, as GMT.' ), 843 'type' => 'string', 844 'format' => 'date-time', 845 'context' => array( 'view', 'edit' ), 846 ), 847 'guid' => array( 848 'description' => __( 'GUID for the revision, as it exists in the database.' ), 849 'type' => 'string', 850 'context' => array( 'view', 'edit' ), 851 ), 852 'id' => array( 853 'description' => __( 'Unique identifier for the revision.' ), 854 'type' => 'integer', 855 'context' => array( 'view', 'edit', 'embed' ), 856 ), 857 'modified' => array( 858 'description' => __( "The date the revision was last modified, in the site's timezone." ), 859 'type' => 'string', 860 'format' => 'date-time', 861 'context' => array( 'view', 'edit' ), 862 ), 863 'modified_gmt' => array( 864 'description' => __( 'The date the revision was last modified, as GMT.' ), 865 'type' => 'string', 866 'format' => 'date-time', 867 'context' => array( 'view', 'edit' ), 868 ), 869 'parent' => array( 870 'description' => __( 'The ID for the parent of the revision.' ), 871 'type' => 'integer', 872 'context' => array( 'view', 'edit', 'embed' ), 873 ), 874 'slug' => array( 875 'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ), 876 'type' => 'string', 877 'context' => array( 'view', 'edit', 'embed' ), 878 ), 879 ), 880 ); 881 882 $parent_schema = $this->parent_controller->get_item_schema(); 883 884 if ( ! empty( $parent_schema['properties']['title'] ) ) { 885 $schema['properties']['title'] = $parent_schema['properties']['title']; 886 } 887 888 if ( ! empty( $parent_schema['properties']['content'] ) ) { 889 $schema['properties']['content'] = $parent_schema['properties']['content']; 890 } 891 892 if ( ! empty( $parent_schema['properties']['excerpt'] ) ) { 893 $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt']; 894 } 895 896 if ( ! empty( $parent_schema['properties']['guid'] ) ) { 897 $schema['properties']['guid'] = $parent_schema['properties']['guid']; 898 } 899 900 $schema['properties']['meta'] = $this->meta->get_field_schema(); 901 902 $this->schema = $schema; 903 904 return $this->add_additional_fields_schema( $this->schema ); 905 } 906 907 /** 908 * Retrieves the query params for collections. 909 * 910 * @since 4.7.0 911 * 912 * @return array Collection parameters. 913 */ 914 public function get_collection_params() { 915 $query_params = parent::get_collection_params(); 916 917 $query_params['context']['default'] = 'view'; 918 919 unset( $query_params['per_page']['default'] ); 920 921 $query_params['exclude'] = array( 922 'description' => __( 'Ensure result set excludes specific IDs.' ), 923 'type' => 'array', 924 'items' => array( 925 'type' => 'integer', 926 ), 927 'default' => array(), 928 ); 929 930 $query_params['include'] = array( 931 'description' => __( 'Limit result set to specific IDs.' ), 932 'type' => 'array', 933 'items' => array( 934 'type' => 'integer', 935 ), 936 'default' => array(), 937 ); 938 939 $query_params['offset'] = array( 940 'description' => __( 'Offset the result set by a specific number of items.' ), 941 'type' => 'integer', 942 ); 943 944 $query_params['order'] = array( 945 'description' => __( 'Order sort attribute ascending or descending.' ), 946 'type' => 'string', 947 'default' => 'desc', 948 'enum' => array( 'asc', 'desc' ), 949 ); 950 951 $query_params['orderby'] = array( 952 'description' => __( 'Sort collection by object attribute.' ), 953 'type' => 'string', 954 'default' => 'date', 955 'enum' => array( 956 'date', 957 'id', 958 'include', 959 'relevance', 960 'slug', 961 'include_slugs', 962 'title', 963 ), 964 ); 965 966 return $query_params; 967 } 968 969 /** 970 * Checks the post excerpt and prepare it for single post output. 971 * 972 * @since 4.7.0 973 * 974 * @param string $excerpt The post excerpt. 975 * @param WP_Post $post Post revision object. 976 * @return string Prepared excerpt or empty string. 977 */ 978 protected function prepare_excerpt_response( $excerpt, $post ) { 979 980 /** This filter is documented in wp-includes/post-template.php */ 981 $excerpt = apply_filters( 'the_excerpt', $excerpt ); 982 983 if ( empty( $excerpt ) ) { 984 return ''; 985 } 986 987 return $excerpt; 988 } 989 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Aug 15 08:20:25 2026 | Cross-referenced by PHPXref |