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

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