WPCOM_JSON_API_Site_User_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Site User Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-site-user-endpoint.php  
  1. class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. public static $user_format = array( 
  4. 'ID' => '(int) The ID of the user',  
  5. 'login' => '(string) The login username of the user',  
  6. 'email' => '(string) The email of the user',  
  7. 'name' => '(string) The name to display for the user',  
  8. 'first_name' => '(string) The first name of the user',  
  9. 'last_name' => '(string) The last name of the user',  
  10. 'nice_name' => '(string) The nice_name to display for the user',  
  11. 'URL' => '(string) The primary blog of the user',  
  12. 'avatar_URL' => '(url) Gravatar image URL',  
  13. 'profile_URL' => '(url) Gravatar Profile URL',  
  14. 'site_ID' => '(int) ID of the user\'s primary blog',  
  15. 'roles' => '(array) The roles of the user',  
  16. ); 
  17.  
  18. // /sites/%s/users/%d -> $blog_id, $user_id 
  19. function callback( $path = '', $blog_id = 0, $user_id = 0 ) { 
  20. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  21. if ( is_wp_error( $blog_id ) ) { 
  22. return $blog_id; 
  23. if ( ! current_user_can_for_blog( $blog_id, 'list_users' ) ) { 
  24. return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 ); 
  25.  
  26. // Get the user by ID or login 
  27. $get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id'; 
  28. $user = get_user_by( $get_by, $user_id ); 
  29.  
  30. if ( ! $user ) { 
  31. return new WP_Error( 'unknown_user', 'Unknown user', 404 ); 
  32.  
  33. if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) { 
  34. return new WP_Error( 'unknown_user_for_site', 'Unknown user for site', 404 ); 
  35.  
  36. if ( 'GET' === $this->api->method ) { 
  37. return $this->get_user( $user->ID ); 
  38. } else if ( 'POST' === $this->api->method ) { 
  39. if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) { 
  40. return new WP_Error( 'unauthorized', 'User cannot promote users for specified site', 403 ); 
  41. if ( get_current_user_id() == $user_id ) { 
  42. return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 ); 
  43. return $this->update_user( $user_id ); 
  44. } else { 
  45. return new WP_Error( 'bad_request', 'An unsupported request method was used.' ); 
  46.  
  47. public function get_user( $user_id ) { 
  48. $the_user = $this->get_author( $user_id, true ); 
  49. if ( $the_user && ! is_wp_error( $the_user ) ) { 
  50. $userdata = get_userdata( $user_id ); 
  51. $the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array(); 
  52.  
  53. return $the_user; 
  54.  
  55. /** 
  56. * Updates user data 
  57. * @return (array) 
  58. */ 
  59. public function update_user( $user_id ) { 
  60. $input = $this->input(); 
  61. $user['ID'] = $user_id; 
  62. if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { 
  63. foreach ( $input as $key => $value ) { 
  64. if ( ! is_array( $value ) ) { 
  65. $value = trim( $value ); 
  66. $value = wp_unslash( $value ); 
  67. switch ( $key ) { 
  68. case 'first_name': 
  69. case 'last_name': 
  70. $user[ $key ] = $value; 
  71. break; 
  72. case 'display_name': 
  73. case 'name': 
  74. $user[ 'display_name' ] = $value; 
  75. break; 
  76. if ( isset( $input[ 'roles' ] ) ) { 
  77. if ( is_array( $input['roles'] ) ) { 
  78. $user['role'] = $input['roles'][0]; 
  79. } else { 
  80. $user['role'] = $input['roles']; 
  81. $result = wp_update_user( $user ); 
  82. if ( is_wp_error( $result ) ) { 
  83. return $result; 
  84. return $this->get_user( $user_id ); 
  85.