public function prepare_item_for_response( $item, $request ) {
/*
* Save the previous global post so it can be restored before returning.
* Preparing the revision sets up the global post and post data, which
* must not leak into the rest of the request (e.g. the autosaves endpoint
* is preloaded in the block editor, where a leaked global post can cause
* the editor to be initialized with the wrong post).
*
* Note that $post is intentionally not declared as a global here. It must
* remain local to this method so that a filter which reassigns the global
* post while the response is being prepared (for example on 'the_content')
* cannot change which post the remaining fields are read from.
*/
$previous_post = isset( $GLOBALS['post'] ) && $GLOBALS['post'] instanceof WP_Post ? $GLOBALS['post'] : null;
// Restores the more descriptive, specific name for use within this method.
$post = $item;
$GLOBALS['post'] = $post;
setup_postdata( $post );
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
$response = apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request );
$this->restore_post_data( $previous_post );
return $response;
}
$fields = $this->get_fields_for_response( $request );
$data = array();
if ( in_array( 'author', $fields, true ) ) {
$data['author'] = (int) $post->post_author;
}
if ( in_array( 'date', $fields, true ) ) {
$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
}
if ( in_array( 'date_gmt', $fields, true ) ) {
$data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
}
if ( in_array( 'id', $fields, true ) ) {
$data['id'] = $post->ID;
}
if ( in_array( 'modified', $fields, true ) ) {
$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
}
if ( in_array( 'modified_gmt', $fields, true ) ) {
$data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
}
if ( in_array( 'parent', $fields, true ) ) {
$data['parent'] = (int) $post->post_parent;
}
if ( in_array( 'slug', $fields, true ) ) {
$data['slug'] = $post->post_name;
}
if ( rest_is_field_included( 'guid', $fields ) ) {
$data['guid'] = array();
}
if ( rest_is_field_included( 'guid.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID );
}
if ( rest_is_field_included( 'guid.raw', $fields ) ) {
$data['guid']['raw'] = $post->guid;
}
if ( rest_is_field_included( 'title', $fields ) ) {
$data['title'] = array();
}
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $post->post_title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
$data['title']['rendered'] = get_the_title( $post->ID );
}
if ( rest_is_field_included( 'content', $fields ) ) {
$data['content'] = array();
}
if ( rest_is_field_included( 'content.raw', $fields ) ) {
$data['content']['raw'] = $post->post_content;
}
if ( rest_is_field_included( 'content.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['content']['rendered'] = apply_filters( 'the_content', $post->post_content );
}
if ( rest_is_field_included( 'excerpt', $fields ) ) {
$data['excerpt'] = array();
}
if ( rest_is_field_included( 'excerpt.raw', $fields ) ) {
$data['excerpt']['raw'] = $post->post_excerpt;
}
if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) {
$data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post );
}
if ( rest_is_field_included( 'meta', $fields ) ) {
$data['meta'] = $this->meta->get_value( $post->ID, $request );
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
if ( ! empty( $data['parent'] ) ) {
$response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) );
}
/**
* Filters a revision returned from the REST API.
*
* Allows modification of the revision right before it is returned.
*
* @since 4.7.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post The original revision object.
* @param WP_REST_Request $request Request used to generate the response.
*/
$response = apply_filters( 'rest_prepare_revision', $response, $post, $request );
$this->restore_post_data( $previous_post );
return $response;
}