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

Jetpack_Sync_Users   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 40.35 %

Coupling/Cohesion

Components 0
Dependencies 3
Metric Value
dl 23
loc 57
rs 10
wmc 11
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
D user_role_change() 23 44 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}