[ 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->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 $parent 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          $parent_post_type = get_post_type_object( $parent->post_type );
 353          if ( ! current_user_can( $parent_post_type->cap->delete_post, $parent->ID ) ) {
 354              return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete revisions of this post.' ), array( 'status' => rest_authorization_required_code() ) );
 355          }
 356  
 357          $revision = $this->get_revision( $request['id'] );
 358          if ( is_wp_error( $revision ) ) {
 359              return $revision;
 360          }
 361  
 362          $response = $this->prepare_item_for_response( $revision, $request );
 363          return rest_ensure_response( $response );
 364      }
 365  
 366      /**
 367       * Checks if a given request has access to delete a revision.
 368       *
 369       * @since 4.7.0
 370       *
 371       * @param WP_REST_Request $request Full details about the request.
 372       * @return bool|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
 373       */
 374  	public function delete_item_permissions_check( $request ) {
 375          $parent = $this->get_parent( $request['parent'] );
 376          if ( is_wp_error( $parent ) ) {
 377              return $parent;
 378          }
 379  
 380          $revision = $this->get_revision( $request['id'] );
 381          if ( is_wp_error( $revision ) ) {
 382              return $revision;
 383          }
 384  
 385          $response = $this->get_items_permissions_check( $request );
 386          if ( ! $response || is_wp_error( $response ) ) {
 387              return $response;
 388          }
 389  
 390          $post_type = get_post_type_object( 'revision' );
 391  
 392          if ( ! current_user_can( $post_type->cap->delete_post, $revision->ID ) ) {
 393              return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this revision.' ), array( 'status' => rest_authorization_required_code() ) );
 394          }
 395  
 396          return true;
 397      }
 398  
 399      /**
 400       * Deletes a single revision.
 401       *
 402       * @since 4.7.0
 403       *
 404       * @param WP_REST_Request $request Full details about the request.
 405       * @return true|WP_Error True on success, or WP_Error object on failure.
 406       */
 407  	public function delete_item( $request ) {
 408          $revision = $this->get_revision( $request['id'] );
 409          if ( is_wp_error( $revision ) ) {
 410              return $revision;
 411          }
 412  
 413          $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
 414  
 415          // We don't support trashing for revisions.
 416          if ( ! $force ) {
 417              /* translators: %s: force=true */
 418              return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
 419          }
 420  
 421          $previous = $this->prepare_item_for_response( $revision, $request );
 422  
 423          $result = wp_delete_post( $request['id'], true );
 424  
 425          /**
 426           * Fires after a revision is deleted via the REST API.
 427           *
 428           * @since 4.7.0
 429           *
 430           * @param (mixed) $result The revision object (if it was deleted or moved to the trash successfully)
 431           *                        or false (failure). If the revision was moved to the trash, $result represents
 432           *                        its new state; if it was deleted, $result represents its state before deletion.
 433           * @param WP_REST_Request $request The request sent to the API.
 434           */
 435          do_action( 'rest_delete_revision', $result, $request );
 436  
 437          if ( ! $result ) {
 438              return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
 439          }
 440  
 441          $response = new WP_REST_Response();
 442          $response->set_data(
 443              array(
 444                  'deleted'  => true,
 445                  'previous' => $previous->get_data(),
 446              )
 447          );
 448          return $response;
 449      }
 450  
 451      /**
 452       * Determines the allowed query_vars for a get_items() response and prepares
 453       * them for WP_Query.
 454       *
 455       * @since 5.0.0
 456       *
 457       * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
 458       * @param WP_REST_Request $request       Optional. Full details about the request.
 459       * @return array Items query arguments.
 460       */
 461  	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
 462          $query_args = array();
 463  
 464          foreach ( $prepared_args as $key => $value ) {
 465              /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 466              $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 467          }
 468  
 469          // Map to proper WP_Query orderby param.
 470          if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
 471              $orderby_mappings = array(
 472                  'id'            => 'ID',
 473                  'include'       => 'post__in',
 474                  'slug'          => 'post_name',
 475                  'include_slugs' => 'post_name__in',
 476              );
 477  
 478              if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
 479                  $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
 480              }
 481          }
 482  
 483          return $query_args;
 484      }
 485  
 486      /**
 487       * Prepares the revision for the REST response.
 488       *
 489       * @since 4.7.0
 490       *
 491       * @param WP_Post         $post    Post revision object.
 492       * @param WP_REST_Request $request Request object.
 493       * @return WP_REST_Response Response object.
 494       */
 495  	public function prepare_item_for_response( $post, $request ) {
 496          $GLOBALS['post'] = $post;
 497  
 498          setup_postdata( $post );
 499  
 500          $fields = $this->get_fields_for_response( $request );
 501          $data   = array();
 502  
 503          if ( in_array( 'author', $fields, true ) ) {
 504              $data['author'] = (int) $post->post_author;
 505          }
 506  
 507          if ( in_array( 'date', $fields, true ) ) {
 508              $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
 509          }
 510  
 511          if ( in_array( 'date_gmt', $fields, true ) ) {
 512              $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
 513          }
 514  
 515          if ( in_array( 'id', $fields, true ) ) {
 516              $data['id'] = $post->ID;
 517          }
 518  
 519          if ( in_array( 'modified', $fields, true ) ) {
 520              $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
 521          }
 522  
 523          if ( in_array( 'modified_gmt', $fields, true ) ) {
 524              $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
 525          }
 526  
 527          if ( in_array( 'parent', $fields, true ) ) {
 528              $data['parent'] = (int) $post->post_parent;
 529          }
 530  
 531          if ( in_array( 'slug', $fields, true ) ) {
 532              $data['slug'] = $post->post_name;
 533          }
 534  
 535          if ( in_array( 'guid', $fields, true ) ) {
 536              $data['guid'] = array(
 537                  /** This filter is documented in wp-includes/post-template.php */
 538                  'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
 539                  'raw'      => $post->guid,
 540              );
 541          }
 542  
 543          if ( in_array( 'title', $fields, true ) ) {
 544              $data['title'] = array(
 545                  'raw'      => $post->post_title,
 546                  'rendered' => get_the_title( $post->ID ),
 547              );
 548          }
 549  
 550          if ( in_array( 'content', $fields, true ) ) {
 551  
 552              $data['content'] = array(
 553                  'raw'      => $post->post_content,
 554                  /** This filter is documented in wp-includes/post-template.php */
 555                  'rendered' => apply_filters( 'the_content', $post->post_content ),
 556              );
 557          }
 558  
 559          if ( in_array( 'excerpt', $fields, true ) ) {
 560              $data['excerpt'] = array(
 561                  'raw'      => $post->post_excerpt,
 562                  'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
 563              );
 564          }
 565  
 566          $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
 567          $data     = $this->add_additional_fields_to_object( $data, $request );
 568          $data     = $this->filter_response_by_context( $data, $context );
 569          $response = rest_ensure_response( $data );
 570  
 571          if ( ! empty( $data['parent'] ) ) {
 572              $response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
 573          }
 574  
 575          /**
 576           * Filters a revision returned from the API.
 577           *
 578           * Allows modification of the revision right before it is returned.
 579           *
 580           * @since 4.7.0
 581           *
 582           * @param WP_REST_Response $response The response object.
 583           * @param WP_Post          $post     The original revision object.
 584           * @param WP_REST_Request  $request  Request used to generate the response.
 585           */
 586          return apply_filters( 'rest_prepare_revision', $response, $post, $request );
 587      }
 588  
 589      /**
 590       * Checks the post_date_gmt or modified_gmt and prepare any post or
 591       * modified date for single post output.
 592       *
 593       * @since 4.7.0
 594       *
 595       * @param string      $date_gmt GMT publication time.
 596       * @param string|null $date     Optional. Local publication time. Default null.
 597       * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
 598       */
 599  	protected function prepare_date_response( $date_gmt, $date = null ) {
 600          if ( '0000-00-00 00:00:00' === $date_gmt ) {
 601              return null;
 602          }
 603  
 604          if ( isset( $date ) ) {
 605              return mysql_to_rfc3339( $date );
 606          }
 607  
 608          return mysql_to_rfc3339( $date_gmt );
 609      }
 610  
 611      /**
 612       * Retrieves the revision's schema, conforming to JSON Schema.
 613       *
 614       * @since 4.7.0
 615       *
 616       * @return array Item schema data.
 617       */
 618  	public function get_item_schema() {
 619          if ( $this->schema ) {
 620              return $this->add_additional_fields_schema( $this->schema );
 621          }
 622  
 623          $schema = array(
 624              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 625              'title'      => "{$this->parent_post_type}-revision",
 626              'type'       => 'object',
 627              // Base properties for every Revision.
 628              'properties' => array(
 629                  'author'       => array(
 630                      'description' => __( 'The ID for the author of the object.' ),
 631                      'type'        => 'integer',
 632                      'context'     => array( 'view', 'edit', 'embed' ),
 633                  ),
 634                  'date'         => array(
 635                      'description' => __( "The date the object was published, in the site's timezone." ),
 636                      'type'        => 'string',
 637                      'format'      => 'date-time',
 638                      'context'     => array( 'view', 'edit', 'embed' ),
 639                  ),
 640                  'date_gmt'     => array(
 641                      'description' => __( 'The date the object was published, as GMT.' ),
 642                      'type'        => 'string',
 643                      'format'      => 'date-time',
 644                      'context'     => array( 'view', 'edit' ),
 645                  ),
 646                  'guid'         => array(
 647                      'description' => __( 'GUID for the object, as it exists in the database.' ),
 648                      'type'        => 'string',
 649                      'context'     => array( 'view', 'edit' ),
 650                  ),
 651                  'id'           => array(
 652                      'description' => __( 'Unique identifier for the object.' ),
 653                      'type'        => 'integer',
 654                      'context'     => array( 'view', 'edit', 'embed' ),
 655                  ),
 656                  'modified'     => array(
 657                      'description' => __( "The date the object was last modified, in the site's timezone." ),
 658                      'type'        => 'string',
 659                      'format'      => 'date-time',
 660                      'context'     => array( 'view', 'edit' ),
 661                  ),
 662                  'modified_gmt' => array(
 663                      'description' => __( 'The date the object was last modified, as GMT.' ),
 664                      'type'        => 'string',
 665                      'format'      => 'date-time',
 666                      'context'     => array( 'view', 'edit' ),
 667                  ),
 668                  'parent'       => array(
 669                      'description' => __( 'The ID for the parent of the object.' ),
 670                      'type'        => 'integer',
 671                      'context'     => array( 'view', 'edit', 'embed' ),
 672                  ),
 673                  'slug'         => array(
 674                      'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
 675                      'type'        => 'string',
 676                      'context'     => array( 'view', 'edit', 'embed' ),
 677                  ),
 678              ),
 679          );
 680  
 681          $parent_schema = $this->parent_controller->get_item_schema();
 682  
 683          if ( ! empty( $parent_schema['properties']['title'] ) ) {
 684              $schema['properties']['title'] = $parent_schema['properties']['title'];
 685          }
 686  
 687          if ( ! empty( $parent_schema['properties']['content'] ) ) {
 688              $schema['properties']['content'] = $parent_schema['properties']['content'];
 689          }
 690  
 691          if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
 692              $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
 693          }
 694  
 695          if ( ! empty( $parent_schema['properties']['guid'] ) ) {
 696              $schema['properties']['guid'] = $parent_schema['properties']['guid'];
 697          }
 698  
 699          $this->schema = $schema;
 700          return $this->add_additional_fields_schema( $this->schema );
 701      }
 702  
 703      /**
 704       * Retrieves the query params for collections.
 705       *
 706       * @since 4.7.0
 707       *
 708       * @return array Collection parameters.
 709       */
 710  	public function get_collection_params() {
 711          $query_params = parent::get_collection_params();
 712  
 713          $query_params['context']['default'] = 'view';
 714  
 715          unset( $query_params['per_page']['default'] );
 716  
 717          $query_params['exclude'] = array(
 718              'description' => __( 'Ensure result set excludes specific IDs.' ),
 719              'type'        => 'array',
 720              'items'       => array(
 721                  'type' => 'integer',
 722              ),
 723              'default'     => array(),
 724          );
 725  
 726          $query_params['include'] = array(
 727              'description' => __( 'Limit result set to specific IDs.' ),
 728              'type'        => 'array',
 729              'items'       => array(
 730                  'type' => 'integer',
 731              ),
 732              'default'     => array(),
 733          );
 734  
 735          $query_params['offset'] = array(
 736              'description' => __( 'Offset the result set by a specific number of items.' ),
 737              'type'        => 'integer',
 738          );
 739  
 740          $query_params['order'] = array(
 741              'description' => __( 'Order sort attribute ascending or descending.' ),
 742              'type'        => 'string',
 743              'default'     => 'desc',
 744              'enum'        => array( 'asc', 'desc' ),
 745          );
 746  
 747          $query_params['orderby'] = array(
 748              'description' => __( 'Sort collection by object attribute.' ),
 749              'type'        => 'string',
 750              'default'     => 'date',
 751              'enum'        => array(
 752                  'date',
 753                  'id',
 754                  'include',
 755                  'relevance',
 756                  'slug',
 757                  'include_slugs',
 758                  'title',
 759              ),
 760          );
 761  
 762          return $query_params;
 763      }
 764  
 765      /**
 766       * Checks the post excerpt and prepare it for single post output.
 767       *
 768       * @since 4.7.0
 769       *
 770       * @param string  $excerpt The post excerpt.
 771       * @param WP_Post $post    Post revision object.
 772       * @return string Prepared excerpt or empty string.
 773       */
 774  	protected function prepare_excerpt_response( $excerpt, $post ) {
 775  
 776          /** This filter is documented in wp-includes/post-template.php */
 777          $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
 778  
 779          if ( empty( $excerpt ) ) {
 780              return '';
 781          }
 782  
 783          return $excerpt;
 784      }
 785  }


Generated: Wed Sep 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1