Make WordPress Core


Ignore:
Timestamp:
07/13/2018 06:50:51 AM (7 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-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();
Note: See TracChangeset for help on using the changeset viewer.