home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / rest-api / endpoints / class-wp-rest-users-controller.php < prev   
Encoding:
PHP Script  |  2017-10-04  |  42.6 KB  |  1,379 lines

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