PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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
← All changes | includes/class-wpsc-current-user.php +122 -23 3.4.6 → 3.5.4 View file →
@@ -164,8 +164,24 @@
164 164 }
165 165 }
166 166
167 167 /**
168 + * Return customer object by WordPress user ID.
169 + *
170 + * @param int $user_id - WordPress user ID.
171 + * @return WPSC_Customer
172 + */
173 + public static function get_customer_by_user_id( $user_id ) {
174 +
175 + $user_id = absint( $user_id );
176 + if ( ! $user_id ) {
177 + return new WPSC_Customer();
178 + }
179 +
180 + return WPSC_Customer::get_by_user_id( $user_id );
181 + }
182 +
183 + /**
168 184 * Load current wpsc user
169 185 *
170 186 * @return void
171 187 */
@@ -170,12 +186,20 @@
170 186 * @return void
171 187 */
172 188 public static function load_current_user() {
173 189
174 - global $current_user;
190 + // Use wp_get_current_user() rather than the raw global - the
191 + // global is only populated once something has actually called
192 + // wp_get_current_user() (or a function that does, like
193 + // is_user_logged_in()) earlier in the request. Nothing guarantees
194 + // that has happened yet by the 'init' hook (where this runs), so
195 + // reading the raw global directly can see an empty, not-yet-resolved
196 + // WP_User and wrongly treat a logged-in visitor as a guest for the
197 + // rest of the request.
198 + $wp_user = wp_get_current_user();
175 199
176 200 // wp logged-in user.
177 - $email = $current_user && $current_user->ID ? $current_user->user_email : '';
201 + $email = $wp_user && $wp_user->ID ? $wp_user->user_email : '';
178 202 if ( $email ) {
179 203 self::$current_user = new WPSC_Current_User( $email );
180 204 self::$login_type = 'registered';
181 205 return;
@@ -186,14 +210,14 @@
186 210
187 211 $login_auth = isset( $_COOKIE['wpsc_guest_login_auth'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['wpsc_guest_login_auth'] ) ) : '';
188 212 $login_auth = $login_auth ? json_decode( $login_auth ) : false;
189 213
190 - if ( ! $login_auth ) {
214 + if ( ! $login_auth || ! is_object( $login_auth ) || ! isset( $login_auth->token ) || ! is_string( $login_auth->token ) ) {
191 215 self::$current_user = new WPSC_Current_User();
192 216 return;
193 217 }
194 218
195 - $login_auth->email = $login_auth->email ? sanitize_email( $login_auth->email ) : '';
219 + $login_auth->email = isset( $login_auth->email ) && is_string( $login_auth->email ) ? sanitize_email( $login_auth->email ) : '';
196 220 if ( ! $login_auth->email ) {
197 221 self::$current_user = new WPSC_Current_User();
198 222 return;
199 223 }
@@ -525,9 +549,8 @@
525 549 <small id="wpsc-email-unavailable" style="color: #e84118;font-style:italic;display:none;"><?php esc_attr_e( 'Email is already taken or not allowed!', 'supportcandy' ); ?></small>
526 550 <small id="wpsc-email-available" style="color: #4cd137;font-style:italic;display:none;"><?php esc_attr_e( 'Email is available!', 'supportcandy' ); ?></small>
527 551 <script>
528 552 jQuery('#wpsc-email').change(function(){
529 - console.log('email changed');
530 553 jQuery('#wpsc-email-available').hide();
531 554 jQuery('#wpsc-email-unavailable').hide();
532 555 var email = jQuery(this).val().trim();
533 556 const data = { action: 'wpsc_check_user_availability', type: 'email', email, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_check_user_availability' ) ); ?>' };
@@ -548,31 +571,104 @@
548 571 <input type="password" name="confirm_password" placeholder="<?php esc_attr_e( 'Confirm Password', 'supportcandy' ); ?>"/>
549 572 <?php
550 573
551 574 // recaptcha.
552 - if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
575 + if ( $recaptcha['captcha-provider'] === 'google-recaptcha' && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
553 576 $unique_id = uniqid( 'wpsc_' );
554 577 ?>
555 - <script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" async defer></script> <?php // phpcs:ignore ?>
556 - <div id="<?php echo esc_attr( $unique_id ); ?>" data-sitekey="" style="margin-bottom: 5px;"></div>
557 - <script>
558 - var recaptchaCallback = function() {
559 - var obj = jQuery('#<?php echo esc_attr( $unique_id ); ?>');
560 - grecaptcha.render(obj.attr("id"), {
561 - "sitekey" : "<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>",
562 - "callback" : function(token) {
563 - obj.closest('form').find(".g-recaptcha-response").val(token);
564 - }
565 - });
566 - }
567 - </script>
578 + <script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" async defer></script> <?php // phpcs:ignore ?>
579 + <div id="<?php echo esc_attr( $unique_id ); ?>" data-sitekey="" style="margin-bottom: 5px;"></div>
580 + <script>
581 + var recaptchaCallback = function() {
582 + var obj = jQuery('#<?php echo esc_attr( $unique_id ); ?>');
583 + grecaptcha.render(obj.attr("id"), {
584 + "sitekey" : "<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>",
585 + "callback" : function(token) {
586 + obj.closest('form').find(".g-recaptcha-response").val(token);
587 + }
588 + });
589 + }
590 + </script>
568 591 <?php
569 592 }
570 - if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
593 + if ( $recaptcha['captcha-provider'] === 'google-recaptcha' && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
571 594 ?>
572 595 <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>"></script> <?php // phpcs:ignore ?>
573 596 <?php
574 597 }
598 + if ( $recaptcha['captcha-provider'] === 'cloudflare-turnstile' && $recaptcha['cloudflare-site-key'] && $recaptcha['cloudflare-secret-key'] ) {
599 + ?>
600 + <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script> <?php // phpcs:ignore ?>
601 + <div class="wpsc-tff turnstile wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12 required wpsc-visible" data-cft="turnstile">
602 + <div class="cf-turnstile" data-sitekey="<?php echo esc_attr( $recaptcha['cloudflare-site-key'] ); ?>"></div>
603 + </div>
604 + <script>
605 + jQuery(document).ready(function() {
606 +
607 + function wpscInitTurnstileWidgets() {
608 +
609 + if (
610 + typeof window.turnstile === 'undefined' ||
611 + typeof window.turnstile.render !== 'function'
612 + ) {
613 + return false;
614 + }
615 +
616 + jQuery('.cf-turnstile').each(function() {
617 +
618 + var widget = jQuery(this);
619 +
620 + if (
621 + widget.find('input[name="cf-turnstile-response"]').length ||
622 + widget.find('iframe').length ||
623 + widget.attr('data-wpsc-rendered') === '1'
624 + ) {
625 +
626 + widget.attr('data-wpsc-rendered', '1');
627 +
628 + return;
629 + }
630 +
631 + window.turnstile.render(
632 + this,
633 + {
634 + sitekey: widget.data('sitekey')
635 + }
636 + );
637 +
638 + widget.attr('data-wpsc-rendered', '1');
639 + });
640 +
641 + return true;
642 + }
643 +
644 + if (wpscInitTurnstileWidgets()) {
645 + return;
646 + }
647 +
648 + var attempts = 0;
649 +
650 + var timer = setInterval(
651 + function() {
652 +
653 + attempts++;
654 +
655 + if (
656 + wpscInitTurnstileWidgets() ||
657 + attempts > 50
658 + ) {
659 +
660 + clearInterval(timer);
661 + }
662 +
663 + },
664 + 100
665 + );
666 +
667 + });
668 + </script>
669 + <?php
670 + }
575 671 do_action( 'wpsc_registration_form' );
576 672 ?>
577 673 <div class="wpsc-reg-user">
578 674 <?php
@@ -605,9 +701,9 @@
605 701 </div>
606 702 <?php
607 703 }
608 704 ?>
609 - <div>
705 + </div>
610 706
611 707 <button class="wpsc-button normal primary" onclick="wpsc_set_default_registration(this)"><?php esc_attr_e( 'Sign Up', 'supportcandy' ); ?></button>
612 708 <button class="wpsc-button normal secondary" onclick="window.location.reload();"><?php esc_attr_e( 'Cancel', 'supportcandy' ); ?></button>
613 709 <input type="hidden" name="action" value="wpsc_authenticate_registration"/>
@@ -1095,11 +1191,14 @@
1095 1191 $now = new DateTime();
1096 1192 $data = json_decode( $otp->data );
1097 1193
1098 1194 if (
1099 - isset( $data->auth_type ) &&
1195 + isset( $data->auth_type, $data->auth_token, $login_auth->token ) &&
1196 + is_string( $data->auth_token ) && $data->auth_token !== '' &&
1197 + is_string( $login_auth->token ) &&
1100 1198 ( ( $data->auth_type == 'login' && $page_settings['otp-login'] && in_array( 'guest', $gs['allow-create-ticket'] ) ) || $data->auth_type == 'open-ticket' ) &&
1101 - ( $otp->date_expiry > $now && $data->auth_token == $login_auth->token )
1199 + $otp->date_expiry > $now &&
1200 + hash_equals( $data->auth_token, $login_auth->token )
1102 1201 ) {
1103 1202 self::$login_type = 'guest';
1104 1203 self::$guest_login_type = $data->auth_type;
1105 1204 return true;