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-id.php

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

516 lines 13.1 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_ID' ) ) :
7
8 final class WPSC_DF_ID {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_id';
16
17 /**
18 * Set whether this custom field type is of type date
19 *
20 * @var boolean
21 */
22 public static $is_date = false;
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 = 'BIGINT 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 = false;
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 = '';
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 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 = false;
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 = false;
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 = true;
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 search query.
168 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
169 add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
170
171 // Set custom field type.
172 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
173 }
174
175 /**
176 * Load current class to reference classes
177 *
178 * @param array $classes - Associative array of class names indexed by its slug.
179 * @return array
180 */
181 public static function load_ref_class( $classes ) {
182
183 $classes[ self::$slug ] = array(
184 'class' => __CLASS__,
185 'save-key' => 'id',
186 );
187 return $classes;
188 }
189
190 /**
191 * Add default custom field type to list
192 *
193 * @param array $default_cf_types - default custom field types array.
194 * @return array
195 */
196 public static function default_cf_types( $default_cf_types ) {
197 $default_cf_types[ self::$slug ] = array(
198 'label' => esc_attr__( 'ID', 'supportcandy' ),
199 'class' => __CLASS__,
200 );
201 return $default_cf_types;
202 }
203
204 /**
205 * Print operators for ticket form filter
206 *
207 * @param WPSC_Custom_Field $cf - custom field object.
208 * @param array $filter - Existing filters (if any).
209 * @return void
210 */
211 public static function get_operators( $cf, $filter = array() ) {
212 ?>
213
214 <div class="item conditional">
215 <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' ) ); ?>');">
216 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
217 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
218 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
219 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
220 </select>
221 </div>
222 <?php
223 }
224
225 /**
226 * Print operators for ticket form filter
227 *
228 * @param string $operator - condition operator on which operands should be returned.
229 * @param WPSC_Custom_Field $cf - custom field object.
230 * @param array $filter - Exising functions (if any).
231 * @return void
232 */
233 public static function get_operands( $operator, $cf, $filter = array() ) {
234
235 $value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : '';
236 if ( in_array( $operator, array( 'IN', 'NOT IN' ) ) ) {
237 ?>
238
239 <div class="item conditional operand single">
240 <textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea>
241 </div>
242 <?php
243
244 } else {
245 ?>
246
247 <div class="item conditional operand single">
248 <input
249 type="text"
250 class="operand_val_1"
251 onchange="wpsc_check_tff_visibility()"
252 onkeydown="javascript: return event.keyCode == 69 ? false : true"
253 oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"
254 value="<?php echo intval( $value ); ?>"
255 autocomplete="off"/>
256 </div>
257 <?php
258 }
259 }
260
261 /**
262 * Parse filter and return sql query to be merged in ticket model query builder
263 *
264 * @param WPSC_Custom_Field $cf - custom field of this type.
265 * @param mixed $compare - comparison operator.
266 * @param mixed $val - value to compare.
267 * @return string
268 */
269 public static function parse_filter( $cf, $compare, $val ) {
270
271 global $wpdb;
272 $column = 't.' . sanitize_key( $cf->slug );
273
274 /**
275 * Normalize IDs (INT only)
276 */
277 $normalize_ids = static function ( $values ) {
278
279 $values = is_array( $values ) ? $values : array( $values );
280 $ids = array();
281
282 foreach ( $values as $v ) {
283 if ( is_numeric( $v ) && (int) $v > 0 ) {
284 $ids[] = (int) $v;
285 }
286 }
287
288 return array_unique( $ids );
289 };
290
291 switch ( $compare ) {
292
293 case '=':
294 $ids = $normalize_ids( $val );
295 if ( empty( $ids ) ) {
296 return '1=0';
297 }
298
299 return $wpdb->prepare(
300 "{$column} RLIKE %s",
301 '(^|[|])' . $ids[0] . '($|[|])'
302 );
303
304 case 'IN':
305 $ids = $normalize_ids( $val );
306 if ( empty( $ids ) ) {
307 return '1=0';
308 }
309
310 $regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])';
311
312 return $wpdb->prepare(
313 "{$column} RLIKE %s",
314 $regex
315 );
316
317 case 'NOT IN':
318 $ids = $normalize_ids( $val );
319 if ( empty( $ids ) ) {
320 return '1=1';
321 }
322
323 $regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])';
324
325 return $wpdb->prepare(
326 "{$column} NOT RLIKE %s",
327 $regex
328 );
329
330 default:
331 return '1=1';
332 }
333 }
334
335 /**
336 * Check ticket condition
337 *
338 * @param array $condition - array with condition data.
339 * @param WPSC_Custom_Field $cf - custom field object.
340 * @param WPSC_Ticket $ticket - ticket object.
341 * @return boolean
342 */
343 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
344
345 $flag = true;
346 $value = stripslashes( $ticket->{$cf->slug} );
347 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
348
349 if ( ! $value ) {
350 return false;
351 }
352
353 switch ( $condition['operator'] ) {
354
355 case '=':
356 $flag = $condition['operand_val_1'] == $value ? true : false;
357 break;
358
359 case 'IN':
360 $flag = false;
361 foreach ( $terms as $term ) {
362 $term = intval( trim( $term ) );
363 if ( $term == $value ) {
364 $flag = true;
365 break;
366 }
367 }
368 break;
369
370 case 'NOT IN':
371 foreach ( $terms as $term ) {
372 $term = intval( trim( $term ) );
373 if ( $term == $value ) {
374 $flag = false;
375 break;
376 }
377 }
378 break;
379
380 default:
381 $flag = true;
382 }
383
384 return $flag;
385 }
386
387 /**
388 * Add ticket search compatibility for fields of this custom field type.
389 *
390 * @param array $sql - Array of sql peices that can be joined later.
391 * @param array $filter - User filter.
392 * @param array $custom_fields - Custom fields array applicable for search.
393 * @param string $search - search string.
394 * @param array $allowed_search_fields - Allowed search fields.
395 * @return array
396 */
397 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
398
399 if ( in_array( 'id', $allowed_search_fields ) ) {
400 $sql[] = 't.id LIKE \'%' . $search . '%\'';
401 }
402 return $sql;
403 }
404
405 /**
406 * Return orderby string
407 *
408 * @param WPSC_Custom_Field $cf - custom field object.
409 * @return string
410 */
411 public static function get_orderby_string( $cf ) {
412
413 return 't.id';
414 }
415
416 /**
417 * Return val field for meta query of this type of custom field
418 *
419 * @param array $condition - condition data.
420 * @return mixed
421 */
422 public static function get_meta_value( $condition ) {
423
424 $operator = $condition['operator'];
425 switch ( $operator ) {
426
427 case '=':
428 return $condition['operand_val_1'];
429
430 case 'IN':
431 case 'NOT IN':
432 $val = explode( PHP_EOL, $condition['operand_val_1'] );
433 $temp_val = array();
434 foreach ( $val as $id ) {
435 $id = intval( trim( $id ) );
436 if ( $id ) {
437 $temp_val[] = $id;
438 }
439 }
440 return $temp_val ? $temp_val : false;
441 }
442 return false;
443 }
444
445 /**
446 * Print edit custom field properties
447 *
448 * @param WPSC_Custom_Fields $cf - custom field object.
449 * @param string $field_class - class name of field category.
450 * @return void
451 */
452 public static function get_edit_custom_field_properties( $cf, $field_class ) {
453
454 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
455 ?>
456 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
457 <div class="label-container">
458 <label for="">
459 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
460 </label>
461 </div>
462 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
463 </div>
464 <?php
465 endif;
466 }
467
468 /**
469 * Set custom field properties. Can be used by add/edit custom field.
470 * Ignore phpcs nonce issue as we already checked where it is called from.
471 *
472 * @param WPSC_Custom_Field $cf - custom field object.
473 * @param string $field_class - class of field category.
474 * @return void
475 */
476 public static function set_cf_properties( $cf, $field_class ) {
477
478 // tl_width!
479 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
480 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
481 $cf->tl_width = $tl_width ? $tl_width : 100;
482 }
483
484 // save!
485 $cf->save();
486 }
487
488 /**
489 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
490 *
491 * @param WPSC_Custom_Field $cf - custom field object.
492 * @param WPSC_Ticket $ticket - ticket object.
493 * @param string $module - module name.
494 * @return string
495 */
496 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
497
498 return $ticket->id;
499 }
500
501 /**
502 * Print ticket value for given custom field on ticket list
503 *
504 * @param WPSC_Custom_Field $cf - custom field object.
505 * @param WPSC_Ticket $ticket - ticket object.
506 * @return void
507 */
508 public static function print_tl_ticket_field_val( $cf, $ticket ) {
509
510 echo '#' . esc_attr( $ticket->{$cf->slug} );
511 }
512 }
513 endif;
514
515 WPSC_DF_ID::init();
516