WordPress.org

Make WordPress Core


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

    r42354 r43087  
    692692    public function prepare_item_for_response( $item, $request ) {
    693693
    694         $schema = $this->get_item_schema();
     694        $fields = $this->get_fields_for_response( $request );
    695695        $data   = array();
    696696
    697         if ( ! empty( $schema['properties']['id'] ) ) {
     697        if ( in_array( 'id', $fields, true ) ) {
    698698            $data['id'] = (int) $item->term_id;
    699699        }
    700700
    701         if ( ! empty( $schema['properties']['count'] ) ) {
     701        if ( in_array( 'count', $fields, true ) ) {
    702702            $data['count'] = (int) $item->count;
    703703        }
    704704
    705         if ( ! empty( $schema['properties']['description'] ) ) {
     705        if ( in_array( 'description', $fields, true ) ) {
    706706            $data['description'] = $item->description;
    707707        }
    708708
    709         if ( ! empty( $schema['properties']['link'] ) ) {
     709        if ( in_array( 'link', $fields, true ) ) {
    710710            $data['link'] = get_term_link( $item );
    711711        }
    712712
    713         if ( ! empty( $schema['properties']['name'] ) ) {
     713        if ( in_array( 'name', $fields, true ) ) {
    714714            $data['name'] = $item->name;
    715715        }
    716716
    717         if ( ! empty( $schema['properties']['slug'] ) ) {
     717        if ( in_array( 'slug', $fields, true ) ) {
    718718            $data['slug'] = $item->slug;
    719719        }
    720720
    721         if ( ! empty( $schema['properties']['taxonomy'] ) ) {
     721        if ( in_array( 'taxonomy', $fields, true ) ) {
    722722            $data['taxonomy'] = $item->taxonomy;
    723723        }
    724724
    725         if ( ! empty( $schema['properties']['parent'] ) ) {
     725        if ( in_array( 'parent', $fields, true ) ) {
    726726            $data['parent'] = (int) $item->parent;
    727727        }
    728728
    729         if ( ! empty( $schema['properties']['meta'] ) ) {
     729        if ( in_array( 'meta', $fields, true ) ) {
    730730            $data['meta'] = $this->meta->get_value( $item->term_id, $request );
    731731        }
Note: See TracChangeset for help on using the changeset viewer.