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

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