REST API: Filter responses based on the _fields parameter, before data is processed.

Historically, the REST API would generate the entire response object, including running expensive filters, then it would apply the `_fields` parameter, discarding the fields that weren't specificed.

This change causes `_fields` to be applied earlier, so that only requested fields are processed.

Props danielbachhuber.
See #43874.



git-svn-id: https://develop.svn.wordpress.org/trunk@43087 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-05-02 01:24:30 +00:00
parent 1a4e28818f
commit 4ac3f4c13a
22 changed files with 526 additions and 167 deletions

View File

@@ -1408,20 +1408,20 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
setup_postdata( $post );
$schema = $this->get_item_schema();
$fields = $this->get_fields_for_response( $request );
// Base fields for every post.
$data = array();
if ( ! empty( $schema['properties']['id'] ) ) {
if ( in_array( 'id', $fields, true ) ) {
$data['id'] = $post->ID;
}
if ( ! empty( $schema['properties']['date'] ) ) {
if ( in_array( 'date', $fields, true ) ) {
$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
}
if ( ! empty( $schema['properties']['date_gmt'] ) ) {
if ( in_array( 'date_gmt', $fields, true ) ) {
// For drafts, `post_date_gmt` may not be set, indicating that the
// date of the draft should be updated each time it is saved (see
// #38883). In this case, shim the value based on the `post_date`
@@ -1434,7 +1434,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
}
if ( ! empty( $schema['properties']['guid'] ) ) {
if ( in_array( 'guid', $fields, true ) ) {
$data['guid'] = array(
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
@@ -1442,11 +1442,11 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
}
if ( ! empty( $schema['properties']['modified'] ) ) {
if ( in_array( 'modified', $fields, true ) ) {
$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
}
if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
if ( in_array( 'modified_gmt', $fields, true ) ) {
// For drafts, `post_modified_gmt` may not be set (see
// `post_date_gmt` comments above). In this case, shim the value
// based on the `post_modified` field with the site's timezone
@@ -1459,27 +1459,27 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
}
if ( ! empty( $schema['properties']['password'] ) ) {
if ( in_array( 'password', $fields, true ) ) {
$data['password'] = $post->post_password;
}
if ( ! empty( $schema['properties']['slug'] ) ) {
if ( in_array( 'slug', $fields, true ) ) {
$data['slug'] = $post->post_name;
}
if ( ! empty( $schema['properties']['status'] ) ) {
if ( in_array( 'status', $fields, true ) ) {
$data['status'] = $post->post_status;
}
if ( ! empty( $schema['properties']['type'] ) ) {
if ( in_array( 'type', $fields, true ) ) {
$data['type'] = $post->post_type;
}
if ( ! empty( $schema['properties']['link'] ) ) {
if ( in_array( 'link', $fields, true ) ) {
$data['link'] = get_permalink( $post->ID );
}
if ( ! empty( $schema['properties']['title'] ) ) {
if ( in_array( 'title', $fields, true ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
$data['title'] = array(
@@ -1499,7 +1499,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$has_password_filter = true;
}
if ( ! empty( $schema['properties']['content'] ) ) {
if ( in_array( 'content', $fields, true ) ) {
$data['content'] = array(
'raw' => $post->post_content,
/** This filter is documented in wp-includes/post-template.php */
@@ -1508,7 +1508,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
}
if ( ! empty( $schema['properties']['excerpt'] ) ) {
if ( in_array( 'excerpt', $fields, true ) ) {
/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
$data['excerpt'] = array(
@@ -1523,35 +1523,35 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
remove_filter( 'post_password_required', '__return_false' );
}
if ( ! empty( $schema['properties']['author'] ) ) {
if ( in_array( 'author', $fields, true ) ) {
$data['author'] = (int) $post->post_author;
}
if ( ! empty( $schema['properties']['featured_media'] ) ) {
if ( in_array( 'featured_media', $fields, true ) ) {
$data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
}
if ( ! empty( $schema['properties']['parent'] ) ) {
if ( in_array( 'parent', $fields, true ) ) {
$data['parent'] = (int) $post->post_parent;
}
if ( ! empty( $schema['properties']['menu_order'] ) ) {
if ( in_array( 'menu_order', $fields, true ) ) {
$data['menu_order'] = (int) $post->menu_order;
}
if ( ! empty( $schema['properties']['comment_status'] ) ) {
if ( in_array( 'comment_status', $fields, true ) ) {
$data['comment_status'] = $post->comment_status;
}
if ( ! empty( $schema['properties']['ping_status'] ) ) {
if ( in_array( 'ping_status', $fields, true ) ) {
$data['ping_status'] = $post->ping_status;
}
if ( ! empty( $schema['properties']['sticky'] ) ) {
if ( in_array( 'sticky', $fields, true ) ) {
$data['sticky'] = is_sticky( $post->ID );
}
if ( ! empty( $schema['properties']['template'] ) ) {
if ( in_array( 'template', $fields, true ) ) {
if ( $template = get_page_template_slug( $post->ID ) ) {
$data['template'] = $template;
} else {
@@ -1559,7 +1559,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
}
if ( ! empty( $schema['properties']['format'] ) ) {
if ( in_array( 'format', $fields, true ) ) {
$data['format'] = get_post_format( $post->ID );
// Fill in blank post format.
@@ -1568,7 +1568,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
}
if ( ! empty( $schema['properties']['meta'] ) ) {
if ( in_array( 'meta', $fields, true ) ) {
$data['meta'] = $this->meta->get_value( $post->ID, $request );
}
@@ -1577,7 +1577,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
if ( ! empty( $schema['properties'][ $base ] ) ) {
if ( in_array( $base, $fields, true ) ) {
$terms = get_the_terms( $post, $taxonomy->name );
$data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
}