PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / custom-field-types / class-wpsc-cf-datetime.php

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

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