PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
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.4.1, at includes/class-wpsc-current-user.php

1,070 lines 32.7 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_user_availability', array( __CLASS__, 'check_user_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_user_availability', type : 'username', username, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_check_user_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 <div style="margin: 0 0 5px !important;">
451 <input id="wpsc-email" type="text" name="email_address" style="margin-bottom: 0px !important;" placeholder="<?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>" autocomplete="off"/>
452 <small id="wpsc-email-unavailable" style="color: #e84118;font-style:italic;display:none;"><?php esc_attr_e( 'Email is already taken!', 'supportcandy' ); ?></small>
453 <small id="wpsc-email-available" style="color: #4cd137;font-style:italic;display:none;"><?php esc_attr_e( 'Email is available!', 'supportcandy' ); ?></small>
454 <script>
455 jQuery('#wpsc-email').change(function(){
456 console.log('email changed');
457 jQuery('#wpsc-email-available').hide();
458 jQuery('#wpsc-email-unavailable').hide();
459 var email = jQuery(this).val().trim();
460 const data = { action: 'wpsc_check_user_availability', type: 'email', email, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_check_user_availability' ) ); ?>' };
461 jQuery.post(supportcandy.ajax_url, data, function (response) {
462 jQuery('input[name=is_email]').val(response.isAvailable);
463 if (response.isAvailable == 1) {
464 jQuery('#wpsc-email-unavailable').hide();
465 jQuery('#wpsc-email-available').show();
466 } else {
467 jQuery('#wpsc-email-available').hide();
468 jQuery('#wpsc-email-unavailable').show();
469 }
470 });
471 });
472 </script>
473 </div>
474 <input type="password" name="password" placeholder="<?php esc_attr_e( 'Password', 'supportcandy' ); ?>"/>
475 <input type="password" name="confirm_password" placeholder="<?php esc_attr_e( 'Confirm Password', 'supportcandy' ); ?>"/>
476 <?php
477
478 // recaptcha.
479 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
480 $unique_id = uniqid( 'wpsc_' );
481 ?>
482 <script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" async defer></script> <?php // phpcs:ignore ?>
483 <div id="<?php echo esc_attr( $unique_id ); ?>" data-sitekey="" style="margin-bottom: 5px;"></div>
484 <script>
485 var recaptchaCallback = function() {
486 var obj = jQuery('#<?php echo esc_attr( $unique_id ); ?>');
487 grecaptcha.render(obj.attr("id"), {
488 "sitekey" : "<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>",
489 "callback" : function(token) {
490 obj.closest('form').find(".g-recaptcha-response").val(token);
491 }
492 });
493 }
494 </script>
495 <?php
496 }
497 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
498 ?>
499 <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>"></script> <?php // phpcs:ignore ?>
500 <?php
501 }
502 do_action( 'wpsc_registration_form' );
503 ?>
504 <div class="wpsc-reg-user">
505 <?php
506 if ( $tc['allow-term-and-conditions-reg-user'] ) :
507 ?>
508 <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">
509 <div class="checkbox-container">
510 <?php $unique_id = uniqid( 'wpsc_' ); ?>
511 <input name="wpsc-tandc-reg-user" id="<?php echo esc_attr( $unique_id ); ?>" type="checkbox" value="1"/>
512 <?php
513 $name = WPSC_Translations::get( 'wpsc-term-and-conditions-reg-user', stripslashes( $tc['tandc-text-reg-user'] ) );
514 ?>
515 <label for="<?php echo esc_attr( $unique_id ); ?>"><?php echo wp_kses_post( $name ); ?></label>
516 </div>
517 </div>
518 <?php
519 endif;
520
521 if ( $gdpr['allow-gdpr-reg-user'] ) {
522 ?>
523 <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">
524 <div class="checkbox-container">
525 <?php $unique_id = uniqid( 'wpsc_' ); ?>
526 <input name="wpsc-gdpr-reg-user" id="<?php echo esc_attr( $unique_id ); ?>" type="checkbox" value="1"/>
527 <?php
528 $name = WPSC_Translations::get( 'wpsc-gdpr-reg-user', stripslashes( $gdpr['gdpr-text-reg-user'] ) );
529 ?>
530 <label for="<?php echo esc_attr( $unique_id ); ?>"><?php echo wp_kses_post( $name ); ?></label>
531 </div>
532 </div>
533 <?php
534 }
535 ?>
536 <div>
537
538 <button class="wpsc-button normal primary" onclick="wpsc_set_default_registration(this)"><?php esc_attr_e( 'Sign Up', 'supportcandy' ); ?></button>
539 <button class="wpsc-button normal secondary" onclick="window.location.reload();"><?php esc_attr_e( 'Cancel', 'supportcandy' ); ?></button>
540 <input type="hidden" name="action" value="wpsc_authenticate_registration"/>
541 <input type="hidden" name="is_username" value="0"/>
542 <input type="hidden" name="is_email" value="0"/>
543 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_authenticate_registration' ) ); ?>">
544 </form>
545 <?php
546 wp_die();
547 }
548
549 /**
550 * Check username availability
551 *
552 * @return void
553 */
554 public static function check_user_availability() {
555
556 if ( check_ajax_referer( 'wpsc_check_user_availability', '_ajax_nonce', false ) != 1 ) {
557 wp_send_json_error( 'Unauthorised request!', 401 );
558 }
559
560 $page_settings = get_option( 'wpsc-gs-page-settings' );
561 if ( $page_settings['user-registration'] !== 'default' ) {
562 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
563 }
564
565 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
566 if ( in_array( $type, array( 'username', 'email' ) ) === false ) {
567 wp_send_json_error( 'Something went wrong', 400 );
568 }
569
570 if ( $type === 'username' ) {
571 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
572 if ( ! $username ) {
573 wp_send_json_error( 'Something went wrong', 400 );
574 }
575 } elseif ( $type === 'email' ) {
576 $email = isset( $_POST['email'] ) && filter_var( wp_unslash( $_POST['email'] ), FILTER_VALIDATE_EMAIL ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
577 if ( ! $email ) {
578 wp_send_json_error( 'Something went wrong', 400 );
579 }
580 }
581 $flag = $type === 'username' ? self::is_username_available( $username ) : self::is_email_available( $email );
582
583 wp_send_json( array( 'isAvailable' => $flag ? 0 : 1 ) );
584 }
585
586 /**
587 * Send registration OTP for email authentication
588 *
589 * @return void
590 */
591 public static function send_registration_otp() {
592
593 if ( check_ajax_referer( 'wpsc_authenticate_registration', '_ajax_nonce', false ) != 1 ) {
594 wp_send_json_error( 'Unauthorised request!', 401 );
595 }
596 $page_settings = get_option( 'wpsc-gs-page-settings' );
597 if ( $page_settings['user-registration'] !== 'default' ) {
598 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
599 }
600
601 WPSC_MS_Recaptcha::validate( 'submit_registration' );
602
603 $firstname = isset( $_POST['firstname'] ) ? sanitize_text_field( wp_unslash( $_POST['firstname'] ) ) : '';
604 if ( ! $firstname ) {
605 wp_send_json_error( 'Bad request', 400 );
606 }
607
608 $lastname = isset( $_POST['lastname'] ) ? sanitize_text_field( wp_unslash( $_POST['lastname'] ) ) : '';
609 if ( ! $lastname ) {
610 wp_send_json_error( 'Bad request', 400 );
611 }
612
613 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
614 if ( ! $username ) {
615 wp_send_json_error( 'Bad request', 400 );
616 }
617
618 if ( self::is_username_available( $username ) ) {
619 wp_send_json_error( 'Bad request', 400 );
620 }
621
622 $email_address = isset( $_POST['email_address'] ) && filter_var( wp_unslash( $_POST['email_address'] ), FILTER_VALIDATE_EMAIL ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
623 if ( ! $email_address ) {
624 wp_send_json_error( 'Bad request', 400 );
625 }
626
627 if ( self::is_email_available( $email_address ) ) {
628 wp_send_json_error( 'Bad request', 400 );
629 }
630
631 $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; // phpcs:ignore
632 if ( ! $password ) {
633 wp_send_json_error( 'Bad request', 400 );
634 }
635
636 $data = array(
637 'firstname' => $firstname,
638 'lastname' => $lastname,
639 'username' => $username,
640 'email_address' => $email_address,
641 'password' => $password,
642 );
643
644 $data = apply_filters( 'wpsc_register_user_data', $data );
645
646 $otp = WPSC_Email_OTP::insert(
647 array(
648 'email' => $email_address,
649 'date_expiry' => ( new DateTime() )->add( new DateInterval( 'PT1H' ) )->format( 'Y-m-d H:i:s' ),
650 'data' => wp_json_encode( $data ),
651 )
652 );
653
654 // send email notification.
655 WPSC_EN_User_Reg_OTP::send_otp( $otp );
656 ?>
657
658 <h2><?php esc_attr_e( 'Please sign up', 'supportcandy' ); ?></h2>
659 <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>
660 <form onsubmit="return false;" class="wpsc-login wpsc-confirm-registration">
661 <input type="text" name="otp" autocomplete="off"/>
662 <button class="wpsc-button normal primary" onclick="wpsc_confirm_registration(this)"><?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
663 <input type="hidden" name="action" value="wpsc_confirm_registration"/>
664 <input type="hidden" name="otp_id" value="<?php echo esc_attr( $otp->id ); ?>">
665 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_confirm_registration' ) ); ?>"/>
666 </form>
667 <?php
668 wp_die();
669 }
670
671 /**
672 * Checks whether username is available or not
673 *
674 * @param string $username - user name string.
675 * @return boolean
676 */
677 public static function is_username_available( $username ) {
678
679 $user = get_user_by( 'login', $username );
680 return $user ? true : false;
681 }
682
683 /**
684 * Checks whether email is available or not
685 *
686 * @param string $email - email string.
687 * @return boolean
688 */
689 public static function is_email_available( $email ) {
690
691 $user = get_user_by( 'email', $email );
692 return $user ? true : false;
693 }
694
695 /**
696 * Register user after OTP matched
697 *
698 * @return void
699 */
700 public static function register_user() {
701
702 if ( check_ajax_referer( 'wpsc_confirm_registration', '_ajax_nonce', false ) != 1 ) {
703 wp_send_json_error( 'Unauthorised request!', 401 );
704 }
705
706 $page_settings = get_option( 'wpsc-gs-page-settings' );
707 if ( $page_settings['user-registration'] !== 'default' ) {
708 wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 );
709 }
710
711 $verification_otp = isset( $_POST['otp'] ) ? sanitize_text_field( wp_unslash( $_POST['otp'] ) ) : '';
712 if ( ! $verification_otp ) {
713 wp_send_json_error( 'Bad request', 400 );
714 }
715
716 $id = isset( $_POST['otp_id'] ) ? intval( $_POST['otp_id'] ) : '';
717 if ( ! $id ) {
718 wp_send_json_error( 'Bad request', 400 );
719 }
720
721 $otp = new WPSC_Email_OTP( $id );
722 if ( ! $otp->id ) {
723 wp_send_json_error( 'Bad request', 400 );
724 }
725
726 if ( ! $otp->is_valid( $verification_otp ) ) {
727 wp_send_json( array( 'isSuccess' => 0 ) );
728 wp_die();
729 }
730
731 $data = json_decode( $otp->data );
732
733 // insert user.
734 $display_name = $data->firstname . ' ' . $data->lastname;
735 $user_id = wp_insert_user(
736 array(
737 'user_login' => $data->username,
738 'user_pass' => $data->password,
739 'user_email' => $data->email_address,
740 'first_name' => $data->firstname,
741 'last_name' => $data->lastname,
742 'display_name' => $display_name,
743 'role' => 'subscriber',
744 )
745 );
746 if ( is_wp_error( $user_id ) ) {
747 wp_send_json( array( 'isSuccess' => 0 ) );
748 wp_die();
749 }
750
751 $user = wp_signon(
752 array(
753 'user_login' => $data->username,
754 'user_password' => $data->password,
755 )
756 );
757 wp_new_user_notification( $user_id, null, 'admin' );
758 do_action( 'wpsc_after_user_registration', $user, $data );
759 wp_send_json( array( 'isSuccess' => 1 ) );
760 }
761
762 /**
763 * User registrstion OTP email template section
764 *
765 * @param array $sections - section name.
766 * @return array
767 */
768 public static function registration_email_template_section( $sections ) {
769
770 $sections['registration-otp'] = array(
771 'slug' => 'registration_otp',
772 'icon' => 'unlock',
773 'label' => esc_attr__( 'User Registration OTP', 'supportcandy' ),
774 'callback' => 'wpsc_get_en_user_reg_otp',
775 );
776 return $sections;
777 }
778
779 /**
780 * Get guest sign in screen
781 *
782 * @return void
783 */
784 public static function get_guest_sign_in() {
785
786 $gs = get_option( 'wpsc-gs-general' );
787 $page_settings = get_option( 'wpsc-gs-page-settings' );
788 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
789 wp_send_json_error( 'Unauthorozed', 400 );
790 }
791 ?>
792
793 <h2><?php esc_attr_e( 'Please sign in', 'supportcandy' ); ?></h2>
794 <form onsubmit="return false;" class="wpsc-login authenticate-guest-login">
795 <input type="text" name="email_address" placeholder="<?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>" autocomplete="off"/>
796 <button class="wpsc-button normal primary" onclick="wpsc_authenticate_guest_login(this)"><?php esc_attr_e( 'Sign In', 'supportcandy' ); ?></button>
797 <button class="wpsc-button normal secondary" onclick="window.location.reload();"><?php esc_attr_e( 'Cancel', 'supportcandy' ); ?></button>
798 <input type="hidden" name="action" value="wpsc_authenticate_guest_login"/>
799 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_authenticate_guest_login' ) ); ?>">
800 </form>
801 <?php
802 wp_die();
803 }
804
805 /**
806 * Get OTP screen
807 *
808 * @return void
809 */
810 public static function get_guest_sign_in_auth() {
811
812 if ( check_ajax_referer( 'wpsc_authenticate_guest_login', '_ajax_nonce', false ) != 1 ) {
813 wp_send_json_error( 'Unauthorised request!', 401 );
814 }
815 $gs = get_option( 'wpsc-gs-general' );
816 $page_settings = get_option( 'wpsc-gs-page-settings' );
817 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
818 wp_send_json_error( 'Unauthorozed', 400 );
819 }
820
821 $email_address = isset( $_POST['email_address'] ) && filter_var( wp_unslash( $_POST['email_address'] ), FILTER_VALIDATE_EMAIL ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
822 if ( ! $email_address ) {
823 wp_send_json_error( 'Bad request', 400 );
824 }
825
826 $customer = WPSC_Customer::get_by_email( $email_address );
827 if ( ! $customer->id ) {
828 esc_attr_e( 'Invalid email address!', 'supportcandy' );
829 wp_die();
830 }
831
832 $otp = WPSC_Email_OTP::insert(
833 array(
834 'email' => $email_address,
835 'date_expiry' => ( new DateTime() )->add( new DateInterval( 'P1D' ) )->format( 'Y-m-d H:i:s' ),
836 'data' => wp_json_encode(
837 array(
838 'email' => $email_address,
839 )
840 ),
841 )
842 );
843
844 // Send OTP for login.
845 WPSC_EN_Guest_Login_OTP::send_otp( $otp );
846 ?>
847
848 <h2><?php esc_attr_e( 'Please sign in', 'supportcandy' ); ?></h2>
849 <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>
850 <form onsubmit="return false;" class="wpsc-login wpsc-confirm-guest-login">
851 <input type="text" name="otp" autocomplete="off"/>
852 <button class="wpsc-button normal primary" onclick="wpsc_confirm_guest_login(this)"><?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
853 <input type="hidden" name="action" value="wpsc_confirm_guest_login"/>
854 <input type="hidden" name="otp_id" value="<?php echo esc_attr( $otp->id ); ?>">
855 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_confirm_guest_login' ) ); ?>">
856 </form>
857 <?php
858 wp_die();
859 }
860
861 /**
862 * Confirm guest login
863 *
864 * @return void
865 */
866 public static function confirm_guest_login() {
867
868 // Add rate limiting.
869 $ip_address = WPSC_DF_IP_Address::get_current_user_ip();
870 $attempt_key = 'wpsc_otp_attempts_' . md5( $ip_address );
871 $attempts = get_transient( $attempt_key );
872 $attempts = $attempts ? $attempts : 1;
873
874 if ( $attempts >= 5 ) {
875 wp_send_json_error( 'Too many attempts. Please try again later.', 429 );
876 }
877
878 if ( check_ajax_referer( 'wpsc_confirm_guest_login', '_ajax_nonce', false ) != 1 ) {
879 wp_send_json_error( 'Unauthorised request!', 401 );
880 }
881
882 $gs = get_option( 'wpsc-gs-general' );
883 $page_settings = get_option( 'wpsc-gs-page-settings' );
884 if ( ! ( $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) ) {
885 wp_send_json_error( 'Unauthorozed', 400 );
886 }
887
888 $verification_otp = isset( $_POST['otp'] ) ? sanitize_text_field( wp_unslash( $_POST['otp'] ) ) : '';
889 if ( ! $verification_otp ) {
890 wp_send_json_error( 'Bad request', 400 );
891 }
892
893 $id = isset( $_POST['otp_id'] ) ? intval( $_POST['otp_id'] ) : '';
894 if ( ! $id ) {
895 wp_send_json_error( 'Bad request', 400 );
896 }
897
898 $otp = new WPSC_Email_OTP( $id );
899 if ( ! $otp->id ) {
900 wp_send_json_error( 'Bad request', 400 );
901 }
902
903 if ( ! $otp->is_valid( $verification_otp ) ) {
904
905 // Increment attempt counter.
906 ++$attempts;
907 set_transient( $attempt_key, $attempts, 300 ); // 5 minute lockout.
908
909 // Add per-OTP attempt tracking.
910 $otp_attempt_key = 'wpsc_otp_' . $id . '_attempts';
911 $otp_attempts = get_transient( $otp_attempt_key );
912 $otp_attempts = $otp_attempts ? $otp_attempts + 1 : 1;
913 set_transient( $otp_attempt_key, $otp_attempts, 600 );
914
915 if ( $otp_attempts >= 3 ) {
916 WPSC_Email_OTP::destroy( $otp );
917 wp_send_json_error( 'OTP has been invalidated due to too many failed attempts', 403 );
918 }
919
920 wp_send_json( array( 'isSuccess' => 0 ) );
921 wp_die();
922 }
923
924 $data = json_decode( $otp->data, true );
925 $data['auth_token'] = WPSC_Functions::get_random_string( 100 );
926 $data['auth_type'] = 'login';
927 $otp->data = wp_json_encode( $data );
928 $otp->save();
929
930 // Clear rate limiting on success.
931 delete_transient( $attempt_key );
932
933 // add customer record if not set.
934 $customer = WPSC_Customer::get_by_email( $data['email'] );
935 if ( ! $customer->id ) {
936 $user = get_user_by( 'email', $data['email'] );
937 if ( $user ) {
938
939 WPSC_Customer::insert(
940 array(
941 'user' => $user->ID,
942 'name' => $user->display_name,
943 'email' => $user->user_email,
944 )
945 );
946
947 } else {
948
949 WPSC_Customer::insert(
950 array(
951 'user' => 0,
952 'name' => $data['name'],
953 'email' => $data['email'],
954 )
955 );
956 }
957 }
958
959 $auth = array(
960 'email' => $otp->email,
961 'token' => $data['auth_token'],
962 );
963
964 setcookie( 'wpsc_guest_login_auth', wp_json_encode( $auth ), $otp->date_expiry->getTimestamp(), '/' );
965
966 wp_send_json( array( 'isSuccess' => 1 ) );
967 }
968
969 /**
970 * Validate login auth token
971 *
972 * @param object $login_auth - login auth details.
973 * @return boolean
974 */
975 public static function validate_guest_login( $login_auth ) {
976
977 $gs = get_option( 'wpsc-gs-general' );
978 $page_settings = get_option( 'wpsc-gs-page-settings' );
979
980 $results = WPSC_Email_OTP::find(
981 array(
982 'meta_query' => array(
983 'relation' => 'AND',
984 array(
985 'slug' => 'email',
986 'compare' => '=',
987 'val' => $login_auth->email,
988 ),
989 ),
990 )
991 )['results'];
992
993 if ( ! $results ) {
994 return false;
995 }
996
997 $otp = $results[0];
998 if ( ! $otp->id ) {
999 return false;
1000 }
1001
1002 $now = new DateTime();
1003 $data = json_decode( $otp->data );
1004
1005 if (
1006 isset( $data->auth_type ) &&
1007 ( ( $data->auth_type == 'login' && $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) || $data->auth_type == 'open-ticket' ) &&
1008 ( $otp->date_expiry > $now && $data->auth_token == $login_auth->token )
1009 ) {
1010 self::$login_type = 'guest';
1011 self::$guest_login_type = $data->auth_type;
1012 return true;
1013 }
1014
1015 return false;
1016 }
1017
1018 /**
1019 * Add guest login email template
1020 *
1021 * @param array $sections - section name.
1022 * @return array
1023 */
1024 public static function guest_login_email_template_section( $sections ) {
1025
1026 $sections['guest-login-otp'] = array(
1027 'slug' => 'guest_login_otp',
1028 'icon' => 'unlock',
1029 'label' => esc_attr__( 'Guest Login OTP', 'supportcandy' ),
1030 'callback' => 'wpsc_get_en_guest_login_otp',
1031 );
1032 return $sections;
1033 }
1034
1035 /**
1036 * Logout current user
1037 *
1038 * @return void
1039 */
1040 public function logout() {
1041
1042 global $current_user;
1043
1044 $otp = WPSC_Email_OTP::find(
1045 array(
1046 'meta_query' => array(
1047 'relation' => 'AND',
1048 array(
1049 'slug' => 'email',
1050 'compare' => '=',
1051 'val' => $this->customer->email,
1052 ),
1053 ),
1054 )
1055 )['results'];
1056
1057 if ( $otp ) :
1058 WPSC_Email_OTP::destroy( $otp[0] );
1059 @setcookie( 'wpsc_guest_login_auth', '', time(), '/' ); //phpcs:ignore
1060 endif;
1061
1062 if ( $current_user->ID ) {
1063 wp_logout();
1064 }
1065 }
1066 }
1067 endif;
1068
1069 WPSC_Current_User::init();
1070