Nav apraksta

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

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