WP_REST_Users_Controller::prepare_item_for_response() – Prepares a single user output for response.

You appear to be a bot. Output may be restricted

Description

Prepares a single user output for response.

Usage

$WP_REST_Response = WP_REST_Users_Controller::prepare_item_for_response( $item, $request );

Parameters

$item
( WP_User ) required – User object.
$request
( WP_REST_Request ) required – Request object.

Returns

WP_REST_Response Response object.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
Lines:

1 to 100 of 100
  public function prepare_item_for_response( $item, $request ) {
    // Restores the more descriptive, specific name for use within this method.
    $user   = $item;
    $data   = array();
    $fields = $this->get_fields_for_response( $request );

    if ( in_array( 'id', $fields, true ) ) {
      $data['id'] = $user->ID;
    }

    if ( in_array( 'username', $fields, true ) ) {
      $data['username'] = $user->user_login;
    }

    if ( in_array( 'name', $fields, true ) ) {
      $data['name'] = $user->display_name;
    }

    if ( in_array( 'first_name', $fields, true ) ) {
      $data['first_name'] = $user->first_name;
    }

    if ( in_array( 'last_name', $fields, true ) ) {
      $data['last_name'] = $user->last_name;
    }

    if ( in_array( 'email', $fields, true ) ) {
      $data['email'] = $user->user_email;
    }

    if ( in_array( 'url', $fields, true ) ) {
      $data['url'] = $user->user_url;
    }

    if ( in_array( 'description', $fields, true ) ) {
      $data['description'] = $user->description;
    }

    if ( in_array( 'link', $fields, true ) ) {
      $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
    }

    if ( in_array( 'locale', $fields, true ) ) {
      $data['locale'] = get_user_locale( $user );
    }

    if ( in_array( 'nickname', $fields, true ) ) {
      $data['nickname'] = $user->nickname;
    }

    if ( in_array( 'slug', $fields, true ) ) {
      $data['slug'] = $user->user_nicename;
    }

    if ( in_array( 'roles', $fields, true ) ) {
      // Defensively call array_values() to ensure an array is returned.
      $data['roles'] = array_values( $user->roles );
    }

    if ( in_array( 'registered_date', $fields, true ) ) {
      $data['registered_date'] = gmdate( 'c', strtotime( $user->user_registered ) );
    }

    if ( in_array( 'capabilities', $fields, true ) ) {
      $data['capabilities'] = (object) $user->allcaps;
    }

    if ( in_array( 'extra_capabilities', $fields, true ) ) {
      $data['extra_capabilities'] = (object) $user->caps;
    }

    if ( in_array( 'avatar_urls', $fields, true ) ) {
      $data['avatar_urls'] = rest_get_avatar_urls( $user );
    }

    if ( in_array( 'meta', $fields, true ) ) {
      $data['meta'] = $this->meta->get_value( $user->ID, $request );
    }

    $context = ! empty( $request['context'] ) ? $request['context'] : 'embed';

    $data = $this->add_additional_fields_to_object( $data, $request );
    $data = $this->filter_response_by_context( $data, $context );

    // Wrap the data in a response object.
    $response = rest_ensure_response( $data );

    $response->add_links( $this->WP_REST_Users_Controller::prepare_links( $user ) );

    
/**
 * Filters user data returned from the REST API.
 *
 * @since 4.7.0
 *
 * @param WP_REST_Response $response The response object.
 * @param WP_User          $user     User object used to create response.
 * @param WP_REST_Request  $request  Request object.
 */
    return apply_filters( 'rest_prepare_user', $response, $user, $request );
  }
 

 View on GitHub View on Trac