class.wpcom-json-api-list-users-endpoint.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. new WPCOM_JSON_API_List_Users_Endpoint( array(
  3. 'description' => 'List the users of a site.',
  4. 'group' => 'users',
  5. 'stat' => 'users:list',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/users',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'query_parameters' => array(
  12. 'number' => '(int=20) Limit the total number of authors returned.',
  13. 'offset' => '(int=0) The first n authors to be skipped in the returned array.',
  14. 'order' => array(
  15. 'DESC' => 'Return authors in descending order.',
  16. 'ASC' => 'Return authors in ascending order.',
  17. ),
  18. 'order_by' => array(
  19. 'ID' => 'Order by ID (default).',
  20. 'login' => 'Order by username.',
  21. 'nicename' => "Order by nicename.",
  22. 'email' => 'Order by author email address.',
  23. 'url' => 'Order by author URL.',
  24. 'registered' => 'Order by registered date.',
  25. 'display_name' => 'Order by display name.',
  26. 'post_count' => 'Order by number of posts published.',
  27. ),
  28. 'authors_only' => '(bool) Set to true to fetch authors only',
  29. 'type' => "(string) Specify the post type to query authors for. Only works when combined with the `authors_only` flag. Defaults to 'post'. Post types besides post and page need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
  30. 'search' => '(string) Find matching users.',
  31. 'search_columns' => "(array) Specify which columns to check for matching users. Can be any of 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', and 'display_name'. Only works when combined with `search` parameter.",
  32. 'role' => '(string) Specify a specific user role to fetch.'
  33. ),
  34. 'response_format' => array(
  35. 'found' => '(int) The total number of authors found that match the request (ignoring limits and offsets).',
  36. 'authors' => '(array:author) Array of author objects.',
  37. ),
  38. 'example_response' => '{
  39. "found": 1,
  40. "users": [
  41. {
  42. "ID": 78972699,
  43. "login": "apiexamples",
  44. "email": "justin+apiexamples@a8c.com",
  45. "name": "apiexamples",
  46. "first_name": "",
  47. "last_name": "",
  48. "nice_name": "apiexamples",
  49. "URL": "http://apiexamples.wordpress.com",
  50. "avatar_URL": "https://1.gravatar.com/avatar/a2afb7b6c0e23e5d363d8612fb1bd5ad?s=96&d=identicon&r=G",
  51. "profile_URL": "https://en.gravatar.com/apiexamples",
  52. "site_ID": 82974409,
  53. "roles": [
  54. "administrator"
  55. ],
  56. "is_super_admin": false
  57. }
  58. ]
  59. }',
  60. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/users',
  61. 'example_request_data' => array(
  62. 'headers' => array(
  63. 'authorization' => 'Bearer YOUR_API_TOKEN'
  64. ),
  65. )
  66. ) );
  67. class WPCOM_JSON_API_List_Users_Endpoint extends WPCOM_JSON_API_Endpoint {
  68. var $response_format = array(
  69. 'found' => '(int) The total number of authors found that match the request (ignoring limits and offsets).',
  70. 'users' => '(array:author) Array of user objects',
  71. );
  72. // /sites/%s/users/ -> $blog_id
  73. function callback( $path = '', $blog_id = 0 ) {
  74. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  75. if ( is_wp_error( $blog_id ) ) {
  76. return $blog_id;
  77. }
  78. $args = $this->query_args();
  79. $authors_only = ( ! empty( $args['authors_only'] ) );
  80. if ( $args['number'] < 1 ) {
  81. $args['number'] = 20;
  82. } elseif ( 1000 < $args['number'] ) {
  83. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 );
  84. }
  85. if ( $authors_only ) {
  86. if ( empty( $args['type'] ) ) {
  87. $args['type'] = 'post';
  88. }
  89. if ( ! $this->is_post_type_allowed( $args['type'] ) ) {
  90. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
  91. }
  92. $post_type_object = get_post_type_object( $args['type'] );
  93. if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->edit_others_posts ) ) {
  94. return new WP_Error( 'unauthorized', 'User cannot view authors for specified post type', 403 );
  95. }
  96. } elseif ( ! current_user_can( 'list_users' ) ) {
  97. return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 );
  98. }
  99. $query = array(
  100. 'number' => $args['number'],
  101. 'offset' => $args['offset'],
  102. 'order' => $args['order'],
  103. 'orderby' => $args['order_by'],
  104. 'fields' => 'ID',
  105. );
  106. if ( $authors_only ) {
  107. $query['who'] = 'authors';
  108. }
  109. if ( ! empty( $args['search'] ) ) {
  110. $query['search'] = $args['search'];
  111. }
  112. if ( ! empty( $args['search_columns'] ) ) {
  113. // this `user_search_columns` filter is necessary because WP_User_Query does not allow `display_name` as a search column
  114. $this->search_columns = array_intersect( $args['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', 'display_name' ) );
  115. add_filter( 'user_search_columns', array( $this, 'api_user_override_search_columns' ), 10, 3 );
  116. }
  117. if ( ! empty( $args['role'] ) ) {
  118. $query['role'] = $args['role'];
  119. }
  120. $user_query = new WP_User_Query( $query );
  121. remove_filter( 'user_search_columns', array( $this, 'api_user_override_search_columns' ) );
  122. $return = array();
  123. foreach ( array_keys( $this->response_format ) as $key ) {
  124. switch ( $key ) {
  125. case 'found' :
  126. $return[ $key ] = (int) $user_query->get_total();
  127. break;
  128. case 'users' :
  129. $users = array();
  130. $is_multisite = is_multisite();
  131. foreach ( $user_query->get_results() as $u ) {
  132. $the_user = $this->get_author( $u, true );
  133. if ( $the_user && ! is_wp_error( $the_user ) ) {
  134. $userdata = get_userdata( $u );
  135. $the_user->roles = ! is_wp_error( $userdata ) ? array_values( $userdata->roles ) : array();
  136. if ( $is_multisite ) {
  137. $the_user->is_super_admin = user_can( $the_user->ID, 'manage_network' );
  138. }
  139. $users[] = $the_user;
  140. }
  141. }
  142. $return[ $key ] = $users;
  143. break;
  144. }
  145. }
  146. return $return;
  147. }
  148. function api_user_override_search_columns( $search_columns, $search ) {
  149. return $this->search_columns;
  150. }
  151. }