class-wp-rest-users-controller.php 44 KB

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