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


Generated: Wed Nov 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1