| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_New_Ticket' ) ) : |
| 7 |
|
| 8 |
final class WPSC_New_Ticket { |
| 9 |
|
| 10 |
/** |
| 11 |
* Set whether or not ticket form has customer fields |
| 12 |
* |
| 13 |
* @var boolean |
| 14 |
*/ |
| 15 |
public static $has_customer_fields = false; |
| 16 |
|
| 17 |
/** |
| 18 |
* New ticket actions |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private static $new_ticket_actions = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize this class |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
|
| 29 |
// ticket form. |
| 30 |
add_action( 'wp_ajax_wpsc_get_ticket_form', array( __CLASS__, 'get_ticket_form' ) ); |
| 31 |
add_action( 'wp_ajax_nopriv_wpsc_get_ticket_form', array( __CLASS__, 'get_ticket_form' ) ); |
| 32 |
add_action( 'wp_ajax_wpsc_set_ticket_form', array( __CLASS__, 'set_ticket_form' ) ); |
| 33 |
add_action( 'wp_ajax_nopriv_wpsc_set_ticket_form', array( __CLASS__, 'set_ticket_form' ) ); |
| 34 |
|
| 35 |
// Create as. |
| 36 |
add_action( 'wp_ajax_wpsc_get_change_create_as', array( __CLASS__, 'get_change_create_as' ) ); |
| 37 |
add_action( 'wp_ajax_nopriv_wpsc_get_change_create_as', array( __CLASS__, 'get_change_create_as' ) ); |
| 38 |
add_action( 'wp_ajax_wpsc_create_as_autocomplete', array( __CLASS__, 'create_as_autocomplete' ) ); |
| 39 |
add_action( 'wp_ajax_nopriv_wpsc_create_as_autocomplete', array( __CLASS__, 'create_as_autocomplete' ) ); |
| 40 |
add_action( 'wp_ajax_wpsc_add_new_create_as', array( __CLASS__, 'add_new_create_as' ) ); |
| 41 |
add_action( 'wp_ajax_nopriv_wpsc_add_new_create_as', array( __CLASS__, 'add_new_create_as' ) ); |
| 42 |
add_action( 'wp_ajax_wpsc_get_create_as_customer_fields', array( __CLASS__, 'get_create_as_customer_fields' ) ); |
| 43 |
add_action( 'wp_ajax_nopriv_wpsc_get_create_as_customer_fields', array( __CLASS__, 'get_create_as_customer_fields' ) ); |
| 44 |
|
| 45 |
// Visibility. |
| 46 |
add_action( 'wp_ajax_wpsc_check_tff_visibility', array( __CLASS__, 'check_tff_visibility' ) ); |
| 47 |
add_action( 'wp_ajax_nopriv_wpsc_check_tff_visibility', array( __CLASS__, 'check_tff_visibility' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Ticket form ajax callback |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function get_ticket_form() { |
| 56 |
|
| 57 |
if ( check_ajax_referer( 'general', '_ajax_nonce', false ) != 1 ) { |
| 58 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 59 |
} |
| 60 |
|
| 61 |
$current_user = WPSC_Current_User::$current_user; |
| 62 |
$gs = get_option( 'wpsc-gs-general' ); |
| 63 |
$is_created_as = false; |
| 64 |
|
| 65 |
if ( isset( $_POST['create_as_email'] ) && isset( $_POST['create_as_name'] ) ) : |
| 66 |
|
| 67 |
$email = isset( $_POST['create_as_email'] ) ? sanitize_text_field( wp_unslash( $_POST['create_as_email'] ) ) : ''; |
| 68 |
$name = isset( $_POST['create_as_name'] ) ? sanitize_text_field( wp_unslash( $_POST['create_as_name'] ) ) : ''; |
| 69 |
if ( ! $email || ! $name ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$customer = WPSC_Customer::get_by_email( $email ); |
| 74 |
if ( ! $customer->id ) { |
| 75 |
$customer = WPSC_Customer::insert( |
| 76 |
array( |
| 77 |
'user' => 0, |
| 78 |
'name' => $name, |
| 79 |
'email' => $email, |
| 80 |
) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
$is_created_as = true; |
| 85 |
|
| 86 |
endif; |
| 87 |
|
| 88 |
if ( ! ( |
| 89 |
( ! $current_user->user->ID && in_array( 'guest', $gs['allow-create-ticket'] ) ) || |
| 90 |
( $current_user->is_agent && in_array( $current_user->agent->role, $gs['allow-create-ticket'] ) ) || |
| 91 |
( ! $current_user->is_agent && $current_user->user->ID && in_array( 'registered-user', $gs['allow-create-ticket'] ) ) |
| 92 |
) ) { |
| 93 |
?> |
| 94 |
<div style="align-item:center;" ><h6><?php esc_attr_e( 'Unauthorized access!', 'supportcandy' ); ?></h6></div> |
| 95 |
<?php |
| 96 |
wp_die(); |
| 97 |
} |
| 98 |
|
| 99 |
// Get new ticket actions. |
| 100 |
self::$new_ticket_actions = apply_filters( 'wpsc_new_ticket_actions', self::$new_ticket_actions ); |
| 101 |
if ( self::$new_ticket_actions ) { |
| 102 |
?> |
| 103 |
<div class="wpsc-new-ticket-form-actions"> |
| 104 |
<?php |
| 105 |
foreach ( self::$new_ticket_actions as $key => $action ) { |
| 106 |
?> |
| 107 |
<span |
| 108 |
class="wpsc-link wpsc-ntf-<?php echo esc_attr( $key ); ?>" |
| 109 |
onclick="<?php echo esc_attr( $action['callback'] ); ?>"> |
| 110 |
<?php echo esc_attr( $action['label'] ); ?> |
| 111 |
</span> |
| 112 |
<?php |
| 113 |
} |
| 114 |
?> |
| 115 |
</div> |
| 116 |
<?php |
| 117 |
} |
| 118 |
|
| 119 |
do_action( 'wpsc_before_create_ticket_form' ); |
| 120 |
|
| 121 |
// Create ticket on behalf. |
| 122 |
if ( $current_user->is_agent && in_array( $current_user->agent->role, $gs['allow-create-ticket'] ) && $current_user->agent->has_cap( 'create-as' ) ) : |
| 123 |
$cf = WPSC_Custom_Field::get_cf_by_slug( 'customer' ); |
| 124 |
?> |
| 125 |
<div style="width: 100%;"> |
| 126 |
<div class="wpsc-tff customer wpsc-xs-12 wpsc-sm-12 wpsc-md-6 wpsc-lg-4"> |
| 127 |
<div class="wpsc-tff-label"> |
| 128 |
<div class="wpsc-tff-label"> |
| 129 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 130 |
<span class="required-indicator">*</span> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 134 |
<div style="display:flex; flex-direction:column; width:100%;"> |
| 135 |
<select class="create-as"> |
| 136 |
<option value="<?php $current_user->customer->id; ?>"> |
| 137 |
<?php |
| 138 |
/* translators: %1$s: Name, %2$s: Email Address */ |
| 139 |
printf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), esc_attr( $current_user->customer->name ), esc_attr( $current_user->customer->email ) ); |
| 140 |
?> |
| 141 |
</option> |
| 142 |
</select> |
| 143 |
<span class="wpsc-link" onclick="wpsc_get_change_create_as('<?php echo esc_attr( wp_create_nonce( 'wpsc_get_change_create_as' ) ); ?>');" style="margin-top:3px; width:fit-content;"><?php esc_attr_e( 'Add new', 'supportcandy' ); ?></span> |
| 144 |
</div> |
| 145 |
<script> |
| 146 |
jQuery('select.create-as').selectWoo({ |
| 147 |
ajax: { |
| 148 |
url: supportcandy.ajax_url, |
| 149 |
dataType: 'json', |
| 150 |
delay: 250, |
| 151 |
data: function (params) { |
| 152 |
return { |
| 153 |
q: params.term, // search term |
| 154 |
action: 'wpsc_create_as_autocomplete', |
| 155 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_create_as_autocomplete' ) ); ?>' |
| 156 |
}; |
| 157 |
}, |
| 158 |
processResults: function (data, params) { |
| 159 |
var terms = []; |
| 160 |
if ( data ) { |
| 161 |
jQuery.each( data, function( id, customer ) { |
| 162 |
terms.push({ |
| 163 |
id: customer.id, |
| 164 |
text: customer.text, |
| 165 |
email: customer.email, |
| 166 |
name: customer.name |
| 167 |
}); |
| 168 |
}); |
| 169 |
} |
| 170 |
return { |
| 171 |
results: terms |
| 172 |
}; |
| 173 |
}, |
| 174 |
cache: true |
| 175 |
}, |
| 176 |
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work |
| 177 |
minimumInputLength: 1, |
| 178 |
}); |
| 179 |
jQuery('select.create-as').on('select2:select', function (e) { |
| 180 |
var data = e.params.data; |
| 181 |
jQuery('input.name').val(data.name); |
| 182 |
jQuery('input.email').val(data.email); |
| 183 |
wpsc_after_change_create_as(); |
| 184 |
}); |
| 185 |
</script> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
<?php |
| 189 |
endif; |
| 190 |
?> |
| 191 |
|
| 192 |
<form class="wpsc-create-ticket" onsubmit="return false;" action="#"> |
| 193 |
<?php |
| 194 |
// Load form fields. |
| 195 |
$tff = get_option( 'wpsc-tff', array() ); |
| 196 |
foreach ( $tff as $slug => $field ) : |
| 197 |
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug ); |
| 198 |
if ( ! $cf ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
if ( $cf->field === 'customer' && ! self::$has_customer_fields ) { |
| 202 |
self::$has_customer_fields = true; |
| 203 |
} |
| 204 |
echo $cf->type::print_tff( $cf, $field ); // phpcs:ignore |
| 205 |
endforeach; |
| 206 |
do_action( 'wpsc_print_tff' ); |
| 207 |
?> |
| 208 |
<input type="hidden" name="action" value="wpsc_set_ticket_form"/> |
| 209 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_ticket_form' ) ); ?>"> |
| 210 |
</form> |
| 211 |
|
| 212 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 213 |
<div class="submit-container wpsc-ct-actions"> |
| 214 |
<button id="wpsc-ct-submit" class="wpsc-button normal primary margin-right" onclick="wpsc_submit_ticket_form(this);"><?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button> |
| 215 |
<button id="wpsc-ct-reset" class="wpsc-button normal secondary" onclick="wpsc_get_ticket_form();"><?php esc_attr_e( 'Reset Form', 'supportcandy' ); ?></button> |
| 216 |
<div class="wpsc-ct-loader"></div> |
| 217 |
</div> |
| 218 |
</div> |
| 219 |
|
| 220 |
<script> |
| 221 |
|
| 222 |
jQuery(document).ready(function() { |
| 223 |
wpsc_check_tff_visibility(); |
| 224 |
}); |
| 225 |
|
| 226 |
var hasCustomerFields = <?php echo self::$has_customer_fields ? 'true' : 'false'; ?>; |
| 227 |
var prevCustomer = '<?php echo $current_user->is_customer ? esc_attr( $current_user->customer->email ) : ''; ?>'; |
| 228 |
|
| 229 |
/** |
| 230 |
* Submit create ticket form |
| 231 |
*/ |
| 232 |
function wpsc_submit_ticket_form(el) { |
| 233 |
|
| 234 |
if ( ! Array.isArray( supportcandy.checkTffVisibityCallers ) ) { |
| 235 |
supportcandy.checkTffVisibityCallers = []; |
| 236 |
} |
| 237 |
|
| 238 |
if ( supportcandy.checkTffVisibityCallers.length > 0 ) { |
| 239 |
// loop until all ajax calls are done. |
| 240 |
setTimeout(() => wpsc_submit_ticket_form(el), 200); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
if (!wpsc_validate_ticket_form()) return; |
| 245 |
|
| 246 |
wpsc_clear_hidden_fields(); |
| 247 |
|
| 248 |
var is_editor = (typeof isWPSCEditor !== 'undefined') ? isWPSCEditor : 0; |
| 249 |
|
| 250 |
var dataform = new FormData(jQuery('form.wpsc-create-ticket')[0]); |
| 251 |
dataform.append('is_frontend', supportcandy.is_frontend); |
| 252 |
dataform.append('is_editor', is_editor); |
| 253 |
<?php do_action( 'wpsc_js_create_ticket_formdata' ); ?> |
| 254 |
jQuery('.wpsc-body').html(supportcandy.loader_html); |
| 255 |
if (supportcandy.is_frontend === '0') { |
| 256 |
wpsc_scroll_top(); |
| 257 |
} |
| 258 |
|
| 259 |
<?php |
| 260 |
$recaptcha = get_option( 'wpsc-recaptcha-settings' ); |
| 261 |
if ( $recaptcha['captcha-provider'] === 'google-recaptcha' && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) { |
| 262 |
?> |
| 263 |
grecaptcha.ready(function() { |
| 264 |
grecaptcha.execute('<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>', {action: 'submit_ticket'}).then(function(token) { |
| 265 |
dataform.append('g-recaptcha-response', token); |
| 266 |
wpsc_post_ticket_form(dataform); |
| 267 |
}); |
| 268 |
}); |
| 269 |
<?php |
| 270 |
} elseif ( $recaptcha['captcha-provider'] === 'cloudflare-turnstile' && $recaptcha['cloudflare-site-key'] && $recaptcha['cloudflare-secret-key'] ) { |
| 271 |
?> |
| 272 |
var token = dataform.get('cf-turnstile-response'); |
| 273 |
if (!token) { |
| 274 |
alert("<?php esc_attr_e( 'Security verification failed. Please refresh the page and try again.', 'supportcandy' ); ?>"); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
dataform.set( |
| 279 |
'cf-turnstile-response', |
| 280 |
token |
| 281 |
); |
| 282 |
wpsc_post_ticket_form(dataform); |
| 283 |
<?php |
| 284 |
|
| 285 |
} else { |
| 286 |
?> |
| 287 |
wpsc_post_ticket_form(dataform); |
| 288 |
<?php |
| 289 |
} |
| 290 |
?> |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Post ticket for to server |
| 295 |
* |
| 296 |
* @return void |
| 297 |
*/ |
| 298 |
function wpsc_post_ticket_form(dataform) { |
| 299 |
|
| 300 |
var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(); |
| 301 |
jQuery.ajax({ |
| 302 |
url: supportcandy.ajax_url, |
| 303 |
type: 'POST', |
| 304 |
data: dataform, |
| 305 |
processData: false, |
| 306 |
contentType: false, |
| 307 |
error: function (response) { |
| 308 |
alert(response.responseJSON.data[0].message); |
| 309 |
if (is_tinymce) { |
| 310 |
tinyMCE.get('description').setContent(''); |
| 311 |
} |
| 312 |
wpsc_get_ticket_form(); |
| 313 |
}, |
| 314 |
success: function (response, textStatus, xhr) { |
| 315 |
if( response.action == 'ticket' ) { |
| 316 |
wpsc_get_individual_ticket( response.id ); |
| 317 |
}else if( response.action == 'url' ) { |
| 318 |
window.location.href = response.redirectURL; |
| 319 |
}else if( response.action == 'text' ) { |
| 320 |
var classes = 'wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12'; |
| 321 |
jQuery('.wpsc-body').html('<div class="' + classes + '">' + response.thankyouText + '</div>'); |
| 322 |
if (is_tinymce) { |
| 323 |
tinyMCE.get('description').setContent(''); |
| 324 |
} |
| 325 |
} |
| 326 |
wpsc_run_ajax_background_process(); |
| 327 |
} |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Check validations for create ticket form |
| 333 |
* |
| 334 |
* @return Boolean |
| 335 |
*/ |
| 336 |
function wpsc_validate_ticket_form() { |
| 337 |
|
| 338 |
var customFields = jQuery('.wpsc-tff.wpsc-visible'); |
| 339 |
var flag = true; |
| 340 |
var firstInvalidField = null; |
| 341 |
|
| 342 |
// Remove old errors |
| 343 |
clearAllFieldErrors(); |
| 344 |
jQuery.each(customFields, function(index, customField){ |
| 345 |
|
| 346 |
customField = jQuery(customField); |
| 347 |
var customFieldType = customField.data('cft'); |
| 348 |
var isValid = true; |
| 349 |
var isRequired = customField.hasClass('required'); |
| 350 |
|
| 351 |
var fieldLabel = customField.find('.wpsc-tff-label .name').text().trim() || 'This field'; |
| 352 |
switch (customFieldType) { |
| 353 |
<?php do_action( 'wpsc_js_validate_ticket_form' ); ?> |
| 354 |
} |
| 355 |
//Common error handling |
| 356 |
if (!isValid) { |
| 357 |
|
| 358 |
flag = false; |
| 359 |
|
| 360 |
// Show error message |
| 361 |
showFieldError(customField, fieldLabel + ' is required'); |
| 362 |
|
| 363 |
// Track first invalid field |
| 364 |
if (!firstInvalidField) { |
| 365 |
firstInvalidField = customField; |
| 366 |
} |
| 367 |
} |
| 368 |
}); |
| 369 |
|
| 370 |
// Scroll to first error |
| 371 |
if (firstInvalidField) { |
| 372 |
firstInvalidField[0].scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 373 |
} |
| 374 |
return flag; |
| 375 |
} |
| 376 |
|
| 377 |
function showFieldError(field, message) { |
| 378 |
if (field.find('.wpsc-error-msg').length === 0) { |
| 379 |
field.append('<div class="wpsc-error-msg">' + message + '</div>'); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
function clearAllFieldErrors() { |
| 384 |
jQuery('.wpsc-error-msg').remove(); |
| 385 |
} |
| 386 |
|
| 387 |
jQuery(document).on('input change', '.wpsc-tff input, .wpsc-tff textarea, .wpsc-tff select', function(){ |
| 388 |
var field = jQuery(this).closest('.wpsc-tff'); |
| 389 |
field.find('.wpsc-error-msg').remove(); |
| 390 |
}); |
| 391 |
|
| 392 |
function wpsc_clear_hidden_fields() { |
| 393 |
var customFields = jQuery('.wpsc-tff.wpsc-hidden'); |
| 394 |
jQuery.each(customFields, function(index, customField){ |
| 395 |
customField = jQuery(customField); |
| 396 |
var customFieldType = customField.data('cft'); |
| 397 |
switch (customFieldType) { |
| 398 |
<?php do_action( 'wpsc_js_clear_value_hidden_fields' ); ?> |
| 399 |
} |
| 400 |
}); |
| 401 |
} |
| 402 |
|
| 403 |
function wpsc_get_create_as_customer_fields(nonce) { |
| 404 |
|
| 405 |
if (!hasCustomerFields) return; |
| 406 |
|
| 407 |
var curCustomer = jQuery('.wpsc-create-ticket input.email').val().trim(); |
| 408 |
if (curCustomer == prevCustomer) return; |
| 409 |
prevCustomer = curCustomer; |
| 410 |
|
| 411 |
var data = { |
| 412 |
action: 'wpsc_get_create_as_customer_fields', |
| 413 |
email: curCustomer, |
| 414 |
_ajax_nonce: nonce |
| 415 |
}; |
| 416 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 417 |
|
| 418 |
jQuery.each( response, function( key, field ) { |
| 419 |
var currentEl = jQuery('.wpsc-tff.' + field.slug); |
| 420 |
var nextEl = currentEl.next(); |
| 421 |
currentEl.remove(); |
| 422 |
nextEl.before(field.html); |
| 423 |
}); |
| 424 |
wpsc_check_tff_visibility(); |
| 425 |
}); |
| 426 |
} |
| 427 |
<?php |
| 428 |
|
| 429 |
if ( $is_created_as ) { |
| 430 |
?> |
| 431 |
jQuery(document).ready(function() { |
| 432 |
<?php /* translators: %1$s: Name, %2$s: Email Address */ ?> |
| 433 |
var label = '<?php printf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), esc_attr( $customer->name ), esc_attr( $customer->email ) ); ?>'; |
| 434 |
var newOption = new Option(label, <?php echo esc_attr( $customer->id ); ?>, false, false); |
| 435 |
jQuery('select.create-as').append(newOption); |
| 436 |
jQuery('select.create-as').val(<?php echo esc_attr( $customer->id ); ?>).trigger('change'); |
| 437 |
jQuery('input.name').val('<?php echo esc_attr( $customer->name ); ?>'); |
| 438 |
jQuery('input.email').val('<?php echo esc_attr( $customer->email ); ?>'); |
| 439 |
wpsc_after_change_create_as(); |
| 440 |
}); |
| 441 |
<?php |
| 442 |
} |
| 443 |
?> |
| 444 |
|
| 445 |
<?php do_action( 'wpsc_js_ticket_form_functions' ); ?> |
| 446 |
</script> |
| 447 |
<?php |
| 448 |
|
| 449 |
wp_die(); |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Submit ticket |
| 454 |
* |
| 455 |
* @return void |
| 456 |
*/ |
| 457 |
public static function set_ticket_form() { |
| 458 |
|
| 459 |
if ( check_ajax_referer( 'wpsc_set_ticket_form', '_ajax_nonce', false ) != 1 ) { |
| 460 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 461 |
} |
| 462 |
|
| 463 |
WPSC_MS_Recaptcha::validate( 'submit_ticket' ); |
| 464 |
|
| 465 |
$current_user = WPSC_Current_User::$current_user; |
| 466 |
$gs = get_option( 'wpsc-gs-general' ); |
| 467 |
$advanced = get_option( 'wpsc-ms-advanced-settings' ); |
| 468 |
|
| 469 |
if ( ! ( |
| 470 |
( ! $current_user->user->ID && in_array( 'guest', $gs['allow-create-ticket'] ) ) || |
| 471 |
( $current_user->is_agent && in_array( $current_user->agent->role, $gs['allow-create-ticket'] ) ) || |
| 472 |
( ! $current_user->is_agent && $current_user->user->ID && in_array( 'registered-user', $gs['allow-create-ticket'] ) ) |
| 473 |
) ) { |
| 474 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 475 |
} |
| 476 |
|
| 477 |
// group by custom field type. |
| 478 |
$cfs = array(); |
| 479 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 480 |
$cfs[ $cf->type::$slug ][] = $cf; |
| 481 |
} |
| 482 |
|
| 483 |
$data = apply_filters( 'wpsc_create_ticket_data', array(), $cfs, false ); |
| 484 |
|
| 485 |
// Seperate description from $data. |
| 486 |
$description = $data['description']; |
| 487 |
unset( $data['description'] ); |
| 488 |
|
| 489 |
// Seperate description attachments from $data. |
| 490 |
$description_attachments = $data['description_attachments']; |
| 491 |
unset( $data['description_attachments'] ); |
| 492 |
|
| 493 |
$data['last_reply_on'] = ( new DateTime() )->format( 'Y-m-d H:i:s' ); |
| 494 |
|
| 495 |
// insert ticket data. |
| 496 |
$ticket = WPSC_Ticket::insert( $data ); |
| 497 |
|
| 498 |
if ( ! $ticket ) { |
| 499 |
wp_send_json_error( new WP_Error( '001', 'Something went wrong!' ), 500 ); |
| 500 |
} |
| 501 |
|
| 502 |
$thread_customer = $current_user->is_agent && $current_user->customer->id != $ticket->customer->id && $advanced['raised-by-user'] == 'agent' ? $current_user->customer : $ticket->customer; |
| 503 |
$ticket->last_reply_by = $thread_customer->id; |
| 504 |
$ticket->last_reply_source = 'browser'; |
| 505 |
$ticket->auth_code = WPSC_Functions::get_random_string(); |
| 506 |
$ticket->save(); |
| 507 |
|
| 508 |
// replace macros only when current user is an agent. |
| 509 |
$description = $current_user->is_agent ? WPSC_Macros::replace( $description, $ticket ) : $description; |
| 510 |
|
| 511 |
$description = preg_replace( '/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', '', $description ); |
| 512 |
// remove divs from message. |
| 513 |
$description = preg_replace( |
| 514 |
array( '/<div(.*?)>/s', '/<\/div>/s', '/<table(.*?)>/s', '/<tbody(.*?)>/s', '/<tr(.*?)>/s', '/<th(.*?)>/s', '/<td(.*?)>/s', '/<pre(.*?)>/s', '/<\/pre>/s' ), |
| 515 |
array( '<p>', '</p>', '<table>', '<tbody>', '<tr>', '<th>', '<td>', '<p>', '</p>' ), |
| 516 |
$description |
| 517 |
); |
| 518 |
|
| 519 |
// set signature if agent. |
| 520 |
$signature = $current_user->is_agent && ( $current_user->customer->email == $ticket->customer->email || $advanced['raised-by-user'] == 'agent' ) ? $current_user->agent->get_signature() : ''; |
| 521 |
if ( $signature ) { |
| 522 |
$description .= '<br>' . $signature; |
| 523 |
} |
| 524 |
|
| 525 |
// add ticket auth url to tinymce attachments. |
| 526 |
if ( preg_match_all( '/' . preg_quote( home_url( '/' ), '/' ) . '\?wpsc_attachment=(\d*)/', $description, $matches ) ) { |
| 527 |
foreach ( $matches[0] as $url ) { |
| 528 |
$new_url = add_query_arg( 'auth_code', $ticket->auth_code, $url ); |
| 529 |
$description = str_replace( $url, $new_url, $description ); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
// Create report thread. |
| 534 |
$thread = WPSC_Thread::insert( |
| 535 |
array( |
| 536 |
'ticket' => $ticket->id, |
| 537 |
'customer' => $thread_customer->id, |
| 538 |
'type' => 'report', |
| 539 |
'body' => $description, |
| 540 |
'attachments' => $description_attachments, |
| 541 |
'ip_address' => $ticket->ip_address, |
| 542 |
'source' => $ticket->source, |
| 543 |
'os' => $ticket->os, |
| 544 |
'browser' => $ticket->browser, |
| 545 |
) |
| 546 |
); |
| 547 |
|
| 548 |
do_action( 'wpsc_create_new_ticket', $ticket ); |
| 549 |
|
| 550 |
// tinymce img attachments. |
| 551 |
if ( preg_match_all( '/' . preg_quote( home_url( '/' ), '/' ) . '\?wpsc_attachment=(\d*)(&auth_code=[^&\s]*)?/', $description, $matches ) ) { |
| 552 |
foreach ( $matches[1] as $id ) { |
| 553 |
$attachment = new WPSC_Attachment( $id ); |
| 554 |
$attachment->is_active = 1; |
| 555 |
$attachment->source_id = $thread->id; |
| 556 |
$attachment->ticket_id = $ticket->id; |
| 557 |
$attachment->source = 'img_editor'; |
| 558 |
$attachment->save(); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
$response = array( 'id' => $ticket->id ); |
| 563 |
|
| 564 |
$is_frontend = isset( $_POST['is_frontend'] ) ? intval( $_POST['is_frontend'] ) : 0; |
| 565 |
|
| 566 |
$thankyou = get_option( 'wpsc-gs-thankyou-page-settings' ); |
| 567 |
|
| 568 |
if ( $current_user->is_agent ) { |
| 569 |
|
| 570 |
$response['action'] = $thankyou['action-agent']; |
| 571 |
if ( $thankyou['action-agent'] == 'url' && $thankyou['page-url-agent'] && filter_var( $thankyou['page-url-agent'], FILTER_VALIDATE_URL ) ) { |
| 572 |
$response['isRedirect'] = 1; |
| 573 |
$response['redirectURL'] = $thankyou['page-url-agent']; |
| 574 |
} elseif ( $thankyou['action-agent'] == 'text' ) { |
| 575 |
$thank_you = $thankyou['html-agent'] ? WPSC_Translations::get( 'wpsc-thankyou-html-agent', $thankyou['html-agent'] ) : $thankyou['html-agent']; |
| 576 |
$thankyou_text = WPSC_Macros::replace( $thank_you, $ticket ); |
| 577 |
$thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket ); |
| 578 |
$response['isRedirect'] = 0; |
| 579 |
$response['thankyouText'] = $thankyou_text; |
| 580 |
} |
| 581 |
} else { |
| 582 |
|
| 583 |
$response['action'] = $thankyou['action-customer']; |
| 584 |
if ( $thankyou['action-customer'] == 'url' && $thankyou['page-url-customer'] && filter_var( $thankyou['page-url-customer'], FILTER_VALIDATE_URL ) ) { |
| 585 |
$response['isRedirect'] = 1; |
| 586 |
$response['redirectURL'] = $thankyou['page-url-customer']; |
| 587 |
} elseif ( $thankyou['action-customer'] == 'text' || ( $current_user->is_guest && $thankyou['action-customer'] == 'ticket' ) ) { |
| 588 |
$response['action'] = 'text'; |
| 589 |
$thank_you = $thankyou['html-customer'] ? WPSC_Translations::get( 'wpsc-thankyou-html', $thankyou['html-customer'] ) : $thankyou['html-customer']; |
| 590 |
$thankyou_text = WPSC_Macros::replace( $thank_you, $ticket ); |
| 591 |
$thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket ); |
| 592 |
$response['isRedirect'] = 0; |
| 593 |
$response['thankyouText'] = $thankyou_text; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
wp_send_json( $response ); |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Change create as or raised by modal |
| 602 |
* |
| 603 |
* @return void |
| 604 |
*/ |
| 605 |
public static function get_change_create_as() { |
| 606 |
|
| 607 |
if ( check_ajax_referer( 'wpsc_get_change_create_as', '_ajax_nonce', false ) != 1 ) { |
| 608 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 609 |
} |
| 610 |
|
| 611 |
$current_user = WPSC_Current_User::$current_user; |
| 612 |
|
| 613 |
if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) { |
| 614 |
wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 ); |
| 615 |
} |
| 616 |
|
| 617 |
$title = esc_attr__( 'Add new', 'supportcandy' ); |
| 618 |
|
| 619 |
ob_start(); |
| 620 |
$unique_id = uniqid( 'wpsc_' ); |
| 621 |
?> |
| 622 |
<form action="#" onsubmit="return false;" class="frm-add-new-ticket-form-field"> |
| 623 |
|
| 624 |
<div class="wpsc-input-group label"> |
| 625 |
<div class="label-container"> |
| 626 |
<label for=""> |
| 627 |
<?php esc_attr_e( 'Name', 'supportcandy' ); ?> |
| 628 |
<span class="required-char">*</span> |
| 629 |
</label> |
| 630 |
</div> |
| 631 |
<input class="name <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/> |
| 632 |
</div> |
| 633 |
|
| 634 |
<div class="wpsc-input-group label"> |
| 635 |
<div class="label-container"> |
| 636 |
<label for=""> |
| 637 |
<?php esc_attr_e( 'Email Address', 'supportcandy' ); ?> |
| 638 |
<span class="required-char">*</span> |
| 639 |
</label> |
| 640 |
</div> |
| 641 |
<input class="email <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/> |
| 642 |
</div> |
| 643 |
|
| 644 |
</form> |
| 645 |
<?php |
| 646 |
$body = ob_get_clean(); |
| 647 |
|
| 648 |
ob_start(); |
| 649 |
?> |
| 650 |
<button class="wpsc-button small primary" onclick="wpsc_set_change_create_as('<?php echo esc_attr( $unique_id ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_add_new_create_as' ) ); ?>');"> |
| 651 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 652 |
</button> |
| 653 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 654 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 655 |
</button> |
| 656 |
<?php |
| 657 |
do_action( 'wpsc_get_add_new_tff' ); |
| 658 |
$footer = ob_get_clean(); |
| 659 |
|
| 660 |
$response = array( |
| 661 |
'title' => $title, |
| 662 |
'body' => $body, |
| 663 |
'footer' => $footer, |
| 664 |
); |
| 665 |
wp_send_json( $response ); |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Create as auto-complete |
| 670 |
* |
| 671 |
* @return void |
| 672 |
*/ |
| 673 |
public static function create_as_autocomplete() { |
| 674 |
|
| 675 |
if ( check_ajax_referer( 'wpsc_create_as_autocomplete', '_ajax_nonce', false ) != 1 ) { |
| 676 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 677 |
} |
| 678 |
|
| 679 |
$current_user = WPSC_Current_User::$current_user; |
| 680 |
|
| 681 |
if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) { |
| 682 |
wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 ); |
| 683 |
} |
| 684 |
|
| 685 |
$term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : ''; |
| 686 |
if ( ! $term ) { |
| 687 |
wp_send_json_error( new WP_Error( '002', 'Search term should have at least one character!' ), 400 ); |
| 688 |
} |
| 689 |
|
| 690 |
wp_send_json( |
| 691 |
array_map( |
| 692 |
fn( $customer ) => array( |
| 693 |
'id' => $customer->id, |
| 694 |
'text' => sprintf( |
| 695 |
/* translators: %1$s: Name, %2$s: Email Address */ |
| 696 |
esc_attr__( '%1$s (%2$s)', 'supportcandy' ), |
| 697 |
$customer->name, |
| 698 |
$customer->email |
| 699 |
), |
| 700 |
'email' => $customer->email, |
| 701 |
'name' => $customer->name, |
| 702 |
), |
| 703 |
WPSC_Customer::customer_search( $term ) |
| 704 |
) |
| 705 |
); |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
* Create as add new customer |
| 710 |
* |
| 711 |
* @return void |
| 712 |
*/ |
| 713 |
public static function add_new_create_as() { |
| 714 |
|
| 715 |
if ( check_ajax_referer( 'wpsc_add_new_create_as', '_ajax_nonce', false ) != 1 ) { |
| 716 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 717 |
} |
| 718 |
|
| 719 |
$current_user = WPSC_Current_User::$current_user; |
| 720 |
|
| 721 |
if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) { |
| 722 |
wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 ); |
| 723 |
} |
| 724 |
|
| 725 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 726 |
if ( ! $name ) { |
| 727 |
wp_send_json_error( 'Bad request', 400 ); |
| 728 |
} |
| 729 |
|
| 730 |
$email = isset( $_POST['email'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['email'] ) ) ) : ''; |
| 731 |
if ( ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 732 |
wp_send_json_error( 'Bad request', 400 ); |
| 733 |
} |
| 734 |
|
| 735 |
$customer = WPSC_Customer::get_by_email( $email ); |
| 736 |
if ( ! $customer->id ) { |
| 737 |
$customer = WPSC_Customer::insert( |
| 738 |
array( |
| 739 |
'user' => 0, |
| 740 |
'name' => $name, |
| 741 |
'email' => $email, |
| 742 |
) |
| 743 |
); |
| 744 |
} |
| 745 |
|
| 746 |
$response = array( |
| 747 |
'id' => $customer->id, |
| 748 |
'name' => $customer->name, |
| 749 |
'email' => $customer->email, |
| 750 |
/* translators: %1$s: Name, %2$s: Email Address */ |
| 751 |
'label' => sprintf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), $customer->name, $customer->email ), |
| 752 |
); |
| 753 |
|
| 754 |
wp_send_json( $response, 200 ); |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Return visibility condition results |
| 759 |
* |
| 760 |
* @return void |
| 761 |
*/ |
| 762 |
public static function check_tff_visibility() { |
| 763 |
|
| 764 |
$response = array(); |
| 765 |
$tff = get_option( 'wpsc-tff', array() ); |
| 766 |
foreach ( $tff as $slug => $settings ) { |
| 767 |
|
| 768 |
$visibility = WPSC_TFF::get_visibility( $settings, true ); |
| 769 |
if ( ! count( $visibility ) ) { |
| 770 |
continue; |
| 771 |
} |
| 772 |
|
| 773 |
$current_user = WPSC_Current_User::$current_user; |
| 774 |
|
| 775 |
if ( |
| 776 |
isset( $settings['allowed_user'] ) && |
| 777 |
( |
| 778 |
( $current_user->is_agent && $settings['allowed_user'] == 'customer' ) || |
| 779 |
( ! $current_user->is_agent && $settings['allowed_user'] == 'agent' ) |
| 780 |
) |
| 781 |
) { |
| 782 |
continue; |
| 783 |
} |
| 784 |
|
| 785 |
$flag = true; |
| 786 |
foreach ( $visibility as $and_condition ) { |
| 787 |
|
| 788 |
$temp = false; |
| 789 |
foreach ( $and_condition as $or_condition ) { |
| 790 |
|
| 791 |
if ( ! isset( WPSC_Ticket_Conditions::$conditions[ $or_condition['slug'] ] ) ) { |
| 792 |
continue; |
| 793 |
} |
| 794 |
|
| 795 |
if ( isset( $response[ $or_condition['slug'] ] ) && ! $response[ $or_condition['slug'] ] ) { |
| 796 |
continue; |
| 797 |
} |
| 798 |
|
| 799 |
$cf = WPSC_Custom_Field::get_cf_by_slug( $or_condition['slug'] ); |
| 800 |
$value = $cf->type::get_tff_value( $cf->slug, $cf ); |
| 801 |
if ( $cf->type::is_valid( $or_condition, $cf, $value ) ) { |
| 802 |
$temp = true; |
| 803 |
break; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
if ( ! $temp ) { |
| 808 |
$flag = false; |
| 809 |
break; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
$response[ $slug ] = $flag ? 1 : 0; |
| 814 |
} |
| 815 |
|
| 816 |
wp_send_json( $response ); |
| 817 |
} |
| 818 |
|
| 819 |
/** |
| 820 |
* Create as customer field values |
| 821 |
* |
| 822 |
* @return void |
| 823 |
*/ |
| 824 |
public static function get_create_as_customer_fields() { |
| 825 |
|
| 826 |
if ( check_ajax_referer( 'wpsc_get_create_as_customer_fields', '_ajax_nonce', false ) != 1 ) { |
| 827 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 828 |
} |
| 829 |
|
| 830 |
$current_user = WPSC_Current_User::$current_user; |
| 831 |
if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) { |
| 832 |
wp_send_json_error( new WP_Error( '001', 'Unauthorized!' ), 400 ); |
| 833 |
} |
| 834 |
|
| 835 |
$email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : ''; |
| 836 |
if ( ! $email ) { |
| 837 |
wp_send_json_error( new WP_Error( '002', 'Something went wrong!' ), 400 ); |
| 838 |
} |
| 839 |
|
| 840 |
WPSC_Current_User::change_current_user( $email ); |
| 841 |
|
| 842 |
$tff = get_option( 'wpsc-tff' ); |
| 843 |
$response = array(); |
| 844 |
foreach ( $tff as $slug => $properties ) { |
| 845 |
|
| 846 |
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug ); |
| 847 |
if ( ! $cf ) { |
| 848 |
continue; |
| 849 |
} |
| 850 |
if ( $cf->field === 'customer' && ! $cf->type::$is_default ) { |
| 851 |
$response[] = array( |
| 852 |
'slug' => $slug, |
| 853 |
'html' => $cf->type::print_tff( $cf, $properties ), |
| 854 |
); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
wp_send_json( $response ); |
| 859 |
} |
| 860 |
} |
| 861 |
endif; |
| 862 |
|
| 863 |
WPSC_New_Ticket::init(); |
| 864 |
|