Make WordPress Core


Ignore:
Timestamp:
07/13/2018 06:50:51 AM (8 years ago)
Author:
pento
Message:

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.

Merges [43087] to the 4.9 branch.

Props danielbachhuber.
See #43874.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r41735 r43445  
    852852     */
    853853    public function prepare_item_for_response( $comment, $request ) {
    854         $data = array(
    855             'id'                 => (int) $comment->comment_ID,
    856             'post'               => (int) $comment->comment_post_ID,
    857             'parent'             => (int) $comment->comment_parent,
    858             'author'             => (int) $comment->user_id,
    859             'author_name'        => $comment->comment_author,
    860             'author_email'       => $comment->comment_author_email,
    861             'author_url'         => $comment->comment_author_url,
    862             'author_ip'          => $comment->comment_author_IP,
    863             'author_user_agent'  => $comment->comment_agent,
    864             'date'               => mysql_to_rfc3339( $comment->comment_date ),
    865             'date_gmt'           => mysql_to_rfc3339( $comment->comment_date_gmt ),
    866             'content'            => array(
     854
     855        $fields = $this->get_fields_for_response( $request );
     856        $data   = array();
     857
     858        if ( in_array( 'id', $fields, true ) ) {
     859            $data['id'] = (int) $comment->comment_ID;
     860        }
     861
     862        if ( in_array( 'post', $fields, true ) ) {
     863            $data['post'] = (int) $comment->comment_post_ID;
     864        }
     865
     866        if ( in_array( 'parent', $fields, true ) ) {
     867            $data['parent'] = (int) $comment->comment_parent;
     868        }
     869
     870        if ( in_array( 'author', $fields, true ) ) {
     871            $data['author'] = (int) $comment->user_id;
     872        }
     873
     874        if ( in_array( 'author_name', $fields, true ) ) {
     875            $data['author_name'] = $comment->comment_author;
     876        }
     877
     878        if ( in_array( 'author_email', $fields, true ) ) {
     879            $data['author_email'] = $comment->comment_author_email;
     880        }
     881
     882        if ( in_array( 'author_url', $fields, true ) ) {
     883            $data['author_url'] = $comment->comment_author_url;
     884        }
     885
     886        if ( in_array( 'author_ip', $fields, true ) ) {
     887            $data['author_ip'] = $comment->comment_author_IP;
     888        }
     889
     890        if ( in_array( 'author_user_agent', $fields, true ) ) {
     891            $data['author_user_agent'] = $comment->comment_agent;
     892        }
     893
     894        if ( in_array( 'date', $fields, true ) ) {
     895            $data['date'] = mysql_to_rfc3339( $comment->comment_date );
     896        }
     897
     898        if ( in_array( 'date_gmt', $fields, true ) ) {
     899            $data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt );
     900        }
     901
     902        if ( in_array( 'content', $fields, true ) ) {
     903            $data['content'] = array(
    867904                /** This filter is documented in wp-includes/comment-template.php */
    868905                'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
    869906                'raw'      => $comment->comment_content,
    870             ),
    871             'link'               => get_comment_link( $comment ),
    872             'status'             => $this->prepare_status_response( $comment->comment_approved ),
    873             'type'               => get_comment_type( $comment->comment_ID ),
    874         );
    875 
    876         $schema = $this->get_item_schema();
    877 
    878         if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) {
     907            );
     908        }
     909
     910        if ( in_array( 'link', $fields, true ) ) {
     911            $data['link'] = get_comment_link( $comment );
     912        }
     913
     914        if ( in_array( 'status', $fields, true ) ) {
     915            $data['status'] = $this->prepare_status_response( $comment->comment_approved );
     916        }
     917
     918        if ( in_array( 'type', $fields, true ) ) {
     919            $data['type'] = get_comment_type( $comment->comment_ID );
     920        }
     921
     922        if ( in_array( 'author_avatar_urls', $fields, true ) ) {
    879923            $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email );
    880924        }
    881925
    882         if ( ! empty( $schema['properties']['meta'] ) ) {
     926        if ( in_array( 'meta', $fields, true ) ) {
    883927            $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
    884928        }
Note: See TracChangeset for help on using the changeset viewer.