[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-users-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Users_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class used to manage users via the REST API.
  12   *
  13   * @since 4.7.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Users_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Instance of a user meta fields object.
  21       *
  22       * @since 4.7.0
  23       * @var WP_REST_User_Meta_Fields
  24       */
  25      protected $meta;
  26  
  27      /**
  28       * Constructor.
  29       *
  30       * @since 4.7.0
  31       */
  32  	public function __construct() {
  33          $this->namespace = 'wp/v2';
  34          $this->rest_base = 'users';
  35  
  36          $this->meta = new WP_REST_User_Meta_Fields();
  37      }
  38  
  39      /**
  40       * Registers the routes for the objects of the controller.
  41       *
  42       * @since 4.7.0
  43       *
  44       * @see register_rest_route()
  45       */
  46  	public function register_routes() {
  47  
  48          register_rest_route(
  49              $this->namespace,
  50              '/' . $this->rest_base,
  51              array(
  52                  array(
  53                      'methods'             => WP_REST_Server::READABLE,
  54                      'callback'            => array( $this, 'get_items' ),
  55                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  56                      'args'                => $this->get_collection_params(),
  57                  ),
  58                  array(
  59                      'methods'             => WP_REST_Server::CREATABLE,
  60                      'callback'            => array( $this, 'create_item' ),
  61                      'permission_callback' => array( $this, 'create_item_permissions_check' ),
  62                      'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
  63                  ),
  64                  'schema' => array( $this, 'get_public_item_schema' ),
  65              )
  66          );
  67  
  68          register_rest_route(
  69              $this->namespace,
  70              '/' . $this->rest_base . '/(?P<id>[\d]+)',
  71              array(
  72                  'args'   => array(
  73                      'id' => array(
  74                          'description' => __( 'Unique identifier for the user.' ),
  75                          'type'        => 'integer',
  76                      ),
  77                  ),
  78                  array(
  79                      'methods'             => WP_REST_Server::READABLE,
  80                      'callback'            => array( $this, 'get_item' ),
  81                      'permission_callback' => array( $this, 'get_item_permissions_check' ),
  82                      'args'                => array(
  83                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  84                      ),
  85                  ),
  86                  array(
  87                      'methods'             => WP_REST_Server::EDITABLE,
  88                      'callback'            => array( $this, 'update_item' ),
  89                      'permission_callback' => array( $this, 'update_item_permissions_check' ),
  90                      'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  91                  ),
  92                  array(
  93                      'methods'             => WP_REST_Server::DELETABLE,
  94                      'callback'            => array( $this, 'delete_item' ),
  95                      'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  96                      'args'                => array(
  97                          'force'    => array(
  98                              'type'        => 'boolean',
  99                              'default'     => false,
 100                              'description' => __( 'Required to be true, as users do not support trashing.' ),
 101                          ),
 102                          'reassign' => array(
 103                              'type'              => 'integer',
 104                              'description'       => __( 'Reassign the deleted user\'s posts and links to this user ID.' ),
 105                              'required'          => true,
 106                              'sanitize_callback' => array( $this, 'check_reassign' ),
 107                          ),
 108                      ),
 109                  ),
 110                  'schema' => array( $this, 'get_public_item_schema' ),
 111              )
 112          );
 113  
 114          register_rest_route(
 115              $this->namespace,
 116              '/' . $this->rest_base . '/me',
 117              array(
 118                  array(
 119                      'methods'  => WP_REST_Server::READABLE,
 120                      'callback' => array( $this, 'get_current_item' ),
 121                      'args'     => array(
 122                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 123                      ),
 124                  ),
 125                  array(
 126                      'methods'             => WP_REST_Server::EDITABLE,
 127                      'callback'            => array( $this, 'update_current_item' ),
 128                      'permission_callback' => array( $this, 'update_current_item_permissions_check' ),
 129                      'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
 130                  ),
 131                  array(
 132                      'methods'             => WP_REST_Server::DELETABLE,
 133                      'callback'            => array( $this, 'delete_current_item' ),
 134                      'permission_callback' => array( $this, 'delete_current_item_permissions_check' ),
 135                      'args'                => array(
 136                          'force'    => array(
 137                              'type'        => 'boolean',
 138                              'default'     => false,
 139                              'description' => __( 'Required to be true, as users do not support trashing.' ),
 140                          ),
 141                          'reassign' => array(
 142                              'type'              => 'integer',
 143                              'description'       => __( 'Reassign the deleted user\'s posts and links to this user ID.' ),
 144                              'required'          => true,
 145                              'sanitize_callback' => array( $this, 'check_reassign' ),
 146                          ),
 147                      ),
 148                  ),
 149                  'schema' => array( $this, 'get_public_item_schema' ),
 150              )
 151          );
 152      }
 153  
 154      /**
 155       * Checks for a valid value for the reassign parameter when deleting users.
 156       *
 157       * The value can be an integer, 'false', false, or ''.
 158       *
 159       * @since 4.7.0
 160       *
 161       * @param int|bool        $value   The value passed to the reassign parameter.
 162       * @param WP_REST_Request $request Full details about the request.
 163       * @param string          $param   The parameter that is being sanitized.
 164       *
 165       * @return int|bool|WP_Error
 166       */
 167  	public function check_reassign( $value, $request, $param ) {
 168          if ( is_numeric( $value ) ) {
 169              return $value;
 170          }
 171  
 172          if ( empty( $value ) || false === $value || 'false' === $value ) {
 173              return false;
 174          }
 175  
 176          return new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
 177      }
 178  
 179      /**
 180       * Permissions check for getting all users.
 181       *
 182       * @since 4.7.0
 183       *
 184       * @param WP_REST_Request $request Full details about the request.
 185       * @return true|WP_Error True if the request has read access, otherwise WP_Error object.
 186       */
 187  	public function get_items_permissions_check( $request ) {
 188          // Check if roles is specified in GET request and if user can list users.
 189          if ( ! empty( $request['roles'] ) && ! current_user_can( 'list_users' ) ) {
 190              return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to filter users by role.' ), array( 'status' => rest_authorization_required_code() ) );
 191          }
 192  
 193          if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
 194              return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
 195          }
 196  
 197          if ( in_array( $request['orderby'], array( 'email', 'registered_date' ), true ) && ! current_user_can( 'list_users' ) ) {
 198              return new WP_Error( 'rest_forbidden_orderby', __( 'Sorry, you are not allowed to order users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) );
 199          }
 200  
 201          if ( 'authors' === $request['who'] ) {
 202              $can_view = false;
 203              $types    = get_post_types( array( 'show_in_rest' => true ), 'objects' );
 204              foreach ( $types as $type ) {
 205                  if ( post_type_supports( $type->name, 'author' )
 206                      && current_user_can( $type->cap->edit_posts ) ) {
 207                      $can_view = true;
 208                  }
 209              }
 210              if ( ! $can_view ) {
 211                  return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) );
 212              }
 213          }
 214  
 215          return true;
 216      }
 217  
 218      /**
 219       * Retrieves all users.
 220       *
 221       * @since 4.7.0
 222       *
 223       * @param WP_REST_Request $request Full details about the request.
 224       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 225       */
 226  	public function get_items( $request ) {
 227  
 228          // Retrieve the list of registered collection query parameters.
 229          $registered = $this->get_collection_params();
 230  
 231          /*
 232           * This array defines mappings between public API query parameters whose
 233           * values are accepted as-passed, and their internal WP_Query parameter
 234           * name equivalents (some are the same). Only values which are also
 235           * present in $registered will be set.
 236           */
 237          $parameter_mappings = array(
 238              'exclude'  => 'exclude',
 239              'include'  => 'include',
 240              'order'    => 'order',
 241              'per_page' => 'number',
 242              'search'   => 'search',
 243              'roles'    => 'role__in',
 244              'slug'     => 'nicename__in',
 245          );
 246  
 247          $prepared_args = array();
 248  
 249          /*
 250           * For each known parameter which is both registered and present in the request,
 251           * set the parameter's value on the query $prepared_args.
 252           */
 253          foreach ( $parameter_mappings as $api_param => $wp_param ) {
 254              if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
 255                  $prepared_args[ $wp_param ] = $request[ $api_param ];
 256              }
 257          }
 258  
 259          if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) {
 260              $prepared_args['offset'] = $request['offset'];
 261          } else {
 262              $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
 263          }
 264  
 265          if ( isset( $registered['orderby'] ) ) {
 266              $orderby_possibles        = array(
 267                  'id'              => 'ID',
 268                  'include'         => 'include',
 269                  'name'            => 'display_name',
 270                  'registered_date' => 'registered',
 271                  'slug'            => 'user_nicename',
 272                  'include_slugs'   => 'nicename__in',
 273                  'email'           => 'user_email',
 274                  'url'             => 'user_url',
 275              );
 276              $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
 277          }
 278  
 279          if ( isset( $registered['who'] ) && ! empty( $request['who'] ) && 'authors' === $request['who'] ) {
 280              $prepared_args['who'] = 'authors';
 281          } elseif ( ! current_user_can( 'list_users' ) ) {
 282              $prepared_args['has_published_posts'] = get_post_types( array( 'show_in_rest' => true ), 'names' );
 283          }
 284  
 285          if ( ! empty( $prepared_args['search'] ) ) {
 286              $prepared_args['search'] = '*' . $prepared_args['search'] . '*';
 287          }
 288          /**
 289           * Filters WP_User_Query arguments when querying users via the REST API.
 290           *
 291           * @link https://developer.wordpress.org/reference/classes/wp_user_query/
 292           *
 293           * @since 4.7.0
 294           *
 295           * @param array           $prepared_args Array of arguments for WP_User_Query.
 296           * @param WP_REST_Request $request       The current request.
 297           */
 298          $prepared_args = apply_filters( 'rest_user_query', $prepared_args, $request );
 299  
 300          $query = new WP_User_Query( $prepared_args );
 301  
 302          $users = array();
 303  
 304          foreach ( $query->results as $user ) {
 305              $data    = $this->prepare_item_for_response( $user, $request );
 306              $users[] = $this->prepare_response_for_collection( $data );
 307          }
 308  
 309          $response = rest_ensure_response( $users );
 310  
 311          // Store pagination values for headers then unset for count query.
 312          $per_page = (int) $prepared_args['number'];
 313          $page     = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
 314  
 315          $prepared_args['fields'] = 'ID';
 316  
 317          $total_users = $query->get_total();
 318  
 319          if ( $total_users < 1 ) {
 320              // Out-of-bounds, run the query again without LIMIT for total count.
 321              unset( $prepared_args['number'], $prepared_args['offset'] );
 322              $count_query = new WP_User_Query( $prepared_args );
 323              $total_users = $count_query->get_total();
 324          }
 325  
 326          $response->header( 'X-WP-Total', (int) $total_users );
 327  
 328          $max_pages = ceil( $total_users / $per_page );
 329  
 330          $response->header( 'X-WP-TotalPages', (int) $max_pages );
 331  
 332          $base = add_query_arg( urlencode_deep( $request->get_query_params() ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
 333          if ( $page > 1 ) {
 334              $prev_page = $page - 1;
 335  
 336              if ( $prev_page > $max_pages ) {
 337                  $prev_page = $max_pages;
 338              }
 339  
 340              $prev_link = add_query_arg( 'page', $prev_page, $base );
 341              $response->link_header( 'prev', $prev_link );
 342          }
 343          if ( $max_pages > $page ) {
 344              $next_page = $page + 1;
 345              $next_link = add_query_arg( 'page', $next_page, $base );
 346  
 347              $response->link_header( 'next', $next_link );
 348          }
 349  
 350          return $response;
 351      }
 352  
 353      /**
 354       * Get the user, if the ID is valid.
 355       *
 356       * @since 4.7.2
 357       *
 358       * @param int $id Supplied ID.
 359       * @return WP_User|WP_Error True if ID is valid, WP_Error otherwise.
 360       */
 361  	protected function get_user( $id ) {
 362          $error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
 363          if ( (int) $id <= 0 ) {
 364              return $error;
 365          }
 366  
 367          $user = get_userdata( (int) $id );
 368          if ( empty( $user ) || ! $user->exists() ) {
 369              return $error;
 370          }
 371  
 372          if ( is_multisite() && ! is_user_member_of_blog( $user->ID ) ) {
 373              return $error;
 374          }
 375  
 376          return $user;
 377      }
 378  
 379      /**
 380       * Checks if a given request has access to read a user.
 381       *
 382       * @since 4.7.0
 383       *
 384       * @param WP_REST_Request $request Full details about the request.
 385       * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
 386       */
 387  	public function get_item_permissions_check( $request ) {
 388          $user = $this->get_user( $request['id'] );
 389          if ( is_wp_error( $user ) ) {
 390              return $user;
 391          }
 392  
 393          $types = get_post_types( array( 'show_in_rest' => true ), 'names' );
 394  
 395          if ( get_current_user_id() === $user->ID ) {
 396              return true;
 397          }
 398  
 399          if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
 400              return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
 401          } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) {
 402              return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
 403          }
 404  
 405          return true;
 406      }
 407  
 408      /**
 409       * Retrieves a single user.
 410       *
 411       * @since 4.7.0
 412       *
 413       * @param WP_REST_Request $request Full details about the request.
 414       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 415       */
 416  	public function get_item( $request ) {
 417          $user = $this->get_user( $request['id'] );
 418          if ( is_wp_error( $user ) ) {
 419              return $user;
 420          }
 421  
 422          $user     = $this->prepare_item_for_response( $user, $request );
 423          $response = rest_ensure_response( $user );
 424  
 425          return $response;
 426      }
 427  
 428      /**
 429       * Retrieves the current user.
 430       *
 431       * @since 4.7.0
 432       *
 433       * @param WP_REST_Request $request Full details about the request.
 434       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 435       */
 436  	public function get_current_item( $request ) {
 437          $current_user_id = get_current_user_id();
 438  
 439          if ( empty( $current_user_id ) ) {
 440              return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) );
 441          }
 442  
 443          $user     = wp_get_current_user();
 444          $response = $this->prepare_item_for_response( $user, $request );
 445          $response = rest_ensure_response( $response );
 446  
 447          return $response;
 448      }
 449  
 450      /**
 451       * Checks if a given request has access create users.
 452       *
 453       * @since 4.7.0
 454       *
 455       * @param WP_REST_Request $request Full details about the request.
 456       * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
 457       */
 458  	public function create_item_permissions_check( $request ) {
 459  
 460          if ( ! current_user_can( 'create_users' ) ) {
 461              return new WP_Error( 'rest_cannot_create_user', __( 'Sorry, you are not allowed to create new users.' ), array( 'status' => rest_authorization_required_code() ) );
 462          }
 463  
 464          return true;
 465      }
 466  
 467      /**
 468       * Creates a single user.
 469       *
 470       * @since 4.7.0
 471       *
 472       * @param WP_REST_Request $request Full details about the request.
 473       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 474       */
 475  	public function create_item( $request ) {
 476          if ( ! empty( $request['id'] ) ) {
 477              return new WP_Error( 'rest_user_exists', __( 'Cannot create existing user.' ), array( 'status' => 400 ) );
 478          }
 479  
 480          $schema = $this->get_item_schema();
 481  
 482          if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
 483              $check_permission = $this->check_role_update( $request['id'], $request['roles'] );
 484  
 485              if ( is_wp_error( $check_permission ) ) {
 486                  return $check_permission;
 487              }
 488          }
 489  
 490          $user = $this->prepare_item_for_database( $request );
 491  
 492          if ( is_multisite() ) {
 493              $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
 494  
 495              if ( is_wp_error( $ret['errors'] ) && $ret['errors']->has_errors() ) {
 496                  $error = new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
 497                  foreach ( $ret['errors']->errors as $code => $messages ) {
 498                      foreach ( $messages as $message ) {
 499                          $error->add( $code, $message );
 500                      }
 501                      if ( $error_data = $error->get_error_data( $code ) ) {
 502                          $error->add_data( $error_data, $code );
 503                      }
 504                  }
 505                  return $error;
 506              }
 507          }
 508  
 509          if ( is_multisite() ) {
 510              $user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email );
 511  
 512              if ( ! $user_id ) {
 513                  return new WP_Error( 'rest_user_create', __( 'Error creating new user.' ), array( 'status' => 500 ) );
 514              }
 515  
 516              $user->ID = $user_id;
 517              $user_id  = wp_update_user( wp_slash( (array) $user ) );
 518  
 519              if ( is_wp_error( $user_id ) ) {
 520                  return $user_id;
 521              }
 522  
 523              $result = add_user_to_blog( get_site()->id, $user_id, '' );
 524              if ( is_wp_error( $result ) ) {
 525                  return $result;
 526              }
 527          } else {
 528              $user_id = wp_insert_user( wp_slash( (array) $user ) );
 529  
 530              if ( is_wp_error( $user_id ) ) {
 531                  return $user_id;
 532              }
 533          }
 534  
 535          $user = get_user_by( 'id', $user_id );
 536  
 537          /**
 538           * Fires immediately after a user is created or updated via the REST API.
 539           *
 540           * @since 4.7.0
 541           *
 542           * @param WP_User         $user     Inserted or updated user object.
 543           * @param WP_REST_Request $request  Request object.
 544           * @param bool            $creating True when creating a user, false when updating.
 545           */
 546          do_action( 'rest_insert_user', $user, $request, true );
 547  
 548          if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
 549              array_map( array( $user, 'add_role' ), $request['roles'] );
 550          }
 551  
 552          if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
 553              $meta_update = $this->meta->update_value( $request['meta'], $user_id );
 554  
 555              if ( is_wp_error( $meta_update ) ) {
 556                  return $meta_update;
 557              }
 558          }
 559  
 560          $user          = get_user_by( 'id', $user_id );
 561          $fields_update = $this->update_additional_fields_for_object( $user, $request );
 562  
 563          if ( is_wp_error( $fields_update ) ) {
 564              return $fields_update;
 565          }
 566  
 567          $request->set_param( 'context', 'edit' );
 568  
 569          /**
 570           * Fires after a user is completely created or updated via the REST API.
 571           *
 572           * @since 5.0.0
 573           *
 574           * @param WP_User         $user     Inserted or updated user object.
 575           * @param WP_REST_Request $request  Request object.
 576           * @param bool            $creating True when creating a user, false when updating.
 577           */
 578          do_action( 'rest_after_insert_user', $user, $request, true );
 579  
 580          $response = $this->prepare_item_for_response( $user, $request );
 581          $response = rest_ensure_response( $response );
 582  
 583          $response->set_status( 201 );
 584          $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );
 585  
 586          return $response;
 587      }
 588  
 589      /**
 590       * Checks if a given request has access to update a user.
 591       *
 592       * @since 4.7.0
 593       *
 594       * @param WP_REST_Request $request Full details about the request.
 595       * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
 596       */
 597  	public function update_item_permissions_check( $request ) {
 598          $user = $this->get_user( $request['id'] );
 599          if ( is_wp_error( $user ) ) {
 600              return $user;
 601          }
 602  
 603          if ( ! empty( $request['roles'] ) ) {
 604              if ( ! current_user_can( 'promote_user', $user->ID ) ) {
 605                  return new WP_Error( 'rest_cannot_edit_roles', __( 'Sorry, you are not allowed to edit roles of this user.' ), array( 'status' => rest_authorization_required_code() ) );
 606              }
 607  
 608              $request_params = array_keys( $request->get_params() );
 609              sort( $request_params );
 610              // If only 'id' and 'roles' are specified (we are only trying to
 611              // edit roles), then only the 'promote_user' cap is required.
 612              if ( $request_params === array( 'id', 'roles' ) ) {
 613                  return true;
 614              }
 615          }
 616  
 617          if ( ! current_user_can( 'edit_user', $user->ID ) ) {
 618              return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
 619          }
 620  
 621          return true;
 622      }
 623  
 624      /**
 625       * Updates a single user.
 626       *
 627       * @since 4.7.0
 628       *
 629       * @param WP_REST_Request $request Full details about the request.
 630       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 631       */
 632  	public function update_item( $request ) {
 633          $user = $this->get_user( $request['id'] );
 634          if ( is_wp_error( $user ) ) {
 635              return $user;
 636          }
 637  
 638          $id = $user->ID;
 639  
 640          if ( ! $user ) {
 641              return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
 642          }
 643  
 644          $owner_id = email_exists( $request['email'] );
 645  
 646          if ( $owner_id && $owner_id !== $id ) {
 647              return new WP_Error( 'rest_user_invalid_email', __( 'Invalid email address.' ), array( 'status' => 400 ) );
 648          }
 649  
 650          if ( ! empty( $request['username'] ) && $request['username'] !== $user->user_login ) {
 651              return new WP_Error( 'rest_user_invalid_argument', __( "Username isn't editable." ), array( 'status' => 400 ) );
 652          }
 653  
 654          if ( ! empty( $request['slug'] ) && $request['slug'] !== $user->user_nicename && get_user_by( 'slug', $request['slug'] ) ) {
 655              return new WP_Error( 'rest_user_invalid_slug', __( 'Invalid slug.' ), array( 'status' => 400 ) );
 656          }
 657  
 658          if ( ! empty( $request['roles'] ) ) {
 659              $check_permission = $this->check_role_update( $id, $request['roles'] );
 660  
 661              if ( is_wp_error( $check_permission ) ) {
 662                  return $check_permission;
 663              }
 664          }
 665  
 666          $user = $this->prepare_item_for_database( $request );
 667  
 668          // Ensure we're operating on the same user we already checked.
 669          $user->ID = $id;
 670  
 671          $user_id = wp_update_user( wp_slash( (array) $user ) );
 672  
 673          if ( is_wp_error( $user_id ) ) {
 674              return $user_id;
 675          }
 676  
 677          $user = get_user_by( 'id', $user_id );
 678  
 679          /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php */
 680          do_action( 'rest_insert_user', $user, $request, false );
 681  
 682          if ( ! empty( $request['roles'] ) ) {
 683              array_map( array( $user, 'add_role' ), $request['roles'] );
 684          }
 685  
 686          $schema = $this->get_item_schema();
 687  
 688          if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
 689              $meta_update = $this->meta->update_value( $request['meta'], $id );
 690  
 691              if ( is_wp_error( $meta_update ) ) {
 692                  return $meta_update;
 693              }
 694          }
 695  
 696          $user          = get_user_by( 'id', $user_id );
 697          $fields_update = $this->update_additional_fields_for_object( $user, $request );
 698  
 699          if ( is_wp_error( $fields_update ) ) {
 700              return $fields_update;
 701          }
 702  
 703          $request->set_param( 'context', 'edit' );
 704  
 705          /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php */
 706          do_action( 'rest_after_insert_user', $user, $request, false );
 707  
 708          $response = $this->prepare_item_for_response( $user, $request );
 709          $response = rest_ensure_response( $response );
 710  
 711          return $response;
 712      }
 713  
 714      /**
 715       * Checks if a given request has access to update the current user.
 716       *
 717       * @since 4.7.0
 718       *
 719       * @param WP_REST_Request $request Full details about the request.
 720       * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
 721       */
 722  	public function update_current_item_permissions_check( $request ) {
 723          $request['id'] = get_current_user_id();
 724  
 725          return $this->update_item_permissions_check( $request );
 726      }
 727  
 728      /**
 729       * Updates the current user.
 730       *
 731       * @since 4.7.0
 732       *
 733       * @param WP_REST_Request $request Full details about the request.
 734       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 735       */
 736  	function update_current_item( $request ) {
 737          $request['id'] = get_current_user_id();
 738  
 739          return $this->update_item( $request );
 740      }
 741  
 742      /**
 743       * Checks if a given request has access delete a user.
 744       *
 745       * @since 4.7.0
 746       *
 747       * @param WP_REST_Request $request Full details about the request.
 748       * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
 749       */
 750  	public function delete_item_permissions_check( $request ) {
 751          $user = $this->get_user( $request['id'] );
 752          if ( is_wp_error( $user ) ) {
 753              return $user;
 754          }
 755  
 756          if ( ! current_user_can( 'delete_user', $user->ID ) ) {
 757              return new WP_Error( 'rest_user_cannot_delete', __( 'Sorry, you are not allowed to delete this user.' ), array( 'status' => rest_authorization_required_code() ) );
 758          }
 759  
 760          return true;
 761      }
 762  
 763      /**
 764       * Deletes a single user.
 765       *
 766       * @since 4.7.0
 767       *
 768       * @param WP_REST_Request $request Full details about the request.
 769       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 770       */
 771  	public function delete_item( $request ) {
 772          // We don't support delete requests in multisite.
 773          if ( is_multisite() ) {
 774              return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 501 ) );
 775          }
 776          $user = $this->get_user( $request['id'] );
 777          if ( is_wp_error( $user ) ) {
 778              return $user;
 779          }
 780  
 781          $id       = $user->ID;
 782          $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] );
 783          $force    = isset( $request['force'] ) ? (bool) $request['force'] : false;
 784  
 785          // We don't support trashing for users.
 786          if ( ! $force ) {
 787              /* translators: %s: force=true */
 788              return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Users do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
 789          }
 790  
 791          if ( ! empty( $reassign ) ) {
 792              if ( $reassign === $id || ! get_userdata( $reassign ) ) {
 793                  return new WP_Error( 'rest_user_invalid_reassign', __( 'Invalid user ID for reassignment.' ), array( 'status' => 400 ) );
 794              }
 795          }
 796  
 797          $request->set_param( 'context', 'edit' );
 798  
 799          $previous = $this->prepare_item_for_response( $user, $request );
 800  
 801          /** Include admin user functions to get access to wp_delete_user() */
 802          require_once  ABSPATH . 'wp-admin/includes/user.php';
 803  
 804          $result = wp_delete_user( $id, $reassign );
 805  
 806          if ( ! $result ) {
 807              return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 500 ) );
 808          }
 809  
 810          $response = new WP_REST_Response();
 811          $response->set_data(
 812              array(
 813                  'deleted'  => true,
 814                  'previous' => $previous->get_data(),
 815              )
 816          );
 817  
 818          /**
 819           * Fires immediately after a user is deleted via the REST API.
 820           *
 821           * @since 4.7.0
 822           *
 823           * @param WP_User          $user     The user data.
 824           * @param WP_REST_Response $response The response returned from the API.
 825           * @param WP_REST_Request  $request  The request sent to the API.
 826           */
 827          do_action( 'rest_delete_user', $user, $response, $request );
 828  
 829          return $response;
 830      }
 831  
 832      /**
 833       * Checks if a given request has access to delete the current user.
 834       *
 835       * @since 4.7.0
 836       *
 837       * @param WP_REST_Request $request Full details about the request.
 838       * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
 839       */
 840  	public function delete_current_item_permissions_check( $request ) {
 841          $request['id'] = get_current_user_id();
 842  
 843          return $this->delete_item_permissions_check( $request );
 844      }
 845  
 846      /**
 847       * Deletes the current user.
 848       *
 849       * @since 4.7.0
 850       *
 851       * @param WP_REST_Request $request Full details about the request.
 852       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 853       */
 854  	function delete_current_item( $request ) {
 855          $request['id'] = get_current_user_id();
 856  
 857          return $this->delete_item( $request );
 858      }
 859  
 860      /**
 861       * Prepares a single user output for response.
 862       *
 863       * @since 4.7.0
 864       *
 865       * @param WP_User         $user    User object.
 866       * @param WP_REST_Request $request Request object.
 867       * @return WP_REST_Response Response object.
 868       */
 869  	public function prepare_item_for_response( $user, $request ) {
 870  
 871          $data   = array();
 872          $fields = $this->get_fields_for_response( $request );
 873  
 874          if ( in_array( 'id', $fields, true ) ) {
 875              $data['id'] = $user->ID;
 876          }
 877  
 878          if ( in_array( 'username', $fields, true ) ) {
 879              $data['username'] = $user->user_login;
 880          }
 881  
 882          if ( in_array( 'name', $fields, true ) ) {
 883              $data['name'] = $user->display_name;
 884          }
 885  
 886          if ( in_array( 'first_name', $fields, true ) ) {
 887              $data['first_name'] = $user->first_name;
 888          }
 889  
 890          if ( in_array( 'last_name', $fields, true ) ) {
 891              $data['last_name'] = $user->last_name;
 892          }
 893  
 894          if ( in_array( 'email', $fields, true ) ) {
 895              $data['email'] = $user->user_email;
 896          }
 897  
 898          if ( in_array( 'url', $fields, true ) ) {
 899              $data['url'] = $user->user_url;
 900          }
 901  
 902          if ( in_array( 'description', $fields, true ) ) {
 903              $data['description'] = $user->description;
 904          }
 905  
 906          if ( in_array( 'link', $fields, true ) ) {
 907              $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
 908          }
 909  
 910          if ( in_array( 'locale', $fields, true ) ) {
 911              $data['locale'] = get_user_locale( $user );
 912          }
 913  
 914          if ( in_array( 'nickname', $fields, true ) ) {
 915              $data['nickname'] = $user->nickname;
 916          }
 917  
 918          if ( in_array( 'slug', $fields, true ) ) {
 919              $data['slug'] = $user->user_nicename;
 920          }
 921  
 922          if ( in_array( 'roles', $fields, true ) ) {
 923              // Defensively call array_values() to ensure an array is returned.
 924              $data['roles'] = array_values( $user->roles );
 925          }
 926  
 927          if ( in_array( 'registered_date', $fields, true ) ) {
 928              $data['registered_date'] = gmdate( 'c', strtotime( $user->user_registered ) );
 929          }
 930  
 931          if ( in_array( 'capabilities', $fields, true ) ) {
 932              $data['capabilities'] = (object) $user->allcaps;
 933          }
 934  
 935          if ( in_array( 'extra_capabilities', $fields, true ) ) {
 936              $data['extra_capabilities'] = (object) $user->caps;
 937          }
 938  
 939          if ( in_array( 'avatar_urls', $fields, true ) ) {
 940              $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
 941          }
 942  
 943          if ( in_array( 'meta', $fields, true ) ) {
 944              $data['meta'] = $this->meta->get_value( $user->ID, $request );
 945          }
 946  
 947          $context = ! empty( $request['context'] ) ? $request['context'] : 'embed';
 948  
 949          $data = $this->add_additional_fields_to_object( $data, $request );
 950          $data = $this->filter_response_by_context( $data, $context );
 951  
 952          // Wrap the data in a response object.
 953          $response = rest_ensure_response( $data );
 954  
 955          $response->add_links( $this->prepare_links( $user ) );
 956  
 957          /**
 958           * Filters user data returned from the REST API.
 959           *
 960           * @since 4.7.0
 961           *
 962           * @param WP_REST_Response $response The response object.
 963           * @param object           $user     User object used to create response.
 964           * @param WP_REST_Request  $request  Request object.
 965           */
 966          return apply_filters( 'rest_prepare_user', $response, $user, $request );
 967      }
 968  
 969      /**
 970       * Prepares links for the user request.
 971       *
 972       * @since 4.7.0
 973       *
 974       * @param WP_Post $user User object.
 975       * @return array Links for the given user.
 976       */
 977  	protected function prepare_links( $user ) {
 978          $links = array(
 979              'self'       => array(
 980                  'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $user->ID ) ),
 981              ),
 982              'collection' => array(
 983                  'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 984              ),
 985          );
 986  
 987          return $links;
 988      }
 989  
 990      /**
 991       * Prepares a single user for creation or update.
 992       *
 993       * @since 4.7.0
 994       *
 995       * @param WP_REST_Request $request Request object.
 996       * @return object $prepared_user User object.
 997       */
 998  	protected function prepare_item_for_database( $request ) {
 999          $prepared_user = new stdClass;
1000  
1001          $schema = $this->get_item_schema();
1002  
1003          // required arguments.
1004          if ( isset( $request['email'] ) && ! empty( $schema['properties']['email'] ) ) {
1005              $prepared_user->user_email = $request['email'];
1006          }
1007  
1008          if ( isset( $request['username'] ) && ! empty( $schema['properties']['username'] ) ) {
1009              $prepared_user->user_login = $request['username'];
1010          }
1011  
1012          if ( isset( $request['password'] ) && ! empty( $schema['properties']['password'] ) ) {
1013              $prepared_user->user_pass = $request['password'];
1014          }
1015  
1016          // optional arguments.
1017          if ( isset( $request['id'] ) ) {
1018              $prepared_user->ID = absint( $request['id'] );
1019          }
1020  
1021          if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
1022              $prepared_user->display_name = $request['name'];
1023          }
1024  
1025          if ( isset( $request['first_name'] ) && ! empty( $schema['properties']['first_name'] ) ) {
1026              $prepared_user->first_name = $request['first_name'];
1027          }
1028  
1029          if ( isset( $request['last_name'] ) && ! empty( $schema['properties']['last_name'] ) ) {
1030              $prepared_user->last_name = $request['last_name'];
1031          }
1032  
1033          if ( isset( $request['nickname'] ) && ! empty( $schema['properties']['nickname'] ) ) {
1034              $prepared_user->nickname = $request['nickname'];
1035          }
1036  
1037          if ( isset( $request['slug'] ) && ! empty( $schema['properties']['slug'] ) ) {
1038              $prepared_user->user_nicename = $request['slug'];
1039          }
1040  
1041          if ( isset( $request['description'] ) && ! empty( $schema['properties']['description'] ) ) {
1042              $prepared_user->description = $request['description'];
1043          }
1044  
1045          if ( isset( $request['url'] ) && ! empty( $schema['properties']['url'] ) ) {
1046              $prepared_user->user_url = $request['url'];
1047          }
1048  
1049          if ( isset( $request['locale'] ) && ! empty( $schema['properties']['locale'] ) ) {
1050              $prepared_user->locale = $request['locale'];
1051          }
1052  
1053          // setting roles will be handled outside of this function.
1054          if ( isset( $request['roles'] ) ) {
1055              $prepared_user->role = false;
1056          }
1057  
1058          /**
1059           * Filters user data before insertion via the REST API.
1060           *
1061           * @since 4.7.0
1062           *
1063           * @param object          $prepared_user User object.
1064           * @param WP_REST_Request $request       Request object.
1065           */
1066          return apply_filters( 'rest_pre_insert_user', $prepared_user, $request );
1067      }
1068  
1069      /**
1070       * Determines if the current user is allowed to make the desired roles change.
1071       *
1072       * @since 4.7.0
1073       *
1074       * @param integer $user_id User ID.
1075       * @param array   $roles   New user roles.
1076       * @return true|WP_Error True if the current user is allowed to make the role change,
1077       *                       otherwise a WP_Error object.
1078       */
1079  	protected function check_role_update( $user_id, $roles ) {
1080          global $wp_roles;
1081  
1082          foreach ( $roles as $role ) {
1083  
1084              if ( ! isset( $wp_roles->role_objects[ $role ] ) ) {
1085                  /* translators: %s: role key */
1086                  return new WP_Error( 'rest_user_invalid_role', sprintf( __( 'The role %s does not exist.' ), $role ), array( 'status' => 400 ) );
1087              }
1088  
1089              $potential_role = $wp_roles->role_objects[ $role ];
1090  
1091              /*
1092               * Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
1093               * Multisite super admins can freely edit their blog roles -- they possess all caps.
1094               */
1095              if ( ! ( is_multisite()
1096                  && current_user_can( 'manage_sites' ) )
1097                  && get_current_user_id() === $user_id
1098                  && ! $potential_role->has_cap( 'edit_users' )
1099              ) {
1100                  return new WP_Error( 'rest_user_invalid_role', __( 'Sorry, you are not allowed to give users that role.' ), array( 'status' => rest_authorization_required_code() ) );
1101              }
1102  
1103              /** Include admin functions to get access to get_editable_roles() */
1104              require_once  ABSPATH . 'wp-admin/includes/admin.php';
1105  
1106              // The new role must be editable by the logged-in user.
1107              $editable_roles = get_editable_roles();
1108  
1109              if ( empty( $editable_roles[ $role ] ) ) {
1110                  return new WP_Error( 'rest_user_invalid_role', __( 'Sorry, you are not allowed to give users that role.' ), array( 'status' => 403 ) );
1111              }
1112          }
1113  
1114          return true;
1115      }
1116  
1117      /**
1118       * Check a username for the REST API.
1119       *
1120       * Performs a couple of checks like edit_user() in wp-admin/includes/user.php.
1121       *
1122       * @since 4.7.0
1123       *
1124       * @param  mixed            $value   The username submitted in the request.
1125       * @param  WP_REST_Request  $request Full details about the request.
1126       * @param  string           $param   The parameter name.
1127       * @return WP_Error|string The sanitized username, if valid, otherwise an error.
1128       */
1129  	public function check_username( $value, $request, $param ) {
1130          $username = (string) $value;
1131  
1132          if ( ! validate_username( $username ) ) {
1133              return new WP_Error( 'rest_user_invalid_username', __( 'Username contains invalid characters.' ), array( 'status' => 400 ) );
1134          }
1135  
1136          /** This filter is documented in wp-includes/user.php */
1137          $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
1138  
1139          if ( in_array( strtolower( $username ), array_map( 'strtolower', $illegal_logins ) ) ) {
1140              return new WP_Error( 'rest_user_invalid_username', __( 'Sorry, that username is not allowed.' ), array( 'status' => 400 ) );
1141          }
1142  
1143          return $username;
1144      }
1145  
1146      /**
1147       * Check a user password for the REST API.
1148       *
1149       * Performs a couple of checks like edit_user() in wp-admin/includes/user.php.
1150       *
1151       * @since 4.7.0
1152       *
1153       * @param  mixed            $value   The password submitted in the request.
1154       * @param  WP_REST_Request  $request Full details about the request.
1155       * @param  string           $param   The parameter name.
1156       * @return WP_Error|string The sanitized password, if valid, otherwise an error.
1157       */
1158  	public function check_user_password( $value, $request, $param ) {
1159          $password = (string) $value;
1160  
1161          if ( empty( $password ) ) {
1162              return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot be empty.' ), array( 'status' => 400 ) );
1163          }
1164  
1165          if ( false !== strpos( $password, '\\' ) ) {
1166              return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot contain the "\\" character.' ), array( 'status' => 400 ) );
1167          }
1168  
1169          return $password;
1170      }
1171  
1172      /**
1173       * Retrieves the user's schema, conforming to JSON Schema.
1174       *
1175       * @since 4.7.0
1176       *
1177       * @return array Item schema data.
1178       */
1179  	public function get_item_schema() {
1180          $schema = array(
1181              '$schema'    => 'http://json-schema.org/draft-04/schema#',
1182              'title'      => 'user',
1183              'type'       => 'object',
1184              'properties' => array(
1185                  'id'                 => array(
1186                      'description' => __( 'Unique identifier for the user.' ),
1187                      'type'        => 'integer',
1188                      'context'     => array( 'embed', 'view', 'edit' ),
1189                      'readonly'    => true,
1190                  ),
1191                  'username'           => array(
1192                      'description' => __( 'Login name for the user.' ),
1193                      'type'        => 'string',
1194                      'context'     => array( 'edit' ),
1195                      'required'    => true,
1196                      'arg_options' => array(
1197                          'sanitize_callback' => array( $this, 'check_username' ),
1198                      ),
1199                  ),
1200                  'name'               => array(
1201                      'description' => __( 'Display name for the user.' ),
1202                      'type'        => 'string',
1203                      'context'     => array( 'embed', 'view', 'edit' ),
1204                      'arg_options' => array(
1205                          'sanitize_callback' => 'sanitize_text_field',
1206                      ),
1207                  ),
1208                  'first_name'         => array(
1209                      'description' => __( 'First name for the user.' ),
1210                      'type'        => 'string',
1211                      'context'     => array( 'edit' ),
1212                      'arg_options' => array(
1213                          'sanitize_callback' => 'sanitize_text_field',
1214                      ),
1215                  ),
1216                  'last_name'          => array(
1217                      'description' => __( 'Last name for the user.' ),
1218                      'type'        => 'string',
1219                      'context'     => array( 'edit' ),
1220                      'arg_options' => array(
1221                          'sanitize_callback' => 'sanitize_text_field',
1222                      ),
1223                  ),
1224                  'email'              => array(
1225                      'description' => __( 'The email address for the user.' ),
1226                      'type'        => 'string',
1227                      'format'      => 'email',
1228                      'context'     => array( 'edit' ),
1229                      'required'    => true,
1230                  ),
1231                  'url'                => array(
1232                      'description' => __( 'URL of the user.' ),
1233                      'type'        => 'string',
1234                      'format'      => 'uri',
1235                      'context'     => array( 'embed', 'view', 'edit' ),
1236                  ),
1237                  'description'        => array(
1238                      'description' => __( 'Description of the user.' ),
1239                      'type'        => 'string',
1240                      'context'     => array( 'embed', 'view', 'edit' ),
1241                  ),
1242                  'link'               => array(
1243                      'description' => __( 'Author URL of the user.' ),
1244                      'type'        => 'string',
1245                      'format'      => 'uri',
1246                      'context'     => array( 'embed', 'view', 'edit' ),
1247                      'readonly'    => true,
1248                  ),
1249                  'locale'             => array(
1250                      'description' => __( 'Locale for the user.' ),
1251                      'type'        => 'string',
1252                      'enum'        => array_merge( array( '', 'en_US' ), get_available_languages() ),
1253                      'context'     => array( 'edit' ),
1254                  ),
1255                  'nickname'           => array(
1256                      'description' => __( 'The nickname for the user.' ),
1257                      'type'        => 'string',
1258                      'context'     => array( 'edit' ),
1259                      'arg_options' => array(
1260                          'sanitize_callback' => 'sanitize_text_field',
1261                      ),
1262                  ),
1263                  'slug'               => array(
1264                      'description' => __( 'An alphanumeric identifier for the user.' ),
1265                      'type'        => 'string',
1266                      'context'     => array( 'embed', 'view', 'edit' ),
1267                      'arg_options' => array(
1268                          'sanitize_callback' => array( $this, 'sanitize_slug' ),
1269                      ),
1270                  ),
1271                  'registered_date'    => array(
1272                      'description' => __( 'Registration date for the user.' ),
1273                      'type'        => 'string',
1274                      'format'      => 'date-time',
1275                      'context'     => array( 'edit' ),
1276                      'readonly'    => true,
1277                  ),
1278                  'roles'              => array(
1279                      'description' => __( 'Roles assigned to the user.' ),
1280                      'type'        => 'array',
1281                      'items'       => array(
1282                          'type' => 'string',
1283                      ),
1284                      'context'     => array( 'edit' ),
1285                  ),
1286                  'password'           => array(
1287                      'description' => __( 'Password for the user (never included).' ),
1288                      'type'        => 'string',
1289                      'context'     => array(), // Password is never displayed.
1290                      'required'    => true,
1291                      'arg_options' => array(
1292                          'sanitize_callback' => array( $this, 'check_user_password' ),
1293                      ),
1294                  ),
1295                  'capabilities'       => array(
1296                      'description' => __( 'All capabilities assigned to the user.' ),
1297                      'type'        => 'object',
1298                      'context'     => array( 'edit' ),
1299                      'readonly'    => true,
1300                  ),
1301                  'extra_capabilities' => array(
1302                      'description' => __( 'Any extra capabilities assigned to the user.' ),
1303                      'type'        => 'object',
1304                      'context'     => array( 'edit' ),
1305                      'readonly'    => true,
1306                  ),
1307              ),
1308          );
1309  
1310          if ( get_option( 'show_avatars' ) ) {
1311              $avatar_properties = array();
1312  
1313              $avatar_sizes = rest_get_avatar_sizes();
1314  
1315              foreach ( $avatar_sizes as $size ) {
1316                  $avatar_properties[ $size ] = array(
1317                      /* translators: %d: avatar image size in pixels */
1318                      'description' => sprintf( __( 'Avatar URL with image size of %d pixels.' ), $size ),
1319                      'type'        => 'string',
1320                      'format'      => 'uri',
1321                      'context'     => array( 'embed', 'view', 'edit' ),
1322                  );
1323              }
1324  
1325              $schema['properties']['avatar_urls'] = array(
1326                  'description' => __( 'Avatar URLs for the user.' ),
1327                  'type'        => 'object',
1328                  'context'     => array( 'embed', 'view', 'edit' ),
1329                  'readonly'    => true,
1330                  'properties'  => $avatar_properties,
1331              );
1332          }
1333  
1334          $schema['properties']['meta'] = $this->meta->get_field_schema();
1335  
1336          return $this->add_additional_fields_schema( $schema );
1337      }
1338  
1339      /**
1340       * Retrieves the query params for collections.
1341       *
1342       * @since 4.7.0
1343       *
1344       * @return array Collection parameters.
1345       */
1346  	public function get_collection_params() {
1347          $query_params = parent::get_collection_params();
1348  
1349          $query_params['context']['default'] = 'view';
1350  
1351          $query_params['exclude'] = array(
1352              'description' => __( 'Ensure result set excludes specific IDs.' ),
1353              'type'        => 'array',
1354              'items'       => array(
1355                  'type' => 'integer',
1356              ),
1357              'default'     => array(),
1358          );
1359  
1360          $query_params['include'] = array(
1361              'description' => __( 'Limit result set to specific IDs.' ),
1362              'type'        => 'array',
1363              'items'       => array(
1364                  'type' => 'integer',
1365              ),
1366              'default'     => array(),
1367          );
1368  
1369          $query_params['offset'] = array(
1370              'description' => __( 'Offset the result set by a specific number of items.' ),
1371              'type'        => 'integer',
1372          );
1373  
1374          $query_params['order'] = array(
1375              'default'     => 'asc',
1376              'description' => __( 'Order sort attribute ascending or descending.' ),
1377              'enum'        => array( 'asc', 'desc' ),
1378              'type'        => 'string',
1379          );
1380  
1381          $query_params['orderby'] = array(
1382              'default'     => 'name',
1383              'description' => __( 'Sort collection by object attribute.' ),
1384              'enum'        => array(
1385                  'id',
1386                  'include',
1387                  'name',
1388                  'registered_date',
1389                  'slug',
1390                  'include_slugs',
1391                  'email',
1392                  'url',
1393              ),
1394              'type'        => 'string',
1395          );
1396  
1397          $query_params['slug'] = array(
1398              'description' => __( 'Limit result set to users with one or more specific slugs.' ),
1399              'type'        => 'array',
1400              'items'       => array(
1401                  'type' => 'string',
1402              ),
1403          );
1404  
1405          $query_params['roles'] = array(
1406              'description' => __( 'Limit result set to users matching at least one specific role provided. Accepts csv list or single role.' ),
1407              'type'        => 'array',
1408              'items'       => array(
1409                  'type' => 'string',
1410              ),
1411          );
1412  
1413          $query_params['who'] = array(
1414              'description' => __( 'Limit result set to users who are considered authors.' ),
1415              'type'        => 'string',
1416              'enum'        => array(
1417                  'authors',
1418              ),
1419          );
1420  
1421          /**
1422           * Filter collection parameters for the users controller.
1423           *
1424           * This filter registers the collection parameter, but does not map the
1425           * collection parameter to an internal WP_User_Query parameter.  Use the
1426           * `rest_user_query` filter to set WP_User_Query arguments.
1427           *
1428           * @since 4.7.0
1429           *
1430           * @param array $query_params JSON Schema-formatted collection parameters.
1431           */
1432          return apply_filters( 'rest_user_collection_params', $query_params );
1433      }
1434  }


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7