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-cf-multi-select.php

class-wpsc-cf-multi-select.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.2, at includes/custom-field-types/class-wpsc-cf-multi-select.php

1,656 lines 50.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_CF_Multi_Select' ) ) :
7
8 final class WPSC_CF_Multi_Select {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'cf_multi_select';
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 = 'TINYTEXT 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_option';
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 = false;
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 = true;
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 has custom options set in options table
103 *
104 * @var boolean
105 */
106 public static $has_options = true;
107
108 /**
109 * Set whether fields created from this custom field type can be given character limits
110 *
111 * @var boolean
112 */
113 public static $has_char_limit = false;
114
115 /**
116 * Set whether fields created from this custom field type can be auto-filled
117 *
118 * @var boolean
119 */
120 public static $is_auto_fill = true;
121
122 /**
123 * Set whether fields created from this custom field type can be available for ticket list sorting
124 *
125 * @var boolean
126 */
127 public static $is_sort = false;
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 = true;
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 // Set custom field type.
168 add_filter( 'wpsc_cf_types', array( __CLASS__, 'add_cf_type' ), 2 );
169
170 // ticket form.
171 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
172 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
173 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
174
175 // create ticket data for rest api.
176 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
177
178 // Ticket model.
179 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
180 add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
181 }
182
183 /**
184 * Load current class to reference classes
185 *
186 * @param array $classes - Associative array of class names indexed by its slug.
187 * @return array
188 */
189 public static function load_ref_class( $classes ) {
190
191 $classes[ self::$slug ] = array(
192 'class' => __CLASS__,
193 'save-key' => 'id',
194 );
195 return $classes;
196 }
197
198 /**
199 * Add custom field type to list
200 *
201 * @param array $cf_types - custom field types array.
202 * @return array
203 */
204 public static function add_cf_type( $cf_types ) {
205
206 $cf_types[ self::$slug ] = array(
207 'label' => esc_attr__( 'Dropdown (multi-select)', 'supportcandy' ),
208 'class' => __CLASS__,
209 );
210 return $cf_types;
211 }
212
213 /**
214 * Print operators for ticket form filter
215 *
216 * @param WPSC_Custom_Field $cf - custom field object.
217 * @param array $filter - Existing filters (if any).
218 * @return void
219 */
220 public static function get_operators( $cf, $filter = array() ) {
221 ?>
222
223 <div class="item conditional">
224 <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' ) ); ?>');">
225 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
226 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
227 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
228 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
229 </select>
230 </div>
231 <?php
232 }
233
234 /**
235 * Print operators for ticket form filter
236 *
237 * @param string $operator - condition operator on which operands should be returned.
238 * @param WPSC_Custom_Field $cf - custom field object.
239 * @param array $filter - Exising functions (if any).
240 * @return void
241 */
242 public static function get_operands( $operator, $cf, $filter = array() ) {
243
244 $is_multiple = $operator !== '=' ? true : false;
245 $options = $cf->get_options();
246 $unique_id = uniqid( 'wpsc_' );
247 ?>
248 <div class="item conditional operand single">
249 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
250 <?php
251 foreach ( $options as $option ) {
252 $selected = '';
253 if ( isset( $filter['operand_val_1'] ) && ( ( $is_multiple && in_array( $option->id, $filter['operand_val_1'] ) ) || ( ! $is_multiple && $option->id == $filter['operand_val_1'] ) ) ) {
254 $selected = 'selected="selected"';
255 }
256 ?>
257 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
258 <?php
259 }
260 ?>
261 </select>
262 </div>
263 <script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script>
264 <?php
265 }
266
267 /**
268 * Check condition for this type
269 *
270 * @param array $condition - condition data.
271 * @param WPSC_Custom_Field $cf - custom field object.
272 * @param mixed $options - value to compare.
273 * @return boolean
274 */
275 public static function is_valid( $condition, $cf, $options ) {
276
277 $response = false;
278 switch ( $condition['operator'] ) {
279
280 case '=':
281 $response = in_array( $condition['operand_val_1'], $options );
282 break;
283
284 case 'IN':
285 $flag = false;
286 foreach ( $condition['operand_val_1'] as $option ) {
287 if ( in_array( $option, $options ) ) {
288 $flag = true;
289 break;
290 }
291 }
292 $response = $flag;
293 break;
294
295 case 'NOT IN':
296 $flag = true;
297 foreach ( $condition['operand_val_1'] as $option ) {
298 if ( in_array( $option, $options ) ) {
299 $flag = false;
300 break;
301 }
302 }
303 $response = $flag;
304 break;
305 }
306 return $response;
307 }
308
309 /**
310 * Parse filter and return sql query to be merged in ticket model query builder
311 *
312 * @param WPSC_Custom_Field $cf - custom field of this type.
313 * @param mixed $compare - comparison operator.
314 * @param mixed $val - value to compare.
315 * @return string
316 */
317 public static function parse_filter( $cf, $compare, $val ) {
318
319 global $wpdb;
320
321 $column = self::get_sql_slug( $cf );
322
323 // Helper to escape regex safely.
324 $escape_regex = static function ( $value ) {
325 return preg_quote( (string) $value, '/' );
326 };
327
328 switch ( $compare ) {
329
330 case '=':
331 if ( $val === '' ) {
332 $pattern = '^$';
333 } else {
334 $pattern = '(^|[|])' . $escape_regex( $val ) . '($|[|])';
335 }
336
337 return $wpdb->prepare(
338 "{$column} RLIKE %s",
339 $pattern
340 );
341
342 case 'IN':
343 case 'NOT IN':
344 if ( ! is_array( $val ) || empty( $val ) ) {
345 return '1=0';
346 }
347
348 $patterns = array();
349
350 foreach ( $val as $value ) {
351 if ( $value === '' ) {
352 $patterns[] = '^$';
353 } else {
354 $patterns[] = preg_quote( (string) $value, '/' );
355 }
356 }
357
358 $pattern = '(^|[|])(' . implode( '|', $patterns ) . ')($|[|])';
359
360 // IMPORTANT: map operator correctly.
361 $sql_operator = ( $compare === 'NOT IN' ) ? 'NOT RLIKE' : 'RLIKE';
362
363 return $wpdb->prepare(
364 "{$column} {$sql_operator} %s",
365 $pattern
366 );
367
368 default:
369 return '1=1';
370 }
371 }
372
373 /**
374 * Check ticket condition
375 *
376 * @param array $condition - array with condition data.
377 * @param WPSC_Custom_Field $cf - custom field object.
378 * @param WPSC_Ticket $ticket - ticket object.
379 * @return boolean
380 */
381 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
382
383 $flag = true;
384 $cf_ids = array_map( fn( $option ) => $option->id, $ticket->{$cf->slug} );
385
386 switch ( $condition['operator'] ) {
387
388 case '=':
389 $flag = in_array( $condition['operand_val_1'], $cf_ids );
390 break;
391
392 case 'IN':
393 $flag = false;
394 foreach ( $cf_ids as $id ) {
395 if ( in_array( $id, $condition['operand_val_1'] ) ) {
396 $flag = true;
397 break;
398 }
399 }
400 break;
401
402 case 'NOT IN':
403 foreach ( $cf_ids as $id ) {
404 if ( in_array( $id, $condition['operand_val_1'] ) ) {
405 $flag = false;
406 break;
407 }
408 }
409 break;
410
411 default:
412 $flag = true;
413 }
414
415 return $flag;
416 }
417
418 /**
419 * Check customer condition
420 *
421 * @param array $condition - array with condition data.
422 * @param WPSC_Custom_Field $cf - custom field object.
423 * @param WPSC_Customer $customer - customer object.
424 * @return boolean
425 */
426 public static function is_valid_customer_condition( $condition, $cf, $customer ) {
427
428 $flag = true;
429 $cf_ids = array_map( fn( $option ) => $option->id, $customer->{$cf->slug} );
430
431 switch ( $condition['operator'] ) {
432
433 case '=':
434 $flag = in_array( $condition['operand_val_1'], $cf_ids );
435 break;
436
437 case 'IN':
438 $flag = false;
439 foreach ( $cf_ids as $id ) {
440 if ( in_array( $id, $condition['operand_val_1'] ) ) {
441 $flag = true;
442 break;
443 }
444 }
445 break;
446
447 case 'NOT IN':
448 foreach ( $cf_ids as $id ) {
449 if ( in_array( $id, $condition['operand_val_1'] ) ) {
450 $flag = false;
451 break;
452 }
453 }
454 break;
455
456 default:
457 $flag = true;
458 }
459
460 return $flag;
461 }
462
463 /**
464 * Print ticket form field
465 *
466 * @param WPSC_Custom_Field $cf - Custom field object.
467 * @param array $tff - Array of ticket form field settings for this field.
468 * @return string
469 */
470 public static function print_tff( $cf, $tff ) {
471
472 $options = $cf->get_options();
473 $val = array();
474 if ( $cf->field == 'ticket' ) {
475 $val = $cf->is_auto_fill && $cf->default_value ? $cf->default_value : array();
476 } else {
477 $current_user = WPSC_Current_User::$current_user;
478 if ( $current_user->is_customer && $current_user->customer->{$cf->slug} ) {
479 $val = array_map( fn( $option )=>$option->id, $current_user->customer->{$cf->slug} );
480 }
481 }
482 $unique_id = uniqid( 'wpsc_' );
483
484 ob_start();
485 ?>
486 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>">
487 <div class="wpsc-tff-label">
488 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
489 <?php
490 if ( $tff['is-required'] ) {
491 ?>
492 <span class="required-indicator">*</span>
493 <?php
494 }
495 ?>
496 </div>
497 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
498 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>[]" onchange="wpsc_check_tff_visibility()" multiple>
499 <?php
500 foreach ( $options as $option ) {
501 $selected = in_array( $option->id, $val ) ? 'selected="selected"' : ''
502 ?>
503 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
504 <?php
505 }
506 ?>
507 </select>
508 <script>
509 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
510 allowClear: false,
511 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
512 });
513 </script>
514 </div>
515 <?php
516 return ob_get_clean();
517 }
518
519 /**
520 * Validate this type field in create ticket
521 *
522 * @return void
523 */
524 public static function js_validate_ticket_form() {
525 ?>
526
527 case '<?php echo esc_attr( self::$slug ); ?>':
528 var val = customField.find('select').first().val();
529 if (isRequired && (!val || val.length === 0)) {
530 isValid = false;
531 }
532 break;
533 <?php
534 echo PHP_EOL;
535 }
536
537 /**
538 * Return custom field value in $_POST
539 * Ignore phpcs nonce issue as we already checked where it is called from.
540 *
541 * @param string $slug - Custom field slug.
542 * @param mixed $cf - Custom field object or false.
543 * @return mixed
544 */
545 public static function get_tff_value( $slug, $cf = false ) {
546
547 return isset( $_POST[ $slug ] ) ? array_filter( array_map( 'intval', $_POST[ $slug ] ) ) : array(); // phpcs:ignore
548 }
549
550 /**
551 * Return default value for custom field of this type
552 *
553 * @param WPSC_Custom_Field $cf - custom field object.
554 * @return mixed
555 */
556 public static function get_default_value( $cf ) {
557
558 return $cf->default_value ? implode( '|', $cf->default_value ) : '';
559 }
560
561 /**
562 * Check and return custom field value for new ticket to be created.
563 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
564 * Ignore phpcs nonce issue as we already checked where it is called from.
565 *
566 * @param array $data - Array of values to to stored in ticket in an insert function.
567 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
568 * @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.
569 * @return array
570 */
571 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
572
573 if ( isset( $custom_fields[ self::$slug ] ) ) {
574 foreach ( $custom_fields[ self::$slug ] as $cf ) {
575 $value = self::get_tff_value( $cf->slug );
576 if ( $cf->field == 'ticket' ) {
577
578 $default_val = self::get_default_value( $cf );
579 $data[ $cf->slug ] = $value ? implode( '|', $value ) : $default_val;
580
581 } elseif ( $cf->field == 'agentonly' ) {
582
583 $data[ $cf->slug ] = self::get_default_value( $cf );
584
585 } elseif ( $cf->field == 'customer' && $data['customer'] != 0 ) {
586
587 $tff = get_option( 'wpsc-tff' );
588 if ( ! $is_my_profile && ! isset( $tff[ $cf->slug ] ) ) {
589 continue;
590 }
591
592 $customer = new WPSC_Customer( $data['customer'] );
593 $existing_val = array_map( fn( $option )=>$option->id, $customer->{$cf->slug} );
594
595 if ( array_diff( $existing_val, $value ) || array_diff( $value, $existing_val ) ) {
596 $customer->{$cf->slug} = $value;
597 $customer->save();
598
599 $prev_val = $existing_val ? implode( '|', $existing_val ) : '';
600 $new_val = $value ? implode( '|', $value ) : '';
601
602 // Set log for this change.
603 WPSC_Log::insert(
604 array(
605 'type' => 'customer',
606 'ref_id' => $customer->id,
607 'modified_by' => WPSC_Current_User::$current_user->customer->id,
608 'body' => wp_json_encode(
609 array(
610 'slug' => $cf->slug,
611 'prev' => $prev_val,
612 'new' => $new_val,
613 )
614 ),
615 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
616 )
617 );
618 }
619 }
620 }
621 }
622 return $data;
623 }
624
625 /**
626 * Set create ticket data for rest api request
627 *
628 * @param array $data - create ticket data array.
629 * @param WP_REST_Request $request - rest request object.
630 * @param array $custom_fields - custom field objects indexed by unique custom field types.
631 * @return array
632 */
633 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
634
635 $current_user = WPSC_Current_User::$current_user;
636 $tff = get_option( 'wpsc-tff' );
637
638 if ( isset( $custom_fields[ self::$slug ] ) ) {
639 foreach ( $custom_fields[ self::$slug ] as $cf ) {
640
641 if (
642 ! in_array( $cf->field, array( 'ticket', 'agentonly', 'customer' ) ) ||
643 ( $cf->field == 'customer' && ! isset( $tff[ $cf->slug ] ) )
644 ) {
645 continue;
646 }
647
648 $value = sanitize_text_field( $request->get_param( $cf->slug ) );
649 if ( $value ) {
650 $value = implode(
651 '|',
652 array_filter(
653 array_map(
654 function ( $id ) {
655 $option = new WPSC_Option( intval( $id ) );
656 return $option->id ? $option->id : false;
657 },
658 explode( ',', $value )
659 )
660 )
661 );
662 }
663
664 if ( in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ) {
665
666 $data[ $cf->slug ] = $cf->field == 'ticket' && $value ? $value : self::get_default_value( $cf );
667
668 } else {
669
670 $customer = new WPSC_Customer( $data['customer'] );
671 $existing_val = array_filter(
672 array_map(
673 fn( $option ) => $option->id ? $option->id : false,
674 $customer->{$cf->slug}
675 )
676 );
677 $new_val = array_filter( explode( '|', $value ) );
678
679 if ( $new_val && ( array_diff( $existing_val, $new_val ) || array_diff( $new_val, $existing_val ) ) ) {
680
681 $customer->{$cf->slug} = $new_val;
682 $customer->save();
683
684 // Set log for this change..
685 WPSC_Log::insert(
686 array(
687 'type' => 'customer',
688 'ref_id' => $customer->id,
689 'modified_by' => $current_user->customer->id,
690 'body' => wp_json_encode(
691 array(
692 'slug' => $cf->slug,
693 'prev' => implode( '|', $existing_val ),
694 'new' => $value,
695 )
696 ),
697 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
698 )
699 );
700 }
701 }
702 }
703 }
704
705 return $data;
706 }
707
708 /**
709 * Return slug string to be used in where condition of ticket model for this type of field
710 *
711 * @param WPSC_Custom_Field $cf - custom field object.
712 * @return string
713 */
714 public static function get_sql_slug( $cf ) {
715
716 $join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.';
717 return $join_char . $cf->slug;
718 }
719
720 /**
721 * Add ticket search compatibility for fields of this custom field type.
722 *
723 * @param array $sql - Array of sql peices that can be joined later.
724 * @param array $filter - User filter.
725 * @param array $custom_fields - Custom fields array applicable for search.
726 * @param string $search - search string.
727 * @param array $allowed_search_fields - Allowed search fields.
728 * @return array
729 */
730 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
731
732 if ( isset( $custom_fields[ self::$slug ] ) ) {
733
734 $search_items = false;
735 foreach ( $custom_fields[ self::$slug ] as $cf ) {
736 if ( in_array( $cf->slug, $allowed_search_fields ) ) {
737 if ( ! $search_items ) {
738 $search_items = WPSC_Option::get_tl_search_string( $search );
739 if ( ! $search_items ) {
740 break;
741 }
742 }
743 $join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.';
744 $sql[] = $join_char . $cf->slug . ' RLIKE \'(^|[|])(' . implode( '|', $search_items ) . ')($|[|])\'';
745 }
746 }
747 }
748
749 return $sql;
750 }
751
752 /**
753 * Return val field for meta query of this type of custom field
754 *
755 * @param array $condition - condition data.
756 * @return mixed
757 */
758 public static function get_meta_value( $condition ) {
759
760 $operator = $condition['operator'];
761 switch ( $operator ) {
762
763 case '=':
764 case 'IN':
765 case 'NOT IN':
766 return $condition['operand_val_1'];
767 }
768 return false;
769 }
770
771 /**
772 * Print generic input field for this type.
773 *
774 * @param WPSC_Custom_Field $cf - custom field object.
775 * @param mixed $value - optional. Input field will be printed with given value.
776 * @return void
777 */
778 public static function print_cf_input( $cf, $value = array() ) {
779
780 if ( ! is_array( $value ) ) {
781
782 $value = array_filter(
783 array_map(
784 function ( $val ) {
785 $option = new WPSC_Option( $val );
786 return $option->id ? $option : '';
787 },
788 explode( '|', $value )
789 )
790 );
791 }
792
793 $options = $cf->get_options();
794 $unique_id = uniqid( 'wpsc_' );
795 ?>
796 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
797 <div class="wpsc-tff-label">
798 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
799 </div>
800 <?php
801 $extra_info = stripslashes( $cf->extra_info );
802 if ( $extra_info ) :
803 ?>
804 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
805 <?php
806 endif
807 ?>
808 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>[]" multiple>
809 <?php
810 $val = array_map( fn( $option )=>$option->id, $value );
811 foreach ( $options as $option ) {
812 $selected = $val && in_array( $option->id, $val ) ? 'selected' : ''
813 ?>
814 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
815 <?php
816 }
817 ?>
818 </select>
819 <script>
820 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
821 allowClear: false,
822 placeholder: ""
823 });
824 </script>
825 </div>
826 <?php
827 }
828
829 /**
830 * Get value for generic field from post.
831 * Ignore phpcs nonce issue as we already checked where it is called from.
832 *
833 * @param WPSC_Custom_Field $cf - custom field object.
834 * @return string
835 */
836 public static function get_cf_input_val( $cf ) {
837
838 $value = isset( $_POST[ $cf->slug ] ) ? array_filter( array_map( 'intval', $_POST[ $cf->slug ] ) ) : array(); // phpcs:ignore
839 return $value ? implode( '|', $value ) : '';
840 }
841
842 /**
843 * Print edit ticket custom field in individual ticket
844 *
845 * @param WPSC_Custom_Field $cf - custom field object.
846 * @param WPSC_Ticket $ticket - ticket object.
847 * @return void
848 */
849 public static function print_edit_ticket_cf( $cf, $ticket ) {
850
851 $options = $cf->get_options();
852 $unique_id = uniqid( 'wpsc_' );
853 ?>
854 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
855 <div class="wpsc-tff-label">
856 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
857 </div>
858 <?php
859 $extra_info = stripslashes( $cf->extra_info );
860 if ( $extra_info ) :
861 ?>
862 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
863 <?php
864 endif
865 ?>
866 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>[]" multiple>
867 <?php
868 $val = array_map( fn( $option )=>$option->id, $ticket->{$cf->slug} );
869 foreach ( $options as $option ) {
870 $selected = $val && in_array( $option->id, $val ) ? 'selected' : ''
871 ?>
872 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
873 <?php
874 }
875 ?>
876
877 </select>
878 <script>
879 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
880 allowClear: false,
881 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
882 });
883 </script>
884 </div>
885 <?php
886 }
887
888 /**
889 * Set edit individual ticket for this custom field type.
890 * Ignore phpcs nonce issue as we already checked where it is called from.
891 *
892 * @param WPSC_Custom_Field $cf - custom field object.
893 * @param WPSC_Ticket $ticket - ticket object.
894 * @return WPSC_Ticket
895 */
896 public static function set_edit_ticket_cf( $cf, $ticket ) {
897
898 $prev = $ticket->{$cf->slug} ? $ticket->{$cf->slug} : array();
899 $prev_ids = array_filter( array_map( fn( $option ) => $option->id ? $option->id : '', $prev ) );
900 $new_ids = isset( $_POST[ $cf->slug ] ) ? array_filter( array_map( 'intval', $_POST[ $cf->slug ] ) ) : array(); // phpcs:ignore
901
902 // Exit if there is no change.
903 if ( ! ( array_diff( $prev_ids, $new_ids ) || array_diff( $new_ids, $prev_ids ) ) ) {
904 return $ticket;
905 }
906
907 // Create wpsc_option objects.
908 $new = array_filter(
909 array_map(
910 function ( $id ) {
911 $option = new WPSC_Option( $id );
912 return $option->id ? $option : '';
913 },
914 $new_ids
915 )
916 );
917
918 // Change value.
919 $ticket->{$cf->slug} = $new;
920 $ticket->save();
921
922 return $ticket;
923 }
924
925 /**
926 * Modify ticket field value of this custom field type using rest api
927 *
928 * @param WPSC_Ticket $ticket - ticket object.
929 * @param WPSC_Custom_Field $cf - custom field.
930 * @param mixed $value - value to be set.
931 * @return void
932 */
933 public static function set_rest_edit_ticket_cf( $ticket, $cf, $value ) {
934
935 $new = array_unique(
936 array_filter(
937 array_map(
938 function ( $id ) {
939 $option = new WPSC_Option( intval( $id ) );
940 return $option->id ? $option->id : false;
941 },
942 explode( ',', sanitize_text_field( $value ) )
943 )
944 )
945 );
946
947 $prev = is_array( $ticket->{$cf->slug} ) ? $ticket->{$cf->slug} : array();
948 $prev = array_filter(
949 array_map(
950 fn( $option ) => $option->id ? $option->id : false,
951 $prev
952 )
953 );
954
955 if ( array_diff( $prev, $new ) || array_diff( $new, $prev ) ) {
956 $ticket->{$cf->slug} = $new;
957 }
958 }
959
960 /**
961 * Insert log thread for this custom field type change
962 *
963 * @param WPSC_Custom_Field $cf - current custom field of this type.
964 * @param WPSC_Ticket $prev - ticket object before making any changes.
965 * @param WPSC_Ticket $new - ticket object after making changes.
966 * @param string $current_date - date string to be stored as create time.
967 * @param int $customer_id - current user customer id for blame.
968 * @return void
969 */
970 public static function insert_ticket_log( $cf, $prev, $new, $current_date, $customer_id ) {
971
972 $prev_ids = array_filter( array_map( fn( $option ) => $option->id ? $option->id : '', $prev->{$cf->slug} ) );
973 $new_ids = array_filter( array_map( fn( $option )=>$option->id ? $option->id : '', $new->{$cf->slug} ) );
974
975 // Exit if there is no change.
976 if ( ! ( array_diff( $prev_ids, $new_ids ) || array_diff( $new_ids, $prev_ids ) ) ) {
977 return;
978 }
979
980 $prev_val = $prev_ids ? implode( '|', $prev_ids ) : '';
981 $new_val = $new_ids ? implode( '|', $new_ids ) : '';
982
983 $thread = WPSC_Thread::insert(
984 array(
985 'ticket' => $prev->id,
986 'customer' => $customer_id,
987 'type' => 'log',
988 'body' => wp_json_encode(
989 array(
990 'slug' => $cf->slug,
991 'prev' => $prev_val,
992 'new' => $new_val,
993 )
994 ),
995 'date_created' => $current_date,
996 'date_updated' => $current_date,
997 )
998 );
999 }
1000
1001 /**
1002 * Return data for this custom field while creating duplicate ticket
1003 *
1004 * @param WPSC_Custom_Field $cf - custom field object.
1005 * @param WPSC_Ticket $ticket - ticket object.
1006 * @return mixed
1007 */
1008 public static function get_duplicate_ticket_data( $cf, $ticket ) {
1009
1010 $val = $ticket->{$cf->slug};
1011 $response = array();
1012 foreach ( $val as $option ) {
1013 $response[] = $option->id;
1014 }
1015 return $response ? implode( '|', $response ) : '';
1016 }
1017
1018 /**
1019 * Print edit field for this type in edit customer info
1020 *
1021 * @param WPSC_Custom_field $cf - custom field object.
1022 * @param WPSC_Customer $customer - customer object.
1023 * @param array $tff - ticket form field data.
1024 * @return string
1025 */
1026 public static function print_edit_customer_info( $cf, $customer, $tff ) {
1027
1028 $options = $cf->get_options();
1029 $val = array();
1030
1031 if ( $customer->id && $customer->{$cf->slug} ) {
1032 foreach ( $customer->{$cf->slug} as $option ) {
1033 $val[] = $option->id;
1034 }
1035 }
1036
1037 $unique_id = uniqid( 'wpsc_' );
1038
1039 ob_start();
1040 ?>
1041 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>">
1042 <div class="wpsc-tff-label">
1043 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
1044 </div>
1045 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
1046 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>[]" multiple>
1047 <?php
1048 foreach ( $options as $option ) {
1049 $selected = in_array( $option->id, $val ) ? 'selected' : '';
1050 ?>
1051 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
1052 <?php
1053 }
1054 ?>
1055 </select>
1056 <script>
1057 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
1058 allowClear: false,
1059 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
1060 });
1061 </script>
1062 </div>
1063 <?php
1064 return ob_get_clean();
1065 }
1066
1067 /**
1068 * Print add new custom field setting properties
1069 *
1070 * @param string $field_class - Class name of the field.
1071 * @return void
1072 */
1073 public static function get_add_new_custom_field_properties( $field_class ) {
1074
1075 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
1076 ?>
1077 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
1078 <div class="label-container">
1079 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
1080 </div>
1081 <input name="extra_info" type="text" autocomplete="off" />
1082 </div>
1083 <?php
1084 }
1085 ?>
1086
1087 <div data-type="select-options" data-required="true" class="wpsc-input-group options">
1088 <div class="label-container">
1089 <label for="">
1090 <?php esc_attr_e( 'Field options', 'supportcandy' ); ?>
1091 <span class="required-char">*</span>
1092 </label>
1093 </div>
1094 <div class="wpsc-options-container ui-sortable"></div>
1095 <script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script>
1096 <button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();">
1097 <?php esc_attr_e( 'Add new', 'supportcandy' ); ?>
1098 </button>
1099 </div>
1100
1101 <?php
1102 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
1103 ?>
1104 <div data-type="multi-select" data-required="false" class="wpsc-input-group default-val-option-multi">
1105 <div class="label-container">
1106 <label for="">
1107 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
1108 </label>
1109 </div>
1110 <select name="default-val-option-multi[]" id="wpsc-default-val-option-multi" multiple="multiple"></select>
1111 </div>
1112 <?php
1113 endif;
1114
1115 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
1116 ?>
1117 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
1118 <div class="label-container">
1119 <label for="">
1120 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
1121 </label>
1122 </div>
1123 <select name="is_auto_fill">
1124 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1125 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1126 </select>
1127 </div>
1128 <?php
1129 endif;
1130
1131 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
1132 ?>
1133 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
1134 <div class="label-container">
1135 <label for="">
1136 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
1137 </label>
1138 </div>
1139 <input type="text" name="placeholder_text" autocomplete="off">
1140 </div>
1141 <?php
1142 endif;
1143
1144 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
1145 ?>
1146 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
1147 <div class="label-container">
1148 <label for="">
1149 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
1150 </label>
1151 </div>
1152 <input type="number" name="tl_width" autocomplete="off">
1153 </div>
1154 <?php
1155 endif;
1156
1157 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
1158 ?>
1159 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
1160 <div class="label-container">
1161 <label for="">
1162 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
1163 </label>
1164 </div>
1165 <select name="is_personal_info">
1166 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1167 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1168 </select>
1169 </div>
1170 <?php
1171 endif;
1172
1173 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
1174 ?>
1175 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
1176 <div class="label-container">
1177 <label for="">
1178 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
1179 </label>
1180 </div>
1181 <select name="allow_my_profile">
1182 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1183 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1184 </select>
1185 </div>
1186 <?php
1187 endif;
1188
1189 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
1190 ?>
1191 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
1192 <div class="label-container">
1193 <label for="">
1194 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
1195 </label>
1196 </div>
1197 <select name="allow_ticket_form">
1198 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1199 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1200 </select>
1201 </div>
1202 <?php
1203 endif;
1204 }
1205
1206 /**
1207 * Print edit custom field properties
1208 *
1209 * @param WPSC_Custom_Fields $cf - custom field object.
1210 * @param string $field_class - class name of field category.
1211 * @return void
1212 */
1213 public static function get_edit_custom_field_properties( $cf, $field_class ) {
1214
1215 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
1216 ?>
1217 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
1218 <div class="label-container">
1219 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
1220 </div>
1221 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
1222 </div>
1223 <?php
1224 endif;
1225 ?>
1226
1227 <div data-type="select-options" data-required="true" class="wpsc-input-group options">
1228 <div class="label-container">
1229 <label for="">
1230 <?php esc_attr_e( 'Field options', 'supportcandy' ); ?>
1231 <span class="required-char">*</span>
1232 </label>
1233 </div>
1234 <div class="wpsc-options-container ui-sortable">
1235 <?php
1236 $options = $cf->get_options();
1237 foreach ( $options as $option ) :
1238 ?>
1239 <div class="wpsc-option-item">
1240 <div class="content">
1241 <div class="wpsc-edit-option-container" style="display: none;">
1242 <input class="edit-option-text" type="text" value="" autocomplete="off" />
1243 <button onclick="wpsc_set_edit_option(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_option' ) ); ?>');">
1244 <?php WPSC_Icons::get( 'check' ); ?>
1245 </button>
1246 <button class="cancel" onclick="wpsc_edit_option_cancel(this);">
1247 <?php WPSC_Icons::get( 'times' ); ?>
1248 </button>
1249 </div>
1250 <div class="wpsc-option-listing-container">
1251 <span class="sort wpsc-sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></span>
1252 <div class="text"><?php echo esc_attr( $option->name ); ?></div>
1253 <span class="edit" onclick="wpsc_edit_option(this);"><?php WPSC_Icons::get( 'edit' ); ?></span>
1254 </div>
1255 </div>
1256 <div class="remove-container">
1257 <span onclick="wpsc_remove_option_item(this)"><?php WPSC_Icons::get( 'times-circle' ); ?></span>
1258 </div>
1259 <input type="hidden" class="option_id" name="options[]" value="<?php echo esc_attr( $option->id ); ?>">
1260 </div>
1261 <?php
1262 endforeach;
1263 ?>
1264 </div>
1265 <script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script>
1266 <button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();">
1267 <?php esc_attr_e( 'Add new', 'supportcandy' ); ?>
1268 </button>
1269 </div>
1270 <?php
1271
1272 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
1273 ?>
1274 <div data-type="multi-select" data-required="false" class="wpsc-input-group default-val-option-multi">
1275 <div class="label-container">
1276 <label for="">
1277 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
1278 </label>
1279 </div>
1280 <select name="default-val-option-multi[]" id="wpsc-default-val-option-multi" multiple="multiple">
1281 <?php
1282 $multi_dopt = $cf->default_value ? $cf->default_value : array();
1283 foreach ( $options as $option ) :
1284 $selected = in_array( $option->id, $multi_dopt ) ? "selected='selected'" : '';
1285 echo '<option ' . esc_attr( $selected ) . ' value=' . esc_attr( $option->id ) . '>' . esc_attr( $option->name ) . '</option>';
1286 endforeach;
1287 ?>
1288 </select>
1289 <script>
1290 jQuery('#wpsc-default-val-option-multi').selectWoo({
1291 allowClear: false,
1292 placeholder: ""
1293 });
1294 </script>
1295 </div>
1296 <?php
1297 endif;
1298
1299 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
1300 ?>
1301 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
1302 <div class="label-container">
1303 <label for="">
1304 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
1305 </label>
1306 </div>
1307 <select name="is_auto_fill">
1308 <option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1309 <option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1310 </select>
1311 </div>
1312 <?php
1313 endif;
1314
1315 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
1316 ?>
1317 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
1318 <div class="label-container">
1319 <label for="">
1320 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
1321 </label>
1322 </div>
1323 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
1324 </div>
1325 <?php
1326 endif;
1327
1328 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
1329 ?>
1330 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
1331 <div class="label-container">
1332 <label for="">
1333 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
1334 </label>
1335 </div>
1336 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
1337 </div>
1338 <?php
1339 endif;
1340
1341 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
1342 ?>
1343 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
1344 <div class="label-container">
1345 <label for="">
1346 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
1347 </label>
1348 </div>
1349 <select name="is_personal_info">
1350 <option <?php selected( $cf->is_personal_info, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1351 <option <?php selected( $cf->is_personal_info, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1352 </select>
1353 </div>
1354 <?php
1355 endif;
1356
1357 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
1358 ?>
1359 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
1360 <div class="label-container">
1361 <label for="">
1362 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
1363 </label>
1364 </div>
1365 <select name="allow_my_profile">
1366 <option <?php selected( $cf->allow_my_profile, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1367 <option <?php selected( $cf->allow_my_profile, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1368 </select>
1369 </div>
1370 <?php
1371 endif;
1372
1373 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
1374 ?>
1375 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
1376 <div class="label-container">
1377 <label for="">
1378 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
1379 </label>
1380 </div>
1381 <select name="allow_ticket_form">
1382 <option <?php selected( $cf->allow_ticket_form, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1383 <option <?php selected( $cf->allow_ticket_form, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1384 </select>
1385 </div>
1386 <?php
1387 endif;
1388 }
1389
1390 /**
1391 * Set custom field properties. Can be used by add/edit custom field.
1392 * Ignore phpcs nonce issue as we already checked where it is called from.
1393 *
1394 * @param WPSC_Custom_Field $cf - custom field object.
1395 * @param string $field_class - class of field category.
1396 * @return void
1397 */
1398 public static function set_cf_properties( $cf, $field_class ) {
1399
1400 // extra info.
1401 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
1402 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
1403 }
1404
1405 // options.
1406 $options = isset( $_POST['options'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash($_POST['options']) ) ) : array(); // phpcs:ignore
1407 if ( ! $options ) {
1408 wp_send_json_error( new WP_Error( '001', 'Bad request!' ), 400 );
1409 }
1410
1411 // default value.
1412 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
1413 $cf->default_value = isset( $_POST['default-val-option-multi'] ) ? array_filter( array_map( 'intval', $_POST['default-val-option-multi'] ) ) : array(); // phpcs:ignore
1414 }
1415
1416 // auto fill.
1417 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
1418 $cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
1419 }
1420
1421 // placeholder!
1422 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
1423 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
1424 }
1425
1426 // personal info.
1427 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) {
1428 $cf->is_personal_info = isset( $_POST['is_personal_info'] ) ? intval( $_POST['is_personal_info'] ) : 0; // phpcs:ignore
1429 }
1430
1431 // my-profile.
1432 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) {
1433 $cf->allow_my_profile = isset( $_POST['allow_my_profile'] ) ? intval( $_POST['allow_my_profile'] ) : 0; // phpcs:ignore
1434 }
1435
1436 // ticket form.
1437 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) {
1438 $cf->allow_ticket_form = isset( $_POST['allow_ticket_form'] ) ? intval( $_POST['allow_ticket_form'] ) : 0; // phpcs:ignore
1439 }
1440
1441 // tl_width!
1442 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
1443 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
1444 $cf->tl_width = $tl_width ? $tl_width : 100;
1445 }
1446
1447 // save!
1448 $cf->save();
1449 $cf->set_options( $options, true );
1450 }
1451
1452 /**
1453 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
1454 *
1455 * @param WPSC_Custom_Field $cf - custom field object.
1456 * @param WPSC_Ticket $ticket - ticket object.
1457 * @param string $module - module name.
1458 * @return string
1459 */
1460 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
1461
1462 $names = array_filter(
1463 array_map(
1464 fn( $option ) => $option->id ? $option->name : '',
1465 $ticket->{$cf->slug}
1466 )
1467 );
1468 $value = $names ? implode( ', ', $names ) : '';
1469
1470 return apply_filters( 'wpsc_ticket_field_val_multi_select', $value, $cf, $ticket, $module );
1471 }
1472
1473 /**
1474 * Print ticket value for given custom field on ticket list
1475 *
1476 * @param WPSC_Custom_Field $cf - custom field object.
1477 * @param WPSC_Ticket $ticket - ticket object.
1478 * @return void
1479 */
1480 public static function print_tl_ticket_field_val( $cf, $ticket ) {
1481
1482 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
1483 }
1484
1485 /**
1486 * Print ticket value for given custom field on widget
1487 *
1488 * @param WPSC_Custom_Field $cf - custom field object.
1489 * @param WPSC_Ticket $ticket - ticket object.
1490 * @return void
1491 */
1492 public static function print_widget_ticket_field_val( $cf, $ticket ) {
1493
1494 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
1495 }
1496
1497 /**
1498 * Returns printable customer value for custom field. Can be used in export tickets, replace macros etc.
1499 *
1500 * @param WPSC_Custom_Field $cf - custom field object.
1501 * @param WPSC_Customer $customer - customer object.
1502 * @return string
1503 */
1504 public static function get_customer_field_val( $cf, $customer ) {
1505
1506 $names = array_filter(
1507 array_map(
1508 fn( $option ) => $option->id ? $option->name : '',
1509 $customer->{$cf->slug}
1510 )
1511 );
1512 return $names ? implode( ', ', $names ) : '';
1513 }
1514
1515 /**
1516 * Print customer value for given custom field on ticket list
1517 *
1518 * @param WPSC_Custom_Field $cf - custom field object.
1519 * @param WPSC_Customer $customer - customer object.
1520 * @return void
1521 */
1522 public static function print_tl_customer_field_val( $cf, $customer ) {
1523
1524 echo esc_attr( self::get_customer_field_val( $cf, $customer ) );
1525 }
1526
1527 /**
1528 * Print customer value for given custom field on widget
1529 *
1530 * @param WPSC_Custom_Field $cf - custom field object.
1531 * @param WPSC_Customer $customer - customer object.
1532 * @return void
1533 */
1534 public static function print_widget_customer_field_val( $cf, $customer ) {
1535
1536 echo esc_attr( self::get_customer_field_val( $cf, $customer ) );
1537 }
1538
1539 /**
1540 * Print given value for custom field
1541 *
1542 * @param WPSC_Custom_Field $cf - custom field object.
1543 * @param mixed $val - value to convert and print.
1544 * @return void
1545 */
1546 public static function print_val( $cf, $val ) {
1547
1548 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
1549 $options = array_filter(
1550 array_map(
1551 function ( $option ) {
1552 if ( is_object( $option ) ) {
1553 return $option;
1554 } elseif ( $option ) {
1555 $option = new WPSC_Option( $option );
1556 return $option->id ? $option : '';
1557 } else {
1558 return '';
1559 }
1560 },
1561 $val
1562 )
1563 );
1564 $option_names = array_map( fn( $option ) => $option->name, $options );
1565 echo $option_names ? esc_attr( implode( ', ', $option_names ) ) : esc_attr__( 'None', 'supportcandy' );
1566 }
1567
1568 /**
1569 * Return printable value for history log macro
1570 *
1571 * @param WPSC_Custom_Field $cf - custom field object.
1572 * @param mixed $val - value to convert and return.
1573 * @return string
1574 */
1575 public static function get_history_log_val( $cf, $val ) {
1576
1577 ob_start();
1578 self::print_val( $cf, $val );
1579 return ob_get_clean();
1580 }
1581
1582 /**
1583 * Print dashboard activity function
1584 *
1585 * @param WPSC_Custom_Field $cf - custom field object.
1586 * @param array $recent_logs - recent_logs object.
1587 * @param array $body - body object.
1588 * @param int $view - check if frontend.
1589 * @return string
1590 */
1591 public static function print_activity( $cf, $recent_logs, $body, $view ) {
1592
1593 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
1594 $ms_values = explode( '|', $body->new );
1595
1596 foreach ( $ms_values as $ms_value ) {
1597 $new_ms = new WPSC_Option( $ms_value );
1598 if ( $new_ms->id ) {
1599 $new_ms_values[] = esc_attr( $new_ms->name );
1600 }
1601 }
1602 if ( ! empty( $new_ms_values ) ) {
1603 $ms_names = implode( ', ', $new_ms_values );
1604 } else {
1605 $ms_names = 'None';
1606 }
1607 return esc_attr( $recent_logs->customer->name ) . ' updated the ' . esc_attr( $cf->name ) . ' value of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $ms_names );
1608 }
1609
1610 /**
1611 * Return given value for custom field
1612 *
1613 * @param WPSC_Custom_Field $cf - custom field object.
1614 * @param mixed $val - value to convert and print.
1615 * @return string
1616 */
1617 public static function get_field_value( $cf, $val ) {
1618
1619 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
1620 $options = array_filter(
1621 array_map(
1622 function ( $option ) {
1623 if ( is_object( $option ) ) {
1624 return $option;
1625 } elseif ( $option ) {
1626 $option = new WPSC_Option( $option );
1627 return $option->id ? $option : '';
1628 } else {
1629 return '';
1630 }
1631 },
1632 $val
1633 )
1634 );
1635 $option_names = array_map( fn( $option ) => $option->name, $options );
1636 return $option_names ? esc_attr( implode( ', ', $option_names ) ) : esc_attr__( 'None', 'supportcandy' );
1637 }
1638
1639 /**
1640 * Clear value of hidden fields
1641 *
1642 * @return void
1643 */
1644 public static function js_clear_value_hidden_fields() {
1645 ?>
1646 case '<?php echo esc_attr( self::$slug ); ?>':
1647 customField.find('select').first().val('');
1648 break;
1649 <?php
1650 echo PHP_EOL;
1651 }
1652 }
1653 endif;
1654
1655 WPSC_CF_Multi_Select::init();
1656