/**
 * Get Social Network Sorted List
 * as per saved in options
 * 
 * Handles to return social networks sorted
 * array to list in page
 * 
 * @package Easy Digital Downloads - Social Login
 * @since 1.0.0
 */
function edd_slg_get_sorted_social_network()
{
    global $edd_options;
    $edd_social_order = get_option('edd_social_order');
    $socials = edd_slg_social_networks();
    if (!isset($edd_social_order) || empty($edd_social_order)) {
        return $socials;
    }
    $sorted_socials = $edd_social_order;
    $return = array();
    for ($i = 0; $i < count($socials); $i++) {
        $return[$sorted_socials[$i]] = $socials[$sorted_socials[$i]];
    }
    return apply_filters('edd_slg_sorted_social_networks', $return);
}
    /**
     * Load Login Page For Social
     * 
     * Handles to load login page for social
     * when no email address found
     * 
     * @package Easy Digital Downloads - Social Login
     * @since 1.0.0
     */
    public function edd_slg_social_login_redirect()
    {
        global $edd_options;
        $socialtype = isset($_GET['eddslgnetwork']) ? $_GET['eddslgnetwork'] : '';
        //get all social networks
        $allsocialtypes = edd_slg_social_networks();
        if (!is_user_logged_in() && isset($_GET['edd_slg_social_login']) && !empty($socialtype) && array_key_exists($socialtype, $allsocialtypes)) {
            // get redirect url from shortcode
            $stcd_redirect_url = EDD()->session->get('edd_slg_stcd_redirect_url');
            //check button clicked from widget then redirect to widget page url
            if (isset($_GET['container']) && $_GET['container'] == 'widget') {
                // get redirect url from widget
                $stcd_redirect_url = EDD()->session->get('edd_slg_stcd_redirect_url_widget');
            }
            $redirect_url = !empty($stcd_redirect_url) ? $stcd_redirect_url : edd_slg_get_current_page_url();
            $data = array();
            //wordpress error class
            $errors = new WP_Error();
            switch ($socialtype) {
                case 'twitter':
                    //get twitter user data
                    $tw_userdata = $this->socialtwitter->edd_slg_get_twitter_user_data();
                    //check user id is set or not for twitter
                    if (!empty($tw_userdata) && isset($tw_userdata->id) && !empty($tw_userdata->id)) {
                        $data['first_name'] = $tw_userdata->name;
                        $data['last_name'] = '';
                        $data['name'] = $tw_userdata->screen_name;
                        //display name of user
                        $data['type'] = 'twitter';
                        $data['all'] = $tw_userdata;
                        $data['link'] = 'https://twitter.com/' . $tw_userdata->screen_name;
                        $data['id'] = $tw_userdata->id;
                    }
                    break;
            }
            //if cart is empty or user is not logged in social media
            //and accessing the url then send back user to checkout page
            if (!isset($data['id']) || empty($data['id'])) {
                /*if( isset( $_GET['page_id'] ) && !empty( $_GET['page_id'] ) ) {
                			$redirect_url = get_permalink( $_GET['page_id'] );
                		} else {
                			$redirect_url = home_url();
                		}*/
                if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget'])) {
                    unset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget']);
                }
                if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url'])) {
                    unset($_SESSION['edd']['edd_slg_stcd_redirect_url']);
                }
                wp_redirect($redirect_url);
                exit;
                //send user to checkout page
                //edd_slg_send_on_checkout_page();
            }
            //when user will click submit button of custom login
            //check user clicks submit button of registration page and get parameter should be valid param
            if (isset($_POST['edd-slg-submit']) && !empty($_POST['edd-slg-submit']) && $_POST['edd-slg-submit'] == __('Register', 'eddslg')) {
                $loginurl = wp_login_url();
                if (isset($_POST['edd_slg_social_email'])) {
                    //check email is set or not
                    $socialemail = $_POST['edd_slg_social_email'];
                    if (empty($socialemail)) {
                        //if email is empty
                        $errors->add('empty_email', '<strong>' . __('ERROR', 'eddslg') . ' :</strong> ' . __('Enter your email address.', 'eddslg'));
                    } elseif (!is_email($socialemail)) {
                        //if email is not valid
                        $errors->add('invalid_email', '<strong>' . __('ERROR', 'eddslg') . ' :</strong> ' . __('The email address did not validate.', 'eddslg'));
                        $socialemail = '';
                    } elseif (email_exists($socialemail)) {
                        //if email is exist or not
                        $errors->add('email_exists', '<strong>' . __('ERROR', 'eddslg') . ' :</strong> ' . __('Email already exists, If you have an account login first.', 'eddslg'));
                    }
                    if ($errors->get_error_code() == '') {
                        //
                        if (!empty($data)) {
                            //check user data is not empty
                            $data['email'] = $socialemail;
                            //create user
                            $usercreated = $this->edd_slg_add_user($data);
                            if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget'])) {
                                unset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget']);
                            }
                            if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url'])) {
                                unset($_SESSION['edd']['edd_slg_stcd_redirect_url']);
                            }
                            wp_redirect($redirect_url);
                            exit;
                            //send user to checkout page
                            //edd_slg_send_on_checkout_page();
                        }
                    }
                }
            }
            //redirect user to custom registration form
            if (isset($_GET['edd_slg_social_login']) && !empty($_GET['edd_slg_social_login'])) {
                //login call back url after registration
                /*$callbackurl = wp_login_url();
                		$callbackurl = add_query_arg('edd_slg_social_login_done', 1, $callbackurl);*/
                $socialemail = isset($_POST['edd_slg_social_email']) ? $_POST['edd_slg_social_email'] : '';
                //check the user who is going to connect with site
                //it is alreay exist with same data or not
                //if user is exist then simply make that user logged in
                $metaquery = array(array('key' => 'edd_slg_social_user_connect_via', 'value' => $data['type']), array('key' => 'edd_slg_social_identifier', 'value' => $data['id']));
                $getusers = get_users(array('meta_query' => $metaquery));
                $wpuser = array_shift($getusers);
                //getting users
                //check user is exist or not conected with same metabox
                if (!empty($wpuser)) {
                    //make user logged in
                    wp_set_auth_cookie($wpuser->ID, false);
                    if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget'])) {
                        unset($_SESSION['edd']['edd_slg_stcd_redirect_url_widget']);
                    }
                    if (isset($_SESSION['edd']['edd_slg_stcd_redirect_url'])) {
                        unset($_SESSION['edd']['edd_slg_stcd_redirect_url']);
                    }
                    wp_redirect($redirect_url);
                    exit;
                } else {
                    //if user is not exist then show register user form
                    login_header(__('Registration Form', 'eddslg'), '<p class="message register">' . __('Please enter your email address to complete registration.', 'eddslg') . '</p>', $errors);
                    ?>
						<form name="registerform" id="registerform" action="" method="post">
							  <p>
								  <label for="wcsl_email"><?php 
                    _e('E-mail', 'eddslg');
                    ?>
<br />
								  <input type="text" name="edd_slg_social_email" id="edd_slg_social_email" class="input" value="<?php 
                    echo $socialemail;
                    ?>
" size="25" tabindex="20" /></label>
							  </p>
							  <p id="reg_passmail">
							  	<?php 
                    _e('Username and Password will be sent to your email.', 'eddslg');
                    ?>
							  </p>
							  <br class="clear" />
							  <p class="submit"><input type="submit" name="edd-slg-submit" id="edd-slg-submit" class="button-primary" value="<?php 
                    _e('Register', 'eddslg');
                    ?>
" tabindex="100" /></p>
						</form>
					<?php 
                    login_footer('user_login');
                    exit;
                }
            }
        }
    }