/**
  * Check if ID can be link or not. if yes the link account.
  */
 public static function link_account_if_possible()
 {
     global $loginRadiusObject, $wpdb, $user_ID;
     $loginRadiusMappingData = array();
     if (null == $loginRadiusObject) {
         $loginRadiusObject = new LoginRadius();
     }
     if (isset($_REQUEST['token']) && is_user_logged_in()) {
         try {
             $loginRadiusUserprofile = $loginRadiusObject->loginradius_get_user_profiledata($_REQUEST['token']);
         } catch (LoginRadiusException $e) {
             // Error Handling
             if ($loginRadiusSettings['enable_degugging'] == '0') {
                 // if debugging is off and Social profile not recieved, redirect to home page.
                 wp_redirect(site_url());
                 exit;
             } else {
                 $loginRadiusUserprofile = null;
                 $message = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : $e->getMessage();
                 error_log($message);
                 // If debug option is set and Social Profile not retrieved
                 Login_Helper::login_radius_notify($message, 'isProfileNotRetrieved');
                 return;
             }
         }
         $loginRadiusMappingData['id'] = !empty($loginRadiusUserprofile->ID) ? $loginRadiusUserprofile->ID : '';
         $loginRadiusMappingData['provider'] = !empty($loginRadiusUserprofile->Provider) ? $loginRadiusUserprofile->Provider : '';
         $loginRadiusMappingData['thumbnail'] = !empty($loginRadiusUserprofile->ThumbnailImageUrl) ? trim($loginRadiusUserprofile->ThumbnailImageUrl) : '';
         if (empty($loginRadiusMappingData['thumbnail']) && $loginRadiusMappingData['provider'] == 'facebook') {
             $loginRadiusMappingData['thumbnail'] = 'http://graph.facebook.com/' . $loginRadiusMappingData['id'] . '/picture?type=large';
         }
         $loginRadiusMappingData['pictureUrl'] = !empty($loginRadiusUserprofile->ImageUrl) ? trim($loginRadiusUserprofile->ImageUrl) : '';
         $wp_user_id = $wpdb->get_var($wpdb->prepare('SELECT user_id FROM ' . $wpdb->usermeta . ' WHERE meta_key="loginradius_provider_id" AND meta_value = %s', $loginRadiusMappingData['id']));
         if (!empty($wp_user_id)) {
             // Check if verified field exist or not.
             $loginRadiusVfyExist = $wpdb->get_var($wpdb->prepare('SELECT user_id FROM ' . $wpdb->usermeta . ' WHERE user_id = %d AND meta_key = "loginradius_isVerified"', $wp_user_id));
             if (!empty($loginRadiusVfyExist)) {
                 // if verified field exists
                 $loginRadiusVerify = $wpdb->get_var($wpdb->prepare('SELECT meta_value FROM ' . $wpdb->usermeta . ' WHERE user_id = %d AND meta_key = "loginradius_isVerified"', $wp_user_id));
                 if ($loginRadiusVerify != '1') {
                     self::link_account($user_ID, $loginRadiusMappingData['id'], $loginRadiusMappingData['provider'], $loginRadiusMappingData['thumbnail'], $loginRadiusMappingData['pictureUrl']);
                     return true;
                 } else {
                     //account already mapped
                     return false;
                 }
             } else {
                 return false;
             }
         } else {
             $loginRadiusMappingProvider = $loginRadiusMappingData['provider'];
             $wp_user_lrid = $wpdb->get_var($wpdb->prepare('SELECT user_id FROM ' . $wpdb->usermeta . ' WHERE meta_key="' . $loginRadiusMappingProvider . 'Lrid" AND meta_value = %s', $loginRadiusMappingData['id']));
             if (!empty($wp_user_lrid)) {
                 $lrVerified = get_user_meta($wp_user_lrid, $loginRadiusMappingProvider . 'LrVerified', true);
                 if ($lrVerified == '1') {
                     // Check if lrid is the same that verified email.
                     // account already mapped
                     return false;
                 } else {
                     // map account
                     self::link_account($user_ID, $loginRadiusMappingData['id'], $loginRadiusMappingData['provider'], $loginRadiusMappingData['thumbnail'], $loginRadiusMappingData['pictureUrl']);
                     return true;
                 }
             } else {
                 // map account
                 self::link_account($user_ID, $loginRadiusMappingData['id'], $loginRadiusMappingData['provider'], $loginRadiusMappingData['thumbnail'], $loginRadiusMappingData['pictureUrl']);
                 return true;
             }
         }
     }
 }
 /**
  * This function is called when token is returned from LoginRadius.
  * it checks for query string variable and fetches data using LoginRadius api.
  * After fetching data, appropriate action is taken on the basis of LoginRadius plugin settings
  */
 public static function login_radius_connect()
 {
     global $wpdb, $loginRadiusSettings, $loginRadiusObject, $lr_social_profile_data_settings;
     if (isset($_GET['loginradius_linking']) && isset($_REQUEST['token'])) {
         LR_Common::perform_linking_operation();
     }
     if (isset($_GET['loginRadiusVk']) && trim($_GET['loginRadiusVk']) != '') {
         //If verification link is clicked
         Login_Helper::verify_user_after_email_confirmation();
     }
     if (isset($_POST['LoginRadius_popupSubmit'])) {
         //If "email required" popup has been submitted
         Login_helper::response_to_popup_submission();
     }
     // If remove button clicked
     if (isset($_GET['loginRadiusMap']) && !empty($_GET['loginRadiusMap']) && isset($_GET['loginRadiusMappingProvider']) && !empty($_GET['loginRadiusMappingProvider']) && is_user_logged_in()) {
         Login_Helper::unlink_provider();
     }
     if (!is_user_logged_in() && isset($_REQUEST['token'])) {
         $token = $_REQUEST['token'];
         try {
             // Fetch user profile using access token ......
             $responseFromLoginRadius = $loginRadiusObject->loginradius_get_user_profiledata($token);
         } catch (LoginRadiusException $e) {
             // Error Handling
             if ($loginRadiusSettings['enable_degugging'] == '0') {
                 // if debugging is off and Social profile not recieved, redirect to home page.
                 wp_redirect(site_url());
                 exit;
             } else {
                 $responseFromLoginRadius = null;
                 $message = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : $e->getMessage();
                 error_log($message);
                 // If debug option is set and Social Profile not retrieved
                 Login_Helper::login_radius_notify($message, 'isProfileNotRetrieved');
                 return;
             }
         }
         if (isset($responseFromLoginRadius->ID) && $responseFromLoginRadius->ID != null) {
             // If profile data is retrieved successfully
             self::$loginRadiusProfileData = Login_Helper::filter_loginradius_data_for_wordpress_use($responseFromLoginRadius);
         }
         $userId = Login_Helper::is_socialid_exists_in_wordpress(self::$loginRadiusProfileData);
         if ($userId) {
             //if Social id exists in wordpress database
             if (1 == get_user_meta($userId, self::$loginRadiusProfileData['Provider'] . 'LrVerified', true) || class_exists('LR_Raas_Install')) {
                 // if user is verified, provide login.
                 Login_Helper::login_user($userId, self::$loginRadiusProfileData);
             } else {
                 // If not verified then display pop up.
                 Login_Helper::login_radius_notify(__('Please verify your email by clicking the confirmation link sent to you.', 'LoginRadius'), 'isEmailNotVerified');
                 return;
             }
         }
         // check if id already exists.
         $loginRadiusUserId = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM " . $wpdb->usermeta . " WHERE meta_key='loginradius_provider_id' AND meta_value = %s", self::$loginRadiusProfileData['ID']));
         if (!empty($loginRadiusUserId)) {
             // id exists
             $tempUserId = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id = %d and meta_key='loginradius_isVerified'", $loginRadiusUserId));
             if (!empty($tempUserId)) {
                 // check if verification field exists.
                 $isVerified = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM " . $wpdb->usermeta . " WHERE user_id = %d and meta_key='loginradius_isVerified'", $loginRadiusUserId));
                 if ($isVerified == '1') {
                     // if email is verified
                     Login_Helper::login_user($loginRadiusUserId, self::$loginRadiusProfileData);
                     return;
                 } else {
                     Login_Helper::login_radius_notify(__('Please verify your email by clicking the confirmation link sent to you.', 'LoginRadius'), 'isEmailNotVerified');
                     return;
                 }
             } else {
                 Login_Helper::login_user($loginRadiusUserId, self::$loginRadiusProfileData);
                 return;
             }
         } else {
             // Email is empty for social profile data.
             $dummyEmail = isset($loginRadiusSettings['LoginRadius_dummyemail']) ? $loginRadiusSettings['LoginRadius_dummyemail'] : '';
             // Create dummy email.
             if (empty(self::$loginRadiusProfileData['Email']) && $dummyEmail == 'dummyemail') {
                 // Email not required according to plugin settings
                 self::$loginRadiusProfileData['Email'] = Login_Helper::generate_dummy_email(self::$loginRadiusProfileData);
             }
             // Retrieve Profile Data in Popup - Custom popup enabled
             if (isset($lr_social_profile_data_settings['enable_custom_popup']) && $lr_social_profile_data_settings['enable_custom_popup'] == '1') {
                 //Has Email from provider or from dummy email
                 $has_email = isset(self::$loginRadiusProfileData['Email']) && !empty(self::$loginRadiusProfileData['Email']) ? true : false;
                 $notdummyemail = isset($loginRadiusSettings['LoginRadius_dummyemail']) && $loginRadiusSettings['LoginRadius_dummyemail'] == 'notdummyemail' ? true : false;
                 $showpopup = false;
                 if (!$has_email && $notdummyemail && isset($lr_social_profile_data_settings['show_email']) && $lr_social_profile_data_settings['show_email'] == '1' || isset($loginRadiusSettings['LoginRadius_dummyemail']) && $loginRadiusSettings['LoginRadius_dummyemail'] == 'notdummyemail') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_gender']) && $lr_social_profile_data_settings['show_gender'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_birthdate']) && $lr_social_profile_data_settings['show_birthdate'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_phonenumber']) && $lr_social_profile_data_settings['show_phonenumber'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_city']) && $lr_social_profile_data_settings['show_city'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_postalcode']) && $lr_social_profile_data_settings['show_postalcode'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_relationshipstatus']) && $lr_social_profile_data_settings['show_relationshipstatus'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_custom_one']) && $lr_social_profile_data_settings['show_custom_one'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_custom_two']) && $lr_social_profile_data_settings['show_custom_two'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_custom_three']) && $lr_social_profile_data_settings['show_custom_three'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_custom_four']) && $lr_social_profile_data_settings['show_custom_four'] == '1') {
                     $showpopup = true;
                 } elseif (isset($lr_social_profile_data_settings['show_custom_five']) && $lr_social_profile_data_settings['show_custom_five'] == '1') {
                     $showpopup = true;
                 }
                 if ($showpopup) {
                     $lrUniqueId = Login_Helper::store_temporary_data(self::$loginRadiusProfileData);
                     $queryString = '?lrid=' . $lrUniqueId;
                     wp_redirect(site_url() . $queryString);
                     exit;
                 } else {
                     Login_Helper::register_user(self::$loginRadiusProfileData);
                     return;
                 }
             } else {
                 // Normal flow no custom popup.
                 if (empty(self::$loginRadiusProfileData['Email'])) {
                     if ($dummyEmail == 'dummyemail') {
                         Login_Helper::register_user(self::$loginRadiusProfileData);
                         return;
                     } else {
                         // Email required according to plugin settings.
                         $lrUniqueId = Login_Helper::store_temporary_data(self::$loginRadiusProfileData);
                         $queryString = '?lrid=' . $lrUniqueId;
                         wp_redirect(site_url() . $queryString);
                         exit;
                     }
                 } else {
                     // Email is not empty
                     $userObject = get_user_by('email', self::$loginRadiusProfileData['Email']);
                     $loginRadiusUserId = is_object($userObject) ? $userObject->ID : '';
                     if (!empty($loginRadiusUserId)) {
                         // Email exists
                         $isVerified = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM " . $wpdb->usermeta . " WHERE user_id = %d and meta_key='loginradius_isVerified'", $loginRadiusUserId));
                         if (!empty($isVerified)) {
                             if ($isVerified == '1') {
                                 // Social linking
                                 LR_Common::link_account($loginRadiusUserId, self::$loginRadiusProfileData['ID'], self::$loginRadiusProfileData['Provider'], self::$loginRadiusProfileData['ThumbnailImageUrl'], self::$loginRadiusProfileData['Provider'], '');
                                 // Login user
                                 Login_Helper::login_user($loginRadiusUserId, self::$loginRadiusProfileData);
                                 return;
                             } else {
                                 $directorySeparator = DIRECTORY_SEPARATOR;
                                 require_once getcwd() . $directorySeparator . 'wp-admin' . $directorySeparator . 'inc' . $directorySeparator . 'user.php';
                                 wp_delete_user($loginRadiusUserId);
                                 Login_Helper::register_user(self::$loginRadiusProfileData);
                             }
                         } else {
                             // Link Account
                             LR_Common::link_account($loginRadiusUserId, self::$loginRadiusProfileData['ID'], self::$loginRadiusProfileData['Provider'], self::$loginRadiusProfileData['ThumbnailImageUrl'], self::$loginRadiusProfileData['Provider'], '');
                             // Login user
                             Login_Helper::login_user($loginRadiusUserId, self::$loginRadiusProfileData);
                             return;
                         }
                     } else {
                         Login_Helper::register_user(self::$loginRadiusProfileData);
                     }
                 }
             }
         }
     }
     // Authentication ends
 }