diff --git a/lib/class-wp-rest-search-controller.php b/lib/class-wp-rest-search-controller.php index 2f9d237309ae7b..5f89d74914e71e 100644 --- a/lib/class-wp-rest-search-controller.php +++ b/lib/class-wp-rest-search-controller.php @@ -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` @@ -307,7 +307,7 @@ 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 ), @@ -315,11 +315,11 @@ public function prepare_item_for_response( $post, $request ) { ); } - 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 @@ -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' ) ); @@ -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 ); @@ -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 ) ); @@ -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 {