Completed
Push — add/changelog-50 ( 40a63e...720d2c )
by Jeremy
108:21 queued 97:51
created

WPCOM_JSON_API_Site_User_Endpoint::update_user()   C

Complexity

Conditions 19
Paths 40

Size

Total Lines 54
Code Lines 36

Duplication

Lines 3
Ratio 5.56 %

Importance

Changes 0
Metric Value
cc 19
eloc 36
nc 40
nop 2
dl 3
loc 54
rs 6.6157
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
3
4
	public static $user_format = array(
5
		'ID'           => '(int) The ID of the user',
6
		'login'        => '(string) The login username of the user',
7
		'email'        => '(string) The email of the user',
8
		'name'         => '(string) The name to display for the user',
9
		'first_name'   => '(string) The first name of the user',
10
		'last_name'    => '(string) The last name of the user',
11
		'nice_name'    => '(string) The nice_name to display for the user',
12
		'URL'          => '(string) The primary blog of the user',
13
		'avatar_URL'   => '(url) Gravatar image URL',
14
		'profile_URL'  => '(url) Gravatar Profile URL',
15
		'site_ID'      => '(int) ID of the user\'s primary blog',
16
		'roles'        => '(array|string) The role or roles of the user',
17
	);
18
19
	// /sites/%s/users/%d -> $blog_id, $user_id
20
	function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
21
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
22
		if ( is_wp_error( $blog_id ) ) {
23
			return $blog_id;
24
		}
25
		if ( ! current_user_can_for_blog( $blog_id, 'list_users' ) ) {
26
			return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 );
27
		}
28
29
		// Get the user by ID or login
30
		$get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id';
31
		$user = get_user_by( $get_by, $user_id );
32
33
		if ( ! $user ) {
34
			return new WP_Error( 'unknown_user', 'Unknown user', 404 );
35
		}
36
37
		if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) {
38
			return new WP_Error( 'unknown_user_for_site', 'Unknown user for site', 404 );
39
		}
40
41
		if ( 'GET' === $this->api->method ) {
42
			return $this->get_user( $user->ID );
43
		} else if ( 'POST' === $this->api->method ) {
44
			if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) {
45
				return new WP_Error( 'unauthorized_no_promote_cap', 'User cannot promote users for specified site', 403 );
46
			}
47
			return $this->update_user( $user_id, $blog_id );
48
		} else {
49
			return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
50
		}
51
	}
52
53
	public function get_user( $user_id ) {
54
		$the_user = $this->get_author( $user_id, true );
55 View Code Duplication
		if ( $the_user && ! is_wp_error( $the_user ) ) {
56
			$userdata = get_userdata( $user_id );
57
			$the_user->roles = ! is_wp_error( $userdata ) ? array_values( $userdata->roles ) : array();
58
		}
59
60
		return $the_user;
61
	}
62
63
	/**
64
	 * Updates user data
65
	 *
66
	 * @return (array)
67
	 */
68
	public function update_user( $user_id, $blog_id ) {
69
		$input = $this->input();
70
		$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...
71
		$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
72
73
		if ( get_current_user_id() == $user_id && isset( $input['roles'] ) ) {
74
			return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 );
75
		}
76
77
		if ( $is_wpcom && $user_id !== get_current_user_id() && $user_id == wpcom_get_blog_owner( $blog_id ) ) {
78
			return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 );
79
		}
80
81
		if ( ! $is_wpcom ) {
82
			foreach ( $input as $key => $value ) {
83
				if ( ! is_array( $value ) ) {
84
					$value = trim( $value );
85
				}
86
				$value = wp_unslash( $value );
87
				switch ( $key ) {
88
					case 'first_name':
89
					case 'last_name':
90
						$user[ $key ] = $value;
91
						break;
92
					case 'display_name':
93
					case 'name':
94
						$user[ 'display_name' ] = $value;
95
						break;
96
				}
97
			}
98
		}
99
100
		if ( isset( $input[ 'roles' ] ) ) {
101
			// For now, we only use the first role in the array.
102
			if ( is_array( $input['roles'] ) ) {
103
				$user['role'] = $input['roles'][0];
104
			} else if ( is_string( $input['roles'] ) ) {
105
				$user['role'] = $input['roles'];
106
			} else {
107
				return new WP_Error( 'invalid_input', __( 'The roles property must be a string or an array.', 'jetpack' ), 400 );
108
			}
109
110
			$editable_roles = array_keys( get_editable_roles() );
111 View Code Duplication
			if ( ! in_array( $user['role'], $editable_roles ) ) {
112
				return new WP_Error( 'invalid_input', sprintf( __( '%s is not a valid role.', 'jetpack' ), $editable_roles ), 400 );
113
			}
114
		}
115
116
		$result = wp_update_user( $user );
117
		if ( is_wp_error( $result ) ) {
118
			return $result;
119
		}
120
		return $this->get_user( $user_id );
121
	}
122
123
}
124