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


Generated : Thu Apr 2 08:20:01 2020 Cross-referenced by PHPXref