Make WordPress Core

Changeset 43445


Ignore:
Timestamp:
07/13/2018 06:50:51 AM (6 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:
23 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43438 r43445  
    285285    public function prepare_item_for_response( $post, $request ) {
    286286        $response = parent::prepare_item_for_response( $post, $request );
    287         $data = $response->get_data();
    288 
    289         $data['description'] = array(
    290             'raw'       => $post->post_content,
     287        $fields   = $this->get_fields_for_response( $request );
     288        $data     = $response->get_data();
     289
     290        if ( in_array( 'description', $fields, true ) ) {
     291            $data['description'] = array(
     292                'raw'      => $post->post_content,
     293                /** This filter is documented in wp-includes/post-template.php */
     294                'rendered' => apply_filters( 'the_content', $post->post_content ),
     295            );
     296        }
     297
     298        if ( in_array( 'caption', $fields, true ) ) {
    291299            /** This filter is documented in wp-includes/post-template.php */
    292             'rendered'  => apply_filters( 'the_content', $post->post_content ),
    293         );
    294 
    295         /** This filter is documented in wp-includes/post-template.php */
    296         $caption = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
    297         $data['caption'] = array(
    298             'raw'       => $post->post_excerpt,
    299             'rendered'  => $caption,
    300         );
    301 
    302         $data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
    303         $data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
    304         $data['mime_type']     = $post->post_mime_type;
    305         $data['media_details'] = wp_get_attachment_metadata( $post->ID );
    306         $data['post']          = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
    307         $data['source_url']    = wp_get_attachment_url( $post->ID );
    308 
    309         // Ensure empty details is an empty object.
    310         if ( empty( $data['media_details'] ) ) {
    311             $data['media_details'] = new stdClass;
    312         } elseif ( ! empty( $data['media_details']['sizes'] ) ) {
    313 
    314             foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {
    315 
    316                 if ( isset( $size_data['mime-type'] ) ) {
    317                     $size_data['mime_type'] = $size_data['mime-type'];
    318                     unset( $size_data['mime-type'] );
     300            $caption         = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     301            $data['caption'] = array(
     302                'raw'      => $post->post_excerpt,
     303                'rendered' => $caption,
     304            );
     305        }
     306
     307        if ( in_array( 'alt_text', $fields, true ) ) {
     308            $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
     309        }
     310
     311        if ( in_array( 'media_type', $fields, true ) ) {
     312            $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
     313        }
     314
     315        if ( in_array( 'mime_type', $fields, true ) ) {
     316            $data['mime_type'] = $post->post_mime_type;
     317        }
     318
     319        if ( in_array( 'media_details', $fields, true ) ) {
     320            $data['media_details'] = wp_get_attachment_metadata( $post->ID );
     321
     322            // Ensure empty details is an empty object.
     323            if ( empty( $data['media_details'] ) ) {
     324                $data['media_details'] = new stdClass;
     325            } elseif ( ! empty( $data['media_details']['sizes'] ) ) {
     326
     327                foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {
     328
     329                    if ( isset( $size_data['mime-type'] ) ) {
     330                        $size_data['mime_type'] = $size_data['mime-type'];
     331                        unset( $size_data['mime-type'] );
     332                    }
     333
     334                    // Use the same method image_downsize() does.
     335                    $image_src = wp_get_attachment_image_src( $post->ID, $size );
     336                    if ( ! $image_src ) {
     337                        continue;
     338                    }
     339
     340                    $size_data['source_url'] = $image_src[0];
    319341                }
    320342
    321                 // Use the same method image_downsize() does.
    322                 $image_src = wp_get_attachment_image_src( $post->ID, $size );
    323                 if ( ! $image_src ) {
    324                     continue;
     343                $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
     344
     345                if ( ! empty( $full_src ) ) {
     346                    $data['media_details']['sizes']['full'] = array(
     347                        'file'       => wp_basename( $full_src[0] ),
     348                        'width'      => $full_src[1],
     349                        'height'     => $full_src[2],
     350                        'mime_type'  => $post->post_mime_type,
     351                        'source_url' => $full_src[0],
     352                    );
    325353                }
    326 
    327                 $size_data['source_url'] = $image_src[0];
    328             }
    329 
    330             $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
    331 
    332             if ( ! empty( $full_src ) ) {
    333                 $data['media_details']['sizes']['full'] = array(
    334                     'file'       => wp_basename( $full_src[0] ),
    335                     'width'      => $full_src[1],
    336                     'height'     => $full_src[2],
    337                     'mime_type'  => $post->post_mime_type,
    338                     'source_url' => $full_src[0],
    339                 );
    340             }
    341         } else {
    342             $data['media_details']['sizes'] = new stdClass;
     354            } else {
     355                $data['media_details']['sizes'] = new stdClass;
     356            }
     357        }
     358
     359        if ( in_array( 'post', $fields, true ) ) {
     360            $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
     361        }
     362
     363        if ( in_array( 'source_url', $fields, true ) ) {
     364            $data['source_url'] = wp_get_attachment_url( $post->ID );
    343365        }
    344366
  • 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        }
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r42000 r43445  
    507507
    508508    /**
     509     * Gets an array of fields to be included on the response.
     510     *
     511     * Included fields are based on item schema and `_fields=` request argument.
     512     *
     513     * @since 4.9.6
     514     *
     515     * @param WP_REST_Request $request Full details about the request.
     516     * @return array Fields to be included in the response.
     517     */
     518    public function get_fields_for_response( $request ) {
     519        $schema = $this->get_item_schema();
     520        $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array();
     521        if ( ! isset( $request['_fields'] ) ) {
     522            return $fields;
     523        }
     524        $requested_fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );
     525        if ( 0 === count( $requested_fields ) ) {
     526            return $fields;
     527        }
     528        // Trim off outside whitespace from the comma delimited list.
     529        $requested_fields = array_map( 'trim', $requested_fields );
     530        // Always persist 'id', because it can be needed for add_additional_fields_to_object().
     531        if ( in_array( 'id', $fields, true ) ) {
     532            $requested_fields[] = 'id';
     533        }
     534        return array_intersect( $fields, $requested_fields );
     535    }
     536
     537    /**
    509538     * Retrieves an array of endpoint arguments from the item schema for the controller.
    510539     *
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    r41731 r43445  
    196196    public function prepare_item_for_response( $status, $request ) {
    197197
    198         $data = array(
    199             'name'         => $status->label,
    200             'private'      => (bool) $status->private,
    201             'protected'    => (bool) $status->protected,
    202             'public'       => (bool) $status->public,
    203             'queryable'    => (bool) $status->publicly_queryable,
    204             'show_in_list' => (bool) $status->show_in_admin_all_list,
    205             'slug'         => $status->name,
    206         );
     198        $fields = $this->get_fields_for_response( $request );
     199        $data   = array();
     200
     201        if ( in_array( 'name', $fields, true ) ) {
     202            $data['name'] = $status->label;
     203        }
     204
     205        if ( in_array( 'private', $fields, true ) ) {
     206            $data['private'] = (bool) $status->private;
     207        }
     208
     209        if ( in_array( 'protected', $fields, true ) ) {
     210            $data['protected'] = (bool) $status->protected;
     211        }
     212
     213        if ( in_array( 'public', $fields, true ) ) {
     214            $data['public'] = (bool) $status->public;
     215        }
     216
     217        if ( in_array( 'queryable', $fields, true ) ) {
     218            $data['queryable'] = (bool) $status->publicly_queryable;
     219        }
     220
     221        if ( in_array( 'show_in_list', $fields, true ) ) {
     222            $data['show_in_list'] = (bool) $status->show_in_admin_all_list;
     223        }
     224
     225        if ( in_array( 'slug', $fields, true ) ) {
     226            $data['slug'] = $status->name;
     227        }
    207228
    208229        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r43038 r43445  
    152152        $supports = get_all_post_type_supports( $post_type->name );
    153153
    154         $data = array(
    155             'capabilities' => $post_type->cap,
    156             'description'  => $post_type->description,
    157             'hierarchical' => $post_type->hierarchical,
    158             'viewable'     => is_post_type_viewable( $post_type ),
    159             'labels'       => $post_type->labels,
    160             'name'         => $post_type->label,
    161             'slug'         => $post_type->name,
    162             'supports'     => $supports,
    163             'taxonomies'   => array_values( $taxonomies ),
    164             'rest_base'    => $base,
    165         );
     154        $fields = $this->get_fields_for_response( $request );
     155        $data   = array();
     156
     157        if ( in_array( 'capabilities', $fields, true ) ) {
     158            $data['capabilities'] = $post_type->cap;
     159        }
     160
     161        if ( in_array( 'description', $fields, true ) ) {
     162            $data['description'] = $post_type->description;
     163        }
     164
     165        if ( in_array( 'hierarchical', $fields, true ) ) {
     166            $data['hierarchical'] = $post_type->hierarchical;
     167        }
     168
     169        if ( in_array( 'viewable', $fields, true ) ) {
     170            $data['viewable'] = is_post_type_viewable( $post_type );
     171        }
     172
     173        if ( in_array( 'labels', $fields, true ) ) {
     174            $data['labels'] = $post_type->labels;
     175        }
     176
     177        if ( in_array( 'name', $fields, true ) ) {
     178            $data['name'] = $post_type->label;
     179        }
     180
     181        if ( in_array( 'slug', $fields, true ) ) {
     182            $data['slug'] = $post_type->name;
     183        }
     184
     185        if ( in_array( 'supports', $fields, true ) ) {
     186            $data['supports'] = $supports;
     187        }
     188
     189        if ( in_array( 'taxonomies', $fields, true ) ) {
     190            $data['taxonomies'] = array_values( $taxonomies );
     191        }
     192
     193        if ( in_array( 'rest_base', $fields, true ) ) {
     194            $data['rest_base'] = $base;
     195        }
     196
    166197        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    167198        $data    = $this->add_additional_fields_to_object( $data, $request );
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r43442 r43445  
    14011401        setup_postdata( $post );
    14021402
    1403         $schema = $this->get_item_schema();
     1403        $fields = $this->get_fields_for_response( $request );
    14041404
    14051405        // Base fields for every post.
    14061406        $data = array();
    14071407
    1408         if ( ! empty( $schema['properties']['id'] ) ) {
     1408        if ( in_array( 'id', $fields, true ) ) {
    14091409            $data['id'] = $post->ID;
    14101410        }
    14111411
    1412         if ( ! empty( $schema['properties']['date'] ) ) {
     1412        if ( in_array( 'date', $fields, true ) ) {
    14131413            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
    14141414        }
    14151415
    1416         if ( ! empty( $schema['properties']['date_gmt'] ) ) {
     1416        if ( in_array( 'date_gmt', $fields, true ) ) {
    14171417            // For drafts, `post_date_gmt` may not be set, indicating that the
    14181418            // date of the draft should be updated each time it is saved (see
     
    14271427        }
    14281428
    1429         if ( ! empty( $schema['properties']['guid'] ) ) {
     1429        if ( in_array( 'guid', $fields, true ) ) {
    14301430            $data['guid'] = array(
    14311431                /** This filter is documented in wp-includes/post-template.php */
     
    14351435        }
    14361436
    1437         if ( ! empty( $schema['properties']['modified'] ) ) {
     1437        if ( in_array( 'modified', $fields, true ) ) {
    14381438            $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
    14391439        }
    14401440
    1441         if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
     1441        if ( in_array( 'modified_gmt', $fields, true ) ) {
    14421442            // For drafts, `post_modified_gmt` may not be set (see
    14431443            // `post_date_gmt` comments above).  In this case, shim the value
     
    14521452        }
    14531453
    1454         if ( ! empty( $schema['properties']['password'] ) ) {
     1454        if ( in_array( 'password', $fields, true ) ) {
    14551455            $data['password'] = $post->post_password;
    14561456        }
    14571457
    1458         if ( ! empty( $schema['properties']['slug'] ) ) {
     1458        if ( in_array( 'slug', $fields, true ) ) {
    14591459            $data['slug'] = $post->post_name;
    14601460        }
    14611461
    1462         if ( ! empty( $schema['properties']['status'] ) ) {
     1462        if ( in_array( 'status', $fields, true ) ) {
    14631463            $data['status'] = $post->post_status;
    14641464        }
    14651465
    1466         if ( ! empty( $schema['properties']['type'] ) ) {
     1466        if ( in_array( 'type', $fields, true ) ) {
    14671467            $data['type'] = $post->post_type;
    14681468        }
    14691469
    1470         if ( ! empty( $schema['properties']['link'] ) ) {
     1470        if ( in_array( 'link', $fields, true ) ) {
    14711471            $data['link'] = get_permalink( $post->ID );
    14721472        }
    14731473
    1474         if ( ! empty( $schema['properties']['title'] ) ) {
     1474        if ( in_array( 'title', $fields, true ) ) {
    14751475            add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
    14761476
     
    14921492        }
    14931493
    1494         if ( ! empty( $schema['properties']['content'] ) ) {
     1494        if ( in_array( 'content', $fields, true ) ) {
    14951495            $data['content'] = array(
    14961496                'raw'       => $post->post_content,
     
    15011501        }
    15021502
    1503         if ( ! empty( $schema['properties']['excerpt'] ) ) {
     1503        if ( in_array( 'excerpt', $fields, true ) ) {
    15041504            /** This filter is documented in wp-includes/post-template.php */
    15051505            $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     
    15161516        }
    15171517
    1518         if ( ! empty( $schema['properties']['author'] ) ) {
     1518        if ( in_array( 'author', $fields, true ) ) {
    15191519            $data['author'] = (int) $post->post_author;
    15201520        }
    15211521
    1522         if ( ! empty( $schema['properties']['featured_media'] ) ) {
     1522        if ( in_array( 'featured_media', $fields, true ) ) {
    15231523            $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
    15241524        }
    15251525
    1526         if ( ! empty( $schema['properties']['parent'] ) ) {
     1526        if ( in_array( 'parent', $fields, true ) ) {
    15271527            $data['parent'] = (int) $post->post_parent;
    15281528        }
    15291529
    1530         if ( ! empty( $schema['properties']['menu_order'] ) ) {
     1530        if ( in_array( 'menu_order', $fields, true ) ) {
    15311531            $data['menu_order'] = (int) $post->menu_order;
    15321532        }
    15331533
    1534         if ( ! empty( $schema['properties']['comment_status'] ) ) {
     1534        if ( in_array( 'comment_status', $fields, true ) ) {
    15351535            $data['comment_status'] = $post->comment_status;
    15361536        }
    15371537
    1538         if ( ! empty( $schema['properties']['ping_status'] ) ) {
     1538        if ( in_array( 'ping_status', $fields, true ) ) {
    15391539            $data['ping_status'] = $post->ping_status;
    15401540        }
    15411541
    1542         if ( ! empty( $schema['properties']['sticky'] ) ) {
     1542        if ( in_array( 'sticky', $fields, true ) ) {
    15431543            $data['sticky'] = is_sticky( $post->ID );
    15441544        }
    15451545
    1546         if ( ! empty( $schema['properties']['template'] ) ) {
     1546        if ( in_array( 'template', $fields, true ) ) {
    15471547            if ( $template = get_page_template_slug( $post->ID ) ) {
    15481548                $data['template'] = $template;
     
    15521552        }
    15531553
    1554         if ( ! empty( $schema['properties']['format'] ) ) {
     1554        if ( in_array( 'format', $fields, true ) ) {
    15551555            $data['format'] = get_post_format( $post->ID );
    15561556
     
    15611561        }
    15621562
    1563         if ( ! empty( $schema['properties']['meta'] ) ) {
     1563        if ( in_array( 'meta', $fields, true ) ) {
    15641564            $data['meta'] = $this->meta->get_value( $post->ID, $request );
    15651565        }
     
    15701570            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    15711571
    1572             if ( ! empty( $schema['properties'][ $base ] ) ) {
     1572            if ( in_array( $base, $fields, true ) ) {
    15731573                $terms = get_the_terms( $post, $taxonomy->name );
    15741574                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r41731 r43445  
    332332        setup_postdata( $post );
    333333
    334         $schema = $this->get_item_schema();
    335 
    336         $data = array();
    337 
    338         if ( ! empty( $schema['properties']['author'] ) ) {
     334        $fields = $this->get_fields_for_response( $request );
     335        $data   = array();
     336
     337        if ( in_array( 'author', $fields, true ) ) {
    339338            $data['author'] = (int) $post->post_author;
    340339        }
    341340
    342         if ( ! empty( $schema['properties']['date'] ) ) {
     341        if ( in_array( 'date', $fields, true ) ) {
    343342            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
    344343        }
    345344
    346         if ( ! empty( $schema['properties']['date_gmt'] ) ) {
     345        if ( in_array( 'date_gmt', $fields, true ) ) {
    347346            $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
    348347        }
    349348
    350         if ( ! empty( $schema['properties']['id'] ) ) {
     349        if ( in_array( 'id', $fields, true ) ) {
    351350            $data['id'] = $post->ID;
    352351        }
    353352
    354         if ( ! empty( $schema['properties']['modified'] ) ) {
     353        if ( in_array( 'modified', $fields, true ) ) {
    355354            $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
    356355        }
    357356
    358         if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
     357        if ( in_array( 'modified_gmt', $fields, true ) ) {
    359358            $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
    360359        }
    361360
    362         if ( ! empty( $schema['properties']['parent'] ) ) {
     361        if ( in_array( 'parent', $fields, true ) ) {
    363362            $data['parent'] = (int) $post->post_parent;
    364363        }
    365364
    366         if ( ! empty( $schema['properties']['slug'] ) ) {
     365        if ( in_array( 'slug', $fields, true ) ) {
    367366            $data['slug'] = $post->post_name;
    368367        }
    369368
    370         if ( ! empty( $schema['properties']['guid'] ) ) {
     369        if ( in_array( 'guid', $fields, true ) ) {
    371370            $data['guid'] = array(
    372371                /** This filter is documented in wp-includes/post-template.php */
     
    376375        }
    377376
    378         if ( ! empty( $schema['properties']['title'] ) ) {
     377        if ( in_array( 'title', $fields, true ) ) {
    379378            $data['title'] = array(
    380379                'raw'      => $post->post_title,
     
    383382        }
    384383
    385         if ( ! empty( $schema['properties']['content'] ) ) {
     384        if ( in_array( 'content', $fields, true ) ) {
    386385
    387386            $data['content'] = array(
     
    392391        }
    393392
    394         if ( ! empty( $schema['properties']['excerpt'] ) ) {
     393        if ( in_array( 'excerpt', $fields, true ) ) {
    395394            $data['excerpt'] = array(
    396395                'raw'      => $post->post_excerpt,
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r43443 r43445  
    178178    public function prepare_item_for_response( $taxonomy, $request ) {
    179179        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    180         $data = array(
    181             'name'         => $taxonomy->label,
    182             'slug'         => $taxonomy->name,
    183             'capabilities' => $taxonomy->cap,
    184             'description'  => $taxonomy->description,
    185             'labels'       => $taxonomy->labels,
    186             'types'        => $taxonomy->object_type,
    187             'show_cloud'   => $taxonomy->show_tagcloud,
    188             'hierarchical' => $taxonomy->hierarchical,
    189             'rest_base'    => $base,
    190         );
     180
     181        $fields = $this->get_fields_for_response( $request );
     182        $data   = array();
     183
     184        if ( in_array( 'name', $fields, true ) ) {
     185            $data['name'] = $taxonomy->label;
     186        }
     187
     188        if ( in_array( 'slug', $fields, true ) ) {
     189            $data['slug'] = $taxonomy->name;
     190        }
     191
     192        if ( in_array( 'capabilities', $fields, true ) ) {
     193            $data['capabilities'] = $taxonomy->cap;
     194        }
     195
     196        if ( in_array( 'description', $fields, true ) ) {
     197            $data['description'] = $taxonomy->description;
     198        }
     199
     200        if ( in_array( 'labels', $fields, true ) ) {
     201            $data['labels'] = $taxonomy->labels;
     202        }
     203
     204        if ( in_array( 'types', $fields, true ) ) {
     205            $data['types'] = $taxonomy->object_type;
     206        }
     207
     208        if ( in_array( 'show_cloud', $fields, true ) ) {
     209            $data['show_cloud'] = $taxonomy->show_tagcloud;
     210        }
     211
     212        if ( in_array( 'hierarchical', $fields, true ) ) {
     213            $data['hierarchical'] = $taxonomy->hierarchical;
     214        }
     215
     216        if ( in_array( 'rest_base', $fields, true ) ) {
     217            $data['rest_base'] = $base;
     218        }
     219
     220        if ( in_array( 'visibility', $fields, true ) ) {
     221            $data['visibility'] = array(
     222                'public'             => (bool) $taxonomy->public,
     223                'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
     224                'show_admin_column'  => (bool) $taxonomy->show_admin_column,
     225                'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,
     226                'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
     227                'show_ui'            => (bool) $taxonomy->show_ui,
     228            );
     229        }
    191230
    192231        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r43443 r43445  
    686686    public function prepare_item_for_response( $item, $request ) {
    687687
    688         $schema = $this->get_item_schema();
     688        $fields = $this->get_fields_for_response( $request );
    689689        $data   = array();
    690690
    691         if ( ! empty( $schema['properties']['id'] ) ) {
     691        if ( in_array( 'id', $fields, true ) ) {
    692692            $data['id'] = (int) $item->term_id;
    693693        }
    694694
    695         if ( ! empty( $schema['properties']['count'] ) ) {
     695        if ( in_array( 'count', $fields, true ) ) {
    696696            $data['count'] = (int) $item->count;
    697697        }
    698698
    699         if ( ! empty( $schema['properties']['description'] ) ) {
     699        if ( in_array( 'description', $fields, true ) ) {
    700700            $data['description'] = $item->description;
    701701        }
    702702
    703         if ( ! empty( $schema['properties']['link'] ) ) {
     703        if ( in_array( 'link', $fields, true ) ) {
    704704            $data['link'] = get_term_link( $item );
    705705        }
    706706
    707         if ( ! empty( $schema['properties']['name'] ) ) {
     707        if ( in_array( 'name', $fields, true ) ) {
    708708            $data['name'] = $item->name;
    709709        }
    710710
    711         if ( ! empty( $schema['properties']['slug'] ) ) {
     711        if ( in_array( 'slug', $fields, true ) ) {
    712712            $data['slug'] = $item->slug;
    713713        }
    714714
    715         if ( ! empty( $schema['properties']['taxonomy'] ) ) {
     715        if ( in_array( 'taxonomy', $fields, true ) ) {
    716716            $data['taxonomy'] = $item->taxonomy;
    717717        }
    718718
    719         if ( ! empty( $schema['properties']['parent'] ) ) {
     719        if ( in_array( 'parent', $fields, true ) ) {
    720720            $data['parent'] = (int) $item->parent;
    721721        }
    722722
    723         if ( ! empty( $schema['properties']['meta'] ) ) {
     723        if ( in_array( 'meta', $fields, true ) ) {
    724724            $data['meta'] = $this->meta->get_value( $item->term_id, $request );
    725725        }
  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r43138 r43445  
    838838
    839839        $data   = array();
    840         $schema = $this->get_item_schema();
    841 
    842         if ( ! empty( $schema['properties']['id'] ) ) {
     840        $fields = $this->get_fields_for_response( $request );
     841
     842        if ( in_array( 'id', $fields, true ) ) {
    843843            $data['id'] = $user->ID;
    844844        }
    845845
    846         if ( ! empty( $schema['properties']['username'] ) ) {
     846        if ( in_array( 'username', $fields, true ) ) {
    847847            $data['username'] = $user->user_login;
    848848        }
    849849
    850         if ( ! empty( $schema['properties']['name'] ) ) {
     850        if ( in_array( 'name', $fields, true ) ) {
    851851            $data['name'] = $user->display_name;
    852852        }
    853853
    854         if ( ! empty( $schema['properties']['first_name'] ) ) {
     854        if ( in_array( 'first_name', $fields, true ) ) {
    855855            $data['first_name'] = $user->first_name;
    856856        }
    857857
    858         if ( ! empty( $schema['properties']['last_name'] ) ) {
     858        if ( in_array( 'last_name', $fields, true ) ) {
    859859            $data['last_name'] = $user->last_name;
    860860        }
    861861
    862         if ( ! empty( $schema['properties']['email'] ) ) {
     862        if ( in_array( 'email', $fields, true ) ) {
    863863            $data['email'] = $user->user_email;
    864864        }
    865865
    866         if ( ! empty( $schema['properties']['url'] ) ) {
     866        if ( in_array( 'url', $fields, true ) ) {
    867867            $data['url'] = $user->user_url;
    868868        }
    869869
    870         if ( ! empty( $schema['properties']['description'] ) ) {
     870        if ( in_array( 'description', $fields, true ) ) {
    871871            $data['description'] = $user->description;
    872872        }
    873873
    874         if ( ! empty( $schema['properties']['link'] ) ) {
     874        if ( in_array( 'link', $fields, true ) ) {
    875875            $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
    876876        }
    877877
    878         if ( ! empty( $schema['properties']['locale'] ) ) {
     878        if ( in_array( 'locale', $fields, true ) ) {
    879879            $data['locale'] = get_user_locale( $user );
    880880        }
    881881
    882         if ( ! empty( $schema['properties']['nickname'] ) ) {
     882        if ( in_array( 'nickname', $fields, true ) ) {
    883883            $data['nickname'] = $user->nickname;
    884884        }
    885885
    886         if ( ! empty( $schema['properties']['slug'] ) ) {
     886        if ( in_array( 'slug', $fields, true ) ) {
    887887            $data['slug'] = $user->user_nicename;
    888888        }
    889889
    890         if ( ! empty( $schema['properties']['roles'] ) ) {
     890        if ( in_array( 'roles', $fields, true ) ) {
    891891            // Defensively call array_values() to ensure an array is returned.
    892892            $data['roles'] = array_values( $user->roles );
    893893        }
    894894
    895         if ( ! empty( $schema['properties']['registered_date'] ) ) {
     895        if ( in_array( 'registered_date', $fields, true ) ) {
    896896            $data['registered_date'] = date( 'c', strtotime( $user->user_registered ) );
    897897        }
    898898
    899         if ( ! empty( $schema['properties']['capabilities'] ) ) {
     899        if ( in_array( 'capabilities', $fields, true ) ) {
    900900            $data['capabilities'] = (object) $user->allcaps;
    901901        }
    902902
    903         if ( ! empty( $schema['properties']['extra_capabilities'] ) ) {
     903        if ( in_array( 'extra_capabilities', $fields, true ) ) {
    904904            $data['extra_capabilities'] = (object) $user->caps;
    905905        }
    906906
    907         if ( ! empty( $schema['properties']['avatar_urls'] ) ) {
     907        if ( in_array( 'avatar_urls', $fields, true ) ) {
    908908            $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
    909909        }
    910910
    911         if ( ! empty( $schema['properties']['meta'] ) ) {
     911        if ( in_array( 'meta', $fields, true ) ) {
    912912            $data['meta'] = $this->meta->get_value( $user->ID, $request );
    913913        }
  • branches/4.9/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43438 r43445  
    10751075    }
    10761076
     1077    public function test_prepare_item_limit_fields() {
     1078        $attachment_id = $this->factory->attachment->create_object(
     1079            $this->test_file, 0, array(
     1080                'post_mime_type' => 'image/jpeg',
     1081                'post_excerpt'   => 'A sample caption',
     1082                'post_author'    => self::$editor_id,
     1083            )
     1084        );
     1085        wp_set_current_user( self::$editor_id );
     1086        $endpoint = new WP_REST_Attachments_Controller( 'post' );
     1087        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
     1088        $request->set_param( 'context', 'edit' );
     1089        $request->set_param( '_fields', 'id,slug' );
     1090        $obj      = get_post( $attachment_id );
     1091        $response = $endpoint->prepare_item_for_response( $obj, $request );
     1092        $this->assertEquals( array(
     1093            'id',
     1094            'slug',
     1095        ), array_keys( $response->get_data() ) );
     1096    }
     1097
    10771098    public function test_get_item_schema() {
    10781099        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
  • branches/4.9/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r43443 r43445  
    828828    }
    829829
     830    public function test_prepare_item_limit_fields() {
     831        $request  = new WP_REST_Request;
     832        $endpoint = new WP_REST_Terms_Controller( 'category' );
     833        $request->set_param( '_fields', 'id,name' );
     834        $term     = get_term( 1, 'category' );
     835        $response = $endpoint->prepare_item_for_response( $term, $request );
     836        $this->assertEquals( array(
     837            'id',
     838            'name',
     839        ), array_keys( $response->get_data() ) );
     840    }
     841
    830842    public function test_prepare_taxonomy_term_child() {
    831843        $child = $this->factory->category->create( array(
  • branches/4.9/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r40101 r43445  
    794794        $data = $response->get_data();
    795795        $this->check_comment_data( $data, 'edit', $response->get_links() );
     796    }
     797
     798    public function test_prepare_item_limit_fields() {
     799        wp_set_current_user( self::$admin_id );
     800        $endpoint = new WP_REST_Comments_Controller;
     801        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     802        $request->set_param( 'context', 'edit' );
     803        $request->set_param( '_fields', 'id,status' );
     804        $obj      = get_comment( self::$approved_id );
     805        $response = $endpoint->prepare_item_for_response( $obj, $request );
     806        $this->assertEquals( array(
     807            'id',
     808            'status',
     809        ), array_keys( $response->get_data() ) );
    796810    }
    797811
  • branches/4.9/tests/phpunit/tests/rest-api/rest-controller.php

    r39161 r43445  
    191191        $this->assertEquals( 'a', $args['somedefault']['default'] );
    192192    }
     193
     194    public function test_get_fields_for_response() {
     195        $controller = new WP_REST_Test_Controller();
     196        $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     197        $fields     = $controller->get_fields_for_response( $request );
     198        $this->assertEquals( array(
     199            'somestring',
     200            'someinteger',
     201            'someboolean',
     202            'someurl',
     203            'somedate',
     204            'someemail',
     205            'someenum',
     206            'someargoptions',
     207            'somedefault',
     208        ), $fields );
     209        $request->set_param( '_fields', 'somestring,someinteger' );
     210        $fields = $controller->get_fields_for_response( $request );
     211        $this->assertEquals( array(
     212            'somestring',
     213            'someinteger',
     214        ), $fields );
     215    }
    193216}
  • branches/4.9/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r41228 r43445  
    334334    public function test_prepare_item() {
    335335
     336    }
     337
     338    public function test_prepare_item_limit_fields() {
     339        wp_set_current_user( self::$editor_id );
     340        $page_id  = $this->factory->post->create(
     341            array(
     342                'post_status' => 'publish',
     343                'post_type'   => 'page',
     344            )
     345        );
     346        $endpoint = new WP_REST_Posts_Controller( 'page' );
     347        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
     348        $request->set_param( 'context', 'edit' );
     349        $request->set_param( '_fields', 'id,slug' );
     350        $obj      = get_post( $page_id );
     351        $response = $endpoint->prepare_item_for_response( $obj, $request );
     352        $this->assertEquals( array(
     353            'id',
     354            'slug',
     355        ), array_keys( $response->get_data() ) );
    336356    }
    337357
  • branches/4.9/tests/phpunit/tests/rest-api/rest-post-statuses-controller.php

    r41176 r43445  
    128128        $data = $endpoint->prepare_item_for_response( $obj, $request );
    129129        $this->check_post_status_obj( $obj, $data->get_data(), $data->get_links() );
     130    }
     131
     132    public function test_prepare_item_limit_fields() {
     133        $obj      = get_post_status_object( 'publish' );
     134        $request  = new WP_REST_Request;
     135        $endpoint = new WP_REST_Post_Statuses_Controller;
     136        $request->set_param( 'context', 'edit' );
     137        $request->set_param( '_fields', 'id,name' );
     138        $response = $endpoint->prepare_item_for_response( $obj, $request );
     139        $this->assertEquals( array(
     140            // 'id' doesn't exist in this context.
     141            'name',
     142        ), array_keys( $response->get_data() ) );
    130143    }
    131144
  • branches/4.9/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r43038 r43445  
    122122        $response = $endpoint->prepare_item_for_response( $obj, $request );
    123123        $this->check_post_type_obj( 'edit', $obj, $response->get_data(), $response->get_links() );
     124    }
     125
     126    public function test_prepare_item_limit_fields() {
     127        $obj      = get_post_type_object( 'post' );
     128        $request  = new WP_REST_Request;
     129        $endpoint = new WP_REST_Post_Types_Controller;
     130        $request->set_param( 'context', 'edit' );
     131        $request->set_param( '_fields', 'id,name' );
     132        $response = $endpoint->prepare_item_for_response( $obj, $request );
     133        $this->assertEquals( array(
     134            // 'id' doesn't exist in this context.
     135            'name',
     136        ), array_keys( $response->get_data() ) );
    124137    }
    125138
  • branches/4.9/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43442 r43445  
    12981298
    12991299        $this->check_get_post_response( $response, 'edit' );
     1300    }
     1301
     1302    public function test_prepare_item_limit_fields() {
     1303        wp_set_current_user( self::$editor_id );
     1304        $endpoint = new WP_REST_Posts_Controller( 'post' );
     1305        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1306        $request->set_param( 'context', 'edit' );
     1307        $request->set_param( '_fields', 'id,slug' );
     1308        $obj      = get_post( self::$post_id );
     1309        $response = $endpoint->prepare_item_for_response( $obj, $request );
     1310        $this->assertEquals( array(
     1311            'id',
     1312            'slug',
     1313        ), array_keys( $response->get_data() ) );
    13001314    }
    13011315
  • branches/4.9/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r41219 r43445  
    224224    }
    225225
     226    public function test_prepare_item_limit_fields() {
     227        wp_set_current_user( self::$editor_id );
     228        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
     229        $endpoint = new WP_REST_Revisions_Controller( 'post' );
     230        $request->set_param( 'context', 'edit' );
     231        $request->set_param( '_fields', 'id,slug' );
     232        $revision = get_post( $this->revision_id1 );
     233        $response = $endpoint->prepare_item_for_response( $revision, $request );
     234        $this->assertEquals( array(
     235            'id',
     236            'slug',
     237        ), array_keys( $response->get_data() ) );
     238    }
     239
    226240    public function test_get_item_schema() {
    227241        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
  • branches/4.9/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r43443 r43445  
    926926    }
    927927
     928    public function test_prepare_item_limit_fields() {
     929        $request  = new WP_REST_Request;
     930        $endpoint = new WP_REST_Terms_Controller( 'post_tag' );
     931        $request->set_param( '_fields', 'id,name' );
     932        $term     = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     933        $response = $endpoint->prepare_item_for_response( $term, $request );
     934        $this->assertEquals( array(
     935            'id',
     936            'name',
     937        ), array_keys( $response->get_data() ) );
     938    }
     939
    928940    public function test_get_item_schema() {
    929941        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
  • branches/4.9/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

    r43443 r43445  
    178178        $response = $endpoint->prepare_item_for_response( $tax, $request );
    179179        $this->check_taxonomy_object( 'edit', $tax, $response->get_data(), $response->get_links() );
     180    }
     181
     182    public function test_prepare_item_limit_fields() {
     183        $tax      = get_taxonomy( 'category' );
     184        $request  = new WP_REST_Request;
     185        $endpoint = new WP_REST_Taxonomies_Controller;
     186        $request->set_param( 'context', 'edit' );
     187        $request->set_param( '_fields', 'id,name' );
     188        $response = $endpoint->prepare_item_for_response( $tax, $request );
     189        $this->assertEquals( array(
     190            // 'id' doesn't exist in this context.
     191            'name',
     192        ), array_keys( $response->get_data() ) );
    180193    }
    181194
  • branches/4.9/tests/phpunit/tests/rest-api/rest-users-controller.php

    r43067 r43445  
    762762        $data = $this->endpoint->prepare_item_for_response( $user, $request );
    763763        $this->check_get_user_response( $data, 'edit' );
     764    }
     765
     766    public function test_prepare_item_limit_fields() {
     767        wp_set_current_user( self::$user );
     768        $request = new WP_REST_Request;
     769        $request->set_param( 'context', 'edit' );
     770        $request->set_param( '_fields', 'id,name' );
     771        $user     = get_user_by( 'id', get_current_user_id() );
     772        $response = $this->endpoint->prepare_item_for_response( $user, $request );
     773        $this->assertEquals( array(
     774            'id',
     775            'name',
     776        ), array_keys( $response->get_data() ) );
    764777    }
    765778
Note: See TracChangeset for help on using the changeset viewer.