WPCOM_JSON_API_Update_User_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-update-user-endpoint.php  
  1. class WPCOM_JSON_API_Update_User_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. function callback( $path = '', $blog_id = 0, $user_id = 0 ) { 
  4. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  5. if ( is_wp_error( $blog_id ) ) { 
  6. return $blog_id; 
  7.  
  8. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  9. if ( wpcom_get_blog_owner( $blog_id ) == $user_id ) { 
  10. return new WP_Error( 'forbidden', 'A site owner can not be removed through this endpoint.', 403 ); 
  11.  
  12. if ( $this->api->ends_with( $path, '/delete' ) ) { 
  13. return $this->delete_or_remove_user( $user_id ); 
  14.  
  15. return false; 
  16.  
  17. /** 
  18. * Checks if a user exists by checking to see if a WP_User object exists for a user ID. 
  19. * @param int $user_id 
  20. * @return bool 
  21. */ 
  22. function user_exists( $user_id ) { 
  23. $user = get_user_by( 'id', $user_id ); 
  24.  
  25. return false != $user && is_a( $user, 'WP_User' ); 
  26.  
  27. /** 
  28. * Validates user input and then decides whether to remove or delete a user. 
  29. * @param int $user_id 
  30. * @return array|WP_Error 
  31. */ 
  32. function delete_or_remove_user( $user_id ) { 
  33. if ( 0 == $user_id ) { 
  34. return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 ); 
  35.  
  36. if ( get_current_user_id() == $user_id ) { 
  37. return new WP_Error( 'invalid_input', 'User can not remove or delete self through this endpoint.', 400 ); 
  38.  
  39. if ( ! $this->user_exists( $user_id ) ) { 
  40. return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 ); 
  41.  
  42. return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id ); 
  43.  
  44. /** 
  45. * Removes a user from the current site. 
  46. * @param int $user_id 
  47. * @return array|WP_Error 
  48. */ 
  49. function remove_user( $user_id ) { 
  50. if ( ! current_user_can( 'remove_users' ) ) { 
  51. return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 ); 
  52.  
  53. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { 
  54. return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 ); 
  55.  
  56. return array( 
  57. 'success' => remove_user_from_blog( $user_id, get_current_blog_id() ) 
  58. ); 
  59.  
  60. /** 
  61. * Deletes a user and optionally reassigns posts to another user. 
  62. * @param int $user_id 
  63. * @return array|WP_Error 
  64. */ 
  65. function delete_user( $user_id ) { 
  66. if ( ! current_user_can( 'delete_users' ) ) { 
  67. return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 ); 
  68.  
  69. $input = (array) $this->input(); 
  70.  
  71. if ( isset( $input['reassign'] ) ) { 
  72. if ( $user_id == $input['reassign'] ) { 
  73. return new WP_Error( 'invalid_input', 'Can not reassign posts to user being deleted.', 400 ); 
  74.  
  75. if ( ! $this->user_exists( $input['reassign'] ) ) { 
  76. return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 ); 
  77.  
  78. return array( 
  79. 'success' => wp_delete_user( $user_id, intval( $input['reassign'] ) ),  
  80. );