WPCOM_VIP_Support_User::action_set_user_role PHP Method

action_set_user_role() public method

Hooks the set_user_role action to check if we're setting the user to the VIP Support role. If we are setting to the VIP Support role, various checks are run, and the transition may be reverted.
public action_set_user_role ( integer $user_id, string $role, array $old_roles )
$user_id integer The ID of the user having their role changed
$role string The name of the new role
$old_roles array Any roles the user was assigned to previously
    public function action_set_user_role($user_id, $role, $old_roles)
    {
        // Avoid recursing, while we're reverting
        if ($this->reverting_role) {
            return;
        }
        $user = new WP_User($user_id);
        // Try to make the conditional checks clearer
        $becoming_support = WPCOM_VIP_Support_Role::VIP_SUPPORT_ROLE == $role;
        $valid_and_verified_email = $this->is_a8c_email($user->user_email) && $this->user_has_verified_email($user_id);
        if ($becoming_support && !$valid_and_verified_email) {
            $this->reverting_role = true;
            // @FIXME This could be expressed more simply, probably :|
            if (!is_array($old_roles) || !isset($old_roles[0])) {
                if ($this->is_a8c_email($user->user_email)) {
                    $revert_role_to = WPCOM_VIP_Support_Role::VIP_SUPPORT_INACTIVE_ROLE;
                } else {
                    $revert_role_to = 'subscriber';
                }
            } else {
                $revert_role_to = $old_roles[0];
            }
            $this->demote_user_from_vip_support_to($user->ID, $revert_role_to);
            if ($this->is_a8c_email($user->user_email) && !$this->user_has_verified_email($user_id)) {
                $this->message_replace = self::MSG_BLOCK_UPGRADE_VERIFY_EMAIL;
                $this->send_verification_email($user_id);
            } else {
                $this->message_replace = self::MSG_BLOCK_UPGRADE_NON_A11N;
            }
            $this->reverting_role = false;
        }
    }