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

@@ -862,35 +862,79 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $comment, $request ) {
$data = array(
'id' => (int) $comment->comment_ID,
'post' => (int) $comment->comment_post_ID,
'parent' => (int) $comment->comment_parent,
'author' => (int) $comment->user_id,
'author_name' => $comment->comment_author,
'author_email' => $comment->comment_author_email,
'author_url' => $comment->comment_author_url,
'author_ip' => $comment->comment_author_IP,
'author_user_agent' => $comment->comment_agent,
'date' => mysql_to_rfc3339( $comment->comment_date ),
'date_gmt' => mysql_to_rfc3339( $comment->comment_date_gmt ),
'content' => array(
$fields = $this->get_fields_for_response( $request );
$data = array();
if ( in_array( 'id', $fields, true ) ) {
$data['id'] = (int) $comment->comment_ID;
}
if ( in_array( 'post', $fields, true ) ) {
$data['post'] = (int) $comment->comment_post_ID;
}
if ( in_array( 'parent', $fields, true ) ) {
$data['parent'] = (int) $comment->comment_parent;
}
if ( in_array( 'author', $fields, true ) ) {
$data['author'] = (int) $comment->user_id;
}
if ( in_array( 'author_name', $fields, true ) ) {
$data['author_name'] = $comment->comment_author;
}
if ( in_array( 'author_email', $fields, true ) ) {
$data['author_email'] = $comment->comment_author_email;
}
if ( in_array( 'author_url', $fields, true ) ) {
$data['author_url'] = $comment->comment_author_url;
}
if ( in_array( 'author_ip', $fields, true ) ) {
$data['author_ip'] = $comment->comment_author_IP;
}
if ( in_array( 'author_user_agent', $fields, true ) ) {
$data['author_user_agent'] = $comment->comment_agent;
}
if ( in_array( 'date', $fields, true ) ) {
$data['date'] = mysql_to_rfc3339( $comment->comment_date );
}
if ( in_array( 'date_gmt', $fields, true ) ) {
$data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt );
}
if ( in_array( 'content', $fields, true ) ) {
$data['content'] = array(
/** This filter is documented in wp-includes/comment-template.php */
'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
'raw' => $comment->comment_content,
),
'link' => get_comment_link( $comment ),
'status' => $this->prepare_status_response( $comment->comment_approved ),
'type' => get_comment_type( $comment->comment_ID ),
);
);
}
$schema = $this->get_item_schema();
if ( in_array( 'link', $fields, true ) ) {
$data['link'] = get_comment_link( $comment );
}
if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) {
if ( in_array( 'status', $fields, true ) ) {
$data['status'] = $this->prepare_status_response( $comment->comment_approved );
}
if ( in_array( 'type', $fields, true ) ) {
$data['type'] = get_comment_type( $comment->comment_ID );
}
if ( in_array( 'author_avatar_urls', $fields, true ) ) {
$data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email );
}
if ( ! empty( $schema['properties']['meta'] ) ) {
if ( in_array( 'meta', $fields, true ) ) {
$data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
}