Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_JSON_API_Update_User_Endpoint | |
0.00% |
0 / 36 |
|
0.00% |
0 / 5 |
600 | |
0.00% |
0 / 1 |
| callback | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| user_exists | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| delete_or_remove_user | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| remove_user | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| delete_user | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
72 | |||
| 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 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit( 0 ); |
| 10 | } |
| 11 | |
| 12 | new WPCOM_JSON_API_Update_User_Endpoint( |
| 13 | array( |
| 14 | 'description' => 'Deletes or removes a user of a site.', |
| 15 | 'group' => 'users', |
| 16 | 'stat' => 'users:delete', |
| 17 | |
| 18 | 'method' => 'POST', |
| 19 | 'path' => '/sites/%s/users/%d/delete', |
| 20 | 'path_labels' => array( |
| 21 | '$site' => '(int|string) The site ID or domain.', |
| 22 | '$user_ID' => '(int) The user\'s ID', |
| 23 | ), |
| 24 | |
| 25 | 'request_format' => array( |
| 26 | 'reassign' => '(int) An optional id of a user to reassign posts to.', |
| 27 | ), |
| 28 | |
| 29 | 'response_format' => array( |
| 30 | 'success' => '(bool) Was the deletion of user successful?', |
| 31 | ), |
| 32 | |
| 33 | 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/users/1/delete', |
| 34 | 'example_request_data' => array( |
| 35 | 'headers' => array( |
| 36 | 'authorization' => 'Bearer YOUR_API_TOKEN', |
| 37 | ), |
| 38 | ), |
| 39 | |
| 40 | 'example_response' => ' |
| 41 | { |
| 42 | "success": true |
| 43 | }', |
| 44 | ) |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * Update site users API class. |
| 49 | * |
| 50 | * @phan-constructor-used-for-side-effects |
| 51 | */ |
| 52 | class WPCOM_JSON_API_Update_User_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 53 | /** |
| 54 | * Update site users API callback. |
| 55 | * |
| 56 | * @param string $path API path. |
| 57 | * @param int $blog_id Blog ID. |
| 58 | * @param int $user_id User ID. |
| 59 | */ |
| 60 | public function callback( $path = '', $blog_id = 0, $user_id = 0 ) { |
| 61 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 62 | if ( is_wp_error( $blog_id ) ) { |
| 63 | return $blog_id; |
| 64 | } |
| 65 | |
| 66 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 67 | if ( (int) wpcom_get_blog_owner( $blog_id ) === (int) $user_id ) { |
| 68 | return new WP_Error( 'forbidden', 'A site owner cannot be removed through this endpoint.', 403 ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ( $this->api->ends_with( $path, '/delete' ) ) { |
| 73 | return $this->delete_or_remove_user( $user_id ); |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Checks if a user exists by checking to see if a WP_User object exists for a user ID. |
| 81 | * |
| 82 | * @param int $user_id User ID. |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function user_exists( $user_id ) { |
| 86 | $user = get_user_by( 'id', $user_id ); |
| 87 | |
| 88 | return false !== $user && is_a( $user, 'WP_User' ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Validates user input and then decides whether to remove or delete a user. |
| 93 | * |
| 94 | * @param int $user_id User ID. |
| 95 | * @return array|WP_Error |
| 96 | */ |
| 97 | public function delete_or_remove_user( $user_id ) { |
| 98 | if ( 0 === (int) $user_id ) { |
| 99 | return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 ); |
| 100 | } |
| 101 | |
| 102 | if ( ! $this->user_exists( $user_id ) ) { |
| 103 | return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 ); |
| 104 | } |
| 105 | |
| 106 | return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Removes a user from the current site. |
| 111 | * |
| 112 | * @param int $user_id User ID. |
| 113 | * @return array|WP_Error |
| 114 | */ |
| 115 | public function remove_user( $user_id ) { |
| 116 | // Skip the check if the user is removing themselves. |
| 117 | if ( ! current_user_can( 'remove_users' ) && get_current_user_id() !== (int) $user_id ) { |
| 118 | return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 ); |
| 119 | } |
| 120 | |
| 121 | if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
| 122 | return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 ); |
| 123 | } |
| 124 | |
| 125 | return array( |
| 126 | 'success' => remove_user_from_blog( $user_id, get_current_blog_id() ), |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Deletes a user and optionally reassigns posts to another user. |
| 132 | * |
| 133 | * @param int $user_id User ID. |
| 134 | * @return array|WP_Error |
| 135 | */ |
| 136 | public function delete_user( $user_id ) { |
| 137 | // Skip the check if the user is deleting themselves. |
| 138 | if ( ! current_user_can( 'delete_users' ) && get_current_user_id() !== (int) $user_id ) { |
| 139 | return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 ); |
| 140 | } |
| 141 | |
| 142 | $input = (array) $this->input(); |
| 143 | $reassign = isset( $input['reassign'] ) ? (int) $input['reassign'] : null; |
| 144 | |
| 145 | if ( $reassign !== null ) { |
| 146 | if ( (int) $user_id === $reassign ) { |
| 147 | return new WP_Error( 'invalid_input', 'Cannot reassign posts to user being deleted.', 400 ); |
| 148 | } |
| 149 | |
| 150 | if ( ! $this->user_exists( $reassign ) ) { |
| 151 | return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | $success = $reassign !== null ? wp_delete_user( $user_id, $reassign ) : wp_delete_user( $user_id ); |
| 156 | |
| 157 | return array( |
| 158 | 'success' => $success, |
| 159 | ); |
| 160 | } |
| 161 | } |