Make WordPress Core


Ignore:
Timestamp:
07/13/2018 06:50:51 AM (7 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-users-controller.php

    r43138 r43445  
    838838
    839839        $data   = array();
    840         $schema = $this->get_item_schema();
    841 
    842         if ( ! empty( $schema['properties']['id'] ) ) {
     840        $fields = $this->get_fields_for_response( $request );
     841
     842        if ( in_array( 'id', $fields, true ) ) {
    843843            $data['id'] = $user->ID;
    844844        }
    845845
    846         if ( ! empty( $schema['properties']['username'] ) ) {
     846        if ( in_array( 'username', $fields, true ) ) {
    847847            $data['username'] = $user->user_login;
    848848        }
    849849
    850         if ( ! empty( $schema['properties']['name'] ) ) {
     850        if ( in_array( 'name', $fields, true ) ) {
    851851            $data['name'] = $user->display_name;
    852852        }
    853853
    854         if ( ! empty( $schema['properties']['first_name'] ) ) {
     854        if ( in_array( 'first_name', $fields, true ) ) {
    855855            $data['first_name'] = $user->first_name;
    856856        }
    857857
    858         if ( ! empty( $schema['properties']['last_name'] ) ) {
     858        if ( in_array( 'last_name', $fields, true ) ) {
    859859            $data['last_name'] = $user->last_name;
    860860        }
    861861
    862         if ( ! empty( $schema['properties']['email'] ) ) {
     862        if ( in_array( 'email', $fields, true ) ) {
    863863            $data['email'] = $user->user_email;
    864864        }
    865865
    866         if ( ! empty( $schema['properties']['url'] ) ) {
     866        if ( in_array( 'url', $fields, true ) ) {
    867867            $data['url'] = $user->user_url;
    868868        }
    869869
    870         if ( ! empty( $schema['properties']['description'] ) ) {
     870        if ( in_array( 'description', $fields, true ) ) {
    871871            $data['description'] = $user->description;
    872872        }
    873873
    874         if ( ! empty( $schema['properties']['link'] ) ) {
     874        if ( in_array( 'link', $fields, true ) ) {
    875875            $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
    876876        }
    877877
    878         if ( ! empty( $schema['properties']['locale'] ) ) {
     878        if ( in_array( 'locale', $fields, true ) ) {
    879879            $data['locale'] = get_user_locale( $user );
    880880        }
    881881
    882         if ( ! empty( $schema['properties']['nickname'] ) ) {
     882        if ( in_array( 'nickname', $fields, true ) ) {
    883883            $data['nickname'] = $user->nickname;
    884884        }
    885885
    886         if ( ! empty( $schema['properties']['slug'] ) ) {
     886        if ( in_array( 'slug', $fields, true ) ) {
    887887            $data['slug'] = $user->user_nicename;
    888888        }
    889889
    890         if ( ! empty( $schema['properties']['roles'] ) ) {
     890        if ( in_array( 'roles', $fields, true ) ) {
    891891            // Defensively call array_values() to ensure an array is returned.
    892892            $data['roles'] = array_values( $user->roles );
    893893        }
    894894
    895         if ( ! empty( $schema['properties']['registered_date'] ) ) {
     895        if ( in_array( 'registered_date', $fields, true ) ) {
    896896            $data['registered_date'] = date( 'c', strtotime( $user->user_registered ) );
    897897        }
    898898
    899         if ( ! empty( $schema['properties']['capabilities'] ) ) {
     899        if ( in_array( 'capabilities', $fields, true ) ) {
    900900            $data['capabilities'] = (object) $user->allcaps;
    901901        }
    902902
    903         if ( ! empty( $schema['properties']['extra_capabilities'] ) ) {
     903        if ( in_array( 'extra_capabilities', $fields, true ) ) {
    904904            $data['extra_capabilities'] = (object) $user->caps;
    905905        }
    906906
    907         if ( ! empty( $schema['properties']['avatar_urls'] ) ) {
     907        if ( in_array( 'avatar_urls', $fields, true ) ) {
    908908            $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
    909909        }
    910910
    911         if ( ! empty( $schema['properties']['meta'] ) ) {
     911        if ( in_array( 'meta', $fields, true ) ) {
    912912            $data['meta'] = $this->meta->get_value( $user->ID, $request );
    913913        }
Note: See TracChangeset for help on using the changeset viewer.