public function test_maybe_demote_master_user_method()
 {
     // set up
     $current_master_id = $this->factory->user->create(array('user_login' => 'current_master'));
     $new_master_id = $this->factory->user->create(array('user_login' => 'new_master'));
     $current_master = get_user_by('id', $current_master_id);
     $current_master->set_role('author');
     $new_master = get_user_by('id', $new_master_id);
     $new_master->set_role('administrator');
     Jetpack_Options::update_option('master_user', $current_master_id);
     Jetpack_Options::update_option('user_tokens', array($current_master_id => 'apple.a.' . $current_master_id, $new_master_id => 'kiwi.a.' . $new_master_id));
     // maybe
     Jetpack_Sync_Users::maybe_demote_master_user($current_master_id);
     $this->assertEquals($new_master_id, Jetpack_Options::get_option('master_user'));
     // don't demote user that if the user is still an admin.
     Jetpack_Sync_Users::maybe_demote_master_user($new_master_id);
     $this->assertEquals('administrator', $new_master->roles[0]);
     $this->assertEquals($new_master_id, Jetpack_Options::get_option('master_user'), 'Do not demote the master user if the user is still an admin');
     $new_master->set_role('author');
     // don't demote user if the user one the only admin that is connected.
     Jetpack_Sync_Users::maybe_demote_master_user($new_master_id);
     $this->assertEquals($new_master_id, Jetpack_Options::get_option('master_user'), 'Do not demote user if the user is the only connected user.');
 }
Esempio n. 2
0
 /**
  * Synchronize connected user role changes
  */
 function user_role_change($user_id)
 {
     _deprecated_function(__METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()');
     Jetpack_Sync_Users::user_role_change($user_id);
 }
    {
        return Jetpack::sign_role(self::get_role($user_id));
    }
    static function update_role_on_com($user_id)
    {
        $signed_role = self::get_signed_role($user_id);
        Jetpack::xmlrpc_async_call('jetpack.updateRole', $user_id, $signed_role);
    }
    static function maybe_demote_master_user($user_id)
    {
        $master_user_id = Jetpack_Options::get_option('master_user');
        $role = self::get_role($user_id);
        if ($user_id == $master_user_id && 'administrator' != $role) {
            $query = new WP_User_Query(array('fields' => array('id'), 'role' => 'administrator', 'orderby' => 'id', 'exclude' => array($master_user_id)));
            $new_master = false;
            foreach ($query->results as $result) {
                $found_user_id = absint($result->id);
                if ($found_user_id && Jetpack::is_user_connected($found_user_id)) {
                    $new_master = $found_user_id;
                    break;
                }
            }
            if ($new_master) {
                Jetpack_Options::update_option('master_user', $new_master);
            }
            // else disconnect..?
        }
    }
}
Jetpack_Sync_Users::init();