PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.8
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.8
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 / custom-field-types / class-wpsc-df-assigned-agent.php

class-wpsc-df-assigned-agent.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.8, at includes/custom-field-types/class-wpsc-df-assigned-agent.php

1,473 lines 44.1 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_DF_Assigned_Agent' ) ) :
7
8 final class WPSC_DF_Assigned_Agent {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_assigned_agent';
16
17 /**
18 * Set whether this custom field type is of type date
19 *
20 * @var boolean
21 */
22 public static $is_date = false;
23
24 /**
25 * Set whether this custom field type has applicable to date range
26 *
27 * @var boolean
28 */
29 public static $has_date_range = false;
30
31 /**
32 * Set whether this custom field type has multiple values
33 *
34 * @var boolean
35 */
36 public static $has_multiple_val = true;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = 'TEXT NULL DEFAULT NULL';
44
45 /**
46 * Set whether this custom field type has reference to other class
47 *
48 * @var boolean
49 */
50 public static $has_ref = true;
51
52 /**
53 * Reference class for this custom field type so that its value(s) return with object or array of objects automatically. Empty string indicate no reference.
54 *
55 * @var string
56 */
57 public static $ref_class = 'wpsc_agent';
58
59 /**
60 * Set whether this custom field field type is system default (no fields can be created from it).
61 *
62 * @var boolean
63 */
64 public static $is_default = true;
65
66 /**
67 * Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc.
68 *
69 * @var boolean
70 */
71 public static $has_extra_info = true;
72
73 /**
74 * Set whether this custom field type can accept personal info.
75 *
76 * @var boolean
77 */
78 public static $has_personal_info = false;
79
80 /**
81 * Set whether fields created from this custom field type is allowed in create ticket form
82 *
83 * @var boolean
84 */
85 public static $is_ctf = true;
86
87 /**
88 * Set whether fields created from this custom field type is allowed in ticket list
89 *
90 * @var boolean
91 */
92 public static $is_list = true;
93
94 /**
95 * Set whether fields created from this custom field type is allowed in ticket filter
96 *
97 * @var boolean
98 */
99 public static $is_filter = true;
100
101 /**
102 * Set whether fields created from this custom field type can be given character limits
103 *
104 * @var boolean
105 */
106 public static $has_char_limit = false;
107
108 /**
109 * Set whether fields created from this custom field type has custom options set in options table
110 *
111 * @var boolean
112 */
113 public static $has_options = false;
114
115 /**
116 * Set whether fields created from this custom field type can be available for ticket list sorting
117 *
118 * @var boolean
119 */
120 public static $is_sort = false;
121
122 /**
123 * Set whether fields created from this custom field type can be auto-filled
124 *
125 * @var boolean
126 */
127 public static $is_auto_fill = true;
128
129 /**
130 * Set whether fields created from this custom field type can have placeholder
131 *
132 * @var boolean
133 */
134 public static $is_placeholder = true;
135
136 /**
137 * Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form
138 *
139 * @var boolean
140 */
141 public static $is_visibility_conditions = true;
142
143 /**
144 * Set whether fields created from this custom field type is applicable for macros
145 *
146 * @var boolean
147 */
148 public static $has_macro = true;
149
150 /**
151 * Set whether fields of this custom field type is applicalbe for search on ticket list page.
152 *
153 * @var boolean
154 */
155 public static $is_search = false;
156
157 /**
158 * Initialize the class
159 *
160 * @return void
161 */
162 public static function init() {
163
164 // Get object of this class.
165 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
166
167 // TFF settings!
168 add_action( 'wpsc_get_add_new_tff', array( __CLASS__, 'get_add_new_tff' ) );
169 add_action( 'wpsc_jse_add_tff_change_field', array( __CLASS__, 'js_add_tff_change' ) );
170 add_filter( 'wpsc_tff_add_new', array( __CLASS__, 'set_add_new_tff' ), 10, 2 );
171 add_action( 'wpsc_get_edit_tff', array( __CLASS__, 'get_edit_tff' ), 10, 2 );
172 add_filter( 'wpsc_set_edit_tff', array( __CLASS__, 'set_edit_tff' ), 10, 2 );
173
174 // ticket form.
175 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
176 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
177 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
178
179 // create ticket data for rest api.
180 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
181
182 // Assign default agent if not already assigned.
183 add_action( 'wpsc_create_new_ticket', array( __CLASS__, 'assign_default_agent' ) );
184
185 // agent autocomplete filter access only.
186 add_action( 'wp_ajax_wpsc_agent_autocomplete_assigned_agent', array( __CLASS__, 'agent_autocomplete_assigned_agent' ) );
187 add_action( 'wp_ajax_nopriv_wpsc_agent_autocomplete_assigned_agent', array( __CLASS__, 'agent_autocomplete_assigned_agent' ) );
188
189 // autocomplete.
190 add_action( 'wp_ajax_wpsc_agent_autocomplete_df_aa', array( __CLASS__, 'agent_autocomplete_df_aa' ) );
191 add_action( 'wp_ajax_nopriv_wpsc_agent_autocomplete_df_aa', array( __CLASS__, 'agent_autocomplete_df_aa' ) );
192
193 // rest api.
194 add_filter( 'wpsc_rest_prevent_ticket_data', array( __CLASS__, 'rest_prevent_ticket_data' ) );
195
196 // Agent assign ticket to self.
197 add_action( 'wp_ajax_wpsc_self_assign_ticket', array( __CLASS__, 'self_assign_ticket' ) );
198 add_action( 'wp_ajax_nopriv_wpsc_self_assign_ticket', array( __CLASS__, 'self_assign_ticket' ) );
199 }
200
201 /**
202 * Load current class to reference classes
203 *
204 * @param array $classes - Associative array of class names indexed by its slug.
205 * @return array
206 */
207 public static function load_ref_class( $classes ) {
208
209 $classes[ self::$slug ] = array(
210 'class' => __CLASS__,
211 'save-key' => 'id',
212 );
213 return $classes;
214 }
215
216 /**
217 * Print operators for ticket form filter
218 *
219 * @param WPSC_Custom_Field $cf - custom field object.
220 * @param array $filter - Existing filters (if any).
221 * @return void
222 */
223 public static function get_operators( $cf, $filter = array() ) {
224 ?>
225
226 <div class="item conditional">
227 <select class="operator" onchange="wpsc_tc_get_operand(this, '<?php echo esc_attr( $cf->slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tc_get_operand' ) ); ?>');">
228 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
229 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
230 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
231 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
232 </select>
233 </div>
234 <?php
235 }
236
237 /**
238 * Print operators for ticket form filter
239 *
240 * @param string $operator - condition operator on which operands should be returned.
241 * @param WPSC_Custom_Field $cf - custom field object.
242 * @param array $filter - Exising functions (if any).
243 * @return void
244 */
245 public static function get_operands( $operator, $cf, $filter = array() ) {
246
247 $is_multiple = $operator !== '=' ? true : false;
248 $unique_id = uniqid( 'wpsc_' );
249 ?>
250 <div class="item conditional operand single">
251 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
252
253 <?php
254 if ( $is_multiple && isset( $filter['operand_val_1'] ) ) {
255
256 foreach ( $filter['operand_val_1'] as $agent_id ) {
257
258 if ( $agent_id == '0' ) {
259
260 ?>
261 <option selected="selected" value="0"><?php esc_attr_e( 'None', 'supportcandy' ); ?></option>
262 <?php
263
264 } else {
265
266 $agent = new WPSC_Agent( intval( $agent_id ) )
267 ?>
268 <option selected="selected" value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
269 <?php
270 }
271 }
272 }
273
274 if ( ! $is_multiple && isset( $filter['operand_val_1'] ) ) {
275
276 if ( $filter['operand_val_1'] == '0' ) {
277
278 ?>
279 <option selected="selected" value="0"><?php esc_attr_e( 'None', 'supportcandy' ); ?></option>
280 <?php
281
282 } else {
283
284 $agent = new WPSC_Agent( intval( $filter['operand_val_1'] ) )
285 ?>
286 <option selected="selected" value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
287 <?php
288 }
289 }
290 ?>
291
292 </select>
293 </div>
294 <script>
295 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
296 ajax: {
297 url: supportcandy.ajax_url,
298 dataType: 'json',
299 delay: 250,
300 data: function (params) {
301 return {
302 q: params.term, // search term
303 page: params.page,
304 action: 'wpsc_agent_autocomplete_assigned_agent',
305 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_assigned_agent' ) ); ?>',
306 };
307 },
308 processResults: function (data, params) {
309 var terms = [];
310 if ( data ) {
311 jQuery.each( data, function( id, text ) {
312 terms.push( { id: text.id, text: text.title } );
313 });
314 }
315 return {
316 results: terms
317 };
318 },
319 cache: true
320 },
321 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
322 minimumInputLength: 0
323 });
324 </script>
325 <?php
326 }
327
328 /**
329 * Check whether or not given condition is valid when it compared with given value.
330 *
331 * @param array $condition - condition data.
332 * @param WPSC_Custom_Field $cf - custom field object.
333 * @param mixed $assigned_agents - value to comapre.
334 * @return boolean
335 */
336 public static function is_valid( $condition, $cf, $assigned_agents ) {
337
338 $response = false;
339 switch ( $condition['operator'] ) {
340
341 case '=':
342 if ( $condition['operand_val_1'] == 0 ) {
343 $response = ! $assigned_agents ? true : false;
344 } else {
345 $response = in_array( $condition['operand_val_1'], $assigned_agents );
346 }
347 break;
348
349 case 'IN':
350 $flag = false;
351 foreach ( $condition['operand_val_1'] as $agent ) {
352
353 if ( ( $agent == 0 && ! $assigned_agents ) ||
354 in_array( $agent, $assigned_agents )
355 ) {
356 $flag = true;
357 break;
358 }
359 }
360 $response = $flag;
361 break;
362
363 case 'NOT IN':
364 $flag = true;
365 foreach ( $condition['operand_val_1'] as $agent ) {
366
367 if ( ( $agent == 0 && ! $assigned_agents ) ||
368 in_array( $agent, $assigned_agents )
369 ) {
370 $flag = false;
371 break;
372 }
373 }
374 $response = $flag;
375 break;
376 }
377 return $response;
378 }
379
380 /**
381 * Parse filter and return sql query to be merged in ticket model query builder
382 *
383 * @param WPSC_Custom_Field $cf - custom field of this type.
384 * @param mixed $compare - comparison operator.
385 * @param mixed $val - value to compare.
386 * @return string
387 */
388 public static function parse_filter( $cf, $compare, $val ) {
389
390 $str = '';
391
392 switch ( $compare ) {
393
394 case '=':
395 if ( $val == '' || $val == '0' ) {
396 $val = '^$';
397 }
398 $str = 't.' . $cf->slug . ' RLIKE \'(^|[|])' . esc_sql( $val ) . '($|[|])\'';
399 break;
400
401 case 'IN':
402 foreach ( $val as $index => $value ) {
403 if ( $value == '' || $value == '0' ) {
404 $val[ $index ] = '^$';
405 }
406 }
407 $str = 't.' . $cf->slug . ' RLIKE \'(^|[|])(' . implode( '|', esc_sql( $val ) ) . ')($|[|])\'';
408 break;
409
410 case 'NOT IN':
411 foreach ( esc_sql( $val ) as $index => $value ) {
412 if ( $value == '' || $value == '0' ) {
413 $val[ $index ] = '^$';
414 }
415 }
416 $str = 't.' . $cf->slug . ' NOT RLIKE \'(^|[|])(' . implode( '|', esc_sql( $val ) ) . ')($|[|])\'';
417 break;
418
419 default:
420 $str = '1=1';
421 }
422
423 return $str;
424 }
425
426 /**
427 * Check ticket condition
428 *
429 * @param array $condition - array with condition data.
430 * @param WPSC_Custom_Field $cf - custom field object.
431 * @param WPSC_Ticket $ticket - ticket object.
432 * @return boolean
433 */
434 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
435
436 $flag = true;
437 $agent_ids = array_filter(
438 array_map(
439 fn( $agent ) => $agent->id,
440 $ticket->{$cf->slug}
441 )
442 );
443
444 switch ( $condition['operator'] ) {
445
446 case '=':
447 if ( $condition['operand_val_1'] == 0 ) {
448 $flag = ! $agent_ids ? true : false;
449 } else {
450 $flag = in_array( $condition['operand_val_1'], $agent_ids ) ? true : false;
451 }
452 break;
453
454 case 'IN':
455 if ( ! $agent_ids ) {
456 $flag = in_array( '0', $condition['operand_val_1'] ) ? true : false;
457 } else {
458
459 $flag = false;
460 foreach ( $agent_ids as $id ) {
461 if ( in_array( $id, $condition['operand_val_1'] ) ) {
462 $flag = true;
463 break;
464 }
465 }
466 }
467 break;
468
469 case 'NOT IN':
470 if ( ! $agent_ids ) {
471 $flag = ! in_array( '0', $condition['operand_val_1'] ) ? true : false;
472 } else {
473
474 foreach ( $agent_ids as $id ) {
475 if ( in_array( $id, $condition['operand_val_1'] ) ) {
476 $flag = false;
477 break;
478 }
479 }
480 }
481 break;
482
483 default:
484 $flag = true;
485 }
486
487 return $flag;
488 }
489
490 /**
491 * Print ticket form field
492 *
493 * @param WPSC_Custom_Field $cf - Custom field object.
494 * @param array $tff - Array of ticket form field settings for this field.
495 * @return string
496 */
497 public static function print_tff( $cf, $tff ) {
498
499 $current_user = WPSC_Current_User::$current_user;
500 $is_multiple = $tff['is_multiple'] ? true : false;
501 $allowed_user = $tff['allowed_user'];
502
503 if ( ! (
504 ( $current_user->is_agent && in_array( $allowed_user, array( 'agent', 'both' ) ) ) ||
505 ( ! $current_user->is_agent && in_array( $allowed_user, array( 'customer', 'both' ) ) )
506 ) ) {
507 return;
508 }
509
510 $unique_id = uniqid( 'wpsc_' );
511 $default_val = $cf->is_auto_fill && $cf->default_value ? $cf->default_value : array();
512
513 ob_start();
514
515 ?>
516 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>">
517
518 <div class="wpsc-tff-label">
519 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
520
521 <?php
522 if ( $tff['is-required'] ) {
523 ?>
524 <span class="required-indicator">*</span>
525 <?php
526 }
527 ?>
528
529 </div>
530
531 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
532
533 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>[]" onchange="wpsc_check_tff_visibility()" <?php echo $is_multiple ? 'multiple' : ''; ?>>
534
535 <?php
536 if ( ! $tff['is_ajax'] ) {
537
538 $agents = WPSC_Agent::find(
539 array(
540 'items_per_page' => 0,
541 'meta_query' => array(
542 'relation' => 'AND',
543 array(
544 'slug' => 'is_active',
545 'compare' => '=',
546 'val' => 1,
547 ),
548 ),
549 )
550 )['results'];
551
552 foreach ( $agents as $agent ) {
553
554 $selected = in_array( $agent->id, $default_val ) ? 'selected' : ''
555
556 ?>
557 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
558 <?php
559
560 }
561 } else {
562
563 foreach ( $default_val as $agent_id ) {
564
565 $agent = new WPSC_Agent( $agent_id );
566
567 ?>
568 <option selected value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
569 <?php
570
571 }
572 }
573 ?>
574 </select>
575 <script>
576
577 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo(
578 <?php
579 if ( $tff['is_ajax'] ) :
580 ?>
581
582 {
583 ajax: {
584 url: supportcandy.ajax_url,
585 dataType: 'json',
586 delay: 250,
587 data: function (params) {
588 return {
589 q: params.term, // search term
590 page: params.page,
591 action: 'wpsc_agent_autocomplete_df_aa',
592 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_df_aa' ) ); ?>',
593 isMultiple: 1,
594 };
595 },
596 processResults: function (data, params) {
597 var terms = [];
598 if ( data ) {
599 jQuery.each( data, function( id, text ) {
600 terms.push( { id: text.id, text: text.title } );
601 });
602 }
603 return {
604 results: terms
605 };
606 },
607 cache: true
608 },
609 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
610 minimumInputLength: 0,
611 allowClear: false,
612 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
613 }
614 <?php
615 endif;
616 ?>
617 );
618 </script>
619 </div>
620 <?php
621 return ob_get_clean();
622 }
623
624 /**
625 * Validate this type field in create ticket
626 *
627 * @return void
628 */
629 public static function js_validate_ticket_form() {
630 ?>
631
632 case '<?php echo esc_attr( self::$slug ); ?>':
633 var val = customField.find('select').first().val();
634 if (customField.hasClass('required') && val.length === 0) {
635 isValid = false;
636 alert(supportcandy.translations.req_fields_missing);
637 }
638 break;
639 <?php
640 echo PHP_EOL;
641 }
642
643 /**
644 * Return custom field value in $_POST
645 * Ignore phpcs nonce issue as we already checked where it is called from.
646 *
647 * @param string $slug - Custom field slug.
648 * @param mixed $cf - Custom field object or false.
649 * @return mixed
650 */
651 public static function get_tff_value( $slug, $cf = false ) {
652
653 if ( isset( $_POST[ $slug ] ) ) { // phpcs:ignore
654 $assignee = array_map( 'intval', $_POST[ $slug ] ); // phpcs:ignore
655 return array_unique(
656 array_filter(
657 array_map(
658 function ( $id ) {
659 $agent = new WPSC_Agent( $id );
660 return $agent->id ? $agent->id : false;
661 },
662 $assignee
663 )
664 )
665 );
666 }
667
668 return array();
669 }
670
671 /**
672 * Print tff fields related to assigned agent in add new tff
673 *
674 * @param string $unique_id - unique id for the tff form.
675 * @return void
676 */
677 public static function get_add_new_tff( $unique_id ) {
678
679 ?>
680 <div class="wpsc-input-group is-ajax <?php echo esc_attr( $unique_id ); ?>" style="display: none;">
681 <div class="label-container">
682 <label for=""><?php esc_attr_e( 'Ajax loading of results?', 'supportcandy' ); ?></label>
683 </div>
684 <select name="is-ajax">
685 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
686 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
687 </select>
688 </div>
689
690 <div class="wpsc-input-group is-multiple <?php echo esc_attr( $unique_id ); ?>" style="display: none;">
691 <div class="label-container">
692 <label for=""><?php esc_attr_e( 'Is multiple?', 'supportcandy' ); ?></label>
693 </div>
694 <select name="is-multiple">
695 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
696 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
697 </select>
698 </div>
699
700 <div class="wpsc-input-group allowed-user <?php echo esc_attr( $unique_id ); ?>" style="display: none;">
701 <div class="label-container">
702 <label for=""><?php esc_attr_e( 'Allowed user', 'supportcandy' ); ?></label>
703 </div>
704 <select name="allowed-user">
705 <option value="agent"><?php esc_attr_e( 'Agent', 'supportcandy' ); ?></option>
706 <option value="customer"><?php esc_attr_e( 'Customer', 'supportcandy' ); ?></option>
707 <option value="both"><?php esc_attr_e( 'Both', 'supportcandy' ); ?></option>
708 </select>
709 </div>
710 <?php
711 }
712
713 /**
714 * Change event callback extension for tff add new field
715 *
716 * @param string $unique_id - unique id for the tff form.
717 * @return void
718 */
719 public static function js_add_tff_change( $unique_id ) {
720 ?>
721
722 // Assigned agent
723 if (slug == 'assigned_agent') {
724 jQuery('div.allowed-user.<?php echo esc_attr( $unique_id ); ?>').show();
725 jQuery('div.is-multiple.<?php echo esc_attr( $unique_id ); ?>').show();
726 jQuery('div.is-ajax.<?php echo esc_attr( $unique_id ); ?>').show();
727 } else {
728 jQuery('div.allowed-user.<?php echo esc_attr( $unique_id ); ?>').hide();
729 jQuery('div.is-multiple.<?php echo esc_attr( $unique_id ); ?>').hide();
730 jQuery('div.is-ajax.<?php echo esc_attr( $unique_id ); ?>').hide();
731 }
732 <?php
733 }
734
735 /**
736 * Add options for assigned agent while saving field
737 *
738 * @param array $field - custom field data.
739 * @param WPSC_Custom_Field $cf - custom field object.
740 * @return array
741 */
742 public static function set_add_new_tff( $field, $cf ) {
743
744 // ignore phpcs nonce issue as we already checked where it is called from.
745 if ( $cf->slug == 'assigned_agent' ) {
746 $field['is_ajax'] = isset( $_POST['is-ajax'] ) ? intval( $_POST['is-ajax'] ) : 1; // phpcs:ignore
747 $field['is_multiple'] = isset( $_POST['is-multiple'] ) ? intval( $_POST['is-multiple'] ) : 1; // phpcs:ignore
748 $field['allowed_user'] = isset( $_POST['allowed-user'] ) ? sanitize_text_field( wp_unslash( $_POST['allowed-user'] ) ) : 'agent'; // phpcs:ignore
749 }
750 return $field;
751 }
752
753 /**
754 * Print tff fields related to assigned agent in edit tff form
755 *
756 * @param array $field - custom field data.
757 * @param WPSC_Custom_Field $cf - custom field object.
758 * @return void
759 */
760 public static function get_edit_tff( $field, $cf ) {
761
762 if ( $cf->slug == 'assigned_agent' ) :
763 ?>
764
765 <div class="wpsc-input-group">
766 <div class="label-container">
767 <label for=""><?php esc_attr_e( 'Ajax loading of results?', 'supportcandy' ); ?></label>
768 </div>
769 <select name="is-ajax">
770 <option <?php selected( $field['is_ajax'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
771 <option <?php selected( $field['is_ajax'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
772 </select>
773 </div>
774
775 <div class="wpsc-input-group">
776 <div class="label-container">
777 <label for=""><?php esc_attr_e( 'Is multiple?', 'supportcandy' ); ?></label>
778 </div>
779 <select name="is-multiple">
780 <option <?php selected( $field['is_multiple'], 1 ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
781 <option <?php selected( $field['is_multiple'], 0 ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
782 </select>
783 </div>
784
785 <div class="wpsc-input-group">
786 <div class="label-container">
787 <label for=""><?php esc_attr_e( 'Allowed user', 'supportcandy' ); ?></label>
788 </div>
789 <select name="allowed-user">
790 <option <?php selected( $field['allowed_user'], 'agent' ); ?> value="agent"><?php esc_attr_e( 'Agent', 'supportcandy' ); ?></option>
791 <option <?php selected( $field['allowed_user'], 'customer' ); ?> value="customer"><?php esc_attr_e( 'Customer', 'supportcandy' ); ?></option>
792 <option <?php selected( $field['allowed_user'], 'both' ); ?> value="both"><?php esc_attr_e( 'Both', 'supportcandy' ); ?></option>
793 </select>
794 </div>
795 <?php
796
797 endif;
798 }
799
800 /**
801 * Set edit tff values for assigned agent
802 *
803 * @param array $field - custom field data.
804 * @param WPSC_Custom_Field $cf - custom field object.
805 * @return array
806 */
807 public static function set_edit_tff( $field, $cf ) {
808
809 // ignore phpcs nonce issue as we already checked where it is called from.
810 if ( $cf->slug == 'assigned_agent' ) {
811 $field['is_ajax'] = isset( $_POST['is-ajax'] ) ? intval( $_POST['is-ajax'] ) : 1; // phpcs:ignore
812 $field['is_multiple'] = isset( $_POST['is-multiple'] ) ? intval( $_POST['is-multiple'] ) : 1; // phpcs:ignore
813 $field['allowed_user'] = isset( $_POST['allowed-user'] ) ? sanitize_text_field( wp_unslash( $_POST['allowed-user'] ) ) : 'agent'; // phpcs:ignore
814 }
815 return $field;
816 }
817
818 /**
819 * Return default value for custom field of this type
820 *
821 * @param WPSC_Custom_Field $cf - custom field object.
822 * @return mixed
823 */
824 public static function get_default_value( $cf ) {
825
826 return $cf->default_value ? implode( '|', $cf->default_value ) : '';
827 }
828
829 /**
830 * Check and return custom field value for new ticket to be created.
831 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
832 * Ignore phpcs nonce issue as we already checked where it is called from.
833 *
834 * @param array $data - Array of values to to stored in ticket in an insert function.
835 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
836 * @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called.
837 * @return array
838 */
839 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
840
841 if ( $is_my_profile ) {
842 return;
843 }
844
845 $current_user = WPSC_Current_User::$current_user;
846 $assignee = self::get_tff_value( 'assigned_agent' );
847 $tff = get_option( 'wpsc-tff' );
848
849 if (
850 ( ! isset( $tff['assigned_agent'] ) && $assignee ) ||
851 (
852 $assignee &&
853 (
854 ( $current_user->is_agent && ! in_array( $tff['assigned_agent']['allowed_user'], array( 'agent', 'both' ) ) ) ||
855 ( ! $current_user->is_agent && $current_user->is_customer && ! in_array( $tff['assigned_agent']['allowed_user'], array( 'customer', 'both' ) ) )
856 )
857 )
858 ) {
859 wp_send_json_error( new WP_Error( 'WPSC_DF_Assigned_Agent', 'Unauthorized!' ), 400 );
860 }
861
862 $data['assigned_agent'] = $assignee ? implode( '|', $assignee ) : '';
863 return $data;
864 }
865
866 /**
867 * Set create ticket data for rest api request
868 *
869 * @param array $data - create ticket data array.
870 * @param WP_REST_Request $request - rest request object.
871 * @param array $custom_fields - custom field objects indexed by unique custom field types.
872 * @return array
873 */
874 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
875
876 $current_user = WPSC_Current_User::$current_user;
877 $tff = get_option( 'wpsc-tff' );
878
879 $assignee = array_unique(
880 array_filter(
881 array_map(
882 function ( $id ) {
883 $agent = new WPSC_Agent( intval( $id ) );
884 return $agent->id ? $agent->id : false;
885 },
886 explode( ',', sanitize_text_field( $request->get_param( 'assigned_agent' ) ) )
887 )
888 )
889 );
890
891 if (
892 ( ! isset( $tff['assigned_agent'] ) && $assignee ) ||
893 (
894 $assignee &&
895 (
896 ( $current_user->is_agent && ! in_array( $tff['assigned_agent']['allowed_user'], array( 'agent', 'both' ) ) ) ||
897 ( ! $current_user->is_agent && $current_user->is_customer && ! in_array( $tff['assigned_agent']['allowed_user'], array( 'customer', 'both' ) ) )
898 )
899 )
900 ) {
901 $data['assigned_agent'] = '';
902 return $data;
903 }
904
905 $data['assigned_agent'] = $assignee ? implode( '|', $assignee ) : '';
906 return $data;
907 }
908
909 /**
910 * Return val field for meta query of this type of custom field
911 *
912 * @param array $condition - condition data.
913 * @return mixed
914 */
915 public static function get_meta_value( $condition ) {
916
917 $operator = $condition['operator'];
918 switch ( $operator ) {
919
920 case '=':
921 case 'IN':
922 case 'NOT IN':
923 return $condition['operand_val_1'];
924 }
925 return false;
926 }
927
928 /**
929 * Print edit custom field properties
930 *
931 * @param WPSC_Custom_Fields $cf - custom field object.
932 * @param string $field_class - class name of field category.
933 * @return void
934 */
935 public static function get_edit_custom_field_properties( $cf, $field_class ) {
936
937 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
938 ?>
939 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
940 <div class="label-container">
941 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
942 </div>
943 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
944 </div>
945 <?php
946 endif;
947
948 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
949 ?>
950
951 <div data-type="textfield" data-required="false" class="wpsc-input-group default_value">
952
953 <div class="label-container">
954 <label for="">
955 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
956 </label>
957 </div>
958
959 <div class="ticket-fields-assigned-agent">
960 <select multiple class="wpsc-default-assigned-agent" name="default_value[]">
961 <?php
962 $multi_dopt = $cf->default_value ? $cf->default_value : array();
963 foreach ( $multi_dopt as $agent_id ) {
964 $agent = new WPSC_Agent( intval( $agent_id ) );
965 ?>
966 <option selected="selected" value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
967 <?php
968 }
969 ?>
970 </select>
971 </div>
972
973 <script>
974 jQuery('.wpsc-default-assigned-agent').selectWoo({
975 ajax: {
976 url: supportcandy.ajax_url,
977 dataType: 'json',
978 delay: 250,
979 data: function (params) {
980 return {
981 q: params.term, // search term
982 page: params.page,
983 action: 'wpsc_agent_autocomplete_admin_access',
984 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_admin_access' ) ); ?>',
985 };
986 },
987 processResults: function (data, params) {
988 var terms = [];
989 if ( data ) {
990 jQuery.each( data, function( id, text ) {
991 terms.push( { id: text.id, text: text.title } );
992 });
993 }
994 return {
995 results: terms
996 };
997 },
998 cache: true
999 },
1000 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
1001 minimumInputLength: 1,
1002 allowClear: false,
1003 placeholder: ""
1004 });
1005 </script>
1006 </div>
1007 <?php
1008 endif;
1009
1010 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
1011 ?>
1012 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
1013 <div class="label-container">
1014 <label for="">
1015 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
1016 </label>
1017 </div>
1018 <select name="is_auto_fill">
1019 <option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1020 <option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1021 </select>
1022 </div>
1023 <?php
1024 endif;
1025
1026 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
1027 ?>
1028 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
1029 <div class="label-container">
1030 <label for="">
1031 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
1032 </label>
1033 </div>
1034 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
1035 </div>
1036 <?php
1037 endif;
1038
1039 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
1040 ?>
1041 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
1042 <div class="label-container">
1043 <label for="">
1044 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
1045 </label>
1046 </div>
1047 <input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off">
1048 </div>
1049 <?php
1050 endif;
1051 }
1052
1053 /**
1054 * Set custom field properties. Can be used by add/edit custom field.
1055 * Ignore phpcs nonce issue as we already checked where it is called from.
1056 *
1057 * @param WPSC_Custom_Field $cf - custom field object.
1058 * @param string $field_class - class of field category.
1059 * @return void
1060 */
1061 public static function set_cf_properties( $cf, $field_class ) {
1062
1063 // extra info.
1064 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
1065 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
1066 }
1067
1068 // default value.
1069 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
1070 $cf->default_value = isset( $_POST['default_value'] ) ? array_filter( array_map( 'intval', $_POST['default_value'] ) ) : array(); // phpcs:ignore
1071 }
1072
1073 // auto fill.
1074 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
1075 $cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
1076 }
1077
1078 // placeholder!
1079 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
1080 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
1081 }
1082
1083 // tl_width!
1084 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
1085 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
1086 $cf->tl_width = $tl_width ? $tl_width : 100;
1087 }
1088
1089 // save!
1090 $cf->save();
1091
1092 // update widget title!
1093 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
1094 $ticket_widgets['assignee']['title'] = $cf->name;
1095 update_option( 'wpsc-ticket-widget', $ticket_widgets );
1096 }
1097
1098 /**
1099 * Assign default agent to ticket if not already assigned
1100 *
1101 * @param WPSC_Ticket $ticket - ticket object.
1102 * @return void
1103 */
1104 public static function assign_default_agent( $ticket ) {
1105
1106 if ( ! $ticket->assigned_agent ) {
1107 $agents = self::get_default_value( WPSC_Custom_Field::get_cf_by_slug( 'assigned_agent' ) );
1108 $agents = $agents ? explode( '|', $agents ) : array();
1109 $ticket->assigned_agent = $agents;
1110 $ticket->save();
1111 }
1112 }
1113
1114 /**
1115 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
1116 *
1117 * @param WPSC_Custom_Field $cf - custom field object.
1118 * @param WPSC_Ticket $ticket - ticket object.
1119 * @param string $module - module name.
1120 * @return string
1121 */
1122 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
1123
1124 $agents = $ticket->{$cf->slug};
1125 $agent_names = array_filter(
1126 array_map(
1127 fn( $agent ) => $agent->id ? $agent->name : '',
1128 $agents
1129 )
1130 );
1131 $value = $agent_names ? implode( ', ', $agent_names ) : esc_attr__( 'None', 'supportcandy' );
1132
1133 return apply_filters( 'wpsc_ticket_field_val_assigned_agent', $value, $cf, $ticket, $module );
1134 }
1135
1136 /**
1137 * Print ticket value for given custom field on ticket list
1138 *
1139 * @param WPSC_Custom_Field $cf - custom field object.
1140 * @param WPSC_Ticket $ticket - ticket object.
1141 * @return void
1142 */
1143 public static function print_tl_ticket_field_val( $cf, $ticket ) {
1144
1145 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
1146 }
1147
1148 /**
1149 * Print ticket value for given custom field on widget
1150 *
1151 * @param WPSC_Custom_Field $cf - custom field object.
1152 * @param WPSC_Ticket $ticket - ticket object.
1153 * @return void
1154 */
1155 public static function print_widget_ticket_field_val( $cf, $ticket ) {
1156
1157 $current_user = WPSC_Current_User::$current_user;
1158 $ms_advanced = get_option( 'wpsc-ms-advanced-settings' );
1159 $agents = array_filter(
1160 array_map(
1161 fn( $agent ) => $agent->id ? $agent : '',
1162 $ticket->{$cf->slug}
1163 )
1164 );
1165
1166 if ( $agents ) {
1167
1168 foreach ( $agents as $agent ) :
1169 ?>
1170 <div class="user-list-item">
1171 <?php
1172 $email = ! $agent->is_agentgroup ? $agent->customer->email : '';
1173 echo get_avatar( $email, 40 );
1174 ?>
1175 <div class="ul-body">
1176 <div class="ul-label"><?php echo esc_attr( $agent->name ); ?></div>
1177 </div>
1178 </div>
1179 <?php
1180 endforeach;
1181
1182 } else {
1183
1184 if ( $ms_advanced['self-assign'] && $ticket->is_active && WPSC_Individual_Ticket::$view_profile == 'agent' && $current_user->agent->has_cap( 'aa-unassigned' ) ) {
1185 ?>
1186 <a class="wpsc-widget-default wpsc-link" onclick="wpsc_self_assign_ticket(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_self_assign_ticket' ) ); ?>')"><?php esc_attr_e( 'Assign to me', 'supportcandy' ); ?></a>
1187 <?php
1188 } else {
1189 ?>
1190 <div class="wpsc-widget-default"><?php esc_attr_e( 'None', 'supportcandy' ); ?></div>
1191 <?php
1192 }
1193 ?>
1194 <?php
1195 }
1196 }
1197
1198 /**
1199 * Assign ticket to self
1200 *
1201 * @return void
1202 */
1203 public static function self_assign_ticket() {
1204
1205 if ( check_ajax_referer( 'wpsc_self_assign_ticket', '_ajax_nonce', false ) != 1 ) {
1206 wp_send_json_error( 'Unauthorised request!', 401 );
1207 }
1208
1209 $current_user = WPSC_Current_User::$current_user;
1210 $agent = new WPSC_Agent( $current_user->agent->id );
1211 if ( ! WPSC_Individual_Ticket::$view_profile == 'agent' && ! $current_user->agent->has_cap( 'aa-unassigned' ) ) {
1212 return;
1213 }
1214
1215 $ticket_id = isset( $_POST['ticket_id'] ) ? intval( $_POST['ticket_id'] ) : 0;
1216 if ( ! $ticket_id ) {
1217 wp_send_json_error( 'Bad Request', 400 );
1218 }
1219
1220 // Ticket id.
1221 $ticket = new WPSC_Ticket( $ticket_id );
1222 if ( ! $ticket->id ) {
1223 wp_send_json_error( 'Something went wrong!', 400 );
1224 }
1225
1226 if ( $ticket->assigned_agent ) {
1227 wp_send_json_error( 'Something went wrong!', 400 );
1228 }
1229
1230 $new = array( $agent );
1231 $ticket->assigned_agent = $new;
1232 $ticket->save();
1233
1234 do_action( 'wpsc_change_assignee', $ticket, array(), $new, $current_user->customer->id );
1235
1236 // Action if current user assigns ticket to self.
1237 do_action( 'wpsc_self_assign_ticket', $ticket, array(), $new, $current_user->customer->id );
1238
1239 wp_die();
1240 }
1241
1242 /**
1243 * Print given value for custom field
1244 *
1245 * @param WPSC_Custom_Field $cf - custom field object.
1246 * @param mixed $val - value to convert and print.
1247 * @return void
1248 */
1249 public static function print_val( $cf, $val ) {
1250
1251 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
1252 $agents = array_filter(
1253 array_map(
1254 function ( $agent ) {
1255 if ( is_object( $agent ) ) {
1256 return $agent->id ? $agent : '';
1257 } elseif ( $agent ) {
1258 $agent = new WPSC_Agent( $agent );
1259 return $agent->id ? $agent : '';
1260 } else {
1261 return '';
1262 }
1263 },
1264 $val
1265 )
1266 );
1267 $agent_names = array_filter(
1268 array_map(
1269 fn( $agent ) => $agent->id ? $agent->name : '',
1270 $agents
1271 )
1272 );
1273 echo $agent_names ? esc_attr( implode( ', ', $agent_names ) ) : esc_attr__( 'None', 'supportcandy' );
1274 }
1275
1276 /**
1277 * Return printable value for history log macro
1278 *
1279 * @param WPSC_Custom_Field $cf - custom field object.
1280 * @param mixed $val - value to convert and return.
1281 * @return string
1282 */
1283 public static function get_history_log_val( $cf, $val ) {
1284
1285 ob_start();
1286 self::print_val( $cf, $val );
1287 return ob_get_clean();
1288 }
1289
1290 /**
1291 * Agent autocomplete for filter access
1292 *
1293 * @return void
1294 */
1295 public static function agent_autocomplete_assigned_agent() {
1296
1297 if ( check_ajax_referer( 'wpsc_agent_autocomplete_assigned_agent', '_ajax_nonce', false ) !== 1 ) {
1298 wp_send_json_error( 'Unauthorised request!', 401 );
1299 }
1300
1301 $current_user = WPSC_Current_User::$current_user;
1302 if ( $current_user->is_agent ) {
1303 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1304 } elseif ( $current_user->is_customer && ! $current_user->is_agent ) {
1305 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
1306 }
1307
1308 if ( ! ( in_array( 'assigned_agent', $filter_items ) || WPSC_Functions::is_site_admin() ) ) {
1309 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1310 }
1311
1312 $filters = array(
1313 'term' => isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '',
1314 'filter_by' => 'all',
1315 'sort_by' => 'name',
1316 'isMultiple' => 0,
1317 );
1318
1319 $filters['isAgentgroup'] = 0;
1320 if ( class_exists( 'WPSC_Agentgroups' ) ) {
1321 $filters['isAgentgroup'] = isset( $_GET['isAgentgroup'] ) ? intval( $_GET['isAgentgroup'] ) : null;
1322 }
1323
1324 $response = WPSC_Agent::agent_autocomplete( $filters );
1325 wp_send_json( $response );
1326 }
1327
1328 /**
1329 * Agent autocomplete for filter access
1330 *
1331 * @return void
1332 */
1333 public static function agent_autocomplete_df_aa() {
1334
1335 if ( check_ajax_referer( 'wpsc_agent_autocomplete_df_aa', '_ajax_nonce', false ) !== 1 ) {
1336 wp_send_json_error( 'Unauthorised request!', 401 );
1337 }
1338
1339 $tff = get_option( 'wpsc-tff', array() );
1340
1341 $allowed_user = $tff['assigned_agent']['allowed_user'];
1342
1343 $current_user = WPSC_Current_User::$current_user;
1344 if ( ! (
1345 ( $current_user->is_agent && in_array( $allowed_user, array( 'agent', 'both' ) ) ) ||
1346 ( ! $current_user->is_agent && in_array( $allowed_user, array( 'customer', 'both' ) ) )
1347 ) ) {
1348 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1349 }
1350
1351 $filters = array(
1352 'term' => isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '',
1353 'filter_by' => 'all',
1354 'sort_by' => 'name',
1355 'isMultiple' => 0,
1356 );
1357
1358 $filters['isAgentgroup'] = 0;
1359 if ( class_exists( 'WPSC_Agentgroups' ) ) {
1360 $filters['isAgentgroup'] = isset( $_GET['isAgentgroup'] ) ? intval( $_GET['isAgentgroup'] ) : null;
1361 }
1362
1363 $response = WPSC_Agent::agent_autocomplete( $filters );
1364 wp_send_json( $response );
1365 }
1366
1367 /**
1368 * Rest api filter for ticket data to prevent from sending it to client
1369 *
1370 * @param array $data - array of slugs to prevent from.
1371 * @return array
1372 */
1373 public static function rest_prevent_ticket_data( $data ) {
1374
1375 $current_user = WPSC_Current_User::$current_user;
1376 if ( ! $current_user->is_agent ) {
1377 $tff = get_option( 'wpsc-tff' );
1378 $list_items = $current_user->get_tl_list_items();
1379 $widgets = get_option( 'wpsc-ticket-widget' );
1380 if ( ! (
1381 isset( $tff['assigned_agent'] ) ||
1382 in_array( 'assigned_agent', $list_items ) ||
1383 (
1384 $widgets['assignee']['is_enable'] &&
1385 $widgets['assignee']['allow-customer']
1386 )
1387 ) ) {
1388 $data[] = 'assigned_agent';
1389 }
1390 }
1391 return $data;
1392 }
1393
1394 /**
1395 * Print dashboard activity function
1396 *
1397 * @param WPSC_Custom_Field $cf - custom field object.
1398 * @param array $recent_logs - recent_logs object.
1399 * @param array $body - body object.
1400 * @param int $view - check if frontend.
1401 * @return string
1402 */
1403 public static function print_activity( $cf, $recent_logs, $body, $view ) {
1404
1405 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
1406 $new_aa_names = array();
1407 foreach ( $body->new as $agent ) {
1408 $new_aa = new WPSC_Agent( $agent );
1409 if ( $new_aa->id ) {
1410 $new_aa_names[] = esc_attr( $new_aa->name );
1411 }
1412 }
1413 if ( ! empty( $new_aa_names ) ) {
1414
1415 $aa_names = implode( ', ', $new_aa_names );
1416 } else {
1417 $aa_names = 'None';
1418 }
1419 return esc_attr( $recent_logs->customer->name ) . ' assigned ticket <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $aa_names );
1420 }
1421
1422 /**
1423 * Return given value for custom field
1424 *
1425 * @param WPSC_Custom_Field $cf - custom field object.
1426 * @param mixed $val - value to convert and print.
1427 * @return string
1428 */
1429 public static function get_field_value( $cf, $val ) {
1430
1431 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
1432 $agents = array_filter(
1433 array_map(
1434 function ( $agent ) {
1435 if ( is_object( $agent ) ) {
1436 return $agent->id ? $agent : '';
1437 } elseif ( $agent ) {
1438 $agent = new WPSC_Agent( $agent );
1439 return $agent->id ? $agent : '';
1440 } else {
1441 return '';
1442 }
1443 },
1444 $val
1445 )
1446 );
1447 $agent_names = array_filter(
1448 array_map(
1449 fn( $agent ) => $agent->id ? $agent->name : '',
1450 $agents
1451 )
1452 );
1453 return $agent_names ? esc_attr( implode( ', ', $agent_names ) ) : esc_attr__( 'None', 'supportcandy' );
1454 }
1455
1456 /**
1457 * Clear value of hidden fields
1458 *
1459 * @return void
1460 */
1461 public static function js_clear_value_hidden_fields() {
1462 ?>
1463 case '<?php echo esc_attr( self::$slug ); ?>':
1464 customField.find('select').first().val('');
1465 break;
1466 <?php
1467 echo PHP_EOL;
1468 }
1469 }
1470 endif;
1471
1472 WPSC_DF_Assigned_Agent::init();
1473