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
supportcandy / includes / admin / tickets / class-wpsc-new-ticket.php

class-wpsc-new-ticket.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/admin/tickets/class-wpsc-new-ticket.php

871 lines 28.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_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"\']*)?(?:&(?:amp;)?wpsc_nonce=([a-f0-9]*))?/', $description, $matches ) ) {
552 foreach ( $matches[1] as $key => $id ) {
553 $attachment = new WPSC_Attachment( $id );
554 // Only the uploader may bind their own, not-yet-bound
555 // attachment, proven by the recorded uploader identity or
556 // by the per-attachment nonce carried in the image url.
557 $nonce = isset( $matches[2][ $key ] ) ? $matches[2][ $key ] : '';
558 if ( ! WPSC_Attachment::can_claim( $attachment, $nonce ) ) {
559 continue;
560 }
561 $attachment->is_active = 1;
562 $attachment->source_id = $thread->id;
563 $attachment->ticket_id = $ticket->id;
564 $attachment->source = 'img_editor';
565 $attachment->save();
566 }
567 }
568
569 $response = array( 'id' => $ticket->id );
570
571 $is_frontend = isset( $_POST['is_frontend'] ) ? intval( $_POST['is_frontend'] ) : 0;
572
573 $thankyou = get_option( 'wpsc-gs-thankyou-page-settings' );
574
575 if ( $current_user->is_agent ) {
576
577 $response['action'] = $thankyou['action-agent'];
578 if ( $thankyou['action-agent'] == 'url' && $thankyou['page-url-agent'] && filter_var( $thankyou['page-url-agent'], FILTER_VALIDATE_URL ) ) {
579 $response['isRedirect'] = 1;
580 $response['redirectURL'] = $thankyou['page-url-agent'];
581 } elseif ( $thankyou['action-agent'] == 'text' ) {
582 $thank_you = $thankyou['html-agent'] ? WPSC_Translations::get( 'wpsc-thankyou-html-agent', $thankyou['html-agent'] ) : $thankyou['html-agent'];
583 $thankyou_text = WPSC_Macros::replace( $thank_you, $ticket );
584 $thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket );
585 $response['isRedirect'] = 0;
586 $response['thankyouText'] = $thankyou_text;
587 }
588 } else {
589
590 $response['action'] = $thankyou['action-customer'];
591 if ( $thankyou['action-customer'] == 'url' && $thankyou['page-url-customer'] && filter_var( $thankyou['page-url-customer'], FILTER_VALIDATE_URL ) ) {
592 $response['isRedirect'] = 1;
593 $response['redirectURL'] = $thankyou['page-url-customer'];
594 } elseif ( $thankyou['action-customer'] == 'text' || ( $current_user->is_guest && $thankyou['action-customer'] == 'ticket' ) ) {
595 $response['action'] = 'text';
596 $thank_you = $thankyou['html-customer'] ? WPSC_Translations::get( 'wpsc-thankyou-html', $thankyou['html-customer'] ) : $thankyou['html-customer'];
597 $thankyou_text = WPSC_Macros::replace( $thank_you, $ticket );
598 $thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket );
599 $response['isRedirect'] = 0;
600 $response['thankyouText'] = $thankyou_text;
601 }
602 }
603
604 wp_send_json( $response );
605 }
606
607 /**
608 * Change create as or raised by modal
609 *
610 * @return void
611 */
612 public static function get_change_create_as() {
613
614 if ( check_ajax_referer( 'wpsc_get_change_create_as', '_ajax_nonce', false ) != 1 ) {
615 wp_send_json_error( 'Unauthorized request!', 401 );
616 }
617
618 $current_user = WPSC_Current_User::$current_user;
619
620 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
621 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
622 }
623
624 $title = esc_attr__( 'Add new', 'supportcandy' );
625
626 ob_start();
627 $unique_id = uniqid( 'wpsc_' );
628 ?>
629 <form action="#" onsubmit="return false;" class="frm-add-new-ticket-form-field">
630
631 <div class="wpsc-input-group label">
632 <div class="label-container">
633 <label for="">
634 <?php esc_attr_e( 'Name', 'supportcandy' ); ?>
635 <span class="required-char">*</span>
636 </label>
637 </div>
638 <input class="name <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/>
639 </div>
640
641 <div class="wpsc-input-group label">
642 <div class="label-container">
643 <label for="">
644 <?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>
645 <span class="required-char">*</span>
646 </label>
647 </div>
648 <input class="email <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/>
649 </div>
650
651 </form>
652 <?php
653 $body = ob_get_clean();
654
655 ob_start();
656 ?>
657 <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' ) ); ?>');">
658 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
659 </button>
660 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
661 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
662 </button>
663 <?php
664 do_action( 'wpsc_get_add_new_tff' );
665 $footer = ob_get_clean();
666
667 $response = array(
668 'title' => $title,
669 'body' => $body,
670 'footer' => $footer,
671 );
672 wp_send_json( $response );
673 }
674
675 /**
676 * Create as auto-complete
677 *
678 * @return void
679 */
680 public static function create_as_autocomplete() {
681
682 if ( check_ajax_referer( 'wpsc_create_as_autocomplete', '_ajax_nonce', false ) != 1 ) {
683 wp_send_json_error( 'Unauthorized request!', 401 );
684 }
685
686 $current_user = WPSC_Current_User::$current_user;
687
688 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
689 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
690 }
691
692 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
693 if ( ! $term ) {
694 wp_send_json_error( new WP_Error( '002', 'Search term should have at least one character!' ), 400 );
695 }
696
697 wp_send_json(
698 array_map(
699 fn( $customer ) => array(
700 'id' => $customer->id,
701 'text' => sprintf(
702 /* translators: %1$s: Name, %2$s: Email Address */
703 esc_attr__( '%1$s (%2$s)', 'supportcandy' ),
704 $customer->name,
705 $customer->email
706 ),
707 'email' => $customer->email,
708 'name' => $customer->name,
709 ),
710 WPSC_Customer::customer_search( $term )
711 )
712 );
713 }
714
715 /**
716 * Create as add new customer
717 *
718 * @return void
719 */
720 public static function add_new_create_as() {
721
722 if ( check_ajax_referer( 'wpsc_add_new_create_as', '_ajax_nonce', false ) != 1 ) {
723 wp_send_json_error( 'Unauthorized request!', 401 );
724 }
725
726 $current_user = WPSC_Current_User::$current_user;
727
728 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
729 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
730 }
731
732 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
733 if ( ! $name ) {
734 wp_send_json_error( 'Bad request', 400 );
735 }
736
737 $email = isset( $_POST['email'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['email'] ) ) ) : '';
738 if ( ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
739 wp_send_json_error( 'Bad request', 400 );
740 }
741
742 $customer = WPSC_Customer::get_by_email( $email );
743 if ( ! $customer->id ) {
744 $customer = WPSC_Customer::insert(
745 array(
746 'user' => 0,
747 'name' => $name,
748 'email' => $email,
749 )
750 );
751 }
752
753 $response = array(
754 'id' => $customer->id,
755 'name' => $customer->name,
756 'email' => $customer->email,
757 /* translators: %1$s: Name, %2$s: Email Address */
758 'label' => sprintf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), $customer->name, $customer->email ),
759 );
760
761 wp_send_json( $response, 200 );
762 }
763
764 /**
765 * Return visibility condition results
766 *
767 * @return void
768 */
769 public static function check_tff_visibility() {
770
771 $response = array();
772 $tff = get_option( 'wpsc-tff', array() );
773 foreach ( $tff as $slug => $settings ) {
774
775 $visibility = WPSC_TFF::get_visibility( $settings, true );
776 if ( ! count( $visibility ) ) {
777 continue;
778 }
779
780 $current_user = WPSC_Current_User::$current_user;
781
782 if (
783 isset( $settings['allowed_user'] ) &&
784 (
785 ( $current_user->is_agent && $settings['allowed_user'] == 'customer' ) ||
786 ( ! $current_user->is_agent && $settings['allowed_user'] == 'agent' )
787 )
788 ) {
789 continue;
790 }
791
792 $flag = true;
793 foreach ( $visibility as $and_condition ) {
794
795 $temp = false;
796 foreach ( $and_condition as $or_condition ) {
797
798 if ( ! isset( WPSC_Ticket_Conditions::$conditions[ $or_condition['slug'] ] ) ) {
799 continue;
800 }
801
802 if ( isset( $response[ $or_condition['slug'] ] ) && ! $response[ $or_condition['slug'] ] ) {
803 continue;
804 }
805
806 $cf = WPSC_Custom_Field::get_cf_by_slug( $or_condition['slug'] );
807 $value = $cf->type::get_tff_value( $cf->slug, $cf );
808 if ( $cf->type::is_valid( $or_condition, $cf, $value ) ) {
809 $temp = true;
810 break;
811 }
812 }
813
814 if ( ! $temp ) {
815 $flag = false;
816 break;
817 }
818 }
819
820 $response[ $slug ] = $flag ? 1 : 0;
821 }
822
823 wp_send_json( $response );
824 }
825
826 /**
827 * Create as customer field values
828 *
829 * @return void
830 */
831 public static function get_create_as_customer_fields() {
832
833 if ( check_ajax_referer( 'wpsc_get_create_as_customer_fields', '_ajax_nonce', false ) != 1 ) {
834 wp_send_json_error( 'Unauthorized request!', 401 );
835 }
836
837 $current_user = WPSC_Current_User::$current_user;
838 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
839 wp_send_json_error( new WP_Error( '001', 'Unauthorized!' ), 400 );
840 }
841
842 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
843 if ( ! $email ) {
844 wp_send_json_error( new WP_Error( '002', 'Something went wrong!' ), 400 );
845 }
846
847 WPSC_Current_User::change_current_user( $email );
848
849 $tff = get_option( 'wpsc-tff' );
850 $response = array();
851 foreach ( $tff as $slug => $properties ) {
852
853 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
854 if ( ! $cf ) {
855 continue;
856 }
857 if ( $cf->field === 'customer' && ! $cf->type::$is_default ) {
858 $response[] = array(
859 'slug' => $slug,
860 'html' => $cf->type::print_tff( $cf, $properties ),
861 );
862 }
863 }
864
865 wp_send_json( $response );
866 }
867 }
868 endif;
869
870 WPSC_New_Ticket::init();
871