| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Current_User_Profile' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Current_User_Profile { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
*/ |
| 13 |
public static function init() { |
| 14 |
|
| 15 |
// Get agent settings ajax request. |
| 16 |
add_action( 'wp_ajax_wpsc_get_user_profile', array( __CLASS__, 'get_user_profile' ) ); |
| 17 |
add_action( 'wp_ajax_nopriv_wpsc_get_user_profile', array( __CLASS__, 'get_user_profile' ) ); |
| 18 |
add_action( 'wp_ajax_wpsc_set_my_profile', array( __CLASS__, 'set_my_profile' ) ); |
| 19 |
add_action( 'wp_ajax_nopriv_wpsc_set_my_profile', array( __CLASS__, 'set_my_profile' ) ); |
| 20 |
|
| 21 |
// User logout. |
| 22 |
add_action( 'wp_ajax_wpsc_user_logout', array( __CLASS__, 'logout' ) ); |
| 23 |
add_action( 'wp_ajax_nopriv_wpsc_user_logout', array( __CLASS__, 'logout' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* New ticket ajax callback |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public static function get_user_profile() { |
| 32 |
|
| 33 |
$current_user = WPSC_Current_User::$current_user; |
| 34 |
?> |
| 35 |
<form class="wpsc-my-profile" onsubmit="return false;" action="#"> |
| 36 |
<?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'name' ); ?> |
| 37 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 38 |
<div class="wpsc-up-container"> |
| 39 |
<div class="wpsc-up-picture img"> |
| 40 |
<?php echo get_avatar( $current_user->customer->email, 50 ); ?> |
| 41 |
</div> |
| 42 |
<div class="wpsc-up-info"> |
| 43 |
<div class="wpsc-up-name"><?php echo esc_attr( $current_user->customer->name ); ?></div> |
| 44 |
<div class="wpsc-up-email"><?php echo esc_attr( $current_user->customer->email ); ?></div> |
| 45 |
</div> |
| 46 |
</div> |
| 47 |
</div> |
| 48 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 49 |
<div class="wpsc-tff-label"> |
| 50 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 51 |
</div> |
| 52 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 53 |
<input |
| 54 |
type="text" |
| 55 |
name="<?php echo esc_attr( $cf->slug ); ?>" |
| 56 |
value="<?php echo esc_attr( $current_user->customer->name ); ?>" |
| 57 |
placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>" |
| 58 |
autocomplete="off"/> |
| 59 |
</div> |
| 60 |
<?php |
| 61 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 62 |
if ( $cf->field !== 'customer' || ! $cf->allow_my_profile || in_array( $cf->slug, WPSC_DF_Customer::$ignore_customer_info_cft ) ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
$properties = array( |
| 66 |
'is-required' => 0, |
| 67 |
'width' => 'full', |
| 68 |
'visibility' => '', |
| 69 |
); |
| 70 |
echo $cf->type::print_tff( $cf, $properties ); // phpcs:ignore |
| 71 |
} |
| 72 |
do_action( 'wpsc_my_profile' ) |
| 73 |
?> |
| 74 |
<input type="hidden" name="action" value="wpsc_set_my_profile"/> |
| 75 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_my_profile' ) ); ?>"> |
| 76 |
</form> |
| 77 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 78 |
<div class="submit-container"> |
| 79 |
<button class="wpsc-button normal primary" onclick="wpsc_submit_my_profile(this);"><?php esc_attr_e( 'Save Changes', 'supportcandy' ); ?></button> |
| 80 |
<button class="wpsc-button normal secondary" onclick="wpsc_user_logout(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_user_logout' ) ); ?>')"><?php esc_attr_e( 'Logout', 'supportcandy' ); ?></button> |
| 81 |
</div> |
| 82 |
</div> |
| 83 |
<script> |
| 84 |
|
| 85 |
/** |
| 86 |
* Submit My Profile form |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
function wpsc_submit_my_profile(el) { |
| 91 |
|
| 92 |
if (wpsc_is_description_text()) { |
| 93 |
if (!confirm(supportcandy.translations.warning_message)) { |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
var dataform = new FormData(jQuery('form.wpsc-my-profile')[0]); |
| 99 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 100 |
|
| 101 |
<?php |
| 102 |
$recaptcha = get_option( 'wpsc-recaptcha-settings' ); |
| 103 |
if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) { |
| 104 |
?> |
| 105 |
grecaptcha.ready(function() { |
| 106 |
grecaptcha.execute('<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>', {action: 'submit_my_profile'}).then(function(token) { |
| 107 |
dataform.append('g-recaptcha-response', token); |
| 108 |
wpsc_post_my_profile_form(dataform); |
| 109 |
}); |
| 110 |
}); |
| 111 |
<?php |
| 112 |
} else { |
| 113 |
?> |
| 114 |
wpsc_post_my_profile_form(dataform); |
| 115 |
<?php |
| 116 |
} |
| 117 |
?> |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Post my profile form |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
function wpsc_post_my_profile_form(dataform) { |
| 126 |
|
| 127 |
jQuery.ajax({ |
| 128 |
url: supportcandy.ajax_url, |
| 129 |
type: 'POST', |
| 130 |
data: dataform, |
| 131 |
processData: false, |
| 132 |
contentType: false, |
| 133 |
error: function (res) { |
| 134 |
wpsc_get_user_profile(); |
| 135 |
}, |
| 136 |
success: function (res, textStatus, xhr) { |
| 137 |
wpsc_get_user_profile(); |
| 138 |
} |
| 139 |
}); |
| 140 |
} |
| 141 |
<?php do_action( 'wpsc_js_my_profile_functions' ); ?> |
| 142 |
</script> |
| 143 |
<?php |
| 144 |
wp_die(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Set my profile |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public static function set_my_profile() { |
| 153 |
|
| 154 |
if ( check_ajax_referer( 'wpsc_set_my_profile', '_ajax_nonce', false ) != 1 ) { |
| 155 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 156 |
} |
| 157 |
|
| 158 |
WPSC_MS_Recaptcha::validate( 'submit_my_profile' ); |
| 159 |
|
| 160 |
$current_user = WPSC_Current_User::$current_user; |
| 161 |
if ( ! $current_user->is_customer ) { |
| 162 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 163 |
} |
| 164 |
|
| 165 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 166 |
if ( ! $name ) { |
| 167 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 168 |
} |
| 169 |
|
| 170 |
if ( $current_user->customer->name != $name ) { |
| 171 |
$current_user->customer->name = $name; |
| 172 |
$current_user->customer->save(); |
| 173 |
// Update WP User if available. |
| 174 |
if ( $current_user->user ) { |
| 175 |
wp_update_user( |
| 176 |
array( |
| 177 |
'ID' => $current_user->user->ID, |
| 178 |
'display_name' => $name, |
| 179 |
) |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$cfs = array(); |
| 185 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 186 |
|
| 187 |
if ( $cf->field !== 'customer' || $cf->type::$is_default || ! $cf->allow_my_profile ) { |
| 188 |
continue; |
| 189 |
} |
| 190 |
$cfs[ $cf->type::$slug ][] = $cf; |
| 191 |
} |
| 192 |
|
| 193 |
foreach ( $cfs as $slug => $fields ) { |
| 194 |
WPSC_Functions::$ref_classes[ $slug ]['class']::set_create_ticket_data( array( 'customer' => $current_user->customer->id ), $cfs, true ); |
| 195 |
} |
| 196 |
|
| 197 |
wp_die(); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Logout current user |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
public static function logout() { |
| 206 |
|
| 207 |
if ( check_ajax_referer( 'wpsc_user_logout', '_ajax_nonce', false ) != 1 ) { |
| 208 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 209 |
} |
| 210 |
|
| 211 |
$current_user = WPSC_Current_User::$current_user; |
| 212 |
$current_user->logout(); |
| 213 |
wp_die(); |
| 214 |
} |
| 215 |
} |
| 216 |
endif; |
| 217 |
|
| 218 |
WPSC_Current_User_Profile::init(); |
| 219 |
|