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-df-date-closed.php

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

617 lines 17.8 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_DF_Date_Closed' ) ) :
7
8 final class WPSC_DF_Date_Closed {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_date_closed';
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 = false;
30
31 /**
32 * Set whether this custom field type has multiple values
33 *
34 * @var boolean
35 */
36 public static $has_multiple_val = false;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = '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 = true;
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 = false;
72
73 /**
74 * Set whether this custom field type can accept personal info.
75 *
76 * @var boolean
77 */
78 public static $has_personal_info = false;
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 = false;
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 auto-filled
117 *
118 * @var boolean
119 */
120 public static $is_auto_fill = false;
121
122 /**
123 * Set whether fields created from this custom field type can be available for ticket list sorting
124 *
125 * @var boolean
126 */
127 public static $is_sort = true;
128
129 /**
130 * Set whether fields created from this custom field type can have placeholder
131 *
132 * @var boolean
133 */
134 public static $is_placeholder = false;
135
136 /**
137 * Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form
138 *
139 * @var boolean
140 */
141 public static $is_visibility_conditions = false;
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_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
169 }
170
171 /**
172 * Load current class to reference classes
173 *
174 * @param array $classes - Associative array of class names indexed by its slug.
175 * @return array
176 */
177 public static function load_ref_class( $classes ) {
178
179 $classes[ self::$slug ] = array(
180 'class' => __CLASS__,
181 'save-key' => 'id',
182 );
183 return $classes;
184 }
185
186 /**
187 * Add default custom field type to list
188 *
189 * @param array $default_cf_types - default custom field types array.
190 * @return array
191 */
192 public static function default_cf_types( $default_cf_types ) {
193 $default_cf_types[ self::$slug ] = array(
194 'label' => esc_attr__( 'Date Closed', 'supportcandy' ),
195 'class' => __CLASS__,
196 );
197 return $default_cf_types;
198 }
199
200 /**
201 * Print operators for ticket form filter
202 *
203 * @param WPSC_Custom_Field $cf - custom field object.
204 * @param array $filter - Existing filters (if any).
205 * @return void
206 */
207 public static function get_operators( $cf, $filter = array() ) {
208 ?>
209
210 <div class="item conditional">
211 <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' ) ); ?>');">
212 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
213 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
214 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<' ); ?> value="<"><?php esc_attr_e( 'Less than', 'supportcandy' ); ?></option>
215 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<=' ); ?> value="<="><?php esc_attr_e( 'Less than or equals', 'supportcandy' ); ?></option>
216 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>' ); ?> value=">"><?php esc_attr_e( 'Greater than', 'supportcandy' ); ?></option>
217 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>=' ); ?> value=">="><?php esc_attr_e( 'Greater than or equals', 'supportcandy' ); ?></option>
218 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'BETWEEN' ); ?> value="BETWEEN"><?php esc_attr_e( 'Between', 'supportcandy' ); ?></option>
219 </select>
220 </div>
221 <?php
222 }
223
224 /**
225 * Print operators for ticket form filter
226 *
227 * @param string $operator - condition operator on which operands should be returned.
228 * @param WPSC_Custom_Field $cf - custom field object.
229 * @param array $filter - Exising functions (if any).
230 * @return void
231 */
232 public static function get_operands( $operator, $cf, $filter = array() ) {
233
234 $unique_id = uniqid( 'wpsc_' );
235 $is_between = $operator == 'BETWEEN' ? true : false;
236 $operand_val_1 = isset( $filter['operand_val_1'] ) ? $filter['operand_val_1'] : '';
237 $operand_val_2 = isset( $filter['operand_val_2'] ) ? $filter['operand_val_2'] : '';
238 ?>
239
240 <div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>">
241 <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 ); ?>">
242 </div>
243 <script>
244 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
245 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
246 });
247 </script>
248 <?php
249
250 if ( $is_between ) {
251 ?>
252 <div class="item conditional operand">
253 <input type="text" class="operand_val_2 <?php echo esc_attr( $unique_id ); ?>" placeholder="<?php esc_attr_e( 'To date', 'supportcandy' ); ?>" value="">
254 </div>
255 <script>
256 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').change(function() {
257 var fromDate = jQuery(this).val().trim();
258 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
259 minDate: fromDate,
260 defaultDate: fromDate,
261 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
262 });
263 });
264 <?php
265 if ( $operand_val_2 ) {
266 ?>
267 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
268 minDate: '<?php echo esc_attr( $operand_val_1 ); ?>',
269 defaultDate: '<?php echo esc_attr( $operand_val_2 ); ?>',
270 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
271 });
272 <?php
273 }
274 ?>
275 </script>
276 <?php
277 }
278 }
279
280 /**
281 * Parse filter and return sql query to be merged in ticket model query builder
282 *
283 * @param WPSC_Custom_Field $cf - custom field of this type.
284 * @param mixed $compare - comparison operator.
285 * @param mixed $val - value to compare.
286 * @return string
287 */
288 public static function parse_filter( $cf, $compare, $val ) {
289
290 global $wpdb;
291
292 $slug = is_string( $cf->slug ) ? trim( $cf->slug ) : '';
293 if ( $slug === '' || ! preg_match( '/^[a-zA-Z0-9_]+$/', $slug ) ) {
294 return '1=0';
295 }
296 $column = 't.' . $cf->slug;
297
298 if ( in_array( $compare, array( '=', '<', '>', '<=', '>=' ), true ) ) {
299 if ( ! WPSC_Functions::is_valid_date( $val ) ) {
300 return '1=0';
301 }
302 }
303
304 switch ( $compare ) {
305
306 case '=':
307 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
308 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
309
310 return $wpdb->prepare(
311 "{$column} BETWEEN %s AND %s",
312 $from,
313 $to
314 );
315
316 case '<':
317 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
318
319 return $wpdb->prepare(
320 "{$column} < %s",
321 $from
322 );
323
324 case '>':
325 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
326
327 return $wpdb->prepare(
328 "{$column} > %s",
329 $to
330 );
331
332 case '<=':
333 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
334 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
335
336 return $wpdb->prepare(
337 "({$column} <= %s OR {$column} BETWEEN %s AND %s)",
338 $from,
339 $from,
340 $to
341 );
342
343 case '>=':
344 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
345 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
346
347 return $wpdb->prepare(
348 "({$column} >= %s OR {$column} BETWEEN %s AND %s)",
349 $to,
350 $from,
351 $to
352 );
353
354 case 'BETWEEN':
355 if (
356 ! is_array( $val ) ||
357 ! isset( $val['operand_val_1'], $val['operand_val_2'] ) ||
358 ! WPSC_Functions::is_valid_datetime( $val['operand_val_1'] ) ||
359 ! WPSC_Functions::is_valid_datetime( $val['operand_val_2'] )
360 ) {
361 return '1=0';
362 }
363
364 return $wpdb->prepare(
365 "{$column} BETWEEN %s AND %s",
366 $val['operand_val_1'],
367 $val['operand_val_2']
368 );
369
370 default:
371 return '1=1';
372 }
373 }
374
375 /**
376 * Return orderby string
377 *
378 * @param WPSC_Custom_Field $cf - custom field object.
379 * @return string
380 */
381 public static function get_orderby_string( $cf ) {
382
383 return 't.' . $cf->slug;
384 }
385
386 /**
387 * Check ticket condition
388 *
389 * @param array $condition - array with condition data.
390 * @param WPSC_Custom_Field $cf - custom field object.
391 * @param WPSC_Ticket $ticket - ticket object.
392 * @return boolean
393 */
394 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
395
396 $flag = true;
397 $date = $ticket->{$cf->slug};
398 if ( ! is_object( $date ) ) {
399 return false;
400 }
401
402 $date->setTimezone( wp_timezone() );
403
404 switch ( $condition['operator'] ) {
405
406 case '=':
407 $flag = $date->format( 'Y-m-d' ) == $condition['operand_val_1'] ? true : false;
408 break;
409
410 case '<':
411 $operand = new DateTime( $condition['operand_val_1'] );
412 $flag = $operand < $date ? true : false;
413 break;
414
415 case '>':
416 $operand = new DateTime( $condition['operand_val_1'] );
417 $flag = $operand > $date ? true : false;
418 break;
419
420 case '<=':
421 $operand = new DateTime( $condition['operand_val_1'] );
422 $flag = $operand <= $date ? true : false;
423 break;
424
425 case '>=':
426 $operand = new DateTime( $condition['operand_val_1'] );
427 $flag = $operand >= $date ? true : false;
428 break;
429
430 case 'BETWEEN':
431 $operand1 = new DateTime( $condition['operand_val_1'] );
432 $operand2 = new DateTime( $condition['operand_val_2'] );
433 $flag = $operand1 <= $date && $operand2 >= $date ? true : false;
434 break;
435
436 default:
437 $flag = true;
438 }
439
440 return $flag;
441 }
442
443 /**
444 * Return default value for custom field of this type
445 *
446 * @param WPSC_Custom_Field $cf - custom field object.
447 * @return mixed
448 */
449 public static function get_default_value( $cf ) {
450
451 return '';
452 }
453
454 /**
455 * Return val field for meta query of this type of custom field
456 *
457 * @param array $condition - condition data.
458 * @return mixed
459 */
460 public static function get_meta_value( $condition ) {
461
462 $operator = $condition['operator'];
463 switch ( $operator ) {
464
465 case '=':
466 case '<':
467 case '<=':
468 case '>':
469 case '>=':
470 return $condition['operand_val_1'];
471
472 case 'BETWEEN':
473 if (
474 ! ( isset( $condition['operand_val_1'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_1'] ) ) ||
475 ! ( isset( $condition['operand_val_2'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_2'] ) )
476 ) {
477 return false;
478 }
479
480 return array(
481 'operand_val_1' => $condition['operand_val_1'],
482 'operand_val_2' => $condition['operand_val_2'],
483 );
484 }
485 return false;
486 }
487
488 /**
489 * Print edit custom field properties
490 *
491 * @param WPSC_Custom_Fields $cf - custom field object.
492 * @param string $field_class - class name of field category.
493 * @return void
494 */
495 public static function get_edit_custom_field_properties( $cf, $field_class ) {
496 ?>
497
498 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
499 <div class="label-container">
500 <label for=""><?php esc_attr_e( 'Display as', 'supportcandy' ); ?></label>
501 </div>
502 <select name="date_display_as">
503 <option <?php selected( $cf->date_display_as, 'date' ); ?> value="date"><?php esc_attr_e( 'Date Format', 'supportcandy' ); ?></option>
504 <option <?php selected( $cf->date_display_as, 'diff' ); ?> value="diff"><?php esc_attr_e( 'Date Difference (e.g. 1 hour ago)', 'supportcandy' ); ?></option>
505 </select>
506 </div>
507
508 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
509 <div class="label-container">
510 <label for=""><?php esc_attr_e( 'Date format', 'supportcandy' ); ?></label>
511 </div>
512 <input type="text" name="date_format" value="<?php echo esc_attr( $cf->date_format ); ?>" autocomplete="off" />
513 </div>
514 <?php
515
516 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
517 ?>
518 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
519 <div class="label-container">
520 <label for="">
521 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
522 </label>
523 </div>
524 <input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off">
525 </div>
526 <?php
527 endif;
528 }
529
530 /**
531 * Set custom field properties. Can be used by add/edit custom field.
532 * Ignore phpcs nonce issue as we already checked where it is called from.
533 *
534 * @param WPSC_Custom_Field $cf - custom field object.
535 * @param string $field_class - class of field category.
536 * @return void
537 */
538 public static function set_cf_properties( $cf, $field_class ) {
539
540 // date display as.
541 $cf->date_display_as = isset( $_POST['date_display_as'] ) ? sanitize_text_field( wp_unslash( $_POST['date_display_as'] ) ) : 'date'; // phpcs:ignore
542
543 // date format.
544 $cf->date_format = isset( $_POST['date_format'] ) ? sanitize_text_field( wp_unslash( $_POST['date_format'] ) ) : ''; // phpcs:ignore
545
546 // tl_width!
547 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
548 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
549 $cf->tl_width = $tl_width ? $tl_width : 100;
550 }
551
552 // save!
553 $cf->save();
554 }
555
556 /**
557 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
558 *
559 * @param WPSC_Custom_Field $cf - custom field object.
560 * @param WPSC_Ticket $ticket - ticket object.
561 * @param string $module - module name.
562 * @return string
563 */
564 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
565
566 $general_settings = get_option( 'wpsc-gs-general' );
567 $format = $cf->date_format ? $cf->date_format : $general_settings['default-date-format'];
568 $date = $ticket->{$cf->slug};
569 $value = is_object( $date ) ? wp_date( $format, $date->setTimezone( wp_timezone() )->getTimestamp() ) : '';
570
571 return apply_filters( 'wpsc_ticket_field_val_date_closed', $value, $cf, $ticket, $module );
572 }
573
574 /**
575 * Print ticket value for given custom field on ticket list
576 *
577 * @param WPSC_Custom_Field $cf - custom field object.
578 * @param WPSC_Ticket $ticket - ticket object.
579 * @return void
580 */
581 public static function print_tl_ticket_field_val( $cf, $ticket ) {
582
583 $date = $ticket->{$cf->slug};
584 if ( ! is_object( $date ) ) {
585 return;
586 }
587
588 $now = new DateTime();
589 $date_str = self::get_ticket_field_val( $cf, $ticket );
590 $diff_str = WPSC_Functions::date_interval_highest_unit_ago( $date->diff( $now ) );
591 if ( $cf->date_display_as == 'date' ) {
592 ?>
593 <span title="<?php echo esc_attr( $diff_str ); ?>"><?php echo esc_attr( $date_str ); ?></span>
594 <?php
595 } else {
596 ?>
597 <span title="<?php echo esc_attr( $date_str ); ?>"><?php echo esc_attr( $diff_str ); ?></span>
598 <?php
599 }
600 }
601
602 /**
603 * Print ticket value for given custom field on widget
604 *
605 * @param WPSC_Custom_Field $cf - custom field object.
606 * @param WPSC_Ticket $ticket - ticket object.
607 * @return void
608 */
609 public static function print_widget_ticket_field_val( $cf, $ticket ) {
610
611 self::print_tl_ticket_field_val( $cf, $ticket );
612 }
613 }
614 endif;
615
616 WPSC_DF_Date_Closed::init();
617