Completed
Push — update/non-admin-view ( b1ee9d...762240 )
by
unknown
10:19
created

Jetpack_Sync_Users::get_signed_role()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Class Jetpack_Sync_Users
5
 *
6
 * Responsible for syncing user data changes.
7
 */
8
class Jetpack_Sync_Users {
9
	static $user_roles = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $user_roles.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
11
	static function init() {
12
		if ( Jetpack::is_active() ) {
13
			// Kick off synchronization of user role when it changes
14
			add_action( 'set_user_role', array( __CLASS__, 'user_role_change' ) );
15
		}
16
	}
17
18
	/**
19
	 * Synchronize connected user role changes
20
	 */
21
	static function user_role_change( $user_id ) {
22
		if ( Jetpack::is_user_connected( $user_id ) ) {
23
			self::update_role_on_com( $user_id );
24
			//try to choose a new master if we're demoting the current one
25
			self::maybe_demote_master_user( $user_id );
26
		}
27
	}
28
29
	static function get_role( $user_id ) {
30
		if ( isset( $user_roles[ $user_id ] ) ) {
0 ignored issues
show
Bug introduced by
The variable $user_roles seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
31
			return $user_roles[ $user_id ];
32
		}
33
34
		$current_user_id = get_current_user_id();
35
		wp_set_current_user( $user_id );
36
		$role        = Jetpack::translate_current_user_to_role();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 8 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
37
		wp_set_current_user( $current_user_id );
38
		$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...
39
40
		return $role;
41
	}
42
43
	static function get_signed_role( $user_id ) {
44
		return Jetpack::sign_role( self::get_role( $user_id ) );
45
	}
46
47
	static function update_role_on_com( $user_id ) {
48
		$signed_role = self::get_signed_role( $user_id );
49
		Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
50
	}
51
52
	static function maybe_demote_master_user( $user_id ) {
53
		$master_user_id = Jetpack_Options::get_option( 'master_user' );
54
		$role = self::get_role( $user_id );
55
		if ( $user_id == $master_user_id && 'administrator' != $role ) {
56
			$query      = new WP_User_Query(
57
				array(
58
					'fields'  => array( 'id' ),
59
					'role'    => 'administrator',
60
					'orderby' => 'id',
61
					'exclude' => array( $master_user_id ),
62
				)
63
			);
64
			$new_master = false;
65
			foreach ( $query->results as $result ) {
66
				$found_user_id = absint( $result->id );
67
				if ( $found_user_id && Jetpack::is_user_connected( $found_user_id ) ) {
68
					$new_master = $found_user_id;
69
					break;
70
				}
71
			}
72
73
			if ( $new_master ) {
74
				Jetpack_Options::update_option( 'master_user', $new_master );
75
			}
76
			// else disconnect..?
77
		}
78
	}
79
}
80
Jetpack_Sync_Users::init();