Jetpack_Sync_Users

Class Jetpack_Sync_Users.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-users.php  
  1. class Jetpack_Sync_Users { 
  2. static $user_roles = array(); 
  3.  
  4. static function init() { 
  5. if ( Jetpack::is_active() ) { 
  6. // Kick off synchronization of user role when it changes 
  7. add_action( 'set_user_role', array( __CLASS__, 'user_role_change' ) ); 
  8.  
  9. /** 
  10. * Synchronize connected user role changes 
  11. */ 
  12. static function user_role_change( $user_id ) { 
  13. if ( Jetpack::is_user_connected( $user_id ) ) { 
  14. self::update_role_on_com( $user_id ); 
  15. //try to choose a new master if we're demoting the current one 
  16. self::maybe_demote_master_user( $user_id ); 
  17.  
  18. static function get_role( $user_id ) { 
  19. if ( isset( $user_roles[ $user_id ] ) ) { 
  20. return $user_roles[ $user_id ]; 
  21.  
  22. $current_user_id = get_current_user_id(); 
  23. wp_set_current_user( $user_id ); 
  24. $role = Jetpack::translate_current_user_to_role(); 
  25. wp_set_current_user( $current_user_id ); 
  26. $user_roles[ $user_id ] = $role; 
  27.  
  28. return $role; 
  29.  
  30. static function get_signed_role( $user_id ) { 
  31. return Jetpack::sign_role( self::get_role( $user_id ) ); 
  32.  
  33. static function update_role_on_com( $user_id ) { 
  34. $signed_role = self::get_signed_role( $user_id ); 
  35. Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role ); 
  36.  
  37. static function maybe_demote_master_user( $user_id ) { 
  38. $master_user_id = Jetpack_Options::get_option( 'master_user' ); 
  39. $role = self::get_role( $user_id ); 
  40. if ( $user_id == $master_user_id && 'administrator' != $role ) { 
  41. $query = new WP_User_Query( 
  42. array( 
  43. 'fields' => array( 'id' ),  
  44. 'role' => 'administrator',  
  45. 'orderby' => 'id',  
  46. 'exclude' => array( $master_user_id ),  
  47. ); 
  48. $new_master = false; 
  49. foreach ( $query->results as $result ) { 
  50. $found_user_id = absint( $result->id ); 
  51. if ( $found_user_id && Jetpack::is_user_connected( $found_user_id ) ) { 
  52. $new_master = $found_user_id; 
  53. break; 
  54.  
  55. if ( $new_master ) { 
  56. Jetpack_Options::update_option( 'master_user', $new_master ); 
  57. // else disconnect..?