| [ 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 * 599 * @global WP_Post $post Global post object. 600 * 601 * @param WP_Post $item Post revision object. 602 * @param WP_REST_Request $request Request object. 603 * @return WP_REST_Response Response object. 604 */ 605 public function prepare_item_for_response( $item, $request ) { 606 // Restores the more descriptive, specific name for use within this method. 607 $post = $item; 608 609 $GLOBALS['post'] = $post; 610 611 setup_postdata( $post ); 612 613 // Don't prepare the response body for HEAD requests. 614 if ( $request->is_method( 'HEAD' ) ) { 615 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */ 616 return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request ); 617 } 618 619 $fields = $this->get_fields_for_response( $request ); 620 $data = array(); 621 622 if ( in_array( 'author', $fields, true ) ) { 623 $data['author'] = (int) $post->post_author; 624 } 625 626 if ( in_array( 'date', $fields, true ) ) { 627 $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); 628 } 629 630 if ( in_array( 'date_gmt', $fields, true ) ) { 631 $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); 632 } 633 634 if ( in_array( 'id', $fields, true ) ) { 635 $data['id'] = $post->ID; 636 } 637 638 if ( in_array( 'modified', $fields, true ) ) { 639 $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); 640 } 641 642 if ( in_array( 'modified_gmt', $fields, true ) ) { 643 $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); 644 } 645 646 if ( in_array( 'parent', $fields, true ) ) { 647 $data['parent'] = (int) $post->post_parent; 648 } 649 650 if ( in_array( 'slug', $fields, true ) ) { 651 $data['slug'] = $post->post_name; 652 } 653 654 if ( rest_is_field_included( 'guid', $fields ) ) { 655 $data['guid'] = array(); 656 } 657 if ( rest_is_field_included( 'guid.rendered', $fields ) ) { 658 /** This filter is documented in wp-includes/post-template.php */ 659 $data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID ); 660 } 661 if ( rest_is_field_included( 'guid.raw', $fields ) ) { 662 $data['guid']['raw'] = $post->guid; 663 } 664 665 if ( rest_is_field_included( 'title', $fields ) ) { 666 $data['title'] = array(); 667 } 668 if ( rest_is_field_included( 'title.raw', $fields ) ) { 669 $data['title']['raw'] = $post->post_title; 670 } 671 if ( rest_is_field_included( 'title.rendered', $fields ) ) { 672 $data['title']['rendered'] = get_the_title( $post->ID ); 673 } 674 675 if ( rest_is_field_included( 'content', $fields ) ) { 676 $data['content'] = array(); 677 } 678 if ( rest_is_field_included( 'content.raw', $fields ) ) { 679 $data['content']['raw'] = $post->post_content; 680 } 681 if ( rest_is_field_included( 'content.rendered', $fields ) ) { 682 /** This filter is documented in wp-includes/post-template.php */ 683 $data['content']['rendered'] = apply_filters( 'the_content', $post->post_content ); 684 } 685 686 if ( rest_is_field_included( 'excerpt', $fields ) ) { 687 $data['excerpt'] = array(); 688 } 689 if ( rest_is_field_included( 'excerpt.raw', $fields ) ) { 690 $data['excerpt']['raw'] = $post->post_excerpt; 691 } 692 if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) { 693 $data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post ); 694 } 695 696 if ( rest_is_field_included( 'meta', $fields ) ) { 697 $data['meta'] = $this->meta->get_value( $post->ID, $request ); 698 } 699 700 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 701 $data = $this->add_additional_fields_to_object( $data, $request ); 702 $data = $this->filter_response_by_context( $data, $context ); 703 $response = rest_ensure_response( $data ); 704 705 if ( ! empty( $data['parent'] ) ) { 706 $response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) ); 707 } 708 709 /** 710 * Filters a revision returned from the REST API. 711 * 712 * Allows modification of the revision right before it is returned. 713 * 714 * @since 4.7.0 715 * 716 * @param WP_REST_Response $response The response object. 717 * @param WP_Post $post The original revision object. 718 * @param WP_REST_Request $request Request used to generate the response. 719 */ 720 return apply_filters( 'rest_prepare_revision', $response, $post, $request ); 721 } 722 723 /** 724 * Checks the post_date_gmt or modified_gmt and prepare any post or 725 * modified date for single post output. 726 * 727 * @since 4.7.0 728 * 729 * @param string $date_gmt GMT publication time. 730 * @param string|null $date Optional. Local publication time. Default null. 731 * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null. 732 */ 733 protected function prepare_date_response( $date_gmt, $date = null ) { 734 if ( '0000-00-00 00:00:00' === $date_gmt ) { 735 return null; 736 } 737 738 if ( isset( $date ) ) { 739 return mysql_to_rfc3339( $date ); 740 } 741 742 return mysql_to_rfc3339( $date_gmt ); 743 } 744 745 /** 746 * Retrieves the revision's schema, conforming to JSON Schema. 747 * 748 * @since 4.7.0 749 * 750 * @return array Item schema data. 751 */ 752 public function get_item_schema() { 753 if ( $this->schema ) { 754 return $this->add_additional_fields_schema( $this->schema ); 755 } 756 757 $schema = array( 758 '$schema' => 'http://json-schema.org/draft-04/schema#', 759 'title' => "{$this->parent_post_type}-revision", 760 'type' => 'object', 761 // Base properties for every Revision. 762 'properties' => array( 763 'author' => array( 764 'description' => __( 'The ID for the author of the revision.' ), 765 'type' => 'integer', 766 'context' => array( 'view', 'edit', 'embed' ), 767 ), 768 'date' => array( 769 'description' => __( "The date the revision was published, in the site's timezone." ), 770 'type' => 'string', 771 'format' => 'date-time', 772 'context' => array( 'view', 'edit', 'embed' ), 773 ), 774 'date_gmt' => array( 775 'description' => __( 'The date the revision was published, as GMT.' ), 776 'type' => 'string', 777 'format' => 'date-time', 778 'context' => array( 'view', 'edit' ), 779 ), 780 'guid' => array( 781 'description' => __( 'GUID for the revision, as it exists in the database.' ), 782 'type' => 'string', 783 'context' => array( 'view', 'edit' ), 784 ), 785 'id' => array( 786 'description' => __( 'Unique identifier for the revision.' ), 787 'type' => 'integer', 788 'context' => array( 'view', 'edit', 'embed' ), 789 ), 790 'modified' => array( 791 'description' => __( "The date the revision was last modified, in the site's timezone." ), 792 'type' => 'string', 793 'format' => 'date-time', 794 'context' => array( 'view', 'edit' ), 795 ), 796 'modified_gmt' => array( 797 'description' => __( 'The date the revision was last modified, as GMT.' ), 798 'type' => 'string', 799 'format' => 'date-time', 800 'context' => array( 'view', 'edit' ), 801 ), 802 'parent' => array( 803 'description' => __( 'The ID for the parent of the revision.' ), 804 'type' => 'integer', 805 'context' => array( 'view', 'edit', 'embed' ), 806 ), 807 'slug' => array( 808 'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ), 809 'type' => 'string', 810 'context' => array( 'view', 'edit', 'embed' ), 811 ), 812 ), 813 ); 814 815 $parent_schema = $this->parent_controller->get_item_schema(); 816 817 if ( ! empty( $parent_schema['properties']['title'] ) ) { 818 $schema['properties']['title'] = $parent_schema['properties']['title']; 819 } 820 821 if ( ! empty( $parent_schema['properties']['content'] ) ) { 822 $schema['properties']['content'] = $parent_schema['properties']['content']; 823 } 824 825 if ( ! empty( $parent_schema['properties']['excerpt'] ) ) { 826 $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt']; 827 } 828 829 if ( ! empty( $parent_schema['properties']['guid'] ) ) { 830 $schema['properties']['guid'] = $parent_schema['properties']['guid']; 831 } 832 833 $schema['properties']['meta'] = $this->meta->get_field_schema(); 834 835 $this->schema = $schema; 836 837 return $this->add_additional_fields_schema( $this->schema ); 838 } 839 840 /** 841 * Retrieves the query params for collections. 842 * 843 * @since 4.7.0 844 * 845 * @return array Collection parameters. 846 */ 847 public function get_collection_params() { 848 $query_params = parent::get_collection_params(); 849 850 $query_params['context']['default'] = 'view'; 851 852 unset( $query_params['per_page']['default'] ); 853 854 $query_params['exclude'] = array( 855 'description' => __( 'Ensure result set excludes specific IDs.' ), 856 'type' => 'array', 857 'items' => array( 858 'type' => 'integer', 859 ), 860 'default' => array(), 861 ); 862 863 $query_params['include'] = array( 864 'description' => __( 'Limit result set to specific IDs.' ), 865 'type' => 'array', 866 'items' => array( 867 'type' => 'integer', 868 ), 869 'default' => array(), 870 ); 871 872 $query_params['offset'] = array( 873 'description' => __( 'Offset the result set by a specific number of items.' ), 874 'type' => 'integer', 875 ); 876 877 $query_params['order'] = array( 878 'description' => __( 'Order sort attribute ascending or descending.' ), 879 'type' => 'string', 880 'default' => 'desc', 881 'enum' => array( 'asc', 'desc' ), 882 ); 883 884 $query_params['orderby'] = array( 885 'description' => __( 'Sort collection by object attribute.' ), 886 'type' => 'string', 887 'default' => 'date', 888 'enum' => array( 889 'date', 890 'id', 891 'include', 892 'relevance', 893 'slug', 894 'include_slugs', 895 'title', 896 ), 897 ); 898 899 return $query_params; 900 } 901 902 /** 903 * Checks the post excerpt and prepare it for single post output. 904 * 905 * @since 4.7.0 906 * 907 * @param string $excerpt The post excerpt. 908 * @param WP_Post $post Post revision object. 909 * @return string Prepared excerpt or empty string. 910 */ 911 protected function prepare_excerpt_response( $excerpt, $post ) { 912 913 /** This filter is documented in wp-includes/post-template.php */ 914 $excerpt = apply_filters( 'the_excerpt', $excerpt, $post ); 915 916 if ( empty( $excerpt ) ) { 917 return ''; 918 } 919 920 return $excerpt; 921 } 922 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Jun 14 08:20:09 2026 | Cross-referenced by PHPXref |