Completed
Push — branch-7.5 ( 9378b5...5f3718 )
by Jeremy
211:39 queued 203:47
created

Users::maybe_demote_master_user()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 1
dl 0
loc 27
rs 8.5546
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync;
4
5
/**
6
 * Class Users
7
 *
8
 * Responsible for syncing user data changes.
9
 */
10
class Users {
11
	static $user_roles = array();
12
13
	static function init() {
14
		if ( \Jetpack::is_active() ) {
15
			// Kick off synchronization of user role when it changes
16
			add_action( 'set_user_role', array( __CLASS__, 'user_role_change' ) );
17
		}
18
	}
19
20
	/**
21
	 * Synchronize connected user role changes
22
	 */
23
	static function user_role_change( $user_id ) {
24
		if ( \Jetpack::is_user_connected( $user_id ) ) {
25
			self::update_role_on_com( $user_id );
26
			// try to choose a new master if we're demoting the current one
27
			self::maybe_demote_master_user( $user_id );
28
		}
29
	}
30
31
	static function get_role( $user_id ) {
32
		if ( isset( self::$user_roles[ $user_id ] ) ) {
33
			return self::$user_roles[ $user_id ];
34
		}
35
36
		$current_user_id = get_current_user_id();
37
		wp_set_current_user( $user_id );
38
		$role = \Jetpack::translate_current_user_to_role();
39
		wp_set_current_user( $current_user_id );
40
		$user_roles[ $user_id ] = $role;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$user_roles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $user_roles = 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...
41
42
		return $role;
43
	}
44
45
	static function get_signed_role( $user_id ) {
46
		return \Jetpack::sign_role( self::get_role( $user_id ), $user_id );
47
	}
48
49
	static function update_role_on_com( $user_id ) {
50
		$signed_role = self::get_signed_role( $user_id );
51
		\Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
52
	}
53
54
	static function maybe_demote_master_user( $user_id ) {
55
		$master_user_id = \Jetpack_Options::get_option( 'master_user' );
56
		$role           = self::get_role( $user_id );
57
		if ( $user_id == $master_user_id && 'administrator' != $role ) {
58
			$query      = new \WP_User_Query(
59
				array(
60
					'fields'  => array( 'id' ),
61
					'role'    => 'administrator',
62
					'orderby' => 'id',
63
					'exclude' => array( $master_user_id ),
64
				)
65
			);
66
			$new_master = false;
67
			foreach ( $query->results as $result ) {
68
				$found_user_id = absint( $result->id );
69
				if ( $found_user_id && \Jetpack::is_user_connected( $found_user_id ) ) {
70
					$new_master = $found_user_id;
71
					break;
72
				}
73
			}
74
75
			if ( $new_master ) {
76
				\Jetpack_Options::update_option( 'master_user', $new_master );
77
			}
78
			// else disconnect..?
79
		}
80
	}
81
}
82