[ Index ]

PHP Cross Reference of WordPress

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       * Parent controller.
  29       *
  30       * @since 4.7.0
  31       * @var WP_REST_Controller
  32       */
  33      private $parent_controller;
  34  
  35      /**
  36       * The base of the parent controller's route.
  37       *
  38       * @since 4.7.0
  39       * @var string
  40       */
  41      private $parent_base;
  42  
  43      /**
  44       * Constructor.
  45       *
  46       * @since 4.7.0
  47       *
  48       * @param string $parent_post_type Post type of the parent.
  49       */
  50  	public function __construct( $parent_post_type ) {
  51          $this->parent_post_type  = $parent_post_type;
  52          $this->rest_base         = 'revisions';
  53          $post_type_object        = get_post_type_object( $parent_post_type );
  54          $this->parent_base       = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  55          $this->namespace         = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
  56          $this->parent_controller = $post_type_object->get_rest_controller();
  57  
  58          if ( ! $this->parent_controller ) {
  59              $this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
  60          }
  61      }
  62  
  63      /**
  64       * Registers the routes for revisions based on post types supporting revisions.
  65       *
  66       * @since 4.7.0
  67       *
  68       * @see register_rest_route()
  69       */
  70  	public function register_routes() {
  71  
  72          register_rest_route(
  73              $this->namespace,
  74              '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base,
  75              array(
  76                  'args'   => array(
  77                      'parent' => array(
  78                          'description' => __( 'The ID for the parent of the revision.' ),
  79                          'type'        => 'integer',
  80                      ),
  81                  ),
  82                  array(
  83                      'methods'             => WP_REST_Server::READABLE,
  84                      'callback'            => array( $this, 'get_items' ),
  85                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  86                      'args'                => $this->get_collection_params(),
  87                  ),
  88                  'schema' => array( $this, 'get_public_item_schema' ),
  89              )
  90          );
  91  
  92          register_rest_route(
  93              $this->namespace,
  94              '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)',
  95              array(
  96                  'args'   => array(
  97                      'parent' => array(
  98                          'description' => __( 'The ID for the parent of the revision.' ),
  99                          'type'        => 'integer',
 100                      ),
 101                      'id'     => array(
 102                          'description' => __( 'Unique identifier for the revision.' ),
 103                          'type'        => 'integer',
 104                      ),
 105                  ),
 106                  array(
 107                      'methods'             => WP_REST_Server::READABLE,
 108                      'callback'            => array( $this, 'get_item' ),
 109                      'permission_callback' => array( $this, 'get_item_permissions_check' ),
 110                      'args'                => array(
 111                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 112                      ),
 113                  ),
 114                  array(
 115                      'methods'             => WP_REST_Server::DELETABLE,
 116                      'callback'            => array( $this, 'delete_item' ),
 117                      'permission_callback' => array( $this, 'delete_item_permissions_check' ),
 118                      'args'                => array(
 119                          'force' => array(
 120                              'type'        => 'boolean',
 121                              'default'     => false,
 122                              'description' => __( 'Required to be true, as revisions do not support trashing.' ),
 123                          ),
 124                      ),
 125                  ),
 126                  'schema' => array( $this, 'get_public_item_schema' ),
 127              )
 128          );
 129  
 130      }
 131  
 132      /**
 133       * Get the parent post, if the ID is valid.
 134       *
 135       * @since 4.7.2
 136       *
 137       * @param int $parent Supplied ID.
 138       * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
 139       */
 140  	protected function get_parent( $parent ) {
 141          $error = new WP_Error(
 142              'rest_post_invalid_parent',
 143              __( 'Invalid post parent ID.' ),
 144              array( 'status' => 404 )
 145          );
 146          if ( (int) $parent <= 0 ) {
 147              return $error;
 148          }
 149  
 150          $parent = get_post( (int) $parent );
 151          if ( empty( $parent ) || empty( $parent->ID ) || $this->parent_post_type !== $parent->post_type ) {
 152              return $error;
 153          }
 154  
 155          return $parent;
 156      }
 157  
 158      /**
 159       * Checks if a given request has access to get revisions.
 160       *
 161       * @since 4.7.0
 162       *
 163       * @param WP_REST_Request $request Full details about the request.
 164       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
 165       */
 166  	public function get_items_permissions_check( $request ) {
 167          $parent = $this->get_parent( $request['parent'] );
 168          if ( is_wp_error( $parent ) ) {
 169              return $parent;
 170          }
 171  
 172          if ( ! current_user_can( 'edit_post', $parent->ID ) ) {
 173              return new WP_Error(
 174                  'rest_cannot_read',
 175                  __( 'Sorry, you are not allowed to view revisions of this post.' ),
 176                  array( 'status' => rest_authorization_required_code() )
 177              );
 178          }
 179  
 180          return true;
 181      }
 182  
 183      /**
 184       * Get the revision, if the ID is valid.
 185       *
 186       * @since 4.7.2
 187       *
 188       * @param int $id Supplied ID.
 189       * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
 190       */
 191  	protected function get_revision( $id ) {
 192          $error = new WP_Error(
 193              'rest_post_invalid_id',
 194              __( 'Invalid revision ID.' ),
 195              array( 'status' => 404 )
 196          );
 197  
 198          if ( (int) $id <= 0 ) {
 199              return $error;
 200          }
 201  
 202          $revision = get_post( (int) $id );
 203          if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) {
 204              return $error;
 205          }
 206  
 207          return $revision;
 208      }
 209  
 210      /**
 211       * Gets a collection of revisions.
 212       *
 213       * @since 4.7.0
 214       *
 215       * @param WP_REST_Request $request Full details about the request.
 216       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 217       */
 218  	public function get_items( $request ) {
 219          $parent = $this->get_parent( $request['parent'] );
 220          if ( is_wp_error( $parent ) ) {
 221              return $parent;
 222          }
 223  
 224          // Ensure a search string is set in case the orderby is set to 'relevance'.
 225          if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
 226              return new WP_Error(
 227                  'rest_no_search_term_defined',
 228                  __( 'You need to define a search term to order by relevance.' ),
 229                  array( 'status' => 400 )
 230              );
 231          }
 232  
 233          // Ensure an include parameter is set in case the orderby is set to 'include'.
 234          if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
 235              return new WP_Error(
 236                  'rest_orderby_include_missing_include',
 237                  __( 'You need to define an include parameter to order by include.' ),
 238                  array( 'status' => 400 )
 239              );
 240          }
 241  
 242          if ( wp_revisions_enabled( $parent ) ) {
 243              $registered = $this->get_collection_params();
 244              $args       = array(
 245                  'post_parent'      => $parent->ID,
 246                  'post_type'        => 'revision',
 247                  'post_status'      => 'inherit',
 248                  'posts_per_page'   => -1,
 249                  'orderby'          => 'date ID',
 250                  'order'            => 'DESC',
 251                  'suppress_filters' => true,
 252              );
 253  
 254              $parameter_mappings = array(
 255                  'exclude'  => 'post__not_in',
 256                  'include'  => 'post__in',
 257                  'offset'   => 'offset',
 258                  'order'    => 'order',
 259                  'orderby'  => 'orderby',
 260                  'page'     => 'paged',
 261                  'per_page' => 'posts_per_page',
 262                  'search'   => 's',
 263              );
 264  
 265              foreach ( $parameter_mappings as $api_param => $wp_param ) {
 266                  if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
 267                      $args[ $wp_param ] = $request[ $api_param ];
 268                  }
 269              }
 270  
 271              // For backward-compatibility, 'date' needs to resolve to 'date ID'.
 272              if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
 273                  $args['orderby'] = 'date ID';
 274              }
 275  
 276              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 277              $args       = apply_filters( 'rest_revision_query', $args, $request );
 278              $query_args = $this->prepare_items_query( $args, $request );
 279  
 280              $revisions_query = new WP_Query();
 281              $revisions       = $revisions_query->query( $query_args );
 282              $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
 283              $page            = (int) $query_args['paged'];
 284              $total_revisions = $revisions_query->found_posts;
 285  
 286              if ( $total_revisions < 1 ) {
 287                  // Out-of-bounds, run the query again without LIMIT for total count.
 288                  unset( $query_args['paged'], $query_args['offset'] );
 289  
 290                  $count_query = new WP_Query();
 291                  $count_query->query( $query_args );
 292  
 293                  $total_revisions = $count_query->found_posts;
 294              }
 295  
 296              if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
 297                  $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
 298              } else {
 299                  $max_pages = $total_revisions > 0 ? 1 : 0;
 300              }
 301  
 302              if ( $total_revisions > 0 ) {
 303                  if ( $offset >= $total_revisions ) {
 304                      return new WP_Error(
 305                          'rest_revision_invalid_offset_number',
 306                          __( 'The offset number requested is larger than or equal to the number of available revisions.' ),
 307                          array( 'status' => 400 )
 308                      );
 309                  } elseif ( ! $offset && $page > $max_pages ) {
 310                      return new WP_Error(
 311                          'rest_revision_invalid_page_number',
 312                          __( 'The page number requested is larger than the number of pages available.' ),
 313                          array( 'status' => 400 )
 314                      );
 315                  }
 316              }
 317          } else {
 318              $revisions       = array();
 319              $total_revisions = 0;
 320              $max_pages       = 0;
 321              $page            = (int) $request['page'];
 322          }
 323  
 324          $response = array();
 325  
 326          foreach ( $revisions as $revision ) {
 327              $data       = $this->prepare_item_for_response( $revision, $request );
 328              $response[] = $this->prepare_response_for_collection( $data );
 329          }
 330  
 331          $response = rest_ensure_response( $response );
 332  
 333          $response->header( 'X-WP-Total', (int) $total_revisions );
 334          $response->header( 'X-WP-TotalPages', (int) $max_pages );
 335  
 336          $request_params = $request->get_query_params();
 337          $base           = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
 338  
 339          if ( $page > 1 ) {
 340              $prev_page = $page - 1;
 341  
 342              if ( $prev_page > $max_pages ) {
 343                  $prev_page = $max_pages;
 344              }
 345  
 346              $prev_link = add_query_arg( 'page', $prev_page, $base );
 347              $response->link_header( 'prev', $prev_link );
 348          }
 349          if ( $max_pages > $page ) {
 350              $next_page = $page + 1;
 351              $next_link = add_query_arg( 'page', $next_page, $base );
 352  
 353              $response->link_header( 'next', $next_link );
 354          }
 355  
 356          return $response;
 357      }
 358  
 359      /**
 360       * Checks if a given request has access to get a specific revision.
 361       *
 362       * @since 4.7.0
 363       *
 364       * @param WP_REST_Request $request Full details about the request.
 365       * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
 366       */
 367  	public function get_item_permissions_check( $request ) {
 368          return $this->get_items_permissions_check( $request );
 369      }
 370  
 371      /**
 372       * Retrieves one revision from the collection.
 373       *
 374       * @since 4.7.0
 375       *
 376       * @param WP_REST_Request $request Full details about the request.
 377       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 378       */
 379  	public function get_item( $request ) {
 380          $parent = $this->get_parent( $request['parent'] );
 381          if ( is_wp_error( $parent ) ) {
 382              return $parent;
 383          }
 384  
 385          $revision = $this->get_revision( $request['id'] );
 386          if ( is_wp_error( $revision ) ) {
 387              return $revision;
 388          }
 389  
 390          $response = $this->prepare_item_for_response( $revision, $request );
 391          return rest_ensure_response( $response );
 392      }
 393  
 394      /**
 395       * Checks if a given request has access to delete a revision.
 396       *
 397       * @since 4.7.0
 398       *
 399       * @param WP_REST_Request $request Full details about the request.
 400       * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
 401       */
 402  	public function delete_item_permissions_check( $request ) {
 403          $parent = $this->get_parent( $request['parent'] );
 404          if ( is_wp_error( $parent ) ) {
 405              return $parent;
 406          }
 407  
 408          $parent_post_type = get_post_type_object( $parent->post_type );
 409  
 410          if ( ! current_user_can( 'delete_post', $parent->ID ) ) {
 411              return new WP_Error(
 412                  'rest_cannot_delete',
 413                  __( 'Sorry, you are not allowed to delete revisions of this post.' ),
 414                  array( 'status' => rest_authorization_required_code() )
 415              );
 416          }
 417  
 418          $revision = $this->get_revision( $request['id'] );
 419          if ( is_wp_error( $revision ) ) {
 420              return $revision;
 421          }
 422  
 423          $response = $this->get_items_permissions_check( $request );
 424          if ( ! $response || is_wp_error( $response ) ) {
 425              return $response;
 426          }
 427  
 428          if ( ! current_user_can( 'delete_post', $revision->ID ) ) {
 429              return new WP_Error(
 430                  'rest_cannot_delete',
 431                  __( 'Sorry, you are not allowed to delete this revision.' ),
 432                  array( 'status' => rest_authorization_required_code() )
 433              );
 434          }
 435  
 436          return true;
 437      }
 438  
 439      /**
 440       * Deletes a single revision.
 441       *
 442       * @since 4.7.0
 443       *
 444       * @param WP_REST_Request $request Full details about the request.
 445       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 446       */
 447  	public function delete_item( $request ) {
 448          $revision = $this->get_revision( $request['id'] );
 449          if ( is_wp_error( $revision ) ) {
 450              return $revision;
 451          }
 452  
 453          $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
 454  
 455          // We don't support trashing for revisions.
 456          if ( ! $force ) {
 457              return new WP_Error(
 458                  'rest_trash_not_supported',
 459                  /* translators: %s: force=true */
 460                  sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ),
 461                  array( 'status' => 501 )
 462              );
 463          }
 464  
 465          $previous = $this->prepare_item_for_response( $revision, $request );
 466  
 467          $result = wp_delete_post( $request['id'], true );
 468  
 469          /**
 470           * Fires after a revision is deleted via the REST API.
 471           *
 472           * @since 4.7.0
 473           *
 474           * @param WP_Post|false|null $result The revision object (if it was deleted or moved to the Trash successfully)
 475           *                                   or false or null (failure). If the revision was moved to the Trash, $result represents
 476           *                                   its new state; if it was deleted, $result represents its state before deletion.
 477           * @param WP_REST_Request $request The request sent to the API.
 478           */
 479          do_action( 'rest_delete_revision', $result, $request );
 480  
 481          if ( ! $result ) {
 482              return new WP_Error(
 483                  'rest_cannot_delete',
 484                  __( 'The post cannot be deleted.' ),
 485                  array( 'status' => 500 )
 486              );
 487          }
 488  
 489          $response = new WP_REST_Response();
 490          $response->set_data(
 491              array(
 492                  'deleted'  => true,
 493                  'previous' => $previous->get_data(),
 494              )
 495          );
 496          return $response;
 497      }
 498  
 499      /**
 500       * Determines the allowed query_vars for a get_items() response and prepares
 501       * them for WP_Query.
 502       *
 503       * @since 5.0.0
 504       *
 505       * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
 506       * @param WP_REST_Request $request       Optional. Full details about the request.
 507       * @return array Items query arguments.
 508       */
 509  	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
 510          $query_args = array();
 511  
 512          foreach ( $prepared_args as $key => $value ) {
 513              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 514              $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 515          }
 516  
 517          // Map to proper WP_Query orderby param.
 518          if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
 519              $orderby_mappings = array(
 520                  'id'            => 'ID',
 521                  'include'       => 'post__in',
 522                  'slug'          => 'post_name',
 523                  'include_slugs' => 'post_name__in',
 524              );
 525  
 526              if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
 527                  $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
 528              }
 529          }
 530  
 531          return $query_args;
 532      }
 533  
 534      /**
 535       * Prepares the revision for the REST response.
 536       *
 537       * @since 4.7.0
 538       * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
 539       *
 540       * @param WP_Post         $item    Post revision object.
 541       * @param WP_REST_Request $request Request object.
 542       * @return WP_REST_Response Response object.
 543       */
 544  	public function prepare_item_for_response( $item, $request ) {
 545          // Restores the more descriptive, specific name for use within this method.
 546          $post            = $item;
 547          $GLOBALS['post'] = $post;
 548  
 549          setup_postdata( $post );
 550  
 551          $fields = $this->get_fields_for_response( $request );
 552          $data   = array();
 553  
 554          if ( in_array( 'author', $fields, true ) ) {
 555              $data['author'] = (int) $post->post_author;
 556          }
 557  
 558          if ( in_array( 'date', $fields, true ) ) {
 559              $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
 560          }
 561  
 562          if ( in_array( 'date_gmt', $fields, true ) ) {
 563              $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
 564          }
 565  
 566          if ( in_array( 'id', $fields, true ) ) {
 567              $data['id'] = $post->ID;
 568          }
 569  
 570          if ( in_array( 'modified', $fields, true ) ) {
 571              $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
 572          }
 573  
 574          if ( in_array( 'modified_gmt', $fields, true ) ) {
 575              $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
 576          }
 577  
 578          if ( in_array( 'parent', $fields, true ) ) {
 579              $data['parent'] = (int) $post->post_parent;
 580          }
 581  
 582          if ( in_array( 'slug', $fields, true ) ) {
 583              $data['slug'] = $post->post_name;
 584          }
 585  
 586          if ( in_array( 'guid', $fields, true ) ) {
 587              $data['guid'] = array(
 588                  /** This filter is documented in wp-includes/post-template.php */
 589                  'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
 590                  'raw'      => $post->guid,
 591              );
 592          }
 593  
 594          if ( in_array( 'title', $fields, true ) ) {
 595              $data['title'] = array(
 596                  'raw'      => $post->post_title,
 597                  'rendered' => get_the_title( $post->ID ),
 598              );
 599          }
 600  
 601          if ( in_array( 'content', $fields, true ) ) {
 602  
 603              $data['content'] = array(
 604                  'raw'      => $post->post_content,
 605                  /** This filter is documented in wp-includes/post-template.php */
 606                  'rendered' => apply_filters( 'the_content', $post->post_content ),
 607              );
 608          }
 609  
 610          if ( in_array( 'excerpt', $fields, true ) ) {
 611              $data['excerpt'] = array(
 612                  'raw'      => $post->post_excerpt,
 613                  'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
 614              );
 615          }
 616  
 617          $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
 618          $data     = $this->add_additional_fields_to_object( $data, $request );
 619          $data     = $this->filter_response_by_context( $data, $context );
 620          $response = rest_ensure_response( $data );
 621  
 622          if ( ! empty( $data['parent'] ) ) {
 623              $response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
 624          }
 625  
 626          /**
 627           * Filters a revision returned from the REST API.
 628           *
 629           * Allows modification of the revision right before it is returned.
 630           *
 631           * @since 4.7.0
 632           *
 633           * @param WP_REST_Response $response The response object.
 634           * @param WP_Post          $post     The original revision object.
 635           * @param WP_REST_Request  $request  Request used to generate the response.
 636           */
 637          return apply_filters( 'rest_prepare_revision', $response, $post, $request );
 638      }
 639  
 640      /**
 641       * Checks the post_date_gmt or modified_gmt and prepare any post or
 642       * modified date for single post output.
 643       *
 644       * @since 4.7.0
 645       *
 646       * @param string      $date_gmt GMT publication time.
 647       * @param string|null $date     Optional. Local publication time. Default null.
 648       * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
 649       */
 650  	protected function prepare_date_response( $date_gmt, $date = null ) {
 651          if ( '0000-00-00 00:00:00' === $date_gmt ) {
 652              return null;
 653          }
 654  
 655          if ( isset( $date ) ) {
 656              return mysql_to_rfc3339( $date );
 657          }
 658  
 659          return mysql_to_rfc3339( $date_gmt );
 660      }
 661  
 662      /**
 663       * Retrieves the revision's schema, conforming to JSON Schema.
 664       *
 665       * @since 4.7.0
 666       *
 667       * @return array Item schema data.
 668       */
 669  	public function get_item_schema() {
 670          if ( $this->schema ) {
 671              return $this->add_additional_fields_schema( $this->schema );
 672          }
 673  
 674          $schema = array(
 675              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 676              'title'      => "{$this->parent_post_type}-revision",
 677              'type'       => 'object',
 678              // Base properties for every Revision.
 679              'properties' => array(
 680                  'author'       => array(
 681                      'description' => __( 'The ID for the author of the revision.' ),
 682                      'type'        => 'integer',
 683                      'context'     => array( 'view', 'edit', 'embed' ),
 684                  ),
 685                  'date'         => array(
 686                      'description' => __( "The date the revision was published, in the site's timezone." ),
 687                      'type'        => 'string',
 688                      'format'      => 'date-time',
 689                      'context'     => array( 'view', 'edit', 'embed' ),
 690                  ),
 691                  'date_gmt'     => array(
 692                      'description' => __( 'The date the revision was published, as GMT.' ),
 693                      'type'        => 'string',
 694                      'format'      => 'date-time',
 695                      'context'     => array( 'view', 'edit' ),
 696                  ),
 697                  'guid'         => array(
 698                      'description' => __( 'GUID for the revision, as it exists in the database.' ),
 699                      'type'        => 'string',
 700                      'context'     => array( 'view', 'edit' ),
 701                  ),
 702                  'id'           => array(
 703                      'description' => __( 'Unique identifier for the revision.' ),
 704                      'type'        => 'integer',
 705                      'context'     => array( 'view', 'edit', 'embed' ),
 706                  ),
 707                  'modified'     => array(
 708                      'description' => __( "The date the revision was last modified, in the site's timezone." ),
 709                      'type'        => 'string',
 710                      'format'      => 'date-time',
 711                      'context'     => array( 'view', 'edit' ),
 712                  ),
 713                  'modified_gmt' => array(
 714                      'description' => __( 'The date the revision was last modified, as GMT.' ),
 715                      'type'        => 'string',
 716                      'format'      => 'date-time',
 717                      'context'     => array( 'view', 'edit' ),
 718                  ),
 719                  'parent'       => array(
 720                      'description' => __( 'The ID for the parent of the revision.' ),
 721                      'type'        => 'integer',
 722                      'context'     => array( 'view', 'edit', 'embed' ),
 723                  ),
 724                  'slug'         => array(
 725                      'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ),
 726                      'type'        => 'string',
 727                      'context'     => array( 'view', 'edit', 'embed' ),
 728                  ),
 729              ),
 730          );
 731  
 732          $parent_schema = $this->parent_controller->get_item_schema();
 733  
 734          if ( ! empty( $parent_schema['properties']['title'] ) ) {
 735              $schema['properties']['title'] = $parent_schema['properties']['title'];
 736          }
 737  
 738          if ( ! empty( $parent_schema['properties']['content'] ) ) {
 739              $schema['properties']['content'] = $parent_schema['properties']['content'];
 740          }
 741  
 742          if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
 743              $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
 744          }
 745  
 746          if ( ! empty( $parent_schema['properties']['guid'] ) ) {
 747              $schema['properties']['guid'] = $parent_schema['properties']['guid'];
 748          }
 749  
 750          $this->schema = $schema;
 751  
 752          return $this->add_additional_fields_schema( $this->schema );
 753      }
 754  
 755      /**
 756       * Retrieves the query params for collections.
 757       *
 758       * @since 4.7.0
 759       *
 760       * @return array Collection parameters.
 761       */
 762  	public function get_collection_params() {
 763          $query_params = parent::get_collection_params();
 764  
 765          $query_params['context']['default'] = 'view';
 766  
 767          unset( $query_params['per_page']['default'] );
 768  
 769          $query_params['exclude'] = array(
 770              'description' => __( 'Ensure result set excludes specific IDs.' ),
 771              'type'        => 'array',
 772              'items'       => array(
 773                  'type' => 'integer',
 774              ),
 775              'default'     => array(),
 776          );
 777  
 778          $query_params['include'] = array(
 779              'description' => __( 'Limit result set to specific IDs.' ),
 780              'type'        => 'array',
 781              'items'       => array(
 782                  'type' => 'integer',
 783              ),
 784              'default'     => array(),
 785          );
 786  
 787          $query_params['offset'] = array(
 788              'description' => __( 'Offset the result set by a specific number of items.' ),
 789              'type'        => 'integer',
 790          );
 791  
 792          $query_params['order'] = array(
 793              'description' => __( 'Order sort attribute ascending or descending.' ),
 794              'type'        => 'string',
 795              'default'     => 'desc',
 796              'enum'        => array( 'asc', 'desc' ),
 797          );
 798  
 799          $query_params['orderby'] = array(
 800              'description' => __( 'Sort collection by object attribute.' ),
 801              'type'        => 'string',
 802              'default'     => 'date',
 803              'enum'        => array(
 804                  'date',
 805                  'id',
 806                  'include',
 807                  'relevance',
 808                  'slug',
 809                  'include_slugs',
 810                  'title',
 811              ),
 812          );
 813  
 814          return $query_params;
 815      }
 816  
 817      /**
 818       * Checks the post excerpt and prepare it for single post output.
 819       *
 820       * @since 4.7.0
 821       *
 822       * @param string  $excerpt The post excerpt.
 823       * @param WP_Post $post    Post revision object.
 824       * @return string Prepared excerpt or empty string.
 825       */
 826  	protected function prepare_excerpt_response( $excerpt, $post ) {
 827  
 828          /** This filter is documented in wp-includes/post-template.php */
 829          $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
 830  
 831          if ( empty( $excerpt ) ) {
 832              return '';
 833          }
 834  
 835          return $excerpt;
 836      }
 837  }


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1