説明なし

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * Return the domain name of a subscription
  56. *
  57. * @param Store_Subscription $subscription
  58. * @return string
  59. */
  60. protected function get_subscription_domain_name( $subscription ) {
  61. return $subscription->meta;
  62. }
  63. /**
  64. * Get a list of the domains owned by the given user.
  65. *
  66. * @param int $user_id
  67. * @return array
  68. */
  69. protected function domain_subscriptions_for_site_owned_by_user( $user_id ) {
  70. $subscriptions = WPCOM_Store::get_subscriptions( get_current_blog_id(), $user_id, domains::get_domain_products() );
  71. $domains = array_unique( array_map( array( $this, 'get_subscription_domain_name' ), $subscriptions ) );
  72. return array_values( $domains );
  73. }
  74. /**
  75. * Validates user input and then decides whether to remove or delete a user.
  76. * @param int $user_id
  77. * @return array|WP_Error
  78. */
  79. function delete_or_remove_user( $user_id ) {
  80. if ( 0 == $user_id ) {
  81. return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 );
  82. }
  83. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  84. $domains = $this->domain_subscriptions_for_site_owned_by_user( $user_id );
  85. if ( ! empty( $domains ) ) {
  86. $error = new WP_Error( 'user_owns_domain_subscription', join( ', ', $domains ) );
  87. $error->add_data( $domains, 'additional_data' );
  88. return $error;
  89. }
  90. $active_user_subscriptions = WPCOM_Store::get_user_subscriptions( $user_id, get_current_blog_id() );
  91. if ( ! empty( $active_user_subscriptions ) ) {
  92. $product_names = array_values( wp_list_pluck( $active_user_subscriptions, 'product_name' ) );
  93. $error = new WP_Error( 'user_has_active_subscriptions', 'User has active subscriptions' );
  94. $error->add_data( $product_names, 'additional_data' );
  95. return $error;
  96. }
  97. }
  98. if ( get_current_user_id() == $user_id ) {
  99. return new WP_Error( 'invalid_input', 'User can not remove or delete self through this endpoint.', 400 );
  100. }
  101. if ( ! $this->user_exists( $user_id ) ) {
  102. return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 );
  103. }
  104. return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id );
  105. }
  106. /**
  107. * Removes a user from the current site.
  108. * @param int $user_id
  109. * @return array|WP_Error
  110. */
  111. function remove_user( $user_id ) {
  112. if ( ! current_user_can( 'remove_users' ) ) {
  113. return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 );
  114. }
  115. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
  116. return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 );
  117. }
  118. return array(
  119. 'success' => remove_user_from_blog( $user_id, get_current_blog_id() )
  120. );
  121. }
  122. /**
  123. * Deletes a user and optionally reassigns posts to another user.
  124. * @param int $user_id
  125. * @return array|WP_Error
  126. */
  127. function delete_user( $user_id ) {
  128. if ( ! current_user_can( 'delete_users' ) ) {
  129. return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 );
  130. }
  131. $input = (array) $this->input();
  132. if ( isset( $input['reassign'] ) ) {
  133. if ( $user_id == $input['reassign'] ) {
  134. return new WP_Error( 'invalid_input', 'Can not reassign posts to user being deleted.', 400 );
  135. }
  136. if ( ! $this->user_exists( $input['reassign'] ) ) {
  137. return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 );
  138. }
  139. }
  140. return array(
  141. 'success' => wp_delete_user( $user_id, (int) $input['reassign'] ),
  142. );
  143. }
  144. }