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-radio-button.php

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

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