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

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

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