Completed
Push — update/debugger ( 23f436...30b03e )
by
unknown
11:32 queued 01:29
created

WPCOM_JSON_API_Site_User_Endpoint   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 112
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D callback() 0 32 9
A get_user() 0 9 4
D update_user() 0 44 17
1
<?php
2
3
class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
4
5
	public static $user_format = array(
6
		'ID'           => '(int) The ID of the user',
7
		'login'        => '(string) The login username of the user',
8
		'email'        => '(string) The email of the user',
9
		'name'         => '(string) The name to display for the user',
10
		'first_name'   => '(string) The first name of the user',
11
		'last_name'    => '(string) The last name of the user',
12
		'nice_name'    => '(string) The nice_name to display for the user',
13
		'URL'          => '(string) The primary blog of the user',
14
		'avatar_URL'   => '(url) Gravatar image URL',
15
		'profile_URL'  => '(url) Gravatar Profile URL',
16
		'site_ID'      => '(int) ID of the user\'s primary blog',
17
		'roles'        => '(array) The roles of the user',
18
	);
19
20
	// /sites/%s/users/%d -> $blog_id, $user_id
21
	function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
22
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
23
		if ( is_wp_error( $blog_id ) ) {
24
			return $blog_id;
25
		}
26
		if ( ! current_user_can_for_blog( $blog_id, 'list_users' ) ) {
27
			return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 );
28
		}
29
30
		// Get the user by ID or login
31
		$get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id';
32
		$user = get_user_by( $get_by, $user_id );
33
34
		if ( ! $user ) {
35
			return new WP_Error( 'unknown_user', 'Unknown user', 404 );
36
		}
37
38
		if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) {
39
			return new WP_Error( 'unknown_user_for_site', 'Unknown user for site', 404 );
40
		}
41
42
		if ( 'GET' === $this->api->method ) {
43
			return $this->get_user( $user->ID );
44
		} else if ( 'POST' === $this->api->method ) {
45
			if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) {
46
				return new WP_Error( 'unauthorized_no_promote_cap', 'User cannot promote users for specified site', 403 );
47
			}
48
			return $this->update_user( $user_id, $blog_id );
49
		} else {
50
			return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
51
		}
52
	}
53
54
	public function get_user( $user_id ) {
55
		$the_user = $this->get_author( $user_id, true );
56
		if ( $the_user && ! is_wp_error( $the_user ) ) {
57
			$userdata = get_userdata( $user_id );
58
			$the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array();
59
		}
60
61
		return $the_user;
62
	}
63
64
	/**
65
	 * Updates user data
66
	 *
67
	 * @return (array)
68
	 */
69
	public function update_user( $user_id, $blog_id ) {
70
		$input = $this->input();
71
		$user['ID'] = $user_id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$user was never initialized. Although not strictly required by PHP, it is generally a good practice to add $user = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
72
		$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
73
74
		if ( get_current_user_id() == $user_id && isset( $input['roles'] ) ) {
75
			return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 );
76
		}
77
78
		if ( $is_wpcom && $user_id !== get_current_user_id() && $user_id == wpcom_get_blog_owner( $blog_id ) ) {
79
			return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 );
80
		}
81
82
		if ( ! $is_wpcom ) {
83
			foreach ( $input as $key => $value ) {
84
				if ( ! is_array( $value ) ) {
85
					$value = trim( $value );
86
				}
87
				$value = wp_unslash( $value );
88
				switch ( $key ) {
89
					case 'first_name':
90
					case 'last_name':
91
						$user[ $key ] = $value;
92
						break;
93
					case 'display_name':
94
					case 'name':
95
						$user[ 'display_name' ] = $value;
96
						break;
97
				}
98
			}
99
		}
100
		if ( isset( $input[ 'roles' ] ) ) {
101
			if ( is_array( $input['roles'] ) ) {
102
				$user['role'] = $input['roles'][0];
103
			} else {
104
				$user['role'] = $input['roles'];
105
			}
106
		}
107
		$result = wp_update_user( $user );
108
		if ( is_wp_error( $result ) ) {
109
			return $result;
110
		}
111
		return $this->get_user( $user_id );
112
	}
113
114
}
115