PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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-checkbox.php

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

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