summaryrefslogtreecommitdiff
blob: ef5cb2d8e5357d85cee7b27a448f3c8e800b453c (plain)
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
<?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' );
	}

	/**
	 * 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 ( 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'] ) ),
		);
	}
}