Make WordPress Core


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

Props danielbachhuber.
See #43874.

File:
1 edited

Legend:

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

    r42678 r43087  
    14091409        setup_postdata( $post );
    14101410
    1411         $schema = $this->get_item_schema();
     1411        $fields = $this->get_fields_for_response( $request );
    14121412
    14131413        // Base fields for every post.
    14141414        $data = array();
    14151415
    1416         if ( ! empty( $schema['properties']['id'] ) ) {
     1416        if ( in_array( 'id', $fields, true ) ) {
    14171417            $data['id'] = $post->ID;
    14181418        }
    14191419
    1420         if ( ! empty( $schema['properties']['date'] ) ) {
     1420        if ( in_array( 'date', $fields, true ) ) {
    14211421            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
    14221422        }
    14231423
    1424         if ( ! empty( $schema['properties']['date_gmt'] ) ) {
     1424        if ( in_array( 'date_gmt', $fields, true ) ) {
    14251425            // For drafts, `post_date_gmt` may not be set, indicating that the
    14261426            // date of the draft should be updated each time it is saved (see
     
    14351435        }
    14361436
    1437         if ( ! empty( $schema['properties']['guid'] ) ) {
     1437        if ( in_array( 'guid', $fields, true ) ) {
    14381438            $data['guid'] = array(
    14391439                /** This filter is documented in wp-includes/post-template.php */
     
    14431443        }
    14441444
    1445         if ( ! empty( $schema['properties']['modified'] ) ) {
     1445        if ( in_array( 'modified', $fields, true ) ) {
    14461446            $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
    14471447        }
    14481448
    1449         if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
     1449        if ( in_array( 'modified_gmt', $fields, true ) ) {
    14501450            // For drafts, `post_modified_gmt` may not be set (see
    14511451            // `post_date_gmt` comments above).  In this case, shim the value
     
    14601460        }
    14611461
    1462         if ( ! empty( $schema['properties']['password'] ) ) {
     1462        if ( in_array( 'password', $fields, true ) ) {
    14631463            $data['password'] = $post->post_password;
    14641464        }
    14651465
    1466         if ( ! empty( $schema['properties']['slug'] ) ) {
     1466        if ( in_array( 'slug', $fields, true ) ) {
    14671467            $data['slug'] = $post->post_name;
    14681468        }
    14691469
    1470         if ( ! empty( $schema['properties']['status'] ) ) {
     1470        if ( in_array( 'status', $fields, true ) ) {
    14711471            $data['status'] = $post->post_status;
    14721472        }
    14731473
    1474         if ( ! empty( $schema['properties']['type'] ) ) {
     1474        if ( in_array( 'type', $fields, true ) ) {
    14751475            $data['type'] = $post->post_type;
    14761476        }
    14771477
    1478         if ( ! empty( $schema['properties']['link'] ) ) {
     1478        if ( in_array( 'link', $fields, true ) ) {
    14791479            $data['link'] = get_permalink( $post->ID );
    14801480        }
    14811481
    1482         if ( ! empty( $schema['properties']['title'] ) ) {
     1482        if ( in_array( 'title', $fields, true ) ) {
    14831483            add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
    14841484
     
    15001500        }
    15011501
    1502         if ( ! empty( $schema['properties']['content'] ) ) {
     1502        if ( in_array( 'content', $fields, true ) ) {
    15031503            $data['content'] = array(
    15041504                'raw'       => $post->post_content,
     
    15091509        }
    15101510
    1511         if ( ! empty( $schema['properties']['excerpt'] ) ) {
     1511        if ( in_array( 'excerpt', $fields, true ) ) {
    15121512            /** This filter is documented in wp-includes/post-template.php */
    15131513            $excerpt         = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     
    15241524        }
    15251525
    1526         if ( ! empty( $schema['properties']['author'] ) ) {
     1526        if ( in_array( 'author', $fields, true ) ) {
    15271527            $data['author'] = (int) $post->post_author;
    15281528        }
    15291529
    1530         if ( ! empty( $schema['properties']['featured_media'] ) ) {
     1530        if ( in_array( 'featured_media', $fields, true ) ) {
    15311531            $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
    15321532        }
    15331533
    1534         if ( ! empty( $schema['properties']['parent'] ) ) {
     1534        if ( in_array( 'parent', $fields, true ) ) {
    15351535            $data['parent'] = (int) $post->post_parent;
    15361536        }
    15371537
    1538         if ( ! empty( $schema['properties']['menu_order'] ) ) {
     1538        if ( in_array( 'menu_order', $fields, true ) ) {
    15391539            $data['menu_order'] = (int) $post->menu_order;
    15401540        }
    15411541
    1542         if ( ! empty( $schema['properties']['comment_status'] ) ) {
     1542        if ( in_array( 'comment_status', $fields, true ) ) {
    15431543            $data['comment_status'] = $post->comment_status;
    15441544        }
    15451545
    1546         if ( ! empty( $schema['properties']['ping_status'] ) ) {
     1546        if ( in_array( 'ping_status', $fields, true ) ) {
    15471547            $data['ping_status'] = $post->ping_status;
    15481548        }
    15491549
    1550         if ( ! empty( $schema['properties']['sticky'] ) ) {
     1550        if ( in_array( 'sticky', $fields, true ) ) {
    15511551            $data['sticky'] = is_sticky( $post->ID );
    15521552        }
    15531553
    1554         if ( ! empty( $schema['properties']['template'] ) ) {
     1554        if ( in_array( 'template', $fields, true ) ) {
    15551555            if ( $template = get_page_template_slug( $post->ID ) ) {
    15561556                $data['template'] = $template;
     
    15601560        }
    15611561
    1562         if ( ! empty( $schema['properties']['format'] ) ) {
     1562        if ( in_array( 'format', $fields, true ) ) {
    15631563            $data['format'] = get_post_format( $post->ID );
    15641564
     
    15691569        }
    15701570
    1571         if ( ! empty( $schema['properties']['meta'] ) ) {
     1571        if ( in_array( 'meta', $fields, true ) ) {
    15721572            $data['meta'] = $this->meta->get_value( $post->ID, $request );
    15731573        }
     
    15781578            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    15791579
    1580             if ( ! empty( $schema['properties'][ $base ] ) ) {
     1580            if ( in_array( $base, $fields, true ) ) {
    15811581                $terms         = get_the_terms( $post, $taxonomy->name );
    15821582                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
Note: See TracChangeset for help on using the changeset viewer.