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

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