[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-revisions-controller.php (source)

   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       * @param WP_REST_Request $request Full details about the request.
 230       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 231       */
 232  	public function get_items( $request ) {
 233          $parent = $this->get_parent( $request['parent'] );
 234          if ( is_wp_error( $parent ) ) {
 235              return $parent;
 236          }
 237  
 238          // Ensure a search string is set in case the orderby is set to 'relevance'.
 239          if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
 240              return new WP_Error(
 241                  'rest_no_search_term_defined',
 242                  __( 'You need to define a search term to order by relevance.' ),
 243                  array( 'status' => 400 )
 244              );
 245          }
 246  
 247          // Ensure an include parameter is set in case the orderby is set to 'include'.
 248          if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
 249              return new WP_Error(
 250                  'rest_orderby_include_missing_include',
 251                  __( 'You need to define an include parameter to order by include.' ),
 252                  array( 'status' => 400 )
 253              );
 254          }
 255  
 256          $is_head_request = $request->is_method( 'HEAD' );
 257  
 258          if ( wp_revisions_enabled( $parent ) ) {
 259              $registered = $this->get_collection_params();
 260              $args       = array(
 261                  'post_parent'      => $parent->ID,
 262                  'post_type'        => 'revision',
 263                  'post_status'      => 'inherit',
 264                  'posts_per_page'   => -1,
 265                  'orderby'          => 'date ID',
 266                  'order'            => 'DESC',
 267                  'suppress_filters' => true,
 268              );
 269  
 270              $parameter_mappings = array(
 271                  'exclude'  => 'post__not_in',
 272                  'include'  => 'post__in',
 273                  'offset'   => 'offset',
 274                  'order'    => 'order',
 275                  'orderby'  => 'orderby',
 276                  'page'     => 'paged',
 277                  'per_page' => 'posts_per_page',
 278                  'search'   => 's',
 279              );
 280  
 281              foreach ( $parameter_mappings as $api_param => $wp_param ) {
 282                  if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
 283                      $args[ $wp_param ] = $request[ $api_param ];
 284                  }
 285              }
 286  
 287              // For backward-compatibility, 'date' needs to resolve to 'date ID'.
 288              if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
 289                  $args['orderby'] = 'date ID';
 290              }
 291  
 292              if ( $is_head_request ) {
 293                  // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
 294                  $args['fields'] = 'ids';
 295                  // Disable priming post meta for HEAD requests to improve performance.
 296                  $args['update_post_term_cache'] = false;
 297                  $args['update_post_meta_cache'] = false;
 298              }
 299  
 300              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 301              $args       = apply_filters( 'rest_revision_query', $args, $request );
 302              $query_args = $this->prepare_items_query( $args, $request );
 303  
 304              $revisions_query = new WP_Query();
 305              $revisions       = $revisions_query->query( $query_args );
 306              $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
 307              $page            = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
 308              $total_revisions = $revisions_query->found_posts;
 309  
 310              if ( $total_revisions < 1 ) {
 311                  // Out-of-bounds, run the query without pagination/offset to get the total count.
 312                  unset( $query_args['paged'], $query_args['offset'] );
 313  
 314                  $count_query                          = new WP_Query();
 315                  $query_args['fields']                 = 'ids';
 316                  $query_args['posts_per_page']         = 1;
 317                  $query_args['update_post_meta_cache'] = false;
 318                  $query_args['update_post_term_cache'] = false;
 319  
 320                  $count_query->query( $query_args );
 321                  $total_revisions = $count_query->found_posts;
 322              }
 323  
 324              if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
 325                  $max_pages = (int) ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
 326              } else {
 327                  $max_pages = $total_revisions > 0 ? 1 : 0;
 328              }
 329  
 330              if ( $total_revisions > 0 ) {
 331                  if ( $offset >= $total_revisions ) {
 332                      return new WP_Error(
 333                          'rest_revision_invalid_offset_number',
 334                          __( 'The offset number requested is larger than or equal to the number of available revisions.' ),
 335                          array( 'status' => 400 )
 336                      );
 337                  } elseif ( ! $offset && $page > $max_pages ) {
 338                      return new WP_Error(
 339                          'rest_revision_invalid_page_number',
 340                          __( 'The page number requested is larger than the number of pages available.' ),
 341                          array( 'status' => 400 )
 342                      );
 343                  }
 344              }
 345          } else {
 346              $revisions       = array();
 347              $total_revisions = 0;
 348              $max_pages       = 0;
 349              $page            = (int) $request['page'];
 350          }
 351  
 352          if ( ! $is_head_request ) {
 353              $response = array();
 354  
 355              foreach ( $revisions as $revision ) {
 356                  $data       = $this->prepare_item_for_response( $revision, $request );
 357                  $response[] = $this->prepare_response_for_collection( $data );
 358              }
 359  
 360              $response = rest_ensure_response( $response );
 361          } else {
 362              $response = new WP_REST_Response( array() );
 363          }
 364  
 365          $response->header( 'X-WP-Total', (int) $total_revisions );
 366          $response->header( 'X-WP-TotalPages', (int) $max_pages );
 367  
 368          $request_params = $request->get_query_params();
 369          $base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) );
 370          $base           = add_query_arg( urlencode_deep( $request_params ), $base_path );
 371  
 372          if ( $page > 1 ) {
 373              $prev_page = $page - 1;
 374  
 375              if ( $prev_page > $max_pages ) {
 376                  $prev_page = $max_pages;
 377              }
 378  
 379              $prev_link = add_query_arg( 'page', $prev_page, $base );
 380              $response->link_header( 'prev', $prev_link );
 381          }
 382          if ( $max_pages > $page ) {
 383              $next_page = $page + 1;
 384              $next_link = add_query_arg( 'page', $next_page, $base );
 385  
 386              $response->link_header( 'next', $next_link );
 387          }
 388  
 389          return $response;
 390      }
 391  
 392      /**
 393       * Checks if a given request has access to get a specific revision.
 394       *
 395       * @since 4.7.0
 396       *
 397       * @param WP_REST_Request $request Full details about the request.
 398       * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
 399       */
 400  	public function get_item_permissions_check( $request ) {
 401          return $this->get_items_permissions_check( $request );
 402      }
 403  
 404      /**
 405       * Retrieves one revision from the collection.
 406       *
 407       * @since 4.7.0
 408       * @since 6.5.0 Added a condition to check that parent id matches revision parent id.
 409       *
 410       * @param WP_REST_Request $request Full details about the request.
 411       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 412       */
 413  	public function get_item( $request ) {
 414          $parent = $this->get_parent( $request['parent'] );
 415          if ( is_wp_error( $parent ) ) {
 416              return $parent;
 417          }
 418  
 419          $revision = $this->get_revision( $request['id'] );
 420          if ( is_wp_error( $revision ) ) {
 421              return $revision;
 422          }
 423  
 424          if ( (int) $parent->ID !== (int) $revision->post_parent ) {
 425              return new WP_Error(
 426                  'rest_revision_parent_id_mismatch',
 427                  /* translators: %d: A post id. */
 428                  sprintf( __( 'The revision does not belong to the specified parent with id of "%d"' ), $parent->ID ),
 429                  array( 'status' => 404 )
 430              );
 431          }
 432  
 433          $response = $this->prepare_item_for_response( $revision, $request );
 434          return rest_ensure_response( $response );
 435      }
 436  
 437      /**
 438       * Checks if a given request has access to delete a revision.
 439       *
 440       * @since 4.7.0
 441       *
 442       * @param WP_REST_Request $request Full details about the request.
 443       * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
 444       */
 445  	public function delete_item_permissions_check( $request ) {
 446          $parent = $this->get_parent( $request['parent'] );
 447          if ( is_wp_error( $parent ) ) {
 448              return $parent;
 449          }
 450  
 451          if ( ! current_user_can( 'delete_post', $parent->ID ) ) {
 452              return new WP_Error(
 453                  'rest_cannot_delete',
 454                  __( 'Sorry, you are not allowed to delete revisions of this post.' ),
 455                  array( 'status' => rest_authorization_required_code() )
 456              );
 457          }
 458  
 459          $revision = $this->get_revision( $request['id'] );
 460          if ( is_wp_error( $revision ) ) {
 461              return $revision;
 462          }
 463  
 464          $response = $this->get_items_permissions_check( $request );
 465          if ( ! $response || is_wp_error( $response ) ) {
 466              return $response;
 467          }
 468  
 469          if ( ! current_user_can( 'delete_post', $revision->ID ) ) {
 470              return new WP_Error(
 471                  'rest_cannot_delete',
 472                  __( 'Sorry, you are not allowed to delete this revision.' ),
 473                  array( 'status' => rest_authorization_required_code() )
 474              );
 475          }
 476  
 477          return true;
 478      }
 479  
 480      /**
 481       * Deletes a single revision.
 482       *
 483       * @since 4.7.0
 484       *
 485       * @param WP_REST_Request $request Full details about the request.
 486       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 487       */
 488  	public function delete_item( $request ) {
 489          $revision = $this->get_revision( $request['id'] );
 490          if ( is_wp_error( $revision ) ) {
 491              return $revision;
 492          }
 493  
 494          $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
 495  
 496          // We don't support trashing for revisions.
 497          if ( ! $force ) {
 498              return new WP_Error(
 499                  'rest_trash_not_supported',
 500                  /* translators: %s: force=true */
 501                  sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ),
 502                  array( 'status' => 501 )
 503              );
 504          }
 505  
 506          $previous = $this->prepare_item_for_response( $revision, $request );
 507  
 508          $result = wp_delete_post( $request['id'], true );
 509  
 510          /**
 511           * Fires after a revision is deleted via the REST API.
 512           *
 513           * @since 4.7.0
 514           *
 515           * @param WP_Post|false|null $result The revision object (if it was deleted or moved to the Trash successfully)
 516           *                                   or false or null (failure). If the revision was moved to the Trash, $result represents
 517           *                                   its new state; if it was deleted, $result represents its state before deletion.
 518           * @param WP_REST_Request $request The request sent to the API.
 519           */
 520          do_action( 'rest_delete_revision', $result, $request );
 521  
 522          if ( ! $result ) {
 523              return new WP_Error(
 524                  'rest_cannot_delete',
 525                  __( 'The post cannot be deleted.' ),
 526                  array( 'status' => 500 )
 527              );
 528          }
 529  
 530          $response = new WP_REST_Response();
 531          $response->set_data(
 532              array(
 533                  'deleted'  => true,
 534                  'previous' => $previous->get_data(),
 535              )
 536          );
 537          return $response;
 538      }
 539  
 540      /**
 541       * Determines the allowed query_vars for a get_items() response and prepares
 542       * them for WP_Query.
 543       *
 544       * @since 5.0.0
 545       *
 546       * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
 547       * @param WP_REST_Request $request       Optional. Full details about the request.
 548       * @return array Items query arguments.
 549       */
 550  	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
 551          $query_args = array();
 552  
 553          foreach ( $prepared_args as $key => $value ) {
 554              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 555              $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 556          }
 557  
 558          // Map to proper WP_Query orderby param.
 559          if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
 560              $orderby_mappings = array(
 561                  'id'            => 'ID',
 562                  'include'       => 'post__in',
 563                  'slug'          => 'post_name',
 564                  'include_slugs' => 'post_name__in',
 565              );
 566  
 567              if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
 568                  $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
 569              }
 570          }
 571  
 572          return $query_args;
 573      }
 574  
 575      /**
 576       * Prepares the revision for the REST response.
 577       *
 578       * @since 4.7.0
 579       * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
 580       *
 581       * @global WP_Post $post Global post object.
 582       *
 583       * @param WP_Post         $item    Post revision object.
 584       * @param WP_REST_Request $request Request object.
 585       * @return WP_REST_Response Response object.
 586       */
 587  	public function prepare_item_for_response( $item, $request ) {
 588          // Restores the more descriptive, specific name for use within this method.
 589          $post = $item;
 590  
 591          $GLOBALS['post'] = $post;
 592  
 593          setup_postdata( $post );
 594  
 595          // Don't prepare the response body for HEAD requests.
 596          if ( $request->is_method( 'HEAD' ) ) {
 597              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
 598              return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request );
 599          }
 600  
 601          $fields = $this->get_fields_for_response( $request );
 602          $data   = array();
 603  
 604          if ( in_array( 'author', $fields, true ) ) {
 605              $data['author'] = (int) $post->post_author;
 606          }
 607  
 608          if ( in_array( 'date', $fields, true ) ) {
 609              $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
 610          }
 611  
 612          if ( in_array( 'date_gmt', $fields, true ) ) {
 613              $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
 614          }
 615  
 616          if ( in_array( 'id', $fields, true ) ) {
 617              $data['id'] = $post->ID;
 618          }
 619  
 620          if ( in_array( 'modified', $fields, true ) ) {
 621              $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
 622          }
 623  
 624          if ( in_array( 'modified_gmt', $fields, true ) ) {
 625              $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
 626          }
 627  
 628          if ( in_array( 'parent', $fields, true ) ) {
 629              $data['parent'] = (int) $post->post_parent;
 630          }
 631  
 632          if ( in_array( 'slug', $fields, true ) ) {
 633              $data['slug'] = $post->post_name;
 634          }
 635  
 636          if ( in_array( 'guid', $fields, true ) ) {
 637              $data['guid'] = array(
 638                  /** This filter is documented in wp-includes/post-template.php */
 639                  'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
 640                  'raw'      => $post->guid,
 641              );
 642          }
 643  
 644          if ( in_array( 'title', $fields, true ) ) {
 645              $data['title'] = array(
 646                  'raw'      => $post->post_title,
 647                  'rendered' => get_the_title( $post->ID ),
 648              );
 649          }
 650  
 651          if ( in_array( 'content', $fields, true ) ) {
 652  
 653              $data['content'] = array(
 654                  'raw'      => $post->post_content,
 655                  /** This filter is documented in wp-includes/post-template.php */
 656                  'rendered' => apply_filters( 'the_content', $post->post_content ),
 657              );
 658          }
 659  
 660          if ( in_array( 'excerpt', $fields, true ) ) {
 661              $data['excerpt'] = array(
 662                  'raw'      => $post->post_excerpt,
 663                  'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
 664              );
 665          }
 666  
 667          if ( rest_is_field_included( 'meta', $fields ) ) {
 668              $data['meta'] = $this->meta->get_value( $post->ID, $request );
 669          }
 670  
 671          $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
 672          $data     = $this->add_additional_fields_to_object( $data, $request );
 673          $data     = $this->filter_response_by_context( $data, $context );
 674          $response = rest_ensure_response( $data );
 675  
 676          if ( ! empty( $data['parent'] ) ) {
 677              $response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) );
 678          }
 679  
 680          /**
 681           * Filters a revision returned from the REST API.
 682           *
 683           * Allows modification of the revision right before it is returned.
 684           *
 685           * @since 4.7.0
 686           *
 687           * @param WP_REST_Response $response The response object.
 688           * @param WP_Post          $post     The original revision object.
 689           * @param WP_REST_Request  $request  Request used to generate the response.
 690           */
 691          return apply_filters( 'rest_prepare_revision', $response, $post, $request );
 692      }
 693  
 694      /**
 695       * Checks the post_date_gmt or modified_gmt and prepare any post or
 696       * modified date for single post output.
 697       *
 698       * @since 4.7.0
 699       *
 700       * @param string      $date_gmt GMT publication time.
 701       * @param string|null $date     Optional. Local publication time. Default null.
 702       * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
 703       */
 704  	protected function prepare_date_response( $date_gmt, $date = null ) {
 705          if ( '0000-00-00 00:00:00' === $date_gmt ) {
 706              return null;
 707          }
 708  
 709          if ( isset( $date ) ) {
 710              return mysql_to_rfc3339( $date );
 711          }
 712  
 713          return mysql_to_rfc3339( $date_gmt );
 714      }
 715  
 716      /**
 717       * Retrieves the revision's schema, conforming to JSON Schema.
 718       *
 719       * @since 4.7.0
 720       *
 721       * @return array Item schema data.
 722       */
 723  	public function get_item_schema() {
 724          if ( $this->schema ) {
 725              return $this->add_additional_fields_schema( $this->schema );
 726          }
 727  
 728          $schema = array(
 729              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 730              'title'      => "{$this->parent_post_type}-revision",
 731              'type'       => 'object',
 732              // Base properties for every Revision.
 733              'properties' => array(
 734                  'author'       => array(
 735                      'description' => __( 'The ID for the author of the revision.' ),
 736                      'type'        => 'integer',
 737                      'context'     => array( 'view', 'edit', 'embed' ),
 738                  ),
 739                  'date'         => array(
 740                      'description' => __( "The date the revision was published, in the site's timezone." ),
 741                      'type'        => 'string',
 742                      'format'      => 'date-time',
 743                      'context'     => array( 'view', 'edit', 'embed' ),
 744                  ),
 745                  'date_gmt'     => array(
 746                      'description' => __( 'The date the revision was published, as GMT.' ),
 747                      'type'        => 'string',
 748                      'format'      => 'date-time',
 749                      'context'     => array( 'view', 'edit' ),
 750                  ),
 751                  'guid'         => array(
 752                      'description' => __( 'GUID for the revision, as it exists in the database.' ),
 753                      'type'        => 'string',
 754                      'context'     => array( 'view', 'edit' ),
 755                  ),
 756                  'id'           => array(
 757                      'description' => __( 'Unique identifier for the revision.' ),
 758                      'type'        => 'integer',
 759                      'context'     => array( 'view', 'edit', 'embed' ),
 760                  ),
 761                  'modified'     => array(
 762                      'description' => __( "The date the revision was last modified, in the site's timezone." ),
 763                      'type'        => 'string',
 764                      'format'      => 'date-time',
 765                      'context'     => array( 'view', 'edit' ),
 766                  ),
 767                  'modified_gmt' => array(
 768                      'description' => __( 'The date the revision was last modified, as GMT.' ),
 769                      'type'        => 'string',
 770                      'format'      => 'date-time',
 771                      'context'     => array( 'view', 'edit' ),
 772                  ),
 773                  'parent'       => array(
 774                      'description' => __( 'The ID for the parent of the revision.' ),
 775                      'type'        => 'integer',
 776                      'context'     => array( 'view', 'edit', 'embed' ),
 777                  ),
 778                  'slug'         => array(
 779                      'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ),
 780                      'type'        => 'string',
 781                      'context'     => array( 'view', 'edit', 'embed' ),
 782                  ),
 783              ),
 784          );
 785  
 786          $parent_schema = $this->parent_controller->get_item_schema();
 787  
 788          if ( ! empty( $parent_schema['properties']['title'] ) ) {
 789              $schema['properties']['title'] = $parent_schema['properties']['title'];
 790          }
 791  
 792          if ( ! empty( $parent_schema['properties']['content'] ) ) {
 793              $schema['properties']['content'] = $parent_schema['properties']['content'];
 794          }
 795  
 796          if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
 797              $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
 798          }
 799  
 800          if ( ! empty( $parent_schema['properties']['guid'] ) ) {
 801              $schema['properties']['guid'] = $parent_schema['properties']['guid'];
 802          }
 803  
 804          $schema['properties']['meta'] = $this->meta->get_field_schema();
 805  
 806          $this->schema = $schema;
 807  
 808          return $this->add_additional_fields_schema( $this->schema );
 809      }
 810  
 811      /**
 812       * Retrieves the query params for collections.
 813       *
 814       * @since 4.7.0
 815       *
 816       * @return array Collection parameters.
 817       */
 818  	public function get_collection_params() {
 819          $query_params = parent::get_collection_params();
 820  
 821          $query_params['context']['default'] = 'view';
 822  
 823          unset( $query_params['per_page']['default'] );
 824  
 825          $query_params['exclude'] = array(
 826              'description' => __( 'Ensure result set excludes specific IDs.' ),
 827              'type'        => 'array',
 828              'items'       => array(
 829                  'type' => 'integer',
 830              ),
 831              'default'     => array(),
 832          );
 833  
 834          $query_params['include'] = array(
 835              'description' => __( 'Limit result set to specific IDs.' ),
 836              'type'        => 'array',
 837              'items'       => array(
 838                  'type' => 'integer',
 839              ),
 840              'default'     => array(),
 841          );
 842  
 843          $query_params['offset'] = array(
 844              'description' => __( 'Offset the result set by a specific number of items.' ),
 845              'type'        => 'integer',
 846          );
 847  
 848          $query_params['order'] = array(
 849              'description' => __( 'Order sort attribute ascending or descending.' ),
 850              'type'        => 'string',
 851              'default'     => 'desc',
 852              'enum'        => array( 'asc', 'desc' ),
 853          );
 854  
 855          $query_params['orderby'] = array(
 856              'description' => __( 'Sort collection by object attribute.' ),
 857              'type'        => 'string',
 858              'default'     => 'date',
 859              'enum'        => array(
 860                  'date',
 861                  'id',
 862                  'include',
 863                  'relevance',
 864                  'slug',
 865                  'include_slugs',
 866                  'title',
 867              ),
 868          );
 869  
 870          return $query_params;
 871      }
 872  
 873      /**
 874       * Checks the post excerpt and prepare it for single post output.
 875       *
 876       * @since 4.7.0
 877       *
 878       * @param string  $excerpt The post excerpt.
 879       * @param WP_Post $post    Post revision object.
 880       * @return string Prepared excerpt or empty string.
 881       */
 882  	protected function prepare_excerpt_response( $excerpt, $post ) {
 883  
 884          /** This filter is documented in wp-includes/post-template.php */
 885          $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
 886  
 887          if ( empty( $excerpt ) ) {
 888              return '';
 889          }
 890  
 891          return $excerpt;
 892      }
 893  }


Generated : Thu Oct 23 08:20:05 2025 Cross-referenced by PHPXref