/**
 * Request email from user
 */
function oa_social_login_request_email()
{
    //Get the current user
    $current_user = wp_get_current_user();
    //Check if logged in
    if (!empty($current_user->ID) and is_numeric($current_user->ID)) {
        //Current user
        $user_id = $current_user->ID;
        //Check if email has to be requested
        $oa_social_login_request_email = get_user_meta($user_id, 'oa_social_login_request_email', true);
        if (!empty($oa_social_login_request_email)) {
            //Display modal dialog?
            $display_modal = true;
            //Messaging
            $message = '';
            //Read settings
            $settings = get_option('oa_social_login_settings');
            //Make sure that the email is still required
            if (empty($settings['plugin_require_email'])) {
                //Do not display the modal dialog
                $display_modal = false;
                //Stop asking for the email
                delete_user_meta($user_id, 'oa_social_login_request_email');
            }
            //Form submitted
            if (isset($_POST) and !empty($_POST['oa_social_login_action'])) {
                if ($_POST['oa_social_login_action'] == 'confirm_email') {
                    $user_email = empty($_POST['oa_social_login_email']) ? '' : trim($_POST['oa_social_login_email']);
                    if (empty($user_email)) {
                        $message = __('Please enter your email address', 'oa_social_login');
                    } else {
                        if (!is_email($user_email)) {
                            $message = __('This email is not valid', 'oa_social_login');
                        } elseif (email_exists($user_email)) {
                            $message = __('This email is already used by another account', 'oa_social_login');
                        } else {
                            wp_update_user(array('ID' => $user_id, 'user_email' => $user_email));
                            delete_user_meta($user_id, 'oa_social_login_request_email');
                            $display_modal = false;
                        }
                    }
                }
            }
            //Display modal dialog?
            if ($display_modal === true) {
                //Read Settings
                $oa_social_login_settings = get_option('oa_social_login_settings');
                //Read the social network
                $oa_social_login_identity_provider = get_user_meta($user_id, 'oa_social_login_identity_provider', true);
                //Caption
                $caption = isset($oa_social_login_settings['plugin_require_email_text']) ? $oa_social_login_settings['plugin_require_email_text'] : __('<strong>We unfortunately could not retrieve your email address from %s.</strong> Please enter your email address in the form below in order to continue.', 'oa_social_login');
                //Add CSS
                oa_social_login_add_site_css();
                //Show email request form
                ?>
					<div id="oa_social_login_overlay"></div>
					<div id="oa_social_login_modal">
						<div class="oa_social_login_modal_outer">
							<div class="oa_social_login_modal_inner">
			 					<div class="oa_social_login_modal_title">
			 						<?php 
                printf(__('You have successfully connected with %s!', 'oa_social_login'), '<strong>' . $oa_social_login_identity_provider . '</strong>');
                ?>
			 					</div>
			 					<?php 
                if (strlen(trim($caption)) > 0) {
                    ?>
			 								<div class="oa_social_login_modal_notice"><?php 
                    echo str_replace('%s', $oa_social_login_identity_provider, $caption);
                    ?>
</div>
			 							<?php 
                }
                ?>
			 					<div class="oa_social_login_modal_body">
				 					<div class="oa_social_login_modal_subtitle">
				 						<?php 
                _e('Please enter your email address', 'oa_social_login');
                ?>
:
				 					</div>
									<form method="post" action="">
										<fieldset>
											<div>
												<input type="text" name="oa_social_login_email" class="oa_social_login_confirm_text" value="<?php 
                echo !empty($_POST['oa_social_login_email']) ? oa_social_login_esc_attr($_POST['oa_social_login_email']) : '';
                ?>
" />
												<input type="hidden" name="oa_social_login_action" value="confirm_email" size="30" />
											</div>
											<div class="oa_social_login_modal_error">
												<?php 
                echo $message;
                ?>
											</div>
											<div class="oa_social_login_buttons">
												<input class="oa_social_login_button_confirm" type="submit" value="<?php 
                _e('Confirm', 'oa_social_login');
                ?>
" />
												<input class="oa_social_login_button_cancel" type="button" value="<?php 
                _e('Cancel', 'oa_social_login');
                ?>
" onclick="window.location.href='<?php 
                echo esc_url(wp_logout_url(oa_social_login_get_current_url()));
                ?>
'" />
											</div>
										</fieldset>
									</form>
								</div>
							</div>
						</div>
					</div>
				<?php 
            }
        }
    }
}
    /**
     * Show Widget Settings
     */
    public function form($instance)
    {
        //Default settings
        $default_settings = array('widget_title' => __('Connect with', 'oa_social_login') . ':', 'widget_content_before' => '', 'widget_content_after' => '', 'widget_use_small_buttons' => '0', 'widget_hide_for_logged_in_users' => '1');
        foreach ($instance as $key => $value) {
            $instance[$key] = oa_social_login_esc_attr($value);
        }
        $instance = wp_parse_args((array) $instance, $default_settings);
        ?>
			<p>
				<label for="<?php 
        echo $this->get_field_id('widget_title');
        ?>
"><?php 
        _e('Title', 'oa_social_login');
        ?>
:</label>
				<input class="widefat" id="<?php 
        echo $this->get_field_id('widget_title');
        ?>
" name="<?php 
        echo $this->get_field_name('widget_title');
        ?>
" type="text" value="<?php 
        echo $instance['widget_title'];
        ?>
" />
			</p>
			<p>
				<label for="<?php 
        echo $this->get_field_id('widget_content_before');
        ?>
"><?php 
        _e('Insert text/html to add before the widget', 'oa_social_login');
        ?>
:</label>
				<textarea class="widefat" id="<?php 
        echo $this->get_field_id('widget_content_before');
        ?>
" name="<?php 
        echo $this->get_field_name('widget_content_before');
        ?>
"><?php 
        echo $instance['widget_content_before'];
        ?>
</textarea>
			</p>
			<p>
				<label for="<?php 
        echo $this->get_field_id('widget_content_after');
        ?>
"><?php 
        _e('Insert text/html to add after the widget', 'oa_social_login');
        ?>
:</label>
				<textarea class="widefat" id="<?php 
        echo $this->get_field_id('widget_content_after');
        ?>
" name="<?php 
        echo $this->get_field_name('widget_content_after');
        ?>
"><?php 
        echo $instance['widget_content_after'];
        ?>
</textarea>
			</p>
			<p>
				<input type="checkbox" id="<?php 
        echo $this->get_field_id('widget_hide_for_logged_in_users', 'oa_social_login');
        ?>
" name="<?php 
        echo $this->get_field_name('widget_hide_for_logged_in_users');
        ?>
" type="text" value="1" <?php 
        echo !empty($instance['widget_hide_for_logged_in_users']) ? 'checked="checked"' : '';
        ?>
 />
				<label for="<?php 
        echo $this->get_field_id('widget_hide_for_logged_in_users');
        ?>
"><?php 
        _e('Tick to hide widget for logged-in users', 'oa_social_login');
        ?>
</label>
			</p>
			<p>
				<input type="checkbox" id="<?php 
        echo $this->get_field_id('widget_use_small_buttons', 'oa_social_login');
        ?>
" name="<?php 
        echo $this->get_field_name('widget_use_small_buttons');
        ?>
" type="text" value="1" <?php 
        echo !empty($instance['widget_use_small_buttons']) ? 'checked="checked"' : '';
        ?>
 />
				<label for="<?php 
        echo $this->get_field_id('widget_use_small_buttons');
        ?>
"><?php 
        _e('Tick to use small buttons', 'oa_social_login');
        ?>
</label>
			</p>
		<?php 
    }