PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.3
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.3
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-last-reply-on.php

class-wpsc-df-last-reply-on.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.3, at includes/custom-field-types/class-wpsc-df-last-reply-on.php

512 lines 15.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_Last_Reply_On' ) ) :
7
8 final class WPSC_DF_Last_Reply_On {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_last_reply_on';
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
168 /**
169 * Load current class to reference classes
170 *
171 * @param array $classes - Associative array of class names indexed by its slug.
172 * @return array
173 */
174 public static function load_ref_class( $classes ) {
175
176 $classes[ self::$slug ] = array(
177 'class' => __CLASS__,
178 'save-key' => 'id',
179 );
180 return $classes;
181 }
182
183 /**
184 * Print operators for ticket form filter
185 *
186 * @param WPSC_Custom_Field $cf - custom field object.
187 * @param array $filter - Existing filters (if any).
188 * @return void
189 */
190 public static function get_operators( $cf, $filter = array() ) {
191 ?>
192
193 <div class="item conditional">
194 <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' ) ); ?>');">
195 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
196 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
197 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<' ); ?> value="<"><?php esc_attr_e( 'Less than', 'supportcandy' ); ?></option>
198 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<=' ); ?> value="<="><?php esc_attr_e( 'Less than or equals', 'supportcandy' ); ?></option>
199 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>' ); ?> value=">"><?php esc_attr_e( 'Greater than', 'supportcandy' ); ?></option>
200 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>=' ); ?> value=">="><?php esc_attr_e( 'Greater than or equals', 'supportcandy' ); ?></option>
201 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'BETWEEN' ); ?> value="BETWEEN"><?php esc_attr_e( 'Between', 'supportcandy' ); ?></option>
202 </select>
203 </div>
204 <?php
205 }
206
207 /**
208 * Print operators for ticket form filter
209 *
210 * @param string $operator - condition operator on which operands should be returned.
211 * @param WPSC_Custom_Field $cf - custom field object.
212 * @param array $filter - Exising functions (if any).
213 * @return void
214 */
215 public static function get_operands( $operator, $cf, $filter = array() ) {
216
217 $unique_id = uniqid( 'wpsc_' );
218 $is_between = $operator == 'BETWEEN' ? true : false;
219 $operand_val_1 = isset( $filter['operand_val_1'] ) ? $filter['operand_val_1'] : '';
220 $operand_val_2 = isset( $filter['operand_val_2'] ) ? $filter['operand_val_2'] : '';
221 ?>
222
223 <div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>">
224 <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 ); ?>">
225 </div>
226 <script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').flatpickr({});</script>
227 <?php
228
229 if ( $is_between ) {
230 ?>
231 <div class="item conditional operand">
232 <input type="text" class="operand_val_2 <?php echo esc_attr( $unique_id ); ?>" placeholder="<?php esc_attr_e( 'To date', 'supportcandy' ); ?>" value="">
233 </div>
234 <script>
235 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').change(function() {
236 var fromDate = jQuery(this).val().trim();
237 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
238 minDate: fromDate,
239 defaultDate: fromDate,
240 });
241 });
242 <?php
243 if ( $operand_val_2 ) {
244 ?>
245 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
246 minDate: '<?php echo esc_attr( $operand_val_1 ); ?>',
247 defaultDate: '<?php echo esc_attr( $operand_val_2 ); ?>',
248 });
249 <?php
250 }
251 ?>
252 </script>
253 <?php
254 }
255 }
256
257 /**
258 * Parse filter and return sql query to be merged in ticket model query builder
259 *
260 * @param WPSC_Custom_Field $cf - custom field of this type.
261 * @param mixed $compare - comparison operator.
262 * @param mixed $val - value to compare.
263 * @return string
264 */
265 public static function parse_filter( $cf, $compare, $val ) {
266
267 $str = '';
268
269 switch ( $compare ) {
270
271 case '=':
272 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
273 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
274 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'';
275 break;
276
277 case '<':
278 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
279 $str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $from ) . '\'';
280 break;
281
282 case '>':
283 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
284 $str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $to ) . '\'';
285 break;
286
287 case '<=':
288 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
289 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
290 $arr = array(
291 't.' . $cf->slug . $compare . '\'' . esc_sql( $from ) . '\'',
292 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'',
293 );
294 $str = '(' . implode( ' OR ', $arr ) . ')';
295 break;
296
297 case '>=':
298 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
299 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
300 $arr = array(
301 't.' . $cf->slug . $compare . '\'' . esc_sql( $to ) . '\'',
302 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'',
303 );
304 $str = '(' . implode( ' OR ', $arr ) . ')';
305 break;
306
307 case 'BETWEEN':
308 $from = $val['operand_val_1'];
309 $to = $val['operand_val_2'];
310 if ( preg_match( '/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/', $from ) ) {
311 $from = WPSC_Functions::get_utc_date_str( $val['operand_val_1'] );
312 $to = WPSC_Functions::get_utc_date_str( $val['operand_val_2'] );
313 } else {
314 $from = WPSC_Functions::get_utc_date_str( $val['operand_val_1'] . ' 00:00:00' );
315 $to = WPSC_Functions::get_utc_date_str( $val['operand_val_2'] . ' 23:59:59' );
316 }
317 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'';
318 break;
319
320 default:
321 $str = '1=1';
322 }
323
324 return $str;
325 }
326
327 /**
328 * Return orderby string
329 *
330 * @param WPSC_Custom_Field $cf - custom field object.
331 * @return string
332 */
333 public static function get_orderby_string( $cf ) {
334
335 return 't.' . $cf->slug;
336 }
337
338 /**
339 * Return default value for custom field of this type
340 *
341 * @param WPSC_Custom_Field $cf - custom field object.
342 * @return mixed
343 */
344 public static function get_default_value( $cf ) {
345
346 return '';
347 }
348
349 /**
350 * Return val field for meta query of this type of custom field
351 *
352 * @param array $condition - condition data.
353 * @return mixed
354 */
355 public static function get_meta_value( $condition ) {
356
357 $operator = $condition['operator'];
358 switch ( $operator ) {
359
360 case '=':
361 case '<':
362 case '<=':
363 case '>':
364 case '>=':
365 return $condition['operand_val_1'];
366
367 case 'BETWEEN':
368 if (
369 ! ( isset( $condition['operand_val_1'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_1'] ) ) ||
370 ! ( isset( $condition['operand_val_2'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_2'] ) )
371 ) {
372 return false;
373 }
374
375 return array(
376 'operand_val_1' => $condition['operand_val_1'],
377 'operand_val_2' => $condition['operand_val_2'],
378 );
379 }
380 return false;
381 }
382
383 /**
384 * Print edit custom field properties
385 *
386 * @param WPSC_Custom_Fields $cf - custom field object.
387 * @param string $field_class - class name of field category.
388 * @return void
389 */
390 public static function get_edit_custom_field_properties( $cf, $field_class ) {
391 ?>
392
393 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
394 <div class="label-container">
395 <label for=""><?php esc_attr_e( 'Display as', 'supportcandy' ); ?></label>
396 </div>
397 <select name="date_display_as">
398 <option <?php selected( $cf->date_display_as, 'date' ); ?> value="date"><?php esc_attr_e( 'Date Format', 'supportcandy' ); ?></option>
399 <option <?php selected( $cf->date_display_as, 'diff' ); ?> value="diff"><?php esc_attr_e( 'Date Difference (e.g. 1 hour ago)', 'supportcandy' ); ?></option>
400 </select>
401 </div>
402
403 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
404 <div class="label-container">
405 <label for=""><?php esc_attr_e( 'Date format', 'supportcandy' ); ?></label>
406 </div>
407 <input type="text" name="date_format" value="<?php echo esc_attr( $cf->date_format ); ?>" autocomplete="off" />
408 </div>
409 <?php
410
411 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
412 ?>
413 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
414 <div class="label-container">
415 <label for="">
416 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
417 </label>
418 </div>
419 <input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off">
420 </div>
421 <?php
422 endif;
423 }
424
425 /**
426 * Set custom field properties. Can be used by add/edit custom field.
427 * Ignore phpcs nonce issue as we already checked where it is called from.
428 *
429 * @param WPSC_Custom_Field $cf - custom field object.
430 * @param string $field_class - class of field category.
431 * @return void
432 */
433 public static function set_cf_properties( $cf, $field_class ) {
434
435 // date display as.
436 $cf->date_display_as = isset( $_POST['date_display_as'] ) ? sanitize_text_field( wp_unslash( $_POST['date_display_as'] ) ) : 'date'; // phpcs:ignore
437
438 // date format.
439 $cf->date_format = isset( $_POST['date_format'] ) ? sanitize_text_field( wp_unslash( $_POST['date_format'] ) ) : ''; // phpcs:ignore
440
441 // tl_width!
442 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
443 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
444 $cf->tl_width = $tl_width ? $tl_width : 100;
445 }
446
447 // save!
448 $cf->save();
449 }
450
451 /**
452 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
453 *
454 * @param WPSC_Custom_Field $cf - custom field object.
455 * @param WPSC_Ticket $ticket - ticket object.
456 * @param string $module - module name.
457 * @return string
458 */
459 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
460
461 $general_settings = get_option( 'wpsc-gs-general' );
462 $format = $cf->date_format ? $cf->date_format : $general_settings['default-date-format'];
463 $date = $ticket->{$cf->slug};
464 $value = is_object( $date ) ? wp_date( $format, $date->setTimezone( wp_timezone() )->getTimestamp() ) : '';
465
466 return apply_filters( 'wpsc_ticket_field_val_last_reply_on', $value, $cf, $ticket, $module );
467 }
468
469 /**
470 * Print ticket value for given custom field on ticket list
471 *
472 * @param WPSC_Custom_Field $cf - custom field object.
473 * @param WPSC_Ticket $ticket - ticket object.
474 * @return void
475 */
476 public static function print_tl_ticket_field_val( $cf, $ticket ) {
477
478 $date = $ticket->{$cf->slug};
479 if ( ! is_object( $date ) ) {
480 return;
481 }
482
483 $now = new DateTime();
484 $date_str = self::get_ticket_field_val( $cf, $ticket );
485 $diff_str = WPSC_Functions::date_interval_highest_unit_ago( $date->diff( $now ) );
486 if ( $cf->date_display_as == 'date' ) {
487 ?>
488 <span title="<?php echo esc_attr( $diff_str ); ?>"><?php echo esc_attr( $date_str ); ?></span>
489 <?php
490 } else {
491 ?>
492 <span title="<?php echo esc_attr( $date_str ); ?>"><?php echo esc_attr( $diff_str ); ?></span>
493 <?php
494 }
495 }
496
497 /**
498 * Print ticket value for given custom field on widget
499 *
500 * @param WPSC_Custom_Field $cf - custom field object.
501 * @param WPSC_Ticket $ticket - ticket object.
502 * @return void
503 */
504 public static function print_widget_ticket_field_val( $cf, $ticket ) {
505
506 self::print_tl_ticket_field_val( $cf, $ticket );
507 }
508 }
509 endif;
510
511 WPSC_DF_Last_Reply_On::init();
512