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-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,
Note: See TracChangeset for help on using the changeset viewer.