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-date-updated.php

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

674 lines 23.4 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_Updated' ) ) :
7
8 final class WPSC_DF_Date_Updated {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_date_updated';
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 NOT 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 // ticket form.
168 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
169
170 // create ticket data for rest api.
171 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
172 }
173
174 /**
175 * Load current class to reference classes
176 *
177 * @param array $classes - Associative array of class names indexed by its slug.
178 * @return array
179 */
180 public static function load_ref_class( $classes ) {
181
182 $classes[ self::$slug ] = array(
183 'class' => __CLASS__,
184 'save-key' => 'id',
185 );
186 return $classes;
187 }
188
189 /**
190 * Print operators for ticket form filter
191 *
192 * @param WPSC_Custom_Field $cf - custom field object.
193 * @param array $filter - Existing filters (if any).
194 * @return void
195 */
196 public static function get_operators( $cf, $filter = array() ) {
197 ?>
198
199 <div class="item conditional">
200 <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' ) ); ?>');">
201 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
202 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
203 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<' ); ?> value="<"><?php esc_attr_e( 'Less than', 'supportcandy' ); ?></option>
204 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<=' ); ?> value="<="><?php esc_attr_e( 'Less than or equals', 'supportcandy' ); ?></option>
205 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>' ); ?> value=">"><?php esc_attr_e( 'Greater than', 'supportcandy' ); ?></option>
206 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>=' ); ?> value=">="><?php esc_attr_e( 'Greater than or equals', 'supportcandy' ); ?></option>
207 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'BETWEEN' ); ?> value="BETWEEN"><?php esc_attr_e( 'Between', 'supportcandy' ); ?></option>
208 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'today' ); ?> value="today"><?php esc_attr_e( 'Today', 'supportcandy' ); ?></option>
209 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'yesterday' ); ?> value="yesterday"><?php esc_attr_e( 'Yesterday', 'supportcandy' ); ?></option>
210 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'this-week' ); ?> value="this-week"><?php esc_attr_e( 'This week', 'supportcandy' ); ?></option>
211 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-week' ); ?> value="last-week"><?php esc_attr_e( 'Last week', 'supportcandy' ); ?></option>
212 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-7-days' ); ?> value="last-7-days"><?php esc_attr_e( 'Last 7 days', 'supportcandy' ); ?></option>
213 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-30-days' ); ?> value="last-30-days"><?php esc_attr_e( 'Last 30 days', 'supportcandy' ); ?></option>
214 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'this-month' ); ?> value="this-month"><?php esc_attr_e( 'This month', 'supportcandy' ); ?></option>
215 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-month' ); ?> value="last-month"><?php esc_attr_e( 'Last month', 'supportcandy' ); ?></option>
216 </select>
217 </select>
218 </div>
219 <?php
220 }
221
222 /**
223 * Print operators for ticket form filter
224 *
225 * @param string $operator - condition operator on which operands should be returned.
226 * @param WPSC_Custom_Field $cf - custom field object.
227 * @param array $filter - Exising functions (if any).
228 * @return void
229 */
230 public static function get_operands( $operator, $cf, $filter = array() ) {
231
232 $unique_id = uniqid( 'wpsc_' );
233 $is_between = $operator == 'BETWEEN' ? true : false;
234 $operand_val_1 = isset( $filter['operand_val_1'] ) ? $filter['operand_val_1'] : '';
235 $operand_val_2 = isset( $filter['operand_val_2'] ) ? $filter['operand_val_2'] : '';
236
237 $short_filters = array( 'today', 'yesterday', 'this-week', 'last-week', 'last-7-days', 'last-30-days', 'this-month', 'last-month' );
238 if ( in_array( $operator, $short_filters ) ) {
239 $today = ( new DateTime() )->format( 'Y-m-d' );
240 ?>
241 <div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>">
242 <input type="hidden" class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" value="<?php echo esc_attr( $today ); ?>">
243 </div>
244 <?php
245 } else {
246 ?>
247 <div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>">
248 <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 ); ?>">
249 </div>
250 <script>
251 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
252 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
253 });
254 </script>
255 <?php
256 }
257
258 if ( $is_between ) {
259 ?>
260 <div class="item conditional operand">
261 <input type="text" class="operand_val_2 <?php echo esc_attr( $unique_id ); ?>" placeholder="<?php esc_attr_e( 'To date', 'supportcandy' ); ?>" value="">
262 </div>
263 <script>
264 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').change(function() {
265 var fromDate = jQuery(this).val().trim();
266 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
267 minDate: fromDate,
268 defaultDate: fromDate,
269 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
270 });
271 });
272 <?php
273 if ( $operand_val_2 ) {
274 ?>
275 jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({
276 minDate: '<?php echo esc_attr( $operand_val_1 ); ?>',
277 defaultDate: '<?php echo esc_attr( $operand_val_2 ); ?>',
278 locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>",
279 });
280 <?php
281 }
282 ?>
283 </script>
284 <?php
285 }
286 }
287
288 /**
289 * Parse filter and return sql query to be merged in ticket model query builder
290 *
291 * @param WPSC_Custom_Field $cf - custom field of this type.
292 * @param mixed $compare - comparison operator.
293 * @param mixed $val - value to compare.
294 * @return string
295 */
296 public static function parse_filter( $cf, $compare, $val ) {
297
298 $str = '';
299
300 switch ( $compare ) {
301
302 case '=':
303 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
304 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
305 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'';
306 break;
307
308 case '<':
309 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
310 $str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $from ) . '\'';
311 break;
312
313 case '>':
314 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
315 $str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $to ) . '\'';
316 break;
317
318 case '<=':
319 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
320 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
321 $arr = array(
322 't.' . $cf->slug . esc_sql( $compare ) . '\'' . esc_sql( $from ) . '\'',
323 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'',
324 );
325 $str = '(' . implode( ' OR ', $arr ) . ')';
326 break;
327
328 case '>=':
329 $from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' );
330 $to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' );
331 $arr = array(
332 't.' . $cf->slug . esc_sql( $compare ) . '\'' . esc_sql( $to ) . '\'',
333 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'',
334 );
335 $str = '(' . implode( ' OR ', $arr ) . ')';
336 break;
337
338 case 'BETWEEN':
339 $from = WPSC_Functions::get_utc_date_str( $val['operand_val_1'] . ' 00:00:00' );
340 $to = WPSC_Functions::get_utc_date_str( $val['operand_val_2'] . ' 23:59:59' );
341 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'';
342 break;
343
344 case 'today':
345 $today = new DateTime();
346 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $today->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $today->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
347 break;
348
349 case 'yesterday':
350 $yesterday = ( new DateTime() )->modify( '-1 day' );
351 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $yesterday->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $yesterday->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
352 break;
353
354 case 'this-week':
355 $start_week = new DateTime( 'monday this week' );
356 $end_week = new DateTime( 'sunday this week' );
357 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_week->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_week->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
358 break;
359
360 case 'last-week':
361 $start_week = new DateTime( 'monday last week' );
362 $end_week = new DateTime( 'sunday last week' );
363 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_week->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_week->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
364 break;
365
366 case 'last-7-days':
367 $end_date = new DateTime();
368 $start_date = ( new DateTime() )->modify( '-7 days' );
369 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_date->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_date->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
370 break;
371
372 case 'last-30-days':
373 $end_date = new DateTime();
374 $start_date = ( new DateTime() )->modify( '-30 days' );
375 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_date->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_date->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
376 break;
377
378 case 'this-month':
379 $start_month = new DateTime( 'first day of this month' );
380 $end_month = new DateTime( 'last day of this month' );
381 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_month->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_month->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
382 break;
383
384 case 'last-month':
385 $start_month = new DateTime( 'first day of last month' );
386 $end_month = new DateTime( 'last day of last month' );
387 $str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_month->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_month->format( 'Y-m-d 23:59:59 ' ) ) . '\'';
388 break;
389
390 default:
391 $str = '1=1';
392 }
393
394 return $str;
395 }
396
397 /**
398 * Return orderby string
399 *
400 * @param WPSC_Custom_Field $cf - custom field object.
401 * @return string
402 */
403 public static function get_orderby_string( $cf ) {
404
405 return 't.' . $cf->slug;
406 }
407
408 /**
409 * Check ticket condition
410 *
411 * @param array $condition - array with condition data.
412 * @param WPSC_Custom_Field $cf - custom field object.
413 * @param WPSC_Ticket $ticket - ticket object.
414 * @return boolean
415 */
416 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
417
418 $flag = true;
419 $date = $ticket->{$cf->slug};
420 if ( ! is_object( $date ) ) {
421 return false;
422 }
423
424 $date->setTimezone( wp_timezone() );
425
426 switch ( $condition['operator'] ) {
427
428 case '=':
429 $flag = $date->format( 'Y-m-d' ) == $condition['operand_val_1'] ? true : false;
430 break;
431
432 case '<':
433 $operand = new DateTime( $condition['operand_val_1'] );
434 $flag = $operand < $date ? true : false;
435 break;
436
437 case '>':
438 $operand = new DateTime( $condition['operand_val_1'] );
439 $flag = $operand > $date ? true : false;
440 break;
441
442 case '<=':
443 $operand = new DateTime( $condition['operand_val_1'] );
444 $flag = $operand <= $date ? true : false;
445 break;
446
447 case '>=':
448 $operand = new DateTime( $condition['operand_val_1'] );
449 $flag = $operand >= $date ? true : false;
450 break;
451
452 case 'BETWEEN':
453 $operand1 = new DateTime( $condition['operand_val_1'] );
454 $operand2 = new DateTime( $condition['operand_val_2'] );
455 $flag = $operand1 <= $date && $operand2 >= $date ? true : false;
456 break;
457
458 default:
459 $flag = true;
460 }
461
462 return $flag;
463 }
464
465 /**
466 * Return default value for custom field of this type
467 *
468 * @param WPSC_Custom_Field $cf - custom field object.
469 * @return mixed
470 */
471 public static function get_default_value( $cf ) {
472
473 $now = new DateTime();
474 return $now->format( 'Y-m-d H:i:s' );
475 }
476
477 /**
478 * Check and return custom field value for new ticket to be created.
479 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
480 * Ignore phpcs nonce issue as we already checked where it is called from.
481 *
482 * @param array $data - Array of values to to stored in ticket in an insert function.
483 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
484 * @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.
485 * @return array
486 */
487 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
488
489 $data['date_updated'] = self::get_default_value( $custom_fields[ self::$slug ][0] );
490 return $data;
491 }
492
493 /**
494 * Set create ticket data for rest api request
495 *
496 * @param array $data - create ticket data array.
497 * @param WP_REST_Request $request - rest request object.
498 * @param array $custom_fields - custom field objects indexed by unique custom field types.
499 * @return array
500 */
501 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
502
503 $data['date_updated'] = self::get_default_value( $custom_fields[ self::$slug ][0] );
504 return $data;
505 }
506
507 /**
508 * Return val field for meta query of this type of custom field
509 *
510 * @param array $condition - condition data.
511 * @return mixed
512 */
513 public static function get_meta_value( $condition ) {
514
515 $operator = $condition['operator'];
516 switch ( $operator ) {
517
518 case '=':
519 case '<':
520 case '<=':
521 case '>':
522 case '>=':
523 case 'today':
524 case 'yesterday':
525 case 'this-week':
526 case 'last-week':
527 case 'last-7-days':
528 case 'last-30-days':
529 case 'this-month':
530 case 'last-month':
531 return $condition['operand_val_1'];
532
533 case 'BETWEEN':
534 if (
535 ! ( isset( $condition['operand_val_1'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_1'] ) ) ||
536 ! ( isset( $condition['operand_val_2'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_2'] ) )
537 ) {
538 return false;
539 }
540
541 return array(
542 'operand_val_1' => $condition['operand_val_1'],
543 'operand_val_2' => $condition['operand_val_2'],
544 );
545 }
546 return false;
547 }
548
549 /**
550 * Return data for this custom field while creating duplicate ticket
551 *
552 * @param WPSC_Custom_Field $cf - custom field object.
553 * @param WPSC_Ticket $ticket - ticket object.
554 * @return mixed
555 */
556 public static function get_duplicate_ticket_data( $cf, $ticket ) {
557
558 return ( new DateTime() )->format( 'Y-m-d H:i:s' );
559 }
560
561 /**
562 * Print edit custom field properties
563 *
564 * @param WPSC_Custom_Fields $cf - custom field object.
565 * @param string $field_class - class name of field category.
566 * @return void
567 */
568 public static function get_edit_custom_field_properties( $cf, $field_class ) {
569 ?>
570
571 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
572 <div class="label-container">
573 <label for=""><?php esc_attr_e( 'Display as', 'supportcandy' ); ?></label>
574 </div>
575 <select name="date_display_as">
576 <option <?php selected( $cf->date_display_as, 'date' ); ?> value="date"><?php esc_attr_e( 'Date Format', 'supportcandy' ); ?></option>
577 <option <?php selected( $cf->date_display_as, 'diff' ); ?> value="diff"><?php esc_attr_e( 'Date Difference (e.g. 1 hour ago)', 'supportcandy' ); ?></option>
578 </select>
579 </div>
580
581 <div data-type="date-format" data-required="false" class="wpsc-input-group date-format">
582 <div class="label-container">
583 <label for=""><?php esc_attr_e( 'Date format', 'supportcandy' ); ?></label>
584 </div>
585 <input type="text" name="date_format" value="<?php echo esc_attr( $cf->date_format ); ?>" autocomplete="off" />
586 </div>
587 <?php
588
589 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
590 ?>
591 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
592 <div class="label-container">
593 <label for="">
594 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
595 </label>
596 </div>
597 <input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off">
598 </div>
599 <?php
600 endif;
601 }
602
603 /**
604 * Set custom field properties. Can be used by add/edit custom field.
605 * Ignore phpcs nonce issue as we already checked where it is called from.
606 *
607 * @param WPSC_Custom_Field $cf - custom field object.
608 * @param string $field_class - class of field category.
609 * @return void
610 */
611 public static function set_cf_properties( $cf, $field_class ) {
612
613 // date display as.
614 $cf->date_display_as = isset( $_POST['date_display_as'] ) ? sanitize_text_field( wp_unslash( $_POST['date_display_as'] ) ) : 'date'; // phpcs:ignore
615
616 // date format.
617 $cf->date_format = isset( $_POST['date_format'] ) ? sanitize_text_field( wp_unslash( $_POST['date_format'] ) ) : ''; // phpcs:ignore
618
619 // tl_width!
620 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
621 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
622 $cf->tl_width = $tl_width ? $tl_width : 100;
623 }
624
625 // save!
626 $cf->save();
627 }
628
629 /**
630 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
631 *
632 * @param WPSC_Custom_Field $cf - custom field object.
633 * @param WPSC_Ticket $ticket - ticket object.
634 * @param string $module - module name.
635 * @return string
636 */
637 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
638
639 $general_settings = get_option( 'wpsc-gs-general' );
640 $format = $cf->date_format ? $cf->date_format : $general_settings['default-date-format'];
641 $date = $ticket->{$cf->slug};
642 $value = is_object( $date ) ? wp_date( $format, $date->setTimezone( wp_timezone() )->getTimestamp() ) : '';
643
644 return apply_filters( 'wpsc_ticket_field_val_date_updated', $value, $cf, $ticket, $module );
645 }
646
647 /**
648 * Print ticket value for given custom field on ticket list
649 *
650 * @param WPSC_Custom_Field $cf - custom field object.
651 * @param WPSC_Ticket $ticket - ticket object.
652 * @return void
653 */
654 public static function print_tl_ticket_field_val( $cf, $ticket ) {
655
656 $now = new DateTime();
657 $date = $ticket->{$cf->slug};
658 $date_str = self::get_ticket_field_val( $cf, $ticket );
659 $diff_str = WPSC_Functions::date_interval_highest_unit_ago( $date->diff( $now ) );
660 if ( $cf->date_display_as == 'date' ) {
661 ?>
662 <span title="<?php echo esc_attr( $diff_str ); ?>"><?php echo esc_attr( $date_str ); ?></span>
663 <?php
664 } else {
665 ?>
666 <span title="<?php echo esc_attr( $date_str ); ?>"><?php echo esc_attr( $diff_str ); ?></span>
667 <?php
668 }
669 }
670 }
671 endif;
672
673 WPSC_DF_Date_Updated::init();
674