Completed
Push — update/premium-block-flow-trac... ( 403b50...6025a7 )
by Jon
07:25
created

Users::get_signed_role()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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