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/tests/phpunit/tests/rest-api/rest-users-controller.php

    r43001 r43087  
    848848        $data = $this->endpoint->prepare_item_for_response( $user, $request );
    849849        $this->check_get_user_response( $data, 'edit' );
     850    }
     851
     852    public function test_prepare_item_limit_fields() {
     853        wp_set_current_user( self::$user );
     854        $request = new WP_REST_Request;
     855        $request->set_param( 'context', 'edit' );
     856        $request->set_param( '_fields', 'id,name' );
     857        $user     = get_user_by( 'id', get_current_user_id() );
     858        $response = $this->endpoint->prepare_item_for_response( $user, $request );
     859        $this->assertEquals( array(
     860            'id',
     861            'name',
     862        ), array_keys( $response->get_data() ) );
    850863    }
    851864
Note: See TracChangeset for help on using the changeset viewer.