| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: Gutenberg_REST_Revisions_Controller class |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Controller which provides REST endpoint for revisions. |
| 10 |
* |
| 11 |
* This overrides the core WP_REST_Revisions_Controller to use |
| 12 |
* rest_is_field_included() instead of in_array() for content, title, excerpt, |
| 13 |
* and guid fields. This allows clients to request individual sub-fields |
| 14 |
* (e.g. content.raw without content.rendered) via the _fields parameter, |
| 15 |
* avoiding expensive rendering when only raw data is needed. |
| 16 |
* |
| 17 |
* @see WP_REST_Revisions_Controller |
| 18 |
*/ |
| 19 |
class Gutenberg_REST_Revisions_Controller extends WP_REST_Revisions_Controller { |
| 20 |
|
| 21 |
/** |
| 22 |
* Prepares the revision for the REST response. |
| 23 |
* |
| 24 |
* @param WP_Post $item Post revision object. |
| 25 |
* @param WP_REST_Request $request Request object. |
| 26 |
* @return WP_REST_Response Response object. |
| 27 |
*/ |
| 28 |
public function prepare_item_for_response( $item, $request ) { |
| 29 |
// Restores the more descriptive, specific name for use within this method. |
| 30 |
$post = $item; |
| 31 |
|
| 32 |
$GLOBALS['post'] = $post; |
| 33 |
|
| 34 |
setup_postdata( $post ); |
| 35 |
|
| 36 |
// Don't prepare the response body for HEAD requests. |
| 37 |
if ( $request->is_method( 'HEAD' ) ) { |
| 38 |
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */ |
| 39 |
return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request ); |
| 40 |
} |
| 41 |
|
| 42 |
$fields = $this->get_fields_for_response( $request ); |
| 43 |
$data = array(); |
| 44 |
|
| 45 |
if ( in_array( 'author', $fields, true ) ) { |
| 46 |
$data['author'] = (int) $post->post_author; |
| 47 |
} |
| 48 |
|
| 49 |
if ( in_array( 'date', $fields, true ) ) { |
| 50 |
$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( in_array( 'date_gmt', $fields, true ) ) { |
| 54 |
$data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); |
| 55 |
} |
| 56 |
|
| 57 |
if ( in_array( 'id', $fields, true ) ) { |
| 58 |
$data['id'] = $post->ID; |
| 59 |
} |
| 60 |
|
| 61 |
if ( in_array( 'modified', $fields, true ) ) { |
| 62 |
$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( in_array( 'modified_gmt', $fields, true ) ) { |
| 66 |
$data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( in_array( 'parent', $fields, true ) ) { |
| 70 |
$data['parent'] = (int) $post->post_parent; |
| 71 |
} |
| 72 |
|
| 73 |
if ( in_array( 'slug', $fields, true ) ) { |
| 74 |
$data['slug'] = $post->post_name; |
| 75 |
} |
| 76 |
|
| 77 |
if ( rest_is_field_included( 'guid', $fields ) ) { |
| 78 |
$data['guid'] = array(); |
| 79 |
} |
| 80 |
if ( rest_is_field_included( 'guid.rendered', $fields ) ) { |
| 81 |
/** This filter is documented in wp-includes/post-template.php */ |
| 82 |
$data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID ); |
| 83 |
} |
| 84 |
if ( rest_is_field_included( 'guid.raw', $fields ) ) { |
| 85 |
$data['guid']['raw'] = $post->guid; |
| 86 |
} |
| 87 |
|
| 88 |
if ( rest_is_field_included( 'title', $fields ) ) { |
| 89 |
$data['title'] = array(); |
| 90 |
} |
| 91 |
if ( rest_is_field_included( 'title.raw', $fields ) ) { |
| 92 |
$data['title']['raw'] = $post->post_title; |
| 93 |
} |
| 94 |
if ( rest_is_field_included( 'title.rendered', $fields ) ) { |
| 95 |
$data['title']['rendered'] = get_the_title( $post->ID ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( rest_is_field_included( 'content', $fields ) ) { |
| 99 |
$data['content'] = array(); |
| 100 |
} |
| 101 |
if ( rest_is_field_included( 'content.raw', $fields ) ) { |
| 102 |
$data['content']['raw'] = $post->post_content; |
| 103 |
} |
| 104 |
if ( rest_is_field_included( 'content.rendered', $fields ) ) { |
| 105 |
/** This filter is documented in wp-includes/post-template.php */ |
| 106 |
$data['content']['rendered'] = apply_filters( 'the_content', $post->post_content ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( rest_is_field_included( 'excerpt', $fields ) ) { |
| 110 |
$data['excerpt'] = array(); |
| 111 |
} |
| 112 |
if ( rest_is_field_included( 'excerpt.raw', $fields ) ) { |
| 113 |
$data['excerpt']['raw'] = $post->post_excerpt; |
| 114 |
} |
| 115 |
if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) { |
| 116 |
$data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( rest_is_field_included( 'meta', $fields ) ) { |
| 120 |
$data['meta'] = $this->meta->get_value( $post->ID, $request ); |
| 121 |
} |
| 122 |
|
| 123 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 124 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 125 |
$data = $this->filter_response_by_context( $data, $context ); |
| 126 |
$response = rest_ensure_response( $data ); |
| 127 |
|
| 128 |
if ( ! empty( $data['parent'] ) ) { |
| 129 |
$response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Filters a revision returned from the REST API. |
| 134 |
* |
| 135 |
* Allows modification of the revision right before it is returned. |
| 136 |
* |
| 137 |
* @param WP_REST_Response $response The response object. |
| 138 |
* @param WP_Post $post The original revision object. |
| 139 |
* @param WP_REST_Request $request Request used to generate the response. |
| 140 |
*/ |
| 141 |
return apply_filters( 'rest_prepare_revision', $response, $post, $request ); |
| 142 |
} |
| 143 |
} |
| 144 |
|