mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@43087 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -846,78 +846,78 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
||||
public function prepare_item_for_response( $user, $request ) {
|
||||
|
||||
$data = array();
|
||||
$schema = $this->get_item_schema();
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
|
||||
if ( ! empty( $schema['properties']['id'] ) ) {
|
||||
if ( in_array( 'id', $fields, true ) ) {
|
||||
$data['id'] = $user->ID;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['username'] ) ) {
|
||||
if ( in_array( 'username', $fields, true ) ) {
|
||||
$data['username'] = $user->user_login;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['name'] ) ) {
|
||||
if ( in_array( 'name', $fields, true ) ) {
|
||||
$data['name'] = $user->display_name;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['first_name'] ) ) {
|
||||
if ( in_array( 'first_name', $fields, true ) ) {
|
||||
$data['first_name'] = $user->first_name;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['last_name'] ) ) {
|
||||
if ( in_array( 'last_name', $fields, true ) ) {
|
||||
$data['last_name'] = $user->last_name;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['email'] ) ) {
|
||||
if ( in_array( 'email', $fields, true ) ) {
|
||||
$data['email'] = $user->user_email;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['url'] ) ) {
|
||||
if ( in_array( 'url', $fields, true ) ) {
|
||||
$data['url'] = $user->user_url;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['description'] ) ) {
|
||||
if ( in_array( 'description', $fields, true ) ) {
|
||||
$data['description'] = $user->description;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['link'] ) ) {
|
||||
if ( in_array( 'link', $fields, true ) ) {
|
||||
$data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['locale'] ) ) {
|
||||
if ( in_array( 'locale', $fields, true ) ) {
|
||||
$data['locale'] = get_user_locale( $user );
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['nickname'] ) ) {
|
||||
if ( in_array( 'nickname', $fields, true ) ) {
|
||||
$data['nickname'] = $user->nickname;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['slug'] ) ) {
|
||||
if ( in_array( 'slug', $fields, true ) ) {
|
||||
$data['slug'] = $user->user_nicename;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['roles'] ) ) {
|
||||
if ( in_array( 'roles', $fields, true ) ) {
|
||||
// Defensively call array_values() to ensure an array is returned.
|
||||
$data['roles'] = array_values( $user->roles );
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['registered_date'] ) ) {
|
||||
if ( in_array( 'registered_date', $fields, true ) ) {
|
||||
$data['registered_date'] = date( 'c', strtotime( $user->user_registered ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['capabilities'] ) ) {
|
||||
if ( in_array( 'capabilities', $fields, true ) ) {
|
||||
$data['capabilities'] = (object) $user->allcaps;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['extra_capabilities'] ) ) {
|
||||
if ( in_array( 'extra_capabilities', $fields, true ) ) {
|
||||
$data['extra_capabilities'] = (object) $user->caps;
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['avatar_urls'] ) ) {
|
||||
if ( in_array( 'avatar_urls', $fields, true ) ) {
|
||||
$data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
|
||||
}
|
||||
|
||||
if ( ! empty( $schema['properties']['meta'] ) ) {
|
||||
if ( in_array( 'meta', $fields, true ) ) {
|
||||
$data['meta'] = $this->meta->get_value( $user->ID, $request );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user