Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 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.

Props danielbachhuber.
See #43874.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r42678 r43087  
    863863         */
    864864        public function prepare_item_for_response( $comment, $request ) {
    865                 $data = array(
    866                         'id'                => (int) $comment->comment_ID,
    867                         'post'              => (int) $comment->comment_post_ID,
    868                         'parent'            => (int) $comment->comment_parent,
    869                         'author'            => (int) $comment->user_id,
    870                         'author_name'       => $comment->comment_author,
    871                         'author_email'      => $comment->comment_author_email,
    872                         'author_url'        => $comment->comment_author_url,
    873                         'author_ip'         => $comment->comment_author_IP,
    874                         'author_user_agent' => $comment->comment_agent,
    875                         'date'              => mysql_to_rfc3339( $comment->comment_date ),
    876                         'date_gmt'          => mysql_to_rfc3339( $comment->comment_date_gmt ),
    877                         'content'           => array(
     865
     866                $fields = $this->get_fields_for_response( $request );
     867                $data   = array();
     868
     869                if ( in_array( 'id', $fields, true ) ) {
     870                        $data['id'] = (int) $comment->comment_ID;
     871                }
     872
     873                if ( in_array( 'post', $fields, true ) ) {
     874                        $data['post'] = (int) $comment->comment_post_ID;
     875                }
     876
     877                if ( in_array( 'parent', $fields, true ) ) {
     878                        $data['parent'] = (int) $comment->comment_parent;
     879                }
     880
     881                if ( in_array( 'author', $fields, true ) ) {
     882                        $data['author'] = (int) $comment->user_id;
     883                }
     884
     885                if ( in_array( 'author_name', $fields, true ) ) {
     886                        $data['author_name'] = $comment->comment_author;
     887                }
     888
     889                if ( in_array( 'author_email', $fields, true ) ) {
     890                        $data['author_email'] = $comment->comment_author_email;
     891                }
     892
     893                if ( in_array( 'author_url', $fields, true ) ) {
     894                        $data['author_url'] = $comment->comment_author_url;
     895                }
     896
     897                if ( in_array( 'author_ip', $fields, true ) ) {
     898                        $data['author_ip'] = $comment->comment_author_IP;
     899                }
     900
     901                if ( in_array( 'author_user_agent', $fields, true ) ) {
     902                        $data['author_user_agent'] = $comment->comment_agent;
     903                }
     904
     905                if ( in_array( 'date', $fields, true ) ) {
     906                        $data['date'] = mysql_to_rfc3339( $comment->comment_date );
     907                }
     908
     909                if ( in_array( 'date_gmt', $fields, true ) ) {
     910                        $data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt );
     911                }
     912
     913                if ( in_array( 'content', $fields, true ) ) {
     914                        $data['content'] = array(
    878915                                /** This filter is documented in wp-includes/comment-template.php */
    879916                                'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
    880917                                'raw'      => $comment->comment_content,
    881                         ),
    882                         'link'              => get_comment_link( $comment ),
    883                         'status'            => $this->prepare_status_response( $comment->comment_approved ),
    884                         'type'              => get_comment_type( $comment->comment_ID ),
    885                 );
    886 
    887                 $schema = $this->get_item_schema();
    888 
    889                 if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) {
     918                        );
     919                }
     920
     921                if ( in_array( 'link', $fields, true ) ) {
     922                        $data['link'] = get_comment_link( $comment );
     923                }
     924
     925                if ( in_array( 'status', $fields, true ) ) {
     926                        $data['status'] = $this->prepare_status_response( $comment->comment_approved );
     927                }
     928
     929                if ( in_array( 'type', $fields, true ) ) {
     930                        $data['type'] = get_comment_type( $comment->comment_ID );
     931                }
     932
     933                if ( in_array( 'author_avatar_urls', $fields, true ) ) {
    890934                        $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email );
    891935                }
    892936
    893                 if ( ! empty( $schema['properties']['meta'] ) ) {
     937                if ( in_array( 'meta', $fields, true ) ) {
    894938                        $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
    895939                }
Note: See TracChangeset for help on using the changeset viewer.