PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.7
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.3.7, at includes/admin/tickets/class-wpsc-new-ticket.php

803 lines 26.4 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( 'Unauthorised 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 <a class="wpsc-link" href="javascript: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' ); ?></a>
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 (!wpsc_validate_ticket_form()) return;
235
236 wpsc_clear_hidden_fields();
237
238 var is_editor = (typeof isWPSCEditor !== 'undefined') ? isWPSCEditor : 0;
239
240 var dataform = new FormData(jQuery('form.wpsc-create-ticket')[0]);
241 dataform.append('is_frontend', supportcandy.is_frontend);
242 dataform.append('is_editor', is_editor);
243 <?php do_action( 'wpsc_js_create_ticket_formdata' ); ?>
244 jQuery('.wpsc-body').html(supportcandy.loader_html);
245 if (supportcandy.is_frontend === '0') {
246 wpsc_scroll_top();
247 }
248
249 <?php
250 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
251 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
252 ?>
253 grecaptcha.ready(function() {
254 grecaptcha.execute('<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>', {action: 'submit_ticket'}).then(function(token) {
255 dataform.append('g-recaptcha-response', token);
256 wpsc_post_ticket_form(dataform);
257 });
258 });
259 <?php
260 } else {
261 ?>
262 wpsc_post_ticket_form(dataform);
263 <?php
264 }
265 ?>
266 }
267
268 /**
269 * Post ticket for to server
270 *
271 * @return void
272 */
273 function wpsc_post_ticket_form(dataform) {
274
275 var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
276 jQuery.ajax({
277 url: supportcandy.ajax_url,
278 type: 'POST',
279 data: dataform,
280 processData: false,
281 contentType: false,
282 error: function (response) {
283 alert(response.responseJSON.data[0].message);
284 if (is_tinymce) {
285 tinyMCE.get('description').setContent('');
286 }
287 wpsc_get_ticket_form();
288 },
289 success: function (response, textStatus, xhr) {
290 if( response.action == 'ticket' ) {
291 wpsc_get_individual_ticket( response.id );
292 }else if( response.action == 'url' ) {
293 window.location.href = response.redirectURL;
294 }else if( response.action == 'text' ) {
295 var classes = 'wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12';
296 jQuery('.wpsc-body').html('<div class="' + classes + '">' + response.thankyouText + '</div>');
297 if (is_tinymce) {
298 tinyMCE.get('description').setContent('');
299 }
300 }
301 wpsc_run_ajax_background_process();
302 }
303 });
304 }
305
306 /**
307 * Check validations for create ticket form
308 *
309 * @return Boolean
310 */
311 function wpsc_validate_ticket_form() {
312
313 var customFields = jQuery('.wpsc-tff.wpsc-visible');
314 var flag = true;
315 jQuery.each(customFields, function(index, customField){
316
317 customField = jQuery(customField);
318 var customFieldType = customField.data('cft');
319 var isValid = true;
320 switch (customFieldType) {
321 <?php do_action( 'wpsc_js_validate_ticket_form' ); ?>
322 }
323 if (!isValid) {
324 flag = false;
325 return false;
326 }
327 });
328 return flag;
329 }
330
331 function wpsc_clear_hidden_fields() {
332 var customFields = jQuery('.wpsc-tff.wpsc-hidden');
333 jQuery.each(customFields, function(index, customField){
334 customField = jQuery(customField);
335 var customFieldType = customField.data('cft');
336 switch (customFieldType) {
337 <?php do_action( 'wpsc_js_clear_value_hidden_fields' ); ?>
338 }
339 });
340 }
341
342 function wpsc_get_create_as_customer_fields(nonce) {
343
344 if (!hasCustomerFields) return;
345
346 var curCustomer = jQuery('.wpsc-create-ticket input.email').val().trim();
347 if (curCustomer == prevCustomer) return;
348 prevCustomer = curCustomer;
349
350 var data = {
351 action: 'wpsc_get_create_as_customer_fields',
352 email: curCustomer,
353 _ajax_nonce: nonce
354 };
355 jQuery.post(supportcandy.ajax_url, data, function (response) {
356
357 jQuery.each( response, function( key, field ) {
358 var currentEl = jQuery('.wpsc-tff.' + field.slug);
359 var nextEl = currentEl.next();
360 currentEl.remove();
361 nextEl.before(field.html);
362 });
363 wpsc_check_tff_visibility();
364 });
365 }
366 <?php
367
368 if ( $is_created_as ) {
369 ?>
370 jQuery(document).ready(function() {
371 <?php /* translators: %1$s: Name, %2$s: Email Address */ ?>
372 var label = '<?php printf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), esc_attr( $customer->name ), esc_attr( $customer->email ) ); ?>';
373 var newOption = new Option(label, <?php echo esc_attr( $customer->id ); ?>, false, false);
374 jQuery('select.create-as').append(newOption);
375 jQuery('select.create-as').val(<?php echo esc_attr( $customer->id ); ?>).trigger('change');
376 jQuery('input.name').val('<?php echo esc_attr( $customer->name ); ?>');
377 jQuery('input.email').val('<?php echo esc_attr( $customer->email ); ?>');
378 wpsc_after_change_create_as();
379 });
380 <?php
381 }
382 ?>
383
384 <?php do_action( 'wpsc_js_ticket_form_functions' ); ?>
385 </script>
386 <?php
387
388 wp_die();
389 }
390
391 /**
392 * Submit ticket
393 *
394 * @return void
395 */
396 public static function set_ticket_form() {
397
398 if ( check_ajax_referer( 'wpsc_set_ticket_form', '_ajax_nonce', false ) != 1 ) {
399 wp_send_json_error( 'Unauthorised request!', 401 );
400 }
401
402 WPSC_MS_Recaptcha::validate( 'submit_ticket' );
403
404 $current_user = WPSC_Current_User::$current_user;
405 $gs = get_option( 'wpsc-gs-general' );
406 $advanced = get_option( 'wpsc-ms-advanced-settings' );
407
408 if ( ! (
409 ( ! $current_user->user->ID && in_array( 'guest', $gs['allow-create-ticket'] ) ) ||
410 ( $current_user->is_agent && in_array( $current_user->agent->role, $gs['allow-create-ticket'] ) ) ||
411 ( ! $current_user->is_agent && $current_user->user->ID && in_array( 'registered-user', $gs['allow-create-ticket'] ) )
412 ) ) {
413 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
414 }
415
416 // group by custom field type.
417 $cfs = array();
418 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
419 $cfs[ $cf->type::$slug ][] = $cf;
420 }
421
422 $data = apply_filters( 'wpsc_create_ticket_data', array(), $cfs, false );
423
424 // Seperate description from $data.
425 $description = $data['description'];
426 unset( $data['description'] );
427
428 // Seperate description attachments from $data.
429 $description_attachments = $data['description_attachments'];
430 unset( $data['description_attachments'] );
431
432 $data['last_reply_on'] = ( new DateTime() )->format( 'Y-m-d H:i:s' );
433
434 // insert ticket data.
435 $ticket = WPSC_Ticket::insert( $data );
436
437 if ( ! $ticket ) {
438 wp_send_json_error( new WP_Error( '001', 'Something went wrong!' ), 500 );
439 }
440
441 $thread_customer = $current_user->is_agent && $current_user->customer->id != $ticket->customer->id && $advanced['raised-by-user'] == 'agent' ? $current_user->customer : $ticket->customer;
442 $ticket->last_reply_by = $thread_customer->id;
443 $ticket->last_reply_source = 'browser';
444 $ticket->auth_code = WPSC_Functions::get_random_string();
445 $ticket->save();
446
447 // replace macros only when current user is an agent.
448 $description = $current_user->is_agent ? WPSC_Macros::replace( $description, $ticket ) : $description;
449
450 $description = preg_replace( '/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', '', $description );
451 // remove divs from message.
452 $description = preg_replace(
453 array( '/<div(.*?)>/s', '/<\/div>/s', '/<table(.*?)>/s', '/<tbody(.*?)>/s', '/<tr(.*?)>/s', '/<th(.*?)>/s', '/<td(.*?)>/s', '/<pre(.*?)>/s', '/<\/pre>/s' ),
454 array( '<p>', '</p>', '<table>', '<tbody>', '<tr>', '<th>', '<td>', '<p>', '</p>' ),
455 $description
456 );
457
458 // set signature if agent.
459 $signature = $current_user->is_agent && ( $current_user->customer->email == $ticket->customer->email || $advanced['raised-by-user'] == 'agent' ) ? $current_user->agent->get_signature() : '';
460 if ( $signature ) {
461 $description .= '<br>' . $signature;
462 }
463
464 // add ticket auth url to tinymce attachments.
465 if ( preg_match_all( '/' . preg_quote( home_url( '/' ), '/' ) . '\?wpsc_attachment=(\d*)/', $description, $matches ) ) {
466 foreach ( $matches[0] as $url ) {
467 $new_url = add_query_arg( 'auth_code', $ticket->auth_code, $url );
468 $description = str_replace( $url, $new_url, $description );
469 }
470 }
471
472 // Create report thread.
473 $thread = WPSC_Thread::insert(
474 array(
475 'ticket' => $ticket->id,
476 'customer' => $thread_customer->id,
477 'type' => 'report',
478 'body' => $description,
479 'attachments' => $description_attachments,
480 'ip_address' => $ticket->ip_address,
481 'source' => $ticket->source,
482 'os' => $ticket->os,
483 'browser' => $ticket->browser,
484 )
485 );
486
487 do_action( 'wpsc_create_new_ticket', $ticket );
488
489 // tinymce img attachments.
490 if ( preg_match_all( '/' . preg_quote( home_url( '/' ), '/' ) . '\?wpsc_attachment=(\d*)(&auth_code=[^&\s]*)?/', $description, $matches ) ) {
491 foreach ( $matches[1] as $id ) {
492 $attachment = new WPSC_Attachment( $id );
493 $attachment->is_active = 1;
494 $attachment->source_id = $thread->id;
495 $attachment->ticket_id = $ticket->id;
496 $attachment->source = 'img_editor';
497 $attachment->save();
498 }
499 }
500
501 $response = array( 'id' => $ticket->id );
502
503 $is_frontend = isset( $_POST['is_frontend'] ) ? intval( $_POST['is_frontend'] ) : 0;
504
505 $thankyou = get_option( 'wpsc-gs-thankyou-page-settings' );
506
507 if ( $current_user->is_agent ) {
508
509 $response['action'] = $thankyou['action-agent'];
510 if ( $thankyou['action-agent'] == 'url' && $thankyou['page-url-agent'] && filter_var( $thankyou['page-url-agent'], FILTER_VALIDATE_URL ) ) {
511 $response['isRedirect'] = 1;
512 $response['redirectURL'] = $thankyou['page-url-agent'];
513 } elseif ( $thankyou['action-agent'] == 'text' ) {
514 $thank_you = $thankyou['html-agent'] ? WPSC_Translations::get( 'wpsc-thankyou-html-agent', $thankyou['html-agent'] ) : $thankyou['html-agent'];
515 $thankyou_text = WPSC_Macros::replace( $thank_you, $ticket );
516 $thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket );
517 $response['isRedirect'] = 0;
518 $response['thankyouText'] = $thankyou_text;
519 }
520 } else {
521
522 $response['action'] = $thankyou['action-customer'];
523 if ( $thankyou['action-customer'] == 'url' && $thankyou['page-url-customer'] && filter_var( $thankyou['page-url-customer'], FILTER_VALIDATE_URL ) ) {
524 $response['isRedirect'] = 1;
525 $response['redirectURL'] = $thankyou['page-url-customer'];
526 } elseif ( $thankyou['action-customer'] == 'text' || ( $current_user->is_guest && $thankyou['action-customer'] == 'ticket' ) ) {
527 $response['action'] = 'text';
528 $thank_you = $thankyou['html-customer'] ? WPSC_Translations::get( 'wpsc-thankyou-html', $thankyou['html-customer'] ) : $thankyou['html-customer'];
529 $thankyou_text = WPSC_Macros::replace( $thank_you, $ticket );
530 $thankyou_text = apply_filters( 'wpsc_after_thankyou_text', $thankyou_text, $ticket );
531 $response['isRedirect'] = 0;
532 $response['thankyouText'] = $thankyou_text;
533 }
534 }
535
536 wp_send_json( $response );
537 }
538
539 /**
540 * Change create as or raised by modal
541 *
542 * @return void
543 */
544 public static function get_change_create_as() {
545
546 if ( check_ajax_referer( 'wpsc_get_change_create_as', '_ajax_nonce', false ) != 1 ) {
547 wp_send_json_error( 'Unauthorised request!', 401 );
548 }
549
550 $current_user = WPSC_Current_User::$current_user;
551
552 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
553 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
554 }
555
556 $title = esc_attr__( 'Add new', 'supportcandy' );
557
558 ob_start();
559 $unique_id = uniqid( 'wpsc_' );
560 ?>
561 <form action="#" onsubmit="return false;" class="frm-add-new-ticket-form-field">
562
563 <div class="wpsc-input-group label">
564 <div class="label-container">
565 <label for="">
566 <?php esc_attr_e( 'Name', 'supportcandy' ); ?>
567 <span class="required-char">*</span>
568 </label>
569 </div>
570 <input class="name <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/>
571 </div>
572
573 <div class="wpsc-input-group label">
574 <div class="label-container">
575 <label for="">
576 <?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>
577 <span class="required-char">*</span>
578 </label>
579 </div>
580 <input class="email <?php echo esc_attr( $unique_id ); ?>" type="text" autocomplete="off"/>
581 </div>
582
583 </form>
584 <?php
585 $body = ob_get_clean();
586
587 ob_start();
588 ?>
589 <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' ) ); ?>');">
590 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
591 </button>
592 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
593 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
594 </button>
595 <?php
596 do_action( 'wpsc_get_add_new_tff' );
597 $footer = ob_get_clean();
598
599 $response = array(
600 'title' => $title,
601 'body' => $body,
602 'footer' => $footer,
603 );
604 wp_send_json( $response );
605 }
606
607 /**
608 * Create as auto-complete
609 *
610 * @return void
611 */
612 public static function create_as_autocomplete() {
613
614 if ( check_ajax_referer( 'wpsc_create_as_autocomplete', '_ajax_nonce', false ) != 1 ) {
615 wp_send_json_error( 'Unauthorised 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 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
625 if ( ! $term ) {
626 wp_send_json_error( new WP_Error( '002', 'Search term should have at least one character!' ), 400 );
627 }
628
629 wp_send_json(
630 array_map(
631 fn( $customer ) => array(
632 'id' => $customer->id,
633 'text' => sprintf(
634 /* translators: %1$s: Name, %2$s: Email Address */
635 esc_attr__( '%1$s (%2$s)', 'supportcandy' ),
636 $customer->name,
637 $customer->email
638 ),
639 'email' => $customer->email,
640 'name' => $customer->name,
641 ),
642 WPSC_Customer::customer_search( $term )
643 )
644 );
645 }
646
647 /**
648 * Create as add new customer
649 *
650 * @return void
651 */
652 public static function add_new_create_as() {
653
654 if ( check_ajax_referer( 'wpsc_add_new_create_as', '_ajax_nonce', false ) != 1 ) {
655 wp_send_json_error( 'Unauthorised request!', 401 );
656 }
657
658 $current_user = WPSC_Current_User::$current_user;
659
660 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
661 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
662 }
663
664 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
665 if ( ! $name ) {
666 wp_send_json_error( 'Bad request', 400 );
667 }
668
669 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
670 if ( ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
671 wp_send_json_error( 'Bad request', 400 );
672 }
673
674 $customer = WPSC_Customer::get_by_email( $email );
675 if ( ! $customer->id ) {
676 $customer = WPSC_Customer::insert(
677 array(
678 'user' => 0,
679 'name' => $name,
680 'email' => $email,
681 )
682 );
683 }
684
685 $response = array(
686 'id' => $customer->id,
687 'name' => $customer->name,
688 'email' => $customer->email,
689 /* translators: %1$s: Name, %2$s: Email Address */
690 'label' => sprintf( esc_attr__( '%1$s (%2$s)', 'supportcandy' ), $customer->name, $customer->email ),
691 );
692
693 wp_send_json( $response, 200 );
694 }
695
696 /**
697 * Return visibility condition results
698 *
699 * @return void
700 */
701 public static function check_tff_visibility() {
702
703 $response = array();
704 $tff = get_option( 'wpsc-tff', array() );
705 foreach ( $tff as $slug => $settings ) {
706
707 $visibility = WPSC_TFF::get_visibility( $settings, true );
708 if ( ! count( $visibility ) ) {
709 continue;
710 }
711
712 $current_user = WPSC_Current_User::$current_user;
713
714 if (
715 isset( $settings['allowed_user'] ) &&
716 (
717 ( $current_user->is_agent && $settings['allowed_user'] == 'customer' ) ||
718 ( ! $current_user->is_agent && $settings['allowed_user'] == 'agent' )
719 )
720 ) {
721 continue;
722 }
723
724 $flag = true;
725 foreach ( $visibility as $and_condition ) {
726
727 $temp = false;
728 foreach ( $and_condition as $or_condition ) {
729
730 if ( ! isset( WPSC_Ticket_Conditions::$conditions[ $or_condition['slug'] ] ) ) {
731 continue;
732 }
733
734 if ( isset( $response[ $or_condition['slug'] ] ) && ! $response[ $or_condition['slug'] ] ) {
735 continue;
736 }
737
738 $cf = WPSC_Custom_Field::get_cf_by_slug( $or_condition['slug'] );
739 $value = $cf->type::get_tff_value( $cf->slug, $cf );
740 if ( $cf->type::is_valid( $or_condition, $cf, $value ) ) {
741 $temp = true;
742 break;
743 }
744 }
745
746 if ( ! $temp ) {
747 $flag = false;
748 break;
749 }
750 }
751
752 $response[ $slug ] = $flag ? 1 : 0;
753 }
754
755 wp_send_json( $response );
756 }
757
758 /**
759 * Create as customer field values
760 *
761 * @return void
762 */
763 public static function get_create_as_customer_fields() {
764
765 if ( check_ajax_referer( 'wpsc_get_create_as_customer_fields', '_ajax_nonce', false ) != 1 ) {
766 wp_send_json_error( 'Unauthorised request!', 401 );
767 }
768
769 $current_user = WPSC_Current_User::$current_user;
770 if ( ! ( $current_user->is_agent && $current_user->agent->has_cap( 'create-as' ) ) ) {
771 wp_send_json_error( new WP_Error( '001', 'Unauthorized!' ), 400 );
772 }
773
774 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
775 if ( ! $email ) {
776 wp_send_json_error( new WP_Error( '002', 'Something went wrong!' ), 400 );
777 }
778
779 WPSC_Current_User::change_current_user( $email );
780
781 $tff = get_option( 'wpsc-tff' );
782 $response = array();
783 foreach ( $tff as $slug => $properties ) {
784
785 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
786 if ( ! $cf ) {
787 continue;
788 }
789 if ( $cf->field === 'customer' && ! $cf->type::$is_default ) {
790 $response[] = array(
791 'slug' => $slug,
792 'html' => $cf->type::print_tff( $cf, $properties ),
793 );
794 }
795 }
796
797 wp_send_json( $response );
798 }
799 }
800 endif;
801
802 WPSC_New_Ticket::init();
803