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

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

550 lines 14.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_Browser' ) ) :
7
8 final class WPSC_DF_Browser {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_browser';
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 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 = 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 * 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 // TFF!
168 add_action( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
169
170 // Set custom field type.
171 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
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 * Add default custom field type to list
191 *
192 * @param array $default_cf_types - default custom field types array.
193 * @return array
194 */
195 public static function default_cf_types( $default_cf_types ) {
196 $default_cf_types[ self::$slug ] = array(
197 'label' => esc_attr__( 'Browser', 'supportcandy' ),
198 'class' => __CLASS__,
199 );
200 return $default_cf_types;
201 }
202
203 /**
204 * Print operators for ticket form filter
205 *
206 * @param WPSC_Custom_Field $cf - custom field object.
207 * @param array $filter - Existing filters (if any).
208 * @return void
209 */
210 public static function get_operators( $cf, $filter = array() ) {
211 ?>
212
213 <div class="item conditional">
214 <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' ) ); ?>');">
215 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
216 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
217 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php esc_attr_e( 'Has Words', 'supportcandy' ); ?></option>
218 </select>
219 </div>
220 <?php
221 }
222
223 /**
224 * Print operators for ticket form filter
225 *
226 * @param string $operator - condition operator on which operands should be returned.
227 * @param WPSC_Custom_Field $cf - custom field object.
228 * @param array $filter - Exising functions (if any).
229 * @return void
230 */
231 public static function get_operands( $operator, $cf, $filter = array() ) {
232
233 $value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : '';
234 if ( $operator == 'LIKE' ) {
235 ?>
236
237 <div class="item conditional operand single">
238 <textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea>
239 </div>
240 <?php
241
242 } else {
243 ?>
244
245 <div class="item conditional operand single">
246 <input
247 type="text"
248 class="operand_val_1"
249 value="<?php echo esc_attr( $value ); ?>"
250 autocomplete="off"/>
251 </div>
252 <?php
253 }
254 }
255
256 /**
257 * Return allowed browsers
258 *
259 * @return array
260 */
261 private static function allowed_browsers() {
262 return array(
263 'navigator',
264 'firefox',
265 'internet explorer',
266 'google chrome',
267 'maxthon',
268 'opera',
269 'edge',
270 'konqueror',
271 'samsungbrowser',
272 'safari',
273 );
274 }
275
276 /**
277 * Parse filter and return sql query to be merged in ticket model query builder
278 *
279 * @param WPSC_Custom_Field $cf - custom field of this type.
280 * @param mixed $compare - comparison operator.
281 * @param mixed $val - value to compare.
282 * @return string
283 */
284 public static function parse_filter( $cf, $compare, $val ) {
285
286 // Safe column name.
287 $column = 't.' . sanitize_key( $cf->slug );
288 $allowed = self::allowed_browsers();
289
290 /**
291 * Normalize & validate browser values
292 */
293 $normalize = static function ( $values ) use ( $allowed ) {
294
295 $values = is_array( $values ) ? $values : array( $values );
296 $out = array();
297
298 foreach ( $values as $v ) {
299 $v = strtolower( trim( $v ) );
300 if ( in_array( $v, $allowed, true ) ) {
301 $out[] = $v;
302 }
303 }
304
305 return array_unique( $out );
306 };
307
308 switch ( $compare ) {
309
310 case '=':
311 $vals = $normalize( $val );
312 if ( empty( $vals ) ) {
313 return '1=0';
314 }
315
316 return $column . " = '" . esc_sql( $vals[0] ) . "'";
317
318 case 'IN':
319 $vals = $normalize( $val );
320 if ( empty( $vals ) ) {
321 return '1=0';
322 }
323
324 $escaped = array_map( 'esc_sql', $vals );
325 return "CONVERT({$column} USING utf8) IN ('" . implode( "','", $escaped ) . "')";
326
327 default:
328 return '1=1';
329 }
330 }
331
332 /**
333 * Check ticket condition
334 *
335 * @param array $condition - array with condition data.
336 * @param WPSC_Custom_Field $cf - custom field object.
337 * @param WPSC_Ticket $ticket - ticket object.
338 * @return boolean
339 */
340 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
341
342 $flag = true;
343
344 switch ( $condition['operator'] ) {
345
346 case '=':
347 $flag = $ticket->{$cf->slug} == $condition['operand_val_1'] ? true : false;
348 break;
349
350 case 'IN':
351 $flag = in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] );
352 break;
353
354 default:
355 $flag = true;
356 }
357
358 return $flag;
359 }
360
361 /**
362 * Return default value for custom field of this type
363 *
364 * @param WPSC_Custom_Field $cf - custom field object.
365 * @return mixed
366 */
367 public static function get_default_value( $cf ) {
368
369 return '';
370 }
371
372 /**
373 * Check and return custom field value for new ticket to be created.
374 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
375 * Ignore phpcs nonce issue as we already checked where it is called from.
376 *
377 * @param array $data - Array of values to to stored in ticket in an insert function.
378 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
379 * @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.
380 * @return array
381 */
382 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
383
384 $data['browser'] = self::get_user_browser();
385 return $data;
386 }
387
388 /**
389 * Get browser from user agent
390 *
391 * @return String
392 */
393 public static function get_user_browser() {
394
395 $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
396
397 $browsers = array(
398 array(
399 'regex' => '/Navigator(.*)/i',
400 'name' => 'Navigator',
401 ),
402 array(
403 'regex' => '/Firefox(.*)/i',
404 'name' => 'Firefox',
405 ),
406 array(
407 'regex' => '/MSIE(.*)/i',
408 'name' => 'Internet Explorer',
409 ),
410 array(
411 'regex' => '/chrome(.*)/i',
412 'name' => 'Google Chrome',
413 ),
414 array(
415 'regex' => '/MAXTHON(.*)/i',
416 'name' => 'MAXTHON',
417 ),
418 array(
419 'regex' => '/Opera(.*)/i',
420 'name' => 'Opera',
421 ),
422 array(
423 'regex' => '/Edg(.*)/i',
424 'name' => 'Edge',
425 ),
426 array(
427 'regex' => '/Konqueror(.*)/i',
428 'name' => 'Konqueror',
429 ),
430 array(
431 'regex' => '/SamsungBrowser(.*)/i',
432 'name' => 'SamsungBrowser',
433 ),
434 array(
435 'regex' => '/Safari(.*)/i',
436 'name' => 'Safari',
437 ),
438 );
439
440 foreach ( $browsers as $browser ) {
441 if ( preg_match( $browser['regex'], $user_agent ) ) {
442 return $browser['name'];
443 }
444 }
445
446 return '';
447 }
448
449 /**
450 * Return val field for meta query of this type of custom field
451 *
452 * @param array $condition - condition data.
453 * @return mixed
454 */
455 public static function get_meta_value( $condition ) {
456
457 $operator = $condition['operator'];
458 switch ( $operator ) {
459 case '=':
460 case 'LIKE':
461 return $condition['operand_val_1'];
462 }
463 return false;
464 }
465
466 /**
467 * Print edit custom field properties
468 *
469 * @param WPSC_Custom_Fields $cf - custom field object.
470 * @param string $field_class - class name of field category.
471 * @return void
472 */
473 public static function get_edit_custom_field_properties( $cf, $field_class ) {
474
475 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
476 ?>
477 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
478 <div class="label-container">
479 <label for="">
480 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
481 </label>
482 </div>
483 <input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off">
484 </div>
485 <?php
486 endif;
487 }
488
489 /**
490 * Set custom field properties. Can be used by add/edit custom field.
491 * Ignore phpcs nonce issue as we already checked where it is called from.
492 *
493 * @param WPSC_Custom_Field $cf - custom field object.
494 * @param string $field_class - class of field category.
495 * @return void
496 */
497 public static function set_cf_properties( $cf, $field_class ) {
498
499 // tl_width!
500 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
501 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
502 $cf->tl_width = $tl_width ? $tl_width : 100;
503 }
504
505 // save!
506 $cf->save();
507 }
508
509 /**
510 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
511 *
512 * @param WPSC_Custom_Field $cf - custom field object.
513 * @param WPSC_Ticket $ticket - ticket object.
514 * @param string $module - module name.
515 * @return string
516 */
517 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
518
519 return apply_filters( 'wpsc_ticket_field_val_browser', $ticket->{$cf->slug}, $cf, $ticket, $module );
520 }
521
522 /**
523 * Print ticket value for given custom field on ticket list
524 *
525 * @param WPSC_Custom_Field $cf - custom field object.
526 * @param WPSC_Ticket $ticket - ticket object.
527 * @return void
528 */
529 public static function print_tl_ticket_field_val( $cf, $ticket ) {
530
531 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
532 }
533
534 /**
535 * Print ticket value for given custom field on widget
536 *
537 * @param WPSC_Custom_Field $cf - custom field object.
538 * @param WPSC_Ticket $ticket - ticket object.
539 * @return void
540 */
541 public static function print_widget_ticket_field_val( $cf, $ticket ) {
542
543 $browser = $ticket->{$cf->slug};
544 echo $browser ? esc_attr( $browser ) : esc_attr__( 'Not Applicable', 'supportcandy' );
545 }
546 }
547 endif;
548
549 WPSC_DF_Browser::init();
550