/**
  * Callback for admin_menu hook,
  * Register LoginRadius_settings and its sanitization callback. Add Login Radius meta box to pages and posts.
  */
 public function admin_init()
 {
     global $pagenow, $loginRadiusSettings;
     register_setting('LoginRadius_setting_options', 'LoginRadius_settings', array($this, 'validate_options'));
     if ($pagenow == 'profile.php' && isset($_REQUEST['token']) && !class_exists('LR_Raas_Install')) {
         LR_Common::perform_linking_operation();
     }
     if (isset($loginRadiusSettings['LoginRadius_noProvider']) && $loginRadiusSettings['LoginRadius_noProvider'] == '1' || isset($loginRadiusSettings['LoginRadius_enableUserActivation']) && $loginRadiusSettings['LoginRadius_enableUserActivation'] == '1') {
         add_filter('manage_users_columns', array('Admin_Helper', 'add_provider_column_in_users_list'));
         add_action('manage_users_custom_column', array('Admin_Helper', 'login_radius_show_provider'), 15, 3);
         if (isset($loginRadiusSettings['LoginRadius_enableUserActivation']) && $loginRadiusSettings['LoginRadius_enableUserActivation'] == '1') {
             add_filter('admin_head', array('Admin_Helper', 'add_script_for_users_page'), 10);
         }
     }
     // Replicate Social Login configuration to the subblogs in the multisite network
     if (is_multisite() && is_main_site()) {
         add_action('wpmu_new_blog', array($this, 'replicate_settings_to_new_blog'));
         add_action('update_option_LoginRadius_settings', array($this, 'login_radius_update_old_blogs'));
     }
 }
 /**
  * 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
 }