Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
23 / 92
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Update_User_Endpoint
36.51% covered (danger)
36.51%
23 / 63
14.29% covered (danger)
14.29%
1 / 7
367.71
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 user_exists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get_subscription_domain_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 domain_subscriptions_for_site_owned_by_user
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 delete_or_remove_user
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
72
 remove_user
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 delete_user
87.50% covered (warning)
87.50%
21 / 24
0.00% covered (danger)
0.00%
0 / 1
14.38
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
8use Automattic\Jetpack\Connection\Manager;
9use Automattic\Jetpack\Status\Host;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15new WPCOM_JSON_API_Update_User_Endpoint(
16    array(
17        'description'          => 'Deletes or removes a user of a site.',
18        'group'                => 'users',
19        'stat'                 => 'users:delete',
20
21        'method'               => 'POST',
22        'path'                 => '/sites/%s/users/%d/delete',
23        'path_labels'          => array(
24            '$site'    => '(int|string) The site ID or domain.',
25            '$user_ID' => '(int) The user\'s ID',
26        ),
27
28        'request_format'       => array(
29            'reassign' => '(int) An optional id of a user to reassign posts to.',
30        ),
31
32        'response_format'      => array(
33            'success' => '(bool) Was the deletion of user successful?',
34        ),
35
36        'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/users/1/delete',
37        'example_request_data' => array(
38            'headers' => array(
39                'authorization' => 'Bearer YOUR_API_TOKEN',
40            ),
41        ),
42
43        'example_response'     => '
44    {
45        "success": true
46    }',
47    )
48);
49
50/**
51 * Update site users API class.
52 *
53 * @phan-constructor-used-for-side-effects
54 */
55class WPCOM_JSON_API_Update_User_Endpoint extends WPCOM_JSON_API_Endpoint {
56    /**
57     * Update site users API callback.
58     *
59     * @param string $path API path.
60     * @param int    $blog_id Blog ID.
61     * @param int    $user_id User ID.
62     */
63    public function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
64        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
65        if ( is_wp_error( $blog_id ) ) {
66            return $blog_id;
67        }
68
69        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
70            if ( (int) wpcom_get_blog_owner( $blog_id ) === (int) $user_id ) {
71                return new WP_Error( 'forbidden', 'A site owner cannot be removed through this endpoint.', 403 );
72            }
73        }
74
75        if ( $this->api->ends_with( $path, '/delete' ) ) {
76            return $this->delete_or_remove_user( $user_id );
77        }
78
79        return false;
80    }
81
82    /**
83     * Checks if a user exists by checking to see if a WP_User object exists for a user ID.
84     *
85     * @param  int $user_id User ID.
86     * @return bool
87     */
88    public function user_exists( $user_id ) {
89        $user = get_user_by( 'id', $user_id );
90
91        return false !== $user && $user instanceof \WP_User;
92    }
93
94    /**
95     * Return the domain name of a subscription.
96     *
97     * @param  Store_Subscription $subscription Subscription object.
98     * @return string
99     */
100    protected function get_subscription_domain_name( $subscription ) {
101        return $subscription->meta;
102    }
103
104    /**
105     * Get a list of the domains owned by the given user.
106     *
107     * @param  int $user_id User ID.
108     * @return array
109     */
110    protected function domain_subscriptions_for_site_owned_by_user( $user_id ) {
111        $subscriptions = WPCOM_Store::get_subscriptions( get_current_blog_id(), $user_id, domains::get_domain_products() );
112
113        $domains = array_unique( array_map( array( $this, 'get_subscription_domain_name' ), $subscriptions ) );
114
115        return array_values( $domains );
116    }
117
118    /**
119     * Validates user input and then decides whether to remove or delete a user.
120     *
121     * @param  int $user_id User ID.
122     * @return array|WP_Error
123     */
124    public function delete_or_remove_user( $user_id ) {
125        if ( 0 === (int) $user_id ) {
126            return new WP_Error( 'invalid_input', 'A valid user ID must be specified.', 400 );
127        }
128
129        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
130            $domains = $this->domain_subscriptions_for_site_owned_by_user( $user_id );
131            if ( ! empty( $domains ) ) {
132                $error = new WP_Error( 'user_owns_domain_subscription', implode( ', ', $domains ) );
133                $error->add_data( $domains, 'additional_data' );
134                return $error;
135            }
136
137            $active_user_subscriptions = WPCOM_Store::get_user_subscriptions( $user_id, get_current_blog_id() );
138            if ( ! empty( $active_user_subscriptions ) ) {
139                $product_names = array_values( wp_list_pluck( $active_user_subscriptions, 'product_name' ) );
140                $error         = new WP_Error( 'user_has_active_subscriptions', 'User has active subscriptions' );
141                $error->add_data( $product_names, 'additional_data' );
142                return $error;
143            }
144        }
145
146        if ( ! $this->user_exists( $user_id ) ) {
147            return new WP_Error( 'invalid_input', 'A user does not exist with that ID.', 400 );
148        }
149
150        return is_multisite() ? $this->remove_user( $user_id ) : $this->delete_user( $user_id );
151    }
152
153    /**
154     * Removes a user from the current site.
155     *
156     * @param  int $user_id User ID.
157     * @return array|WP_Error
158     */
159    public function remove_user( $user_id ) {
160        // Skip the check if the user is removing themselves.
161        if ( ! current_user_can( 'remove_users' ) && get_current_user_id() !== (int) $user_id ) {
162            return new WP_Error( 'unauthorized', 'User cannot remove users for specified site.', 403 );
163        }
164
165        if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
166            return new WP_Error( 'invalid_input', 'User is not a member of the specified site.', 400 );
167        }
168
169        return array(
170            'success' => remove_user_from_blog( $user_id, get_current_blog_id() ),
171        );
172    }
173
174    /**
175     * Deletes a user and optionally reassigns posts to another user.
176     *
177     * @param  int $user_id User ID.
178     * @return array|WP_Error
179     */
180    public function delete_user( $user_id ) {
181        // Skip the check if the user is deleting themselves.
182        $is_self_removal = get_current_user_id() === (int) $user_id;
183        if ( ! $is_self_removal && ! current_user_can( 'delete_users' ) ) {
184            return new WP_Error( 'unauthorized', 'User cannot delete users for specified site.', 403 );
185        }
186
187        $input    = (array) $this->input();
188        $reassign = isset( $input['reassign'] ) ? (int) $input['reassign'] : null;
189
190        if ( $is_self_removal && ( new Host() )->is_woa_site() ) {
191            $connection_owner_id = (int) ( new Manager() )->get_connection_owner_id();
192            if ( $connection_owner_id === (int) $user_id ) {
193                return new WP_Error( 'forbidden', 'A site owner cannot be removed through this endpoint.', 403 );
194            }
195
196            if ( $connection_owner_id <= 0 || $reassign !== $connection_owner_id || ! user_can( $reassign, 'manage_options' ) ) {
197                return new WP_Error(
198                    'site_owner_unavailable',
199                    __( 'We couldn’t verify the site owner to reassign your content. Ask a site administrator for help before leaving.', 'jetpack' ),
200                    409
201                );
202            }
203        }
204
205        if ( $reassign !== null ) {
206            if ( (int) $user_id === $reassign ) {
207                return new WP_Error( 'invalid_input', 'Cannot reassign posts to user being deleted.', 400 );
208            }
209
210            if ( ! $this->user_exists( $reassign ) ) {
211                return new WP_Error( 'invalid_input', 'User specified in reassign argument is not a member of the specified site.', 400 );
212            }
213        }
214
215        $success = $reassign !== null ? wp_delete_user( $user_id, $reassign ) : wp_delete_user( $user_id );
216
217        return array(
218            'success' => $success,
219        );
220    }
221}