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

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