WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 AM (3 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-revisions-controller.php

    r42343 r43087  
    341341        setup_postdata( $post );
    342342
    343         $schema = $this->get_item_schema();
    344 
    345         $data = array();
    346 
    347         if ( ! empty( $schema['properties']['author'] ) ) {
     343        $fields = $this->get_fields_for_response( $request );
     344        $data   = array();
     345
     346        if ( in_array( 'author', $fields, true ) ) {
    348347            $data['author'] = (int) $post->post_author;
    349348        }
    350349
    351         if ( ! empty( $schema['properties']['date'] ) ) {
     350        if ( in_array( 'date', $fields, true ) ) {
    352351            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
    353352        }
    354353
    355         if ( ! empty( $schema['properties']['date_gmt'] ) ) {
     354        if ( in_array( 'date_gmt', $fields, true ) ) {
    356355            $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
    357356        }
    358357
    359         if ( ! empty( $schema['properties']['id'] ) ) {
     358        if ( in_array( 'id', $fields, true ) ) {
    360359            $data['id'] = $post->ID;
    361360        }
    362361
    363         if ( ! empty( $schema['properties']['modified'] ) ) {
     362        if ( in_array( 'modified', $fields, true ) ) {
    364363            $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
    365364        }
    366365
    367         if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
     366        if ( in_array( 'modified_gmt', $fields, true ) ) {
    368367            $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
    369368        }
    370369
    371         if ( ! empty( $schema['properties']['parent'] ) ) {
     370        if ( in_array( 'parent', $fields, true ) ) {
    372371            $data['parent'] = (int) $post->post_parent;
    373372        }
    374373
    375         if ( ! empty( $schema['properties']['slug'] ) ) {
     374        if ( in_array( 'slug', $fields, true ) ) {
    376375            $data['slug'] = $post->post_name;
    377376        }
    378377
    379         if ( ! empty( $schema['properties']['guid'] ) ) {
     378        if ( in_array( 'guid', $fields, true ) ) {
    380379            $data['guid'] = array(
    381380                /** This filter is documented in wp-includes/post-template.php */
     
    385384        }
    386385
    387         if ( ! empty( $schema['properties']['title'] ) ) {
     386        if ( in_array( 'title', $fields, true ) ) {
    388387            $data['title'] = array(
    389388                'raw'      => $post->post_title,
     
    392391        }
    393392
    394         if ( ! empty( $schema['properties']['content'] ) ) {
     393        if ( in_array( 'content', $fields, true ) ) {
    395394
    396395            $data['content'] = array(
     
    401400        }
    402401
    403         if ( ! empty( $schema['properties']['excerpt'] ) ) {
     402        if ( in_array( 'excerpt', $fields, true ) ) {
    404403            $data['excerpt'] = array(
    405404                'raw'      => $post->post_excerpt,
Note: See TracChangeset for help on using the changeset viewer.