class.wpcom-json-api-update-user-endpoint.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. new WPCOM_JSON_API_Update_User_Endpoint( array(
  3. 'description' => 'Deletes or removes a user of a site.',
  4. 'group' => 'users',
  5. 'stat' => 'users:delete',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/users/%d/delete',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) The site ID or domain.',
  10. '$user_ID' => '(int) The user\'s ID'
  11. ),
  12. 'request_format' => array(
  13. 'reassign' => '(int) An optional id of a user to reassign posts to.',
  14. ),
  15. 'response_format' => array(
  16. 'success' => '(bool) Was the deletion of user successful?',
  17. ),
  18. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/users/1/delete',
  19. 'example_request_data' => array(
  20. 'headers' => array(
  21. 'authorization' => 'Bearer YOUR_API_TOKEN'
  22. ),
  23. ),
  24. 'example_response' => '
  25. {
  26. "success": true
  27. }'
  28. ) );
  29. class WPCOM_JSON_API_Update_User_Endpoint extends WPCOM_JSON_API_Endpoint {
  30. function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
  31. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  32. if ( is_wp_error( $blog_id ) ) {
  33. return $blog_id;
  34. }
  35. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  36. if ( wpcom_get_blog_owner( $blog_id ) == $user_id ) {
  37. return new WP_Error( 'forbidden', 'A site owner can not be removed through this endpoint.', 403 );
  38. }
  39. }
  40. if ( $this->api->ends_with( $path, '/delete' ) ) {
  41. return $this->delete_or_remove_user( $user_id );
  42. }
  43. return false;
  44. }
  45. /**
  46. * Checks if a user exists by checking to see if a WP_User object exists for a user ID.
  47. * @param int $user_id
  48. * @return bool
  49. */
  50. function user_exists( $user_id ) {
  51. $user = get_user_by( 'id', $user_id );
  52. return false != $user && is_a( $user, 'WP_User' );
  53. }
  54. /**
  55. * Validates user input and then decides whether to remove or delete a user.
  56. * @param int $user_id
  57. * @return array|WP_Error
  58. */
  59. function delete_or_remove_user( $user_id ) {
  60. if ( 0 == $user_id ) {
  61. return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 );
  62. }
  63. if ( get_current_user_id() == $user_id ) {
  64. return new WP_Error( 'invalid_input', 'User can not remove or delete self through this endpoint.', 400 );
  65. }
  66. if ( ! $this->user_exists( $user_id ) ) {
  67. return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 );
  68. }
  69. return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id );
  70. }
  71. /**
  72. * Removes a user from the current site.
  73. * @param int $user_id
  74. * @return array|WP_Error
  75. */
  76. function remove_user( $user_id ) {
  77. if ( ! current_user_can( 'remove_users' ) ) {
  78. return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 );
  79. }
  80. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
  81. return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 );
  82. }
  83. return array(
  84. 'success' => remove_user_from_blog( $user_id, get_current_blog_id() )
  85. );
  86. }
  87. /**
  88. * Deletes a user and optionally reassigns posts to another user.
  89. * @param int $user_id
  90. * @return array|WP_Error
  91. */
  92. function delete_user( $user_id ) {
  93. if ( ! current_user_can( 'delete_users' ) ) {
  94. return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 );
  95. }
  96. $input = (array) $this->input();
  97. if ( isset( $input['reassign'] ) ) {
  98. if ( $user_id == $input['reassign'] ) {
  99. return new WP_Error( 'invalid_input', 'Can not reassign posts to user being deleted.', 400 );
  100. }
  101. if ( ! $this->user_exists( $input['reassign'] ) ) {
  102. return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 );
  103. }
  104. }
  105. return array(
  106. 'success' => wp_delete_user( $user_id, intval( $input['reassign'] ) ),
  107. );
  108. }
  109. }