function test_store_user_social_profile()
 {
     $insert_id = wsl_store_hybridauth_user_profile($this->someUserID, $this->someUserIDP, $this->someUserProfile);
     $profile = (array) wsl_get_stored_hybridauth_user_profiles_by_user_id($this->someUserID);
     $this->assertEquals(1, count($profile));
     $this->assertEquals($this->someUserID, $profile[0]->user_id);
     $this->assertEquals($this->someUserIDP, $profile[0]->provider);
     $this->assertEquals($this->someUserProfile->identifier, $profile[0]->identifier);
     $this->assertEquals($this->someUserProfile->email, $profile[0]->email);
     $this->assertEquals($this->someUserProfile->emailVerified, $profile[0]->emailverified);
     $profile = (array) wsl_get_stored_hybridauth_user_id_by_email_verified($this->someUserProfile->emailVerified);
     $this->assertEquals(1, count($profile));
     $profile = (array) wsl_get_stored_hybridauth_user_id_by_provider_and_provider_uid($this->someUserIDP, $this->someUserProfile->identifier);
     $this->assertEquals(1, count($profile));
     $user_id = wsl_get_stored_hybridauth_user_id_by_provider_and_provider_uid($this->someUserIDP, $this->someUserProfile->identifier);
     $this->assertEquals($this->someUserID, $user_id);
     $count = wsl_get_wsl_users_count();
     $this->assertEquals(1, $count);
     $count = wsl_get_stored_hybridauth_user_profiles_count();
     $this->assertEquals(1, $count);
 }
/**
* Returns user data after he authenticate via hybridauth
*
* Steps:
*    1. Grab the user profile from hybridauth
*    2. Run Bouncer::Filters if enabled (domains, emails, profiles urls)
*    3. Check if user exist in database by looking for the couple (Provider name, Provider user ID) or verified email
*    4. Deletegate detection of user id to custom functions / hooks
*    5. If Bouncer::Profile Completion is enabled and user didn't exist, we require the user to complete the registration (user name & email)
*/
function wsl_process_login_get_user_data($provider, $redirect_to)
{
    // HOOKABLE:
    do_action("wsl_process_login_get_user_data_start", $provider, $redirect_to);
    $user_id = null;
    $config = null;
    $hybridauth = null;
    $adapter = null;
    $hybridauth_user_profile = null;
    $requested_user_login = '';
    $requested_user_email = '';
    $wordpress_user_id = 0;
    /* 1. Grab the user profile from social network */
    if (!(isset($_SESSION['wsl::userprofile']) && $_SESSION['wsl::userprofile'] && ($hybridauth_user_profile = json_decode($_SESSION['wsl::userprofile'])))) {
        $hybridauth_user_profile = wsl_process_login_request_user_social_profile($provider);
        $_SESSION['wsl::userprofile'] = json_encode($hybridauth_user_profile);
    }
    $adapter = wsl_process_login_get_provider_adapter($provider);
    $hybridauth_user_email = sanitize_email($hybridauth_user_profile->email);
    /* 2. Run Bouncer::Filters if enabled (domains, emails, profiles urls) */
    // Bouncer::Filters by emails domains name
    if (get_option('wsl_settings_bouncer_new_users_restrict_domain_enabled') == 1) {
        if (empty($hybridauth_user_email)) {
            return wsl_process_login_render_notice_page(_wsl__(get_option('wsl_settings_bouncer_new_users_restrict_domain_text_bounce'), 'wordpress-social-login'));
        }
        $list = get_option('wsl_settings_bouncer_new_users_restrict_domain_list');
        $list = preg_split('/$\\R?^/m', $list);
        $current = strstr($hybridauth_user_email, '@');
        $shall_pass = false;
        foreach ($list as $item) {
            if (trim(strtolower("@{$item}")) == strtolower($current)) {
                $shall_pass = true;
            }
        }
        if (!$shall_pass) {
            return wsl_process_login_render_notice_page(_wsl__(get_option('wsl_settings_bouncer_new_users_restrict_domain_text_bounce'), 'wordpress-social-login'));
        }
    }
    // because instagram doesn't (do any?) have an email, we need to check if the option "require email" is set and then get the email from
    // the user BEFORE we filter by email address
    /* 4 Deletegate detection of user id to custom filters hooks */
    $user_id = (int) wsl_get_stored_hybridauth_user_id_by_provider_and_provider_uid($provider, $hybridauth_user_profile->identifier);
    /* 5. If Bouncer::Profile Completion is enabled and user didn't exist, we require the user to complete the registration (user name & email) */
    if (!$user_id) {
        // Bouncer :: Accept new registrations?
        if (get_option('wsl_settings_bouncer_registration_enabled') == 2) {
            return wsl_process_login_render_notice_page(_wsl__("Registration is now closed.", 'wordpress-social-login'));
        }
        // Bouncer::Accounts linking/mapping
        // > > not implemented yet! Planned for WSL 2.3
        if (get_option('wsl_settings_bouncer_accounts_linking_enabled') == 1) {
            do {
                list($shall_pass, $user_id, $requested_user_login, $requested_user_email) = wsl_process_login_new_users_gateway($provider, $redirect_to, $hybridauth_user_profile);
            } while (!$shall_pass);
            $wordpress_user_id = $user_id;
        } elseif (get_option('wsl_settings_bouncer_profile_completion_require_email') == 1 && empty($hybridauth_user_email) || get_option('wsl_settings_bouncer_profile_completion_change_username') == 1) {
            do {
                list($shall_pass, $user_id, $requested_user_login, $requested_user_email) = wsl_process_login_new_users_gateway($provider, $redirect_to, $hybridauth_user_profile);
            } while (!$shall_pass);
        }
    } else {
        $wordpress_user_id = $user_id;
    }
    $hybridauth_user_email = $requested_user_email;
    // Bouncer::Filters by e-mails addresses
    if (get_option('wsl_settings_bouncer_new_users_restrict_email_enabled') == 1) {
        error_log(__METHOD__ . ' start wsl_settings_bouncer_new_users_restrict_email_enabled.');
        error_log(__METHOD__ . ' hybridauth_user_email is ' . $hybridauth_user_email);
        if (empty($hybridauth_user_email)) {
            return wsl_process_login_render_notice_page(_wsl__(get_option('wsl_settings_bouncer_new_users_restrict_email_text_bounce'), 'wordpress-social-login'));
        }
        $list = get_option('wsl_settings_bouncer_new_users_restrict_email_list');
        $list = preg_split('/$\\R?^/m', $list);
        $shall_pass = false;
        foreach ($list as $item) {
            if (trim(strtolower($item)) == strtolower($hybridauth_user_email)) {
                $shall_pass = true;
            }
        }
        if (!$shall_pass) {
            return wsl_process_login_render_notice_page(_wsl__(get_option('wsl_settings_bouncer_new_users_restrict_email_text_bounce'), 'wordpress-social-login'));
        }
    }
    // Bouncer::Filters by profile urls
    if (get_option('wsl_settings_bouncer_new_users_restrict_profile_enabled') == 1) {
        error_log(__METHOD__ . ' start restrict_profile_enabled.');
        $list = get_option('wsl_settings_bouncer_new_users_restrict_profile_list');
        $list = preg_split('/$\\R?^/m', $list);
        error_log(__METHOD__ . ' $list is ' . print_r($list, true));
        $shall_pass = false;
        foreach ($list as $item) {
            error_log(__METHOD__ . ' $item is ' . $item);
            error_log(__METHOD__ . ' $hybridauth_user_profile->profileURL is ' . $hybridauth_user_profile->profileURL);
            if (trim(strtolower($item)) == strtolower($hybridauth_user_profile->profileURL)) {
                $shall_pass = true;
            }
        }
        if (!$shall_pass) {
            return wsl_process_login_render_notice_page(_wsl__(get_option('wsl_settings_bouncer_new_users_restrict_profile_text_bounce'), 'wordpress-social-login'));
        }
    }
    /* 3. Check if user exist in database by looking for the couple (Provider name, Provider user ID) or verified email */
    // check if user already exist in wslusersprofiles
    $user_id = (int) wsl_get_stored_hybridauth_user_id_by_provider_and_provider_uid($provider, $hybridauth_user_profile->identifier);
    // if not found in wslusersprofiles, then check his verified email
    if (!$user_id && !empty($hybridauth_user_profile->emailVerified)) {
        // check if the verified email exist in wp_users
        $user_id = (int) wsl_wp_email_exists($hybridauth_user_profile->emailVerified);
        // the user exists in Wordpress
        $wordpress_user_id = $user_id;
        // check if the verified email exist in wslusersprofiles
        if (!$user_id) {
            $user_id = (int) wsl_get_stored_hybridauth_user_id_by_email_verified($hybridauth_user_profile->emailVerified);
        }
    }
    /* 4 Deletegate detection of user id to custom filters hooks */
    /* 6. returns user data */
    return array($user_id, $adapter, $hybridauth_user_profile, $requested_user_login, $requested_user_email, $wordpress_user_id);
}