jetpack
2 years ago
class.wpcom-json-api-add-widget-endpoint.php
2 years ago
class.wpcom-json-api-autosave-post-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-bulk-delete-post-endpoint.php
4 years ago
class.wpcom-json-api-bulk-restore-post-endpoint.php
2 years ago
class.wpcom-json-api-bulk-update-comments-endpoint.php
3 years ago
class.wpcom-json-api-comment-endpoint.php
2 years ago
class.wpcom-json-api-delete-media-endpoint.php
4 years ago
class.wpcom-json-api-delete-media-v1-1-endpoint.php
4 years ago
class.wpcom-json-api-edit-media-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-get-autosave-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-get-comment-counts-endpoint.php
4 years ago
class.wpcom-json-api-get-comment-endpoint.php
4 years ago
class.wpcom-json-api-get-comment-history-endpoint.php
4 years ago
class.wpcom-json-api-get-comments-tree-endpoint.php
4 years ago
class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php
4 years ago
class.wpcom-json-api-get-comments-tree-v1-2-endpoint.php
4 years ago
class.wpcom-json-api-get-customcss.php
2 years ago
class.wpcom-json-api-get-media-endpoint.php
4 years ago
class.wpcom-json-api-get-media-v1-1-endpoint.php
4 years ago
class.wpcom-json-api-get-media-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-get-post-counts-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-get-post-endpoint.php
2 years ago
class.wpcom-json-api-get-post-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-get-site-endpoint.php
2 years ago
class.wpcom-json-api-get-site-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-get-taxonomies-endpoint.php
2 years ago
class.wpcom-json-api-get-taxonomy-endpoint.php
4 years ago
class.wpcom-json-api-get-term-endpoint.php
4 years ago
class.wpcom-json-api-list-comments-endpoint.php
2 years ago
class.wpcom-json-api-list-dropdown-pages-endpoint.php
3 years ago
class.wpcom-json-api-list-embeds-endpoint.php
4 years ago
class.wpcom-json-api-list-media-endpoint.php
2 years ago
class.wpcom-json-api-list-media-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-list-media-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-list-post-type-taxonomies-endpoint.php
4 years ago
class.wpcom-json-api-list-post-types-endpoint.php
3 years ago
class.wpcom-json-api-list-posts-endpoint.php
3 years ago
class.wpcom-json-api-list-posts-v1-1-endpoint.php
3 years ago
class.wpcom-json-api-list-posts-v1-2-endpoint.php
3 years ago
class.wpcom-json-api-list-roles-endpoint.php
2 years ago
class.wpcom-json-api-list-shortcodes-endpoint.php
4 years ago
class.wpcom-json-api-list-terms-endpoint.php
2 years ago
class.wpcom-json-api-list-users-endpoint.php
2 years ago
class.wpcom-json-api-menus-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-post-endpoint.php
3 years ago
class.wpcom-json-api-post-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-render-embed-endpoint.php
2 years ago
class.wpcom-json-api-render-embed-reversal-endpoint.php
2 years ago
class.wpcom-json-api-render-endpoint.php
3 years ago
class.wpcom-json-api-render-shortcode-endpoint.php
3 years ago
class.wpcom-json-api-sharing-buttons-endpoint.php
2 years ago
class.wpcom-json-api-site-settings-endpoint.php
2 years ago
class.wpcom-json-api-site-settings-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-site-settings-v1-3-endpoint.php
2 years ago
class.wpcom-json-api-site-settings-v1-4-endpoint.php
2 years ago
class.wpcom-json-api-site-user-endpoint.php
2 years ago
class.wpcom-json-api-taxonomy-endpoint.php
4 years ago
class.wpcom-json-api-update-comment-endpoint.php
2 years ago
class.wpcom-json-api-update-customcss.php
2 years ago
class.wpcom-json-api-update-media-endpoint.php
4 years ago
class.wpcom-json-api-update-media-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-update-post-endpoint.php
3 years ago
class.wpcom-json-api-update-post-v1-1-endpoint.php
3 years ago
class.wpcom-json-api-update-post-v1-2-endpoint.php
2 years ago
class.wpcom-json-api-update-site-homepage-endpoint.php
4 years ago
class.wpcom-json-api-update-site-logo-endpoint.php
2 years ago
class.wpcom-json-api-update-taxonomy-endpoint.php
4 years ago
class.wpcom-json-api-update-term-endpoint.php
4 years ago
class.wpcom-json-api-update-user-endpoint.php
3 years ago
class.wpcom-json-api-upload-media-endpoint.php
3 years ago
class.wpcom-json-api-upload-media-v1-1-endpoint.php
2 years ago
class.wpcom-json-api-update-user-endpoint.php
196 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Update site users API endpoint. |
| 4 | * |
| 5 | * Endpoint: /sites/%s/users/%d/delete |
| 6 | */ |
| 7 | |
| 8 | new WPCOM_JSON_API_Update_User_Endpoint( |
| 9 | array( |
| 10 | 'description' => 'Deletes or removes a user of a site.', |
| 11 | 'group' => 'users', |
| 12 | 'stat' => 'users:delete', |
| 13 | |
| 14 | 'method' => 'POST', |
| 15 | 'path' => '/sites/%s/users/%d/delete', |
| 16 | 'path_labels' => array( |
| 17 | '$site' => '(int|string) The site ID or domain.', |
| 18 | '$user_ID' => '(int) The user\'s ID', |
| 19 | ), |
| 20 | |
| 21 | 'request_format' => array( |
| 22 | 'reassign' => '(int) An optional id of a user to reassign posts to.', |
| 23 | ), |
| 24 | |
| 25 | 'response_format' => array( |
| 26 | 'success' => '(bool) Was the deletion of user successful?', |
| 27 | ), |
| 28 | |
| 29 | 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/users/1/delete', |
| 30 | 'example_request_data' => array( |
| 31 | 'headers' => array( |
| 32 | 'authorization' => 'Bearer YOUR_API_TOKEN', |
| 33 | ), |
| 34 | ), |
| 35 | |
| 36 | 'example_response' => ' |
| 37 | { |
| 38 | "success": true |
| 39 | }', |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Update site users API class. |
| 45 | */ |
| 46 | class WPCOM_JSON_API_Update_User_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 47 | /** |
| 48 | * Update site users API callback. |
| 49 | * |
| 50 | * @param string $path API path. |
| 51 | * @param int $blog_id Blog ID. |
| 52 | * @param int $user_id User ID. |
| 53 | */ |
| 54 | public function callback( $path = '', $blog_id = 0, $user_id = 0 ) { |
| 55 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 56 | if ( is_wp_error( $blog_id ) ) { |
| 57 | return $blog_id; |
| 58 | } |
| 59 | |
| 60 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 61 | if ( (int) wpcom_get_blog_owner( $blog_id ) === (int) $user_id ) { |
| 62 | return new WP_Error( 'forbidden', 'A site owner can not be removed through this endpoint.', 403 ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ( $this->api->ends_with( $path, '/delete' ) ) { |
| 67 | return $this->delete_or_remove_user( $user_id ); |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Checks if a user exists by checking to see if a WP_User object exists for a user ID. |
| 75 | * |
| 76 | * @param int $user_id User ID. |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function user_exists( $user_id ) { |
| 80 | $user = get_user_by( 'id', $user_id ); |
| 81 | |
| 82 | return false !== $user && is_a( $user, 'WP_User' ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the domain name of a subscription. |
| 87 | * |
| 88 | * @param Store_Subscription $subscription Subscription object. |
| 89 | * @return string |
| 90 | */ |
| 91 | protected function get_subscription_domain_name( $subscription ) { |
| 92 | return $subscription->meta; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get a list of the domains owned by the given user. |
| 97 | * |
| 98 | * @param int $user_id User ID. |
| 99 | * @return array |
| 100 | */ |
| 101 | protected function domain_subscriptions_for_site_owned_by_user( $user_id ) { |
| 102 | $subscriptions = WPCOM_Store::get_subscriptions( get_current_blog_id(), $user_id, domains::get_domain_products() ); |
| 103 | |
| 104 | $domains = array_unique( array_map( array( $this, 'get_subscription_domain_name' ), $subscriptions ) ); |
| 105 | |
| 106 | return array_values( $domains ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Validates user input and then decides whether to remove or delete a user. |
| 111 | * |
| 112 | * @param int $user_id User ID. |
| 113 | * @return array|WP_Error |
| 114 | */ |
| 115 | public function delete_or_remove_user( $user_id ) { |
| 116 | if ( 0 === (int) $user_id ) { |
| 117 | return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 ); |
| 118 | } |
| 119 | |
| 120 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 121 | $domains = $this->domain_subscriptions_for_site_owned_by_user( $user_id ); |
| 122 | if ( ! empty( $domains ) ) { |
| 123 | $error = new WP_Error( 'user_owns_domain_subscription', implode( ', ', $domains ) ); |
| 124 | $error->add_data( $domains, 'additional_data' ); |
| 125 | return $error; |
| 126 | } |
| 127 | |
| 128 | $active_user_subscriptions = WPCOM_Store::get_user_subscriptions( $user_id, get_current_blog_id() ); |
| 129 | if ( ! empty( $active_user_subscriptions ) ) { |
| 130 | $product_names = array_values( wp_list_pluck( $active_user_subscriptions, 'product_name' ) ); |
| 131 | $error = new WP_Error( 'user_has_active_subscriptions', 'User has active subscriptions' ); |
| 132 | $error->add_data( $product_names, 'additional_data' ); |
| 133 | return $error; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if ( get_current_user_id() === (int) $user_id ) { |
| 138 | return new WP_Error( 'invalid_input', 'User can not remove or delete self through this endpoint.', 400 ); |
| 139 | } |
| 140 | |
| 141 | if ( ! $this->user_exists( $user_id ) ) { |
| 142 | return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 ); |
| 143 | } |
| 144 | |
| 145 | return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Removes a user from the current site. |
| 150 | * |
| 151 | * @param int $user_id User ID. |
| 152 | * @return array|WP_Error |
| 153 | */ |
| 154 | public function remove_user( $user_id ) { |
| 155 | if ( ! current_user_can( 'remove_users' ) ) { |
| 156 | return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 ); |
| 157 | } |
| 158 | |
| 159 | if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
| 160 | return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 ); |
| 161 | } |
| 162 | |
| 163 | return array( |
| 164 | 'success' => remove_user_from_blog( $user_id, get_current_blog_id() ), |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Deletes a user and optionally reassigns posts to another user. |
| 170 | * |
| 171 | * @param int $user_id User ID. |
| 172 | * @return array|WP_Error |
| 173 | */ |
| 174 | public function delete_user( $user_id ) { |
| 175 | if ( ! current_user_can( 'delete_users' ) ) { |
| 176 | return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 ); |
| 177 | } |
| 178 | |
| 179 | $input = (array) $this->input(); |
| 180 | |
| 181 | if ( isset( $input['reassign'] ) ) { |
| 182 | if ( (int) $user_id === (int) $input['reassign'] ) { |
| 183 | return new WP_Error( 'invalid_input', 'Can not reassign posts to user being deleted.', 400 ); |
| 184 | } |
| 185 | |
| 186 | if ( ! $this->user_exists( $input['reassign'] ) ) { |
| 187 | return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return array( |
| 192 | 'success' => wp_delete_user( $user_id, (int) $input['reassign'] ), |
| 193 | ); |
| 194 | } |
| 195 | } |
| 196 |