PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.8
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.8
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / custom-field-types / class-wpsc-cf-multi-select.php

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

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