Make WordPress Core


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