[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7