| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add to Cart Ajax Class. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Controllers\Frontend\Ajax; |
| 9 |
|
| 10 |
use RadiusTheme\SB\Helpers\Fns; |
| 11 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 12 |
|
| 13 |
// Do not allow directly accessing this file. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit( 'This script cannot be accessed directly.' ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Add to Cart Ajax Class. |
| 20 |
*/ |
| 21 |
class AjaxLogin { |
| 22 |
/** |
| 23 |
* Singleton. |
| 24 |
*/ |
| 25 |
use SingletonTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class Constructor. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
private function __construct() { |
| 33 |
add_action( 'wp_ajax_nopriv_rtsb_ajax_login', [ $this, 'response' ] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Ajax Response. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function response() { |
| 42 |
$checkout_url = wc_get_checkout_url(); |
| 43 |
$has_error = false; |
| 44 |
|
| 45 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 46 |
$response = [ |
| 47 |
'loggedin' => false, |
| 48 |
'redirectto' => $checkout_url, |
| 49 |
'message_type' => 'error', |
| 50 |
'message' => esc_html__( 'Maybe You are not a real user.', 'shopbuilder' ), |
| 51 |
]; |
| 52 |
$has_error = true; |
| 53 |
} |
| 54 |
if ( is_user_logged_in() ) { |
| 55 |
$response = [ |
| 56 |
'loggedin' => true, |
| 57 |
'redirectto' => $checkout_url, |
| 58 |
'message_type' => 'info', |
| 59 |
'message' => esc_html__( 'Already Logged In.', 'shopbuilder' ), |
| 60 |
]; |
| 61 |
$has_error = true; |
| 62 |
} |
| 63 |
|
| 64 |
if ( empty( $_POST['username'] ) || empty( $_POST['password'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 65 |
$response = [ |
| 66 |
'loggedin' => false, |
| 67 |
'redirectto' => $checkout_url, |
| 68 |
'message_type' => 'error', |
| 69 |
'message' => esc_html__( 'Incorrect User Or password.', 'shopbuilder' ), |
| 70 |
]; |
| 71 |
$has_error = true; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! $has_error ) { |
| 75 |
$info['user_login'] = sanitize_text_field( $_POST['username'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 76 |
$info['user_password'] = sanitize_text_field( $_POST['password'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 77 |
$info['remember'] = ! empty( $_POST['rememberme'] ) ? sanitize_text_field( $_POST['rememberme'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 78 |
|
| 79 |
$user_signon = wp_signon( $info, false ); |
| 80 |
|
| 81 |
if ( is_wp_error( $user_signon ) ) { |
| 82 |
$response = [ |
| 83 |
'loggedin' => false, |
| 84 |
'redirectto' => $checkout_url, |
| 85 |
'message_type' => 'error', |
| 86 |
'message' => esc_html__( 'Login Faild.', 'shopbuilder' ), |
| 87 |
]; |
| 88 |
} else { |
| 89 |
$response = [ |
| 90 |
'loggedin' => true, |
| 91 |
'redirectto' => $checkout_url, |
| 92 |
'message_type' => 'success', |
| 93 |
'message' => esc_html__( 'Login successful.', 'shopbuilder' ), |
| 94 |
]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
wc_add_notice( $response['message'], $response['message_type'] ); |
| 99 |
wp_send_json( $response ); |
| 100 |
|
| 101 |
die(); |
| 102 |
} |
| 103 |
} |
| 104 |
|