Skip to content

Commit

Permalink
Only generate item data for the fields specified in the request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Arntz committed May 3, 2018
1 parent 703322c commit d80560f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions lib/class-wp-rest-search-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,20 @@ public function prepare_item_for_response( $post, $request ) {

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`
Expand All @@ -307,19 +307,19 @@ public function prepare_item_for_response( $post, $request ) {
$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 ),
'raw' => $post->guid,
);
}

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
Expand All @@ -332,23 +332,23 @@ public function prepare_item_for_response( $post, $request ) {
$data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
}

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 ) ) {
if ( post_type_supports( $post->post_type, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );

Expand All @@ -366,7 +366,7 @@ public function prepare_item_for_response( $post, $request ) {
}
}

if ( ! empty( $schema['properties']['content'] ) ) {
if ( in_array( 'content', $fields, true ) ) {
if ( post_type_supports( $post->post_type, 'editor' ) ) {
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters( 'the_content', $post->post_content );
Expand All @@ -384,7 +384,7 @@ public function prepare_item_for_response( $post, $request ) {
}
}

if ( ! empty( $schema['properties']['excerpt'] ) ) {
if ( in_array( 'excerpt', $fields, true ) ) {
if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
Expand All @@ -402,7 +402,7 @@ public function prepare_item_for_response( $post, $request ) {
}
}

if ( ! empty( $schema['properties']['author'] ) ) {
if ( in_array( 'author', $fields, true ) ) {
if ( post_type_supports( $post->post_type, 'author' ) ) {
$data['author'] = (int) $post->post_author;
} else {
Expand Down

0 comments on commit d80560f

Please sign in to comment.