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

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

518 lines 14.3 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_Source' ) ) :
7
8 final class WPSC_DF_Source {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_source';
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 = 'VARCHAR(50) 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 = false;
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 = true;
149
150 /**
151 * Source list
152 *
153 * @var boolean
154 */
155 public static $sources = array();
156
157 /**
158 * Set whether fields of this custom field type is applicalbe for search on ticket list page.
159 *
160 * @var boolean
161 */
162 public static $is_search = false;
163
164 /**
165 * Initialize the class
166 *
167 * @return void
168 */
169 public static function init() {
170
171 // Get object of this class.
172 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
173
174 // ticket form.
175 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
176
177 // create ticket data for rest api.
178 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
179
180 // source list.
181 add_action( 'init', array( __CLASS__, 'load_sources' ) );
182
183 // Set custom field type.
184 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
185 }
186
187 /**
188 * Load current class to reference classes
189 *
190 * @param array $classes - Associative array of class names indexed by its slug.
191 * @return array
192 */
193 public static function load_ref_class( $classes ) {
194
195 $classes[ self::$slug ] = array(
196 'class' => __CLASS__,
197 'save-key' => 'id',
198 );
199 return $classes;
200 }
201
202 /**
203 * Add default custom field type to list
204 *
205 * @param array $default_cf_types - default custom field types array.
206 * @return array
207 */
208 public static function default_cf_types( $default_cf_types ) {
209 $default_cf_types[ self::$slug ] = array(
210 'label' => esc_attr__( 'Source', 'supportcandy' ),
211 'class' => __CLASS__,
212 );
213 return $default_cf_types;
214 }
215
216 /**
217 * Load source list
218 *
219 * @return void
220 */
221 public static function load_sources() {
222
223 self::$sources = apply_filters(
224 'wpsc_source_list',
225 array(
226 'browser' => __( 'Browser', 'supportcandy' ),
227 'rest-api' => 'REST API',
228 )
229 );
230 }
231
232 /**
233 * Print operators for ticket form filter
234 *
235 * @param WPSC_Custom_Field $cf - custom field object.
236 * @param array $filter - Existing filters (if any).
237 * @return void
238 */
239 public static function get_operators( $cf, $filter = array() ) {
240 ?>
241
242 <div class="item conditional">
243 <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' ) ); ?>');">
244 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
245 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
246 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
247 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
248 </select>
249 </div>
250 <?php
251 }
252
253 /**
254 * Print operators for ticket form filter
255 *
256 * @param string $operator - condition operator on which operands should be returned.
257 * @param WPSC_Custom_Field $cf - custom field object.
258 * @param array $filter - Exising functions (if any).
259 * @return void
260 */
261 public static function get_operands( $operator, $cf, $filter = array() ) {
262
263 $is_multiple = $operator !== '=' ? true : false;
264 $unique_id = uniqid( 'wpsc_' );
265 ?>
266
267 <div class="item conditional operand single">
268 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
269 <?php
270 foreach ( self::$sources as $key => $label ) {
271 $selected = '';
272 if (
273 isset( $filter['operand_val_1'] ) &&
274 (
275 ( $is_multiple && in_array( $key, $filter['operand_val_1'] ) ) ||
276 ( ! $is_multiple && $key == $filter['operand_val_1'] )
277 )
278 ) {
279 $selected = 'selected';
280 }
281 ?>
282 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $label ); ?></option>
283 <?php
284 }
285 ?>
286 </select>
287 </div>
288 <script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script>
289 <?php
290 }
291
292 /**
293 * Parse filter and return sql query to be merged in ticket model query builder
294 *
295 * @param WPSC_Custom_Field $cf - custom field of this type.
296 * @param mixed $compare - comparison operator.
297 * @param mixed $val - value to compare.
298 * @return string
299 */
300 public static function parse_filter( $cf, $compare, $val ) {
301
302 $slug = is_string( $cf->slug ) ? trim( $cf->slug ) : '';
303 if ( $slug === '' || ! preg_match( '/^[a-zA-Z0-9_]+$/', $slug ) ) {
304 return '1=0';
305 }
306
307 $column = 'CONVERT(t.' . $slug . ' USING utf8)';
308
309 switch ( $compare ) {
310
311 case '=':
312 if ( ! is_scalar( $val ) ) {
313 return '1=0';
314 }
315
316 return 't.' . $slug . "='" . esc_sql( (string) $val ) . "'";
317
318 case 'IN':
319 case 'NOT IN':
320 $values = is_array( $val ) ? $val : array( $val );
321 $clean = array();
322
323 foreach ( $values as $v ) {
324 if ( is_scalar( $v ) && $v !== '' ) {
325 $clean[] = "'" . esc_sql( (string) $v ) . "'";
326 }
327 }
328
329 if ( empty( $clean ) ) {
330 return '1=0';
331 }
332
333 return $column . ' ' . $compare . ' (' . implode( ', ', $clean ) . ')';
334
335 default:
336 return '1=1';
337 }
338 }
339
340 /**
341 * Check ticket condition
342 *
343 * @param array $condition - array with condition data.
344 * @param WPSC_Custom_Field $cf - custom field object.
345 * @param WPSC_Ticket $ticket - ticket object.
346 * @return boolean
347 */
348 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
349
350 $flag = true;
351
352 switch ( $condition['operator'] ) {
353
354 case '=':
355 $flag = $ticket->{$cf->slug} == $condition['operand_val_1'] ? true : false;
356 break;
357
358 case 'IN':
359 $flag = in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] );
360 break;
361
362 case 'NOT IN':
363 $flag = ! in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] );
364 break;
365
366 default:
367 $flag = true;
368 }
369
370 return $flag;
371 }
372
373 /**
374 * Check and return custom field value for new ticket to be created.
375 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
376 * Ignore phpcs nonce issue as we already checked where it is called from.
377 *
378 * @param array $data - Array of values to to stored in ticket in an insert function.
379 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
380 * @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.
381 * @return array
382 */
383 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
384
385 $data['source'] = 'browser';
386 return $data;
387 }
388
389 /**
390 * Set create ticket data for rest api request
391 *
392 * @param array $data - create ticket data array.
393 * @param WP_REST_Request $request - rest request object.
394 * @param array $custom_fields - custom field objects indexed by unique custom field types.
395 * @return array
396 */
397 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
398
399 $data['source'] = 'rest-api';
400 return $data;
401 }
402
403 /**
404 * Return val field for meta query of this type of custom field
405 *
406 * @param array $condition - condition data.
407 * @return mixed
408 */
409 public static function get_meta_value( $condition ) {
410
411 $operator = $condition['operator'];
412 switch ( $operator ) {
413
414 case '=':
415 case 'IN':
416 case 'NOT IN':
417 return $condition['operand_val_1'];
418 }
419 return false;
420 }
421
422 /**
423 * Return data for this custom field while creating duplicate ticket
424 *
425 * @param WPSC_Custom_Field $cf - custom field object.
426 * @param WPSC_Ticket $ticket - ticket object.
427 * @return mixed
428 */
429 public static function get_duplicate_ticket_data( $cf, $ticket ) {
430
431 return '#' . $ticket->id;
432 }
433
434 /**
435 * Print edit custom field properties
436 *
437 * @param WPSC_Custom_Fields $cf - custom field object.
438 * @param string $field_class - class name of field category.
439 * @return void
440 */
441 public static function get_edit_custom_field_properties( $cf, $field_class ) {
442
443 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
444 ?>
445 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
446 <div class="label-container">
447 <label for="">
448 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
449 </label>
450 </div>
451 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
452 </div>
453 <?php
454 endif;
455 }
456
457 /**
458 * Set custom field properties. Can be used by add/edit custom field.
459 * Ignore phpcs nonce issue as we already checked where it is called from.
460 *
461 * @param WPSC_Custom_Field $cf - custom field object.
462 * @param string $field_class - class of field category.
463 * @return void
464 */
465 public static function set_cf_properties( $cf, $field_class ) {
466
467 // tl_width!
468 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
469 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
470 $cf->tl_width = $tl_width ? $tl_width : 100;
471 }
472
473 // save!
474 $cf->save();
475 }
476
477 /**
478 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
479 *
480 * @param WPSC_Custom_Field $cf - custom field object.
481 * @param WPSC_Ticket $ticket - ticket object.
482 * @param string $module - module name.
483 * @return string
484 */
485 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
486
487 $value = $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? self::$sources[ $ticket->{$cf->slug} ] : '';
488 return apply_filters( 'wpsc_ticket_field_val_source', $value, $cf, $ticket, $module );
489 }
490
491 /**
492 * Print ticket value for given custom field on ticket list
493 *
494 * @param WPSC_Custom_Field $cf - custom field object.
495 * @param WPSC_Ticket $ticket - ticket object.
496 * @return void
497 */
498 public static function print_tl_ticket_field_val( $cf, $ticket ) {
499
500 echo $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? esc_attr( self::$sources[ $ticket->{$cf->slug} ] ) : '';
501 }
502
503 /**
504 * Print ticket value for given custom field on widget
505 *
506 * @param WPSC_Custom_Field $cf - custom field object.
507 * @param WPSC_Ticket $ticket - ticket object.
508 * @return void
509 */
510 public static function print_widget_ticket_field_val( $cf, $ticket ) {
511
512 echo $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? esc_attr( self::$sources[ $ticket->{$cf->slug} ] ) : esc_attr__( 'Not Applicable', 'supportcandy' );
513 }
514 }
515 endif;
516
517 WPSC_DF_Source::init();
518