Completed
Push — fix/videopress-missing-from-se... ( 60564b )
by
unknown
22:41 queued 12:31
created

sync/class.jetpack-sync-users.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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 ] ) ) {
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();
37
		wp_set_current_user( $current_user_id );
38
		$user_roles[ $user_id ] = $role;
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
81
Jetpack_Sync_Users::init();
82