Completed
Push — add/sso-analytics ( 683a91...e67d7d )
by
unknown
162:06 queued 152:34
created

Jetpack_Sync_Users::user_role_change()   D

Complexity

Conditions 10
Paths 9

Size

Total Lines 44
Code Lines 27

Duplication

Lines 23
Ratio 52.27 %
Metric Value
cc 10
eloc 27
nc 9
nop 1
dl 23
loc 44
rs 4.8196

How to fix   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
3
/**
4
 * Class Jetpack_Sync_Users
5
 *
6
 * Responsible for syncing user data changes.
7
 */
8
class Jetpack_Sync_Users {
9
10
	static $check_sum_id = 'user_check_sum';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $check_sum_id.

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...
11
12
	static function init() {
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
	 * Synchronize connected user role changes
19
	 */
20
	static function user_role_change( $user_id ) {
21
		if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) {
22
			$current_user_id = get_current_user_id();
23
			wp_set_current_user( $user_id );
24
			$role        = Jetpack::translate_current_user_to_role();
25
			$signed_role = Jetpack::sign_role( $role );
26
			wp_set_current_user( $current_user_id );
27
28
			$master_token   = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
29
			$master_user_id = absint( $master_token->external_user_id );
30
31
			if ( ! $master_user_id ) {
32
				return;
33
			} // this shouldn't happen
34
35
			Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
36
//@todo retry on failure
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
37
38
//try to choose a new master if we're demoting the current one
39 View Code Duplication
			if ( $user_id == $master_user_id && 'administrator' != $role ) {
40
				$query      = new WP_User_Query(
41
					array(
42
						'fields'  => array( 'id' ),
43
						'role'    => 'administrator',
44
						'orderby' => 'id',
45
						'exclude' => array( $master_user_id ),
46
					)
47
				);
48
				$new_master = false;
49
				foreach ( $query->results as $result ) {
50
					$uid = absint( $result->id );
51
					if ( $uid && Jetpack::is_user_connected( $uid ) ) {
52
						$new_master = $uid;
53
						break;
54
					}
55
				}
56
57
				if ( $new_master ) {
58
					Jetpack_Options::update_option( 'master_user', $new_master );
59
				}
60
// else disconnect..?
61
			}
62
		}
63
	}
64
}