Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 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.

Props danielbachhuber.
See #43874.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r43001 r43087  
    847847
    848848                $data   = array();
    849                 $schema = $this->get_item_schema();
    850 
    851                 if ( ! empty( $schema['properties']['id'] ) ) {
     849                $fields = $this->get_fields_for_response( $request );
     850
     851                if ( in_array( 'id', $fields, true ) ) {
    852852                        $data['id'] = $user->ID;
    853853                }
    854854
    855                 if ( ! empty( $schema['properties']['username'] ) ) {
     855                if ( in_array( 'username', $fields, true ) ) {
    856856                        $data['username'] = $user->user_login;
    857857                }
    858858
    859                 if ( ! empty( $schema['properties']['name'] ) ) {
     859                if ( in_array( 'name', $fields, true ) ) {
    860860                        $data['name'] = $user->display_name;
    861861                }
    862862
    863                 if ( ! empty( $schema['properties']['first_name'] ) ) {
     863                if ( in_array( 'first_name', $fields, true ) ) {
    864864                        $data['first_name'] = $user->first_name;
    865865                }
    866866
    867                 if ( ! empty( $schema['properties']['last_name'] ) ) {
     867                if ( in_array( 'last_name', $fields, true ) ) {
    868868                        $data['last_name'] = $user->last_name;
    869869                }
    870870
    871                 if ( ! empty( $schema['properties']['email'] ) ) {
     871                if ( in_array( 'email', $fields, true ) ) {
    872872                        $data['email'] = $user->user_email;
    873873                }
    874874
    875                 if ( ! empty( $schema['properties']['url'] ) ) {
     875                if ( in_array( 'url', $fields, true ) ) {
    876876                        $data['url'] = $user->user_url;
    877877                }
    878878
    879                 if ( ! empty( $schema['properties']['description'] ) ) {
     879                if ( in_array( 'description', $fields, true ) ) {
    880880                        $data['description'] = $user->description;
    881881                }
    882882
    883                 if ( ! empty( $schema['properties']['link'] ) ) {
     883                if ( in_array( 'link', $fields, true ) ) {
    884884                        $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
    885885                }
    886886
    887                 if ( ! empty( $schema['properties']['locale'] ) ) {
     887                if ( in_array( 'locale', $fields, true ) ) {
    888888                        $data['locale'] = get_user_locale( $user );
    889889                }
    890890
    891                 if ( ! empty( $schema['properties']['nickname'] ) ) {
     891                if ( in_array( 'nickname', $fields, true ) ) {
    892892                        $data['nickname'] = $user->nickname;
    893893                }
    894894
    895                 if ( ! empty( $schema['properties']['slug'] ) ) {
     895                if ( in_array( 'slug', $fields, true ) ) {
    896896                        $data['slug'] = $user->user_nicename;
    897897                }
    898898
    899                 if ( ! empty( $schema['properties']['roles'] ) ) {
     899                if ( in_array( 'roles', $fields, true ) ) {
    900900                        // Defensively call array_values() to ensure an array is returned.
    901901                        $data['roles'] = array_values( $user->roles );
    902902                }
    903903
    904                 if ( ! empty( $schema['properties']['registered_date'] ) ) {
     904                if ( in_array( 'registered_date', $fields, true ) ) {
    905905                        $data['registered_date'] = date( 'c', strtotime( $user->user_registered ) );
    906906                }
    907907
    908                 if ( ! empty( $schema['properties']['capabilities'] ) ) {
     908                if ( in_array( 'capabilities', $fields, true ) ) {
    909909                        $data['capabilities'] = (object) $user->allcaps;
    910910                }
    911911
    912                 if ( ! empty( $schema['properties']['extra_capabilities'] ) ) {
     912                if ( in_array( 'extra_capabilities', $fields, true ) ) {
    913913                        $data['extra_capabilities'] = (object) $user->caps;
    914914                }
    915915
    916                 if ( ! empty( $schema['properties']['avatar_urls'] ) ) {
     916                if ( in_array( 'avatar_urls', $fields, true ) ) {
    917917                        $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
    918918                }
    919919
    920                 if ( ! empty( $schema['properties']['meta'] ) ) {
     920                if ( in_array( 'meta', $fields, true ) ) {
    921921                        $data['meta'] = $this->meta->get_value( $user->ID, $request );
    922922                }
Note: See TracChangeset for help on using the changeset viewer.