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

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

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