<?php 

    add_action( 'init', 'ajax_login_init' );
    
    function ajax_login_init() {
        global $az_data;
        wp_register_script( 'ajax-login-script', get_stylesheet_directory_uri() . '/js/ajax-login-script.js', array('jquery'), '1.0', true );
        wp_enqueue_script( 'ajax-login-script' );
        
        if($az_data['recapcha_login']) {
        wp_register_script( 'ajax-login-recapcha', 'https://www.google.com/recaptcha/api.js?hl=fa' , array('jquery'), '' , true );
        wp_enqueue_script( 'ajax-login-recapcha' );
            }
        wp_localize_script( 'ajax-login-script', 'ajax_login_object', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'redirecturl' => home_url(),
            'loadingmessage' => __( 'لطفا صبر کنید ...' ),
            'class' => 'alert alert-info'
        ));

        // Enable the user with no privileges to run ajax_login() in AJAX
        add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
    }

    // Check if users input information is valid
    function ajax_login() {
        
        global $az_data;
        
        // First check the nonce, if it fails the function will break
        check_ajax_referer( 'ajax-login-nonce', 'security' );
        
        if($az_data['recapcha_login']) {
            
            // your secret key
            $secret = $az_data['site_secret'];

            // empty response
            $response = null;

            // check secret key
            $reCaptcha = new ReCaptcha($secret);

            //Nonce is checked, get the POST data and sign user on
            if ($_POST["g-recaptcha-response"]) {
                $response = $reCaptcha->verifyResponse(
                    $_SERVER["REMOTE_ADDR"],
                    $_POST["g-recaptcha-response"]
                );
            }    
        }
        
    if($az_data['recapcha_login']){ 
        
    if ( $response != null && ($response->success) ){
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;
    $user_signon = wp_signon( $info, false );
    if ( is_wp_error( $user_signon ) || $response == null || !($response->success) ){
        echo json_encode( array( 'loggedin'=>false, 'class' => 'alert alert-danger' , 'message'=>__( 'مشخصات ورود اشتباه است!' )));
    } else {
        echo json_encode( array( 'loggedin'=>true, 'class' => 'alert alert-success' , 'message'=>__('ورود موفقیت آمیز بود ...' )));
    }
    }
    else {
         echo json_encode( array( 'loggedin'=>false, 'class' => 'alert alert-danger' , 'message'=>__( 'کد امنیتی را وارد کنید!' )));
    }
    }
    else{
            
         $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;
    $user_signon = wp_signon( $info, false );
    if ( is_wp_error( $user_signon ) ){
        echo json_encode( array( 'loggedin'=>false, 'class' => 'alert alert-danger' , 'message'=>__( 'مشخصات ورود اشتباه است!' )));
    } else {
        echo json_encode( array( 'loggedin'=>true, 'class' => 'alert alert-success' , 'message'=>__('ورود موفقیت آمیز بود ...' )));
    }
        
    }
    die();
}