/**
         * Displaying account linking on profile page
         */
        public static function account_linking_info_on_profile_page()
        {
            global $pagenow, $lr_custom_interface_settings;
            $user_Id = get_current_user_id();
            if (isset($loginRadiusSettings['LoginRadius_socialLinking']) && $loginRadiusSettings['LoginRadius_socialLinking'] == '1' || !$user_Id == '1') {
                return;
            }
            $custom = false;
            if (isset($lr_custom_interface_settings['custom_interface']) && $lr_custom_interface_settings['custom_interface'] == '1') {
                $custom = true;
            }
            if ($pagenow == 'profile.php') {
                if (!class_exists('LR_Raas_Install')) {
                    echo LR_Common::check_linking_status_parameters();
                    // If remove button clicked
                    if (isset($_GET['loginRadiusMap']) && !empty($_GET['loginRadiusMap']) && isset($_GET['loginRadiusMappingProvider']) && !empty($_GET['loginRadiusMappingProvider'])) {
                        Login_Helper::unlink_provider();
                    }
                    LR_Common::link_account_if_possible();
                    ?>
                    <div class="metabox-holder columns-2" id="post-body">
                        <div class="stuffbox wrap" style="padding-bottom:10px">
                            <h3><label><?php 
                    _e('Link your account', 'LoginRadius');
                    ?>
</label></h3>
                            <div class="inside" style='padding:0'>
                                <table  class="form-table editcomment">
                                    <tr>
                                        <td colspan="2"><?php 
                    _e('By adding another account, you can log in with the new account as well!', 'LoginRadius');
                    ?>
</td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <?php 
                    if (!class_exists("Login_Helper")) {
                        require_once LOGINRADIUS_PLUGIN_DIR . 'public/inc/login/class-login-helper.php';
                    }
                    Login_Helper::get_loginradius_interface_container();
                    ?>
                                        </td>
                                    </tr>
                                    <?php 
                    echo LR_Common::get_connected_providers_list();
                    ?>
                                </table>
                            </div>
                        </div>
                    </div>
                    <?php 
                } else {
                    do_action('lr_raas_social_linking');
                }
            }
        }
 /**
  * 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
 }