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