ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-rest-revisions-controller.php (24555B)


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