Automattic /
jetpack
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 | |||
| 10 | static $check_sum_id = 'user_check_sum'; |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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 | } |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.