PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.3
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.3
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.4.3, at includes/custom-field-types/class-wpsc-cf-multi-select.php

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