PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.6
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.6
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / class-wpsc-current-user.php

class-wpsc-current-user.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.6, at includes/class-wpsc-current-user.php

995 lines 29.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Current_User' ) ) :
7
8 final class WPSC_Current_User {
9
10 /**
11 * Current user object to access
12 *
13 * @var WPSC_Current_User
14 */
15 public static $current_user;
16
17 /**
18 * Login type
19 *
20 * @var string
21 */
22 public static $login_type = '';
23
24 /**
25 * Guest login type
26 *
27 * @var string
28 */
29 public static $guest_login_type = '';
30
31 /**
32 * Current user WP object
33 *
34 * @var WP_User
35 */
36 public $user;
37
38 /**
39 * Check whether user is guest
40 *
41 * @var boolean
42 */
43 public $is_guest = false;
44
45 /**
46 * Check whether user is customer or not
47 *
48 * @var boolean
49 */
50 public $is_customer = false;
51
52 /**
53 * Customer object for current user
54 *
55 * @var WPSC_Customer
56 */
57 public $customer;
58
59 /**
60 * Check whether user is an agent or not
61 *
62 * @var boolean
63 */
64 public $is_agent = false;
65
66 /**
67 * Agent object for current user
68 *
69 * @var WPSC_Agent
70 */
71 public $agent;
72
73 /**
74 * Current user level. e.g. customer, agent or admin
75 *
76 * @var string
77 */
78 public $level;
79
80 /**
81 * Initialize this class
82 *
83 * @return void
84 */
85 public static function init() {
86
87 add_action( 'init', array( __CLASS__, 'load_current_user' ) );
88
89 // default login.
90 add_action( 'wp_ajax_nopriv_wpsc_default_login', array( __CLASS__, 'check_user_login' ) );
91
92 // default registration.
93 add_action( 'wp_ajax_nopriv_wpsc_get_default_registration', array( __CLASS__, 'get_user_registration' ) );
94 add_action( 'wp_ajax_nopriv_wpsc_check_username_availability', array( __CLASS__, 'check_username_availability' ) );
95 add_action( 'wp_ajax_nopriv_wpsc_authenticate_registration', array( __CLASS__, 'send_registration_otp' ) );
96 add_action( 'wp_ajax_nopriv_wpsc_confirm_registration', array( __CLASS__, 'register_user' ) );
97
98 // sign-in using otp.
99 add_action( 'wp_ajax_nopriv_wpsc_get_guest_sign_in', array( __CLASS__, 'get_guest_sign_in' ) );
100 add_action( 'wp_ajax_nopriv_wpsc_authenticate_guest_login', array( __CLASS__, 'get_guest_sign_in_auth' ) );
101 add_action( 'wp_ajax_nopriv_wpsc_confirm_guest_login', array( __CLASS__, 'confirm_guest_login' ) );
102
103 // user registration email template.
104 add_filter( 'wpsc_email_notification_page_sections', array( __CLASS__, 'registration_email_template_section' ) );
105
106 // guest login email template.
107 add_filter( 'wpsc_email_notification_page_sections', array( __CLASS__, 'guest_login_email_template_section' ) );
108 }
109
110 /**
111 * Initialize the object
112 *
113 * @param string $email - email address.
114 */
115 public function __construct( $email = '' ) {
116
117 $user = $email ? get_user_by( 'email', $email ) : new WP_User();
118 if ( $user === false ) {
119 $user = new WP_User();
120 }
121 $this->user = $user;
122
123 // is guest.
124 $this->is_guest = $this->user->ID ? false : true;
125
126 // Set customer object.
127 if ( $this->user->ID ) {
128
129 $this->is_customer = true;
130 $customer = WPSC_Customer::get_by_email( $this->user->user_email );
131 if ( $customer->id ) {
132 $this->customer = $customer;
133 } else {
134 $this->customer = WPSC_Customer::insert(
135 array(
136 'user' => $this->user->ID,
137 'name' => $this->user->display_name,
138 'email' => $this->user->user_email,
139 )
140 );
141 }
142 } elseif ( $email ) {
143
144 $this->is_customer = true;
145 $this->customer = WPSC_Customer::get_by_email( $email );
146 }
147
148 // Set agent object.
149 $agent = WPSC_Agent::get_by_user_id( $this->user->ID );
150 if ( $agent->id && $agent->is_active ) {
151 $this->is_agent = true;
152 $this->agent = $agent;
153 }
154
155 // set leval.
156 if ( WPSC_Functions::is_site_admin() ) {
157 $this->level = 'admin';
158 } elseif ( $this->is_agent ) {
159 $this->level = 'agent';
160 } elseif ( $this->is_customer ) {
161 $this->level = 'customer';
162 } else {
163 $this->level = 'none';
164 }
165 }
166
167 /**
168 * Load current wpsc user
169 *
170 * @return void
171 */
172 public static function load_current_user() {
173
174 global $current_user;
175
176 // wp logged-in user.
177 $email = $current_user && $current_user->ID ? $current_user->user_email : '';
178 if ( $email ) {
179 self::$current_user = new WPSC_Current_User( $email );
180 self::$login_type = 'registered';
181 return;
182 }
183
184 // guest login.
185 $gs = get_option( 'wpsc-gs-general' );
186
187 $login_auth = isset( $_COOKIE['wpsc_guest_login_auth'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['wpsc_guest_login_auth'] ) ) : '';
188 $login_auth = $login_auth ? json_decode( $login_auth ) : false;
189
190 if ( ! $login_auth ) {
191 self::$current_user = new WPSC_Current_User();
192 return;
193 }
194
195 $login_auth->email = $login_auth->email ? sanitize_email( $login_auth->email ) : '';
196 if ( ! $login_auth->email ) {
197 self::$current_user = new WPSC_Current_User();
198 return;
199 }
200
201 if ( $login_auth && self::validate_guest_login( $login_auth ) ) {
202 self::$current_user = new WPSC_Current_User( $login_auth->email );
203 return;
204 }
205
206 self::$current_user = new WPSC_Current_User();
207 }
208
209 /**
210 * Change current user
211 *
212 * @param string $email - email string.
213 *
214 * @return string
215 */
216 public static function change_current_user( $email ) {
217
218 $current_user = new WPSC_Current_User( $email );
219 self::$current_user = $current_user;
220 return self::$current_user;
221 }
222
223 /**
224 * Return ticket list filters for the user.
225 *
226 * @return array
227 */
228 public function get_tl_filters() {
229
230 $filters = array(
231 'default' => array(),
232 'saved' => array(),
233 );
234
235 // default filters.
236 $default_filters = get_option( $this->is_agent ? 'wpsc-atl-default-filters' : 'wpsc-ctl-default-filters' );
237 foreach ( $default_filters as $index => $filter ) {
238
239 // exclude if current user does not have access to deleted filter.
240 if ( $index == 'deleted' && ! $this->agent->has_cap( 'dtt-access' ) ) {
241 continue;
242 }
243
244 // exclude if filter is not enabled.
245 if ( ! $filter['is_enable'] ) {
246 continue;
247 }
248
249 $filters['default'][ $index ] = $filter;
250 }
251
252 // saved filters.
253 $filters['saved'] = $this->get_saved_filters();
254
255 // return filters.
256 return $filters;
257 }
258
259 /**
260 * Return all saved filters for current user
261 *
262 * @return array
263 */
264 public function get_saved_filters() {
265
266 $saved_filters = ! $this->is_guest && $this->user->ID ? get_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', true ) : array();
267 return $saved_filters ? $saved_filters : array();
268 }
269
270 /**
271 * Return attachment auth for URLs created in rest api
272 *
273 * @return string
274 */
275 public function get_attachment_auth() {
276
277 $now = new DateTime();
278 $diff = new DateInterval( 'PT1H' );
279
280 $auth = get_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-rest-attachment-auth', true );
281 if ( $auth ) {
282 $dt = new DateTime( $auth['date'] );
283 if ( $now < $dt->add( $diff ) ) {
284 return $auth['key'];
285 }
286 }
287
288 $auth = array(
289 'key' => WPSC_Functions::get_random_string( 12 ),
290 'date' => $now->format( 'Y-m-d H:i:s' ),
291 );
292 update_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-rest-attachment-auth', $auth );
293 return $auth['key'];
294 }
295
296 /**
297 * Get ticket list items
298 *
299 * @return array
300 */
301 public function get_tl_list_items() {
302
303 return $this->is_agent ? get_option( 'wpsc-atl-list-items' ) : get_option( 'wpsc-ctl-list-items' );
304 }
305
306 /**
307 * Get default orderby
308 *
309 * @return array
310 */
311 public function get_tl_default_settings() {
312
313 return $this->is_agent ? get_option( 'wpsc-tl-ms-agent-view' ) : get_option( 'wpsc-tl-ms-customer-view' );
314 }
315
316 /**
317 * Return system query for the current user for ticket list
318 *
319 * @param array $filters - filters.
320 * @return array
321 */
322 public function get_tl_system_query( $filters ) {
323
324 $current_user = self::$current_user;
325
326 $adv_setting = get_option( 'wpsc-ms-advanced-settings' );
327 if ( $adv_setting['public-mode'] && ! $current_user->is_agent ) {
328 return $filters;
329 }
330
331 $system_query = array( 'relation' => 'OR' );
332
333 $system_query[] = array(
334 'slug' => 'customer',
335 'compare' => '=',
336 'val' => $this->customer->id,
337 );
338
339 if ( $this->is_agent ) {
340
341 if ( $this->agent->has_cap( 'view-assigned-me' ) ) {
342 $system_query[] = array(
343 'slug' => 'assigned_agent',
344 'compare' => '=',
345 'val' => $this->agent->id,
346 );
347 }
348
349 if ( $this->agent->has_cap( 'view-unassigned' ) ) {
350 $system_query[] = array(
351 'slug' => 'assigned_agent',
352 'compare' => '=',
353 'val' => '',
354 );
355 }
356
357 if ( $this->agent->has_cap( 'view-assigned-others' ) ) {
358 $system_query[] = array(
359 'slug' => 'assigned_agent',
360 'compare' => 'NOT IN',
361 'val' => array( $this->agent->id, '' ),
362 );
363 }
364 }
365
366 return apply_filters( 'wpsc_tl_current_user_system_query', $system_query, $filters, $this );
367 }
368
369 /**
370 * Check login for default login form
371 *
372 * @return void
373 */
374 public static function check_user_login() {
375
376 if ( check_ajax_referer( 'wpsc_default_login', '_ajax_nonce', false ) != 1 ) {
377 wp_send_json_error( 'Unauthorised request!', 401 );
378 }
379
380 WPSC_MS_Recaptcha::validate( 'submit_login' );
381
382 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
383 if ( ! $username ) {
384 wp_send_json_error( 'Bad request', 400 );
385 }
386
387 $password = isset( $_POST['password'] ) ? $_POST['password'] : ''; // phpcs:ignore
388 if ( ! $password ) {
389 wp_send_json_error( 'Bad request', 400 );
390 }
391
392 $remember_me = isset( $_POST['remember_me'] ) ? true : false;
393
394 $user = wp_signon(
395 array(
396 'user_login' => $username,
397 'user_password' => $password,
398 'remember' => $remember_me,
399 )
400 );
401
402 $success = is_wp_error( $user ) ? 0 : 1;
403 wp_send_json( array( 'success' => $success ) );
404 }
405
406 /**
407 * Get user registration
408 *
409 * @return void
410 */
411 public static function get_user_registration() {
412
413 $page_settings = get_option( 'wpsc-gs-page-settings' );
414 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
415 $tc = get_option( 'wpsc-term-and-conditions' );
416 $gdpr = get_option( 'wpsc-gdpr-settings' );
417 if ( $page_settings['user-registration'] !== 'default' ) {
418 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
419 }?>
420
421 <h2><?php esc_attr_e( 'Please sign up', 'supportcandy' ); ?></h2>
422 <form onsubmit="return false;" class="wpsc-login wpsc-authenticate-registration">
423 <input type="text" name="firstname" placeholder="<?php esc_attr_e( 'First Name', 'supportcandy' ); ?>" autocomplete="off"/>
424 <input type="text" name="lastname" placeholder="<?php esc_attr_e( 'Last Name', 'supportcandy' ); ?>" autocomplete="off"/>
425
426 <div style="margin: 0 0 5px !important;">
427 <input id="wpsc-username" type="text" name="username" style="margin-bottom: 0px !important;" placeholder="<?php esc_attr_e( 'Username', 'supportcandy' ); ?>" autocomplete="off"/>
428 <small id="wpsc-username-unavailable" style="color: #e84118;font-style:italic;display:none;"><?php esc_attr_e( 'Username is already taken!', 'supportcandy' ); ?></small>
429 <small id="wpsc-username-available" style="color: #4cd137;font-style:italic;display:none;"><?php esc_attr_e( 'Username is available!', 'supportcandy' ); ?></small>
430 <script>
431 jQuery('#wpsc-username').change(function(){
432 jQuery('#wpsc-username-available').hide();
433 jQuery('#wpsc-username-unavailable').hide();
434 var username = jQuery(this).val().trim();
435 const data = { action: 'wpsc_check_username_availability', username, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_check_username_availability' ) ); ?>' };
436 jQuery.post(supportcandy.ajax_url, data, function (response) {
437 jQuery('input[name=is_username]').val(response.isAvailable);
438 if (response.isAvailable == 1) {
439 jQuery('#wpsc-username-unavailable').hide();
440 jQuery('#wpsc-username-available').show();
441 } else {
442 jQuery('#wpsc-username-available').hide();
443 jQuery('#wpsc-username-unavailable').show();
444 }
445 });
446 });
447 </script>
448 </div>
449
450 <input type="text" name="email_address" placeholder="<?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>" autocomplete="off"/>
451 <input type="password" name="password" placeholder="<?php esc_attr_e( 'Password', 'supportcandy' ); ?>"/>
452 <input type="password" name="confirm_password" placeholder="<?php esc_attr_e( 'Confirm Password', 'supportcandy' ); ?>"/>
453 <?php
454
455 // recaptcha.
456 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
457 $unique_id = uniqid( 'wpsc_' );
458 ?>
459 <script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" async defer></script> <?php // phpcs:ignore ?>
460 <div id="<?php echo esc_attr( $unique_id ); ?>" data-sitekey="" style="margin-bottom: 5px;"></div>
461 <script>
462 var recaptchaCallback = function() {
463 var obj = jQuery('#<?php echo esc_attr( $unique_id ); ?>');
464 grecaptcha.render(obj.attr("id"), {
465 "sitekey" : "<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>",
466 "callback" : function(token) {
467 obj.closest('form').find(".g-recaptcha-response").val(token);
468 }
469 });
470 }
471 </script>
472 <?php
473 }
474 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
475 ?>
476 <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>"></script> <?php // phpcs:ignore ?>
477 <?php
478 }
479 do_action( 'wpsc_registration_form' );
480 ?>
481 <div class="wpsc-reg-user">
482 <?php
483 if ( $tc['allow-term-and-conditions-reg-user'] ) :
484 ?>
485 <div class="wpsc-tff term-and-conditions wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12 required wpsc-visible" data-cft="term-and-conditions-reg-user">
486 <div class="checkbox-container">
487 <?php $unique_id = uniqid( 'wpsc_' ); ?>
488 <input name="wpsc-tandc-reg-user" id="<?php echo esc_attr( $unique_id ); ?>" type="checkbox" value="1"/>
489 <?php
490 $name = WPSC_Translations::get( 'wpsc-term-and-conditions-reg-user', stripslashes( $tc['tandc-text-reg-user'] ) );
491 ?>
492 <label for="<?php echo esc_attr( $unique_id ); ?>"><?php echo wp_kses_post( $name ); ?></label>
493 </div>
494 </div>
495 <?php
496 endif;
497
498 if ( $gdpr['allow-gdpr-reg-user'] ) {
499 ?>
500 <div class="wpsc-tff wpsc-gdpr wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12 required wpsc-visible" data-cft="gdpr-reg-user">
501 <div class="checkbox-container">
502 <?php $unique_id = uniqid( 'wpsc_' ); ?>
503 <input name="wpsc-gdpr-reg-user" id="<?php echo esc_attr( $unique_id ); ?>" type="checkbox" value="1"/>
504 <?php
505 $name = WPSC_Translations::get( 'wpsc-gdpr-reg-user', stripslashes( $gdpr['gdpr-text-reg-user'] ) );
506 ?>
507 <label for="<?php echo esc_attr( $unique_id ); ?>"><?php echo wp_kses_post( $name ); ?></label>
508 </div>
509 </div>
510 <?php
511 }
512 ?>
513 <div>
514
515 <button class="wpsc-button normal primary" onclick="wpsc_set_default_registration(this)"><?php esc_attr_e( 'Sign Up', 'supportcandy' ); ?></button>
516 <button class="wpsc-button normal secondary" onclick="window.location.reload();"><?php esc_attr_e( 'Cancel', 'supportcandy' ); ?></button>
517 <input type="hidden" name="action" value="wpsc_authenticate_registration"/>
518 <input type="hidden" name="is_username" value="0"/>
519 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_authenticate_registration' ) ); ?>">
520 </form>
521 <?php
522 wp_die();
523 }
524
525 /**
526 * Check username availability
527 *
528 * @return void
529 */
530 public static function check_username_availability() {
531
532 if ( check_ajax_referer( 'wpsc_check_username_availability', '_ajax_nonce', false ) != 1 ) {
533 wp_send_json_error( 'Unauthorised request!', 401 );
534 }
535
536 $page_settings = get_option( 'wpsc-gs-page-settings' );
537 if ( $page_settings['user-registration'] !== 'default' ) {
538 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
539 }
540
541 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
542 if ( ! $username ) {
543 wp_send_json_error( 'Something went wrong', 400 );
544 }
545
546 $flag = self::is_username_available( $username );
547
548 wp_send_json( array( 'isAvailable' => $flag ? 0 : 1 ) );
549 }
550
551 /**
552 * Send registration OTP for email authentication
553 *
554 * @return void
555 */
556 public static function send_registration_otp() {
557
558 if ( check_ajax_referer( 'wpsc_authenticate_registration', '_ajax_nonce', false ) != 1 ) {
559 wp_send_json_error( 'Unauthorised request!', 401 );
560 }
561 $page_settings = get_option( 'wpsc-gs-page-settings' );
562 if ( $page_settings['user-registration'] !== 'default' ) {
563 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
564 }
565
566 WPSC_MS_Recaptcha::validate( 'submit_registration' );
567
568 $firstname = isset( $_POST['firstname'] ) ? sanitize_text_field( wp_unslash( $_POST['firstname'] ) ) : '';
569 if ( ! $firstname ) {
570 wp_send_json_error( 'Bad request', 400 );
571 }
572
573 $lastname = isset( $_POST['lastname'] ) ? sanitize_text_field( wp_unslash( $_POST['lastname'] ) ) : '';
574 if ( ! $lastname ) {
575 wp_send_json_error( 'Bad request', 400 );
576 }
577
578 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
579 if ( ! $username ) {
580 wp_send_json_error( 'Bad request', 400 );
581 }
582
583 if ( self::is_username_available( $username ) ) {
584 wp_send_json_error( 'Bad request', 400 );
585 }
586
587 $email_address = isset( $_POST['email_address'] ) && filter_var( wp_unslash( $_POST['email_address'] ), FILTER_VALIDATE_EMAIL ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
588 if ( ! $email_address ) {
589 wp_send_json_error( 'Bad request', 400 );
590 }
591
592 $user = get_user_by( 'email', $email_address );
593 if ( $user ) {
594 wp_send_json_error( 'Bad request', 400 );
595 }
596
597 $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; // phpcs:ignore
598 if ( ! $password ) {
599 wp_send_json_error( 'Bad request', 400 );
600 }
601
602 $data = array(
603 'firstname' => $firstname,
604 'lastname' => $lastname,
605 'username' => $username,
606 'email_address' => $email_address,
607 'password' => $password,
608 );
609
610 $data = apply_filters( 'wpsc_register_user_data', $data );
611
612 $otp = WPSC_Email_OTP::insert(
613 array(
614 'email' => $email_address,
615 'date_expiry' => ( new DateTime() )->add( new DateInterval( 'PT1H' ) )->format( 'Y-m-d H:i:s' ),
616 'data' => wp_json_encode( $data ),
617 )
618 );
619
620 // send email notification.
621 WPSC_EN_User_Reg_OTP::send_otp( $otp );
622 ?>
623
624 <h2><?php esc_attr_e( 'Please sign up', 'supportcandy' ); ?></h2>
625 <small style="margin: 0 0 5px;"><?php esc_attr_e( 'We have sent a 6-digit one-time pass code to the email address you provided.', 'supportcandy' ); ?></small>
626 <form onsubmit="return false;" class="wpsc-login wpsc-confirm-registration">
627 <input type="text" name="otp" autocomplete="off"/>
628 <button class="wpsc-button normal primary" onclick="wpsc_confirm_registration(this)"><?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
629 <input type="hidden" name="action" value="wpsc_confirm_registration"/>
630 <input type="hidden" name="otp_id" value="<?php echo esc_attr( $otp->id ); ?>">
631 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_confirm_registration' ) ); ?>"/>
632 </form>
633 <?php
634 wp_die();
635 }
636
637 /**
638 * Checks whether username is available or not
639 *
640 * @param string $username - user name string.
641 * @return boolean
642 */
643 public static function is_username_available( $username ) {
644
645 $user = get_user_by( 'login', $username );
646 return $user ? true : false;
647 }
648
649 /**
650 * Register user after OTP matched
651 *
652 * @return void
653 */
654 public static function register_user() {
655
656 if ( check_ajax_referer( 'wpsc_confirm_registration', '_ajax_nonce', false ) != 1 ) {
657 wp_send_json_error( 'Unauthorised request!', 401 );
658 }
659
660 $page_settings = get_option( 'wpsc-gs-page-settings' );
661 if ( $page_settings['user-registration'] !== 'default' ) {
662 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
663 }
664
665 $verification_otp = isset( $_POST['otp'] ) ? intval( $_POST['otp'] ) : '';
666 if ( ! $verification_otp ) {
667 wp_send_json_error( 'Bad request', 400 );
668 }
669
670 $id = isset( $_POST['otp_id'] ) ? intval( $_POST['otp_id'] ) : '';
671 if ( ! $id ) {
672 wp_send_json_error( 'Bad request', 400 );
673 }
674
675 $otp = new WPSC_Email_OTP( $id );
676 if ( ! $otp->id ) {
677 wp_send_json_error( 'Bad request', 400 );
678 }
679
680 if ( ! $otp->is_valid( $verification_otp ) ) {
681 wp_send_json( array( 'isSuccess' => 0 ) );
682 wp_die();
683 }
684
685 $data = json_decode( $otp->data );
686
687 // insert user.
688 $display_name = $data->firstname . ' ' . $data->lastname;
689 $user_id = wp_insert_user(
690 array(
691 'user_login' => $data->username,
692 'user_pass' => $data->password,
693 'user_email' => $data->email_address,
694 'first_name' => $data->firstname,
695 'last_name' => $data->lastname,
696 'display_name' => $display_name,
697 'role' => 'subscriber',
698 )
699 );
700 if ( is_wp_error( $user_id ) ) {
701 wp_send_json( array( 'isSuccess' => 0 ) );
702 wp_die();
703 }
704
705 $user = wp_signon(
706 array(
707 'user_login' => $data->username,
708 'user_password' => $data->password,
709 )
710 );
711 wp_new_user_notification( $user_id, null, 'admin' );
712 do_action( 'wpsc_after_user_registration', $user, $data );
713 wp_send_json( array( 'isSuccess' => 1 ) );
714 }
715
716 /**
717 * User registrstion OTP email template section
718 *
719 * @param array $sections - section name.
720 * @return array
721 */
722 public static function registration_email_template_section( $sections ) {
723
724 $sections['registration-otp'] = array(
725 'slug' => 'registration_otp',
726 'icon' => 'unlock',
727 'label' => esc_attr__( 'User Registration OTP', 'supportcandy' ),
728 'callback' => 'wpsc_get_en_user_reg_otp',
729 );
730 return $sections;
731 }
732
733 /**
734 * Get guest sign in screen
735 *
736 * @return void
737 */
738 public static function get_guest_sign_in() {
739
740 $gs = get_option( 'wpsc-gs-general' );
741 $page_settings = get_option( 'wpsc-gs-page-settings' );
742 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
743 wp_send_json_error( 'Unauthorozed', 400 );
744 }
745 ?>
746
747 <h2><?php esc_attr_e( 'Please sign in', 'supportcandy' ); ?></h2>
748 <form onsubmit="return false;" class="wpsc-login authenticate-guest-login">
749 <input type="text" name="email_address" placeholder="<?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>" autocomplete="off"/>
750 <button class="wpsc-button normal primary" onclick="wpsc_authenticate_guest_login(this)"><?php esc_attr_e( 'Sign In', 'supportcandy' ); ?></button>
751 <button class="wpsc-button normal secondary" onclick="window.location.reload();"><?php esc_attr_e( 'Cancel', 'supportcandy' ); ?></button>
752 <input type="hidden" name="action" value="wpsc_authenticate_guest_login"/>
753 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_authenticate_guest_login' ) ); ?>">
754 </form>
755 <?php
756 wp_die();
757 }
758
759 /**
760 * Get OTP screen
761 *
762 * @return void
763 */
764 public static function get_guest_sign_in_auth() {
765
766 if ( check_ajax_referer( 'wpsc_authenticate_guest_login', '_ajax_nonce', false ) != 1 ) {
767 wp_send_json_error( 'Unauthorised request!', 401 );
768 }
769 $gs = get_option( 'wpsc-gs-general' );
770 $page_settings = get_option( 'wpsc-gs-page-settings' );
771 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
772 wp_send_json_error( 'Unauthorozed', 400 );
773 }
774
775 $email_address = isset( $_POST['email_address'] ) && filter_var( wp_unslash( $_POST['email_address'] ), FILTER_VALIDATE_EMAIL ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
776 if ( ! $email_address ) {
777 wp_send_json_error( 'Bad request', 400 );
778 }
779
780 $customer = WPSC_Customer::get_by_email( $email_address );
781 if ( ! $customer->id ) {
782 esc_attr_e( 'Invalid email address!', 'supportcandy' );
783 wp_die();
784 }
785
786 $otp = WPSC_Email_OTP::insert(
787 array(
788 'email' => $email_address,
789 'date_expiry' => ( new DateTime() )->add( new DateInterval( 'P1D' ) )->format( 'Y-m-d H:i:s' ),
790 'data' => wp_json_encode(
791 array(
792 'email' => $email_address,
793 )
794 ),
795 )
796 );
797
798 // Send OTP for login.
799 WPSC_EN_Guest_Login_OTP::send_otp( $otp );
800 ?>
801
802 <h2><?php esc_attr_e( 'Please sign in', 'supportcandy' ); ?></h2>
803 <small style="margin: 0 0 5px;"><?php esc_attr_e( 'We have sent a 6-digit one-time pass code to the email address you provided.', 'supportcandy' ); ?></small>
804 <form onsubmit="return false;" class="wpsc-login wpsc-confirm-guest-login">
805 <input type="text" name="otp" autocomplete="off"/>
806 <button class="wpsc-button normal primary" onclick="wpsc_confirm_guest_login(this)"><?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
807 <input type="hidden" name="action" value="wpsc_confirm_guest_login"/>
808 <input type="hidden" name="otp_id" value="<?php echo esc_attr( $otp->id ); ?>">
809 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_confirm_guest_login' ) ); ?>">
810 </form>
811 <?php
812 wp_die();
813 }
814
815 /**
816 * Confirm guest login
817 *
818 * @return void
819 */
820 public static function confirm_guest_login() {
821
822 if ( check_ajax_referer( 'wpsc_confirm_guest_login', '_ajax_nonce', false ) != 1 ) {
823 wp_send_json_error( 'Unauthorised request!', 401 );
824 }
825
826 $gs = get_option( 'wpsc-gs-general' );
827 $page_settings = get_option( 'wpsc-gs-page-settings' );
828 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
829 wp_send_json_error( 'Unauthorozed', 400 );
830 }
831
832 $verification_otp = isset( $_POST['otp'] ) ? intval( $_POST['otp'] ) : '';
833 if ( ! $verification_otp ) {
834 wp_send_json_error( 'Bad request', 400 );
835 }
836
837 $id = isset( $_POST['otp_id'] ) ? intval( $_POST['otp_id'] ) : '';
838 if ( ! $id ) {
839 wp_send_json_error( 'Bad request', 400 );
840 }
841
842 $otp = new WPSC_Email_OTP( $id );
843 if ( ! $otp->id ) {
844 wp_send_json_error( 'Bad request', 400 );
845 }
846
847 if ( ! $otp->is_valid( $verification_otp ) ) {
848 wp_send_json( array( 'isSuccess' => 0 ) );
849 wp_die();
850 }
851
852 $data = json_decode( $otp->data, true );
853 $data['auth_token'] = WPSC_Functions::get_random_string( 100 );
854 $data['auth_type'] = 'login';
855 $otp->data = wp_json_encode( $data );
856 $otp->save();
857
858 // add customer record if not set.
859 $customer = WPSC_Customer::get_by_email( $data['email'] );
860 if ( ! $customer->id ) {
861 $user = get_user_by( 'email', $data['email'] );
862 if ( $user ) {
863
864 WPSC_Customer::insert(
865 array(
866 'user' => $user->ID,
867 'name' => $user->display_name,
868 'email' => $user->user_email,
869 )
870 );
871
872 } else {
873
874 WPSC_Customer::insert(
875 array(
876 'user' => 0,
877 'name' => $data['name'],
878 'email' => $data['email'],
879 )
880 );
881 }
882 }
883
884 $auth = array(
885 'email' => $otp->email,
886 'token' => $data['auth_token'],
887 );
888
889 setcookie( 'wpsc_guest_login_auth', wp_json_encode( $auth ), $otp->date_expiry->getTimestamp(), '/' );
890
891 wp_send_json( array( 'isSuccess' => 1 ) );
892 }
893
894 /**
895 * Validate login auth token
896 *
897 * @param object $login_auth - login auth details.
898 * @return boolean
899 */
900 public static function validate_guest_login( $login_auth ) {
901
902 $gs = get_option( 'wpsc-gs-general' );
903 $page_settings = get_option( 'wpsc-gs-page-settings' );
904
905 $results = WPSC_Email_OTP::find(
906 array(
907 'meta_query' => array(
908 'relation' => 'AND',
909 array(
910 'slug' => 'email',
911 'compare' => '=',
912 'val' => $login_auth->email,
913 ),
914 ),
915 )
916 )['results'];
917
918 if ( ! $results ) {
919 return false;
920 }
921
922 $otp = $results[0];
923 if ( ! $otp->id ) {
924 return false;
925 }
926
927 $now = new DateTime();
928 $data = json_decode( $otp->data );
929
930 if (
931 isset( $data->auth_type ) &&
932 ( ( $data->auth_type == 'login' && $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) || $data->auth_type == 'open-ticket' ) &&
933 ( $otp->date_expiry > $now && $data->auth_token == $login_auth->token )
934 ) {
935 self::$login_type = 'guest';
936 self::$guest_login_type = $data->auth_type;
937 return true;
938 }
939
940 return false;
941 }
942
943 /**
944 * Add guest login email template
945 *
946 * @param array $sections - section name.
947 * @return array
948 */
949 public static function guest_login_email_template_section( $sections ) {
950
951 $sections['guest-login-otp'] = array(
952 'slug' => 'guest_login_otp',
953 'icon' => 'unlock',
954 'label' => esc_attr__( 'Guest Login OTP', 'supportcandy' ),
955 'callback' => 'wpsc_get_en_guest_login_otp',
956 );
957 return $sections;
958 }
959
960 /**
961 * Logout current user
962 *
963 * @return void
964 */
965 public function logout() {
966
967 global $current_user;
968
969 $otp = WPSC_Email_OTP::find(
970 array(
971 'meta_query' => array(
972 'relation' => 'AND',
973 array(
974 'slug' => 'email',
975 'compare' => '=',
976 'val' => $this->customer->email,
977 ),
978 ),
979 )
980 )['results'];
981
982 if ( $otp ) :
983 WPSC_Email_OTP::destroy( $otp[0] );
984 @setcookie( 'wpsc_guest_login_auth', '', time(), '/' ); //phpcs:ignore
985 endif;
986
987 if ( $current_user->ID ) {
988 wp_logout();
989 }
990 }
991 }
992 endif;
993
994 WPSC_Current_User::init();
995