WPCOM_JSON_API_List_Users_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Users Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-list-users-endpoint.php  
  1. class WPCOM_JSON_API_List_Users_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. var $response_format = array( 
  4. 'found' => '(int) The total number of authors found that match the request (ignoring limits and offsets).',  
  5. 'users' => '(array:author) Array of user objects',  
  6. ); 
  7.  
  8. // /sites/%s/users/ -> $blog_id 
  9. function callback( $path = '', $blog_id = 0 ) { 
  10. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  11. if ( is_wp_error( $blog_id ) ) { 
  12. return $blog_id; 
  13.  
  14. $args = $this->query_args(); 
  15.  
  16. $authors_only = ( ! empty( $args['authors_only'] ) ); 
  17.  
  18. if ( $args['number'] < 1 ) { 
  19. $args['number'] = 20; 
  20. } elseif ( 100 < $args['number'] ) { 
  21. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); 
  22.  
  23. if ( $authors_only ) { 
  24. if ( empty( $args['type'] ) ) 
  25. $args['type'] = 'post'; 
  26.  
  27. if ( ! $this->is_post_type_allowed( $args['type'] ) ) { 
  28. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); 
  29.  
  30. $post_type_object = get_post_type_object( $args['type'] ); 
  31. if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { 
  32. return new WP_Error( 'unauthorized', 'User cannot view authors for specified post type', 403 ); 
  33. } elseif ( ! current_user_can( 'list_users' ) ) { 
  34. return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 ); 
  35.  
  36. $query = array( 
  37. 'number' => $args['number'],  
  38. 'offset' => $args['offset'],  
  39. 'order' => $args['order'],  
  40. 'orderby' => $args['order_by'],  
  41. 'fields' => 'ID',  
  42. ); 
  43.  
  44. if ( $authors_only ) 
  45. $query['who'] = 'authors'; 
  46.  
  47. if ( ! empty( $args['search'] ) ) { 
  48. $query['search'] = $args['search']; 
  49.  
  50. if ( ! empty( $args['search_columns'] ) ) { 
  51. // this `user_search_columns` filter is necessary because WP_User_Query does not allow `display_name` as a search column 
  52. $this->search_columns = array_intersect( $args['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', 'display_name' ) ); 
  53. add_filter( 'user_search_columns', array( $this, 'api_user_override_search_columns' ), 10, 3 ); 
  54.  
  55. if ( ! empty( $args['role'] ) ) { 
  56. $query['role'] = $args['role']; 
  57.  
  58. $user_query = new WP_User_Query( $query ); 
  59.  
  60. remove_filter( 'user_search_columns', array( $this, 'api_user_override_search_columns' ) ); 
  61.  
  62. $return = array(); 
  63. foreach ( array_keys( $this->response_format ) as $key ) { 
  64. switch ( $key ) { 
  65. case 'found' : 
  66. $return[$key] = (int) $user_query->get_total(); 
  67. break; 
  68. case 'users' : 
  69. $users = array(); 
  70. $is_multisite = is_multisite(); 
  71. foreach ( $user_query->get_results() as $u ) { 
  72. $the_user = $this->get_author( $u, true ); 
  73. if ( $the_user && ! is_wp_error( $the_user ) ) { 
  74. $userdata = get_userdata( $u ); 
  75. $the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array(); 
  76. if ( $is_multisite ) { 
  77. $the_user->is_super_admin = user_can( $the_user->ID, 'manage_network' ); 
  78. $users[] = $the_user; 
  79.  
  80. $return[$key] = $users; 
  81. break; 
  82.  
  83. return $return; 
  84.  
  85. function api_user_override_search_columns( $search_columns, $search ) { 
  86. return $this->search_columns;