PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
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-os.php

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

544 lines 14.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_OS' ) ) :
7
8 final class WPSC_DF_OS {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_os';
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 * 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
171 /**
172 * Print operators for ticket form filter
173 *
174 * @param WPSC_Custom_Field $cf - custom field object.
175 * @param array $filter - Existing filters (if any).
176 * @return void
177 */
178 public static function get_operators( $cf, $filter = array() ) {
179 ?>
180
181 <div class="item conditional">
182 <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' ) ); ?>');">
183 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
184 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
185 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
186 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
187 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php esc_attr_e( 'Has Words', 'supportcandy' ); ?></option>
188 </select>
189 </div>
190 <?php
191 }
192
193 /**
194 * Print operators for ticket form filter
195 *
196 * @param string $operator - condition operator on which operands should be returned.
197 * @param WPSC_Custom_Field $cf - custom field object.
198 * @param array $filter - Exising functions (if any).
199 * @return void
200 */
201 public static function get_operands( $operator, $cf, $filter = array() ) {
202
203 $value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : '';
204 if ( in_array( $operator, array( 'IN', 'NOT IN', 'LIKE' ) ) ) {
205 ?>
206
207 <div class="item conditional operand single">
208 <textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea>
209 </div>
210 <?php
211
212 } else {
213 ?>
214
215 <div class="item conditional operand single">
216 <input
217 type="text"
218 class="operand_val_1"
219 value="<?php echo esc_attr( $value ); ?>"
220 autocomplete="off"/>
221 </div>
222 <?php
223 }
224 }
225
226 /**
227 * Parse filter and return sql query to be merged in ticket model query builder
228 *
229 * @param WPSC_Custom_Field $cf - custom field of this type.
230 * @param mixed $compare - comparison operator.
231 * @param mixed $val - value to compare.
232 * @return string
233 */
234 public static function parse_filter( $cf, $compare, $val ) {
235
236 $str = '';
237
238 switch ( $compare ) {
239
240 case '=':
241 $str = self::get_sql_slug( $cf ) . '=\'' . esc_sql( $val ) . '\'';
242 break;
243
244 case 'IN':
245 $str = 'CONVERT(t.' . $cf->slug . ' USING utf8) IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
246 break;
247
248 case 'NOT IN':
249 $str = 'CONVERT(t.' . $cf->slug . ' USING utf8) NOT IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
250 break;
251
252 case 'LIKE':
253 $arr = array();
254 $val = explode( PHP_EOL, $val );
255 foreach ( $val as $term ) {
256 $term = str_replace( '*', '%', trim( $term ) );
257 $arr[] = 'CONVERT(t.' . $cf->slug . ' USING utf8) LIKE \'%' . esc_sql( $term ) . '%\'';
258 }
259 $str = '(' . implode( ' OR ', $arr ) . ')';
260 break;
261
262 default:
263 $str = '1=1';
264 }
265
266 return $str;
267 }
268
269 /**
270 * Check ticket condition
271 *
272 * @param array $condition - array with condition data.
273 * @param WPSC_Custom_Field $cf - custom field object.
274 * @param WPSC_Ticket $ticket - ticket object.
275 * @return boolean
276 */
277 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
278
279 $flag = true;
280 $value = stripslashes( $ticket->{$cf->slug} );
281 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
282
283 switch ( $condition['operator'] ) {
284
285 case '=':
286 $flag = $condition['operand_val_1'] == $value ? true : false;
287 break;
288
289 case 'IN':
290 $flag = false;
291 foreach ( $terms as $term ) {
292 $term = trim( $term );
293 if ( $term == $value ) {
294 $flag = true;
295 break;
296 }
297 }
298 break;
299
300 case 'NOT IN':
301 foreach ( $terms as $term ) {
302 $term = trim( $term );
303 if ( $term == $value ) {
304 $flag = false;
305 break;
306 }
307 }
308 break;
309
310 case 'LIKE':
311 $flag = false;
312 foreach ( $terms as $term ) {
313 $index = strpos( $value, trim( stripslashes( $term ) ) );
314 if ( is_numeric( $index ) ) {
315 $flag = true;
316 break;
317 }
318 }
319 break;
320
321 default:
322 $flag = true;
323 }
324
325 return $flag;
326 }
327
328 /**
329 * Load current class to reference classes
330 *
331 * @param array $classes - Associative array of class names indexed by its slug.
332 * @return array
333 */
334 public static function load_ref_class( $classes ) {
335
336 $classes[ self::$slug ] = array(
337 'class' => __CLASS__,
338 'save-key' => 'id',
339 );
340 return $classes;
341 }
342
343 /**
344 * Return default value for custom field of this type
345 *
346 * @param WPSC_Custom_Field $cf - custom field object.
347 * @return mixed
348 */
349 public static function get_default_value( $cf ) {
350
351 return '';
352 }
353
354 /**
355 * Check and return custom field value for new ticket to be created.
356 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
357 * Ignore phpcs nonce issue as we already checked where it is called from.
358 *
359 * @param array $data - Array of values to to stored in ticket in an insert function.
360 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
361 * @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.
362 * @return array
363 */
364 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
365
366 $data['os'] = self::get_user_platform();
367 return $data;
368 }
369
370 /**
371 * Get operating system from user agent
372 *
373 * @return String
374 */
375 public static function get_user_platform() {
376
377 $os_platform = '';
378
379 $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
380
381 $os_array = array(
382 '/windows nt 10/i' => 'Windows 10',
383 '/windows nt 6.3/i' => 'Windows 8.1',
384 '/windows nt 6.2/i' => 'Windows 8',
385 '/windows nt 6.1/i' => 'Windows 7',
386 '/windows nt 6.0/i' => 'Windows Vista',
387 '/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
388 '/windows nt 5.1/i' => 'Windows XP',
389 '/windows xp/i' => 'Windows XP',
390 '/windows nt 5.0/i' => 'Windows 2000',
391 '/windows me/i' => 'Windows ME',
392 '/win98/i' => 'Windows 98',
393 '/win95/i' => 'Windows 95',
394 '/win16/i' => 'Windows 3.11',
395 '/macintosh|mac os x/i' => 'Mac OS X',
396 '/mac_powerpc/i' => 'Mac OS 9',
397 '/linux/i' => 'Linux',
398 '/ubuntu/i' => 'Ubuntu',
399 '/iphone/i' => 'iPhone',
400 '/ipod/i' => 'iPod',
401 '/ipad/i' => 'iPad',
402 '/android/i' => 'Android',
403 '/blackberry/i' => 'BlackBerry',
404 '/webos/i' => 'Mobile',
405 );
406
407 foreach ( $os_array as $regex => $value ) {
408 if ( preg_match( $regex, $user_agent ) ) {
409 $os_platform = $value;
410 break;
411 }
412 }
413
414 return $os_platform;
415 }
416
417 /**
418 * Return slug string to be used in where condition of ticket model for this type of field
419 *
420 * @param WPSC_Custom_Field $cf - custom field object.
421 * @return string
422 */
423 public static function get_sql_slug( $cf ) {
424
425 return 't.' . $cf->slug;
426 }
427
428 /**
429 * Return val field for meta query of this type of custom field
430 *
431 * @param array $condition - condition data.
432 * @return mixed
433 */
434 public static function get_meta_value( $condition ) {
435
436 $operator = $condition['operator'];
437 switch ( $operator ) {
438
439 case '=':
440 return $condition['operand_val_1'];
441
442 case 'IN':
443 case 'NOT IN':
444 $val = explode( PHP_EOL, $condition['operand_val_1'] );
445 $temp_val = array();
446 foreach ( $val as $value ) {
447 $value = trim( $value );
448 if ( $value ) {
449 $temp_val[] = $value;
450 }
451 }
452 return $temp_val ? $temp_val : false;
453
454 case 'LIKE':
455 return trim( $condition['operand_val_1'] );
456 }
457 return false;
458 }
459
460 /**
461 * Print edit custom field properties
462 *
463 * @param WPSC_Custom_Fields $cf - custom field object.
464 * @param string $field_class - class name of field category.
465 * @return void
466 */
467 public static function get_edit_custom_field_properties( $cf, $field_class ) {
468
469 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
470 ?>
471 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
472 <div class="label-container">
473 <label for="">
474 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
475 </label>
476 </div>
477 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
478 </div>
479 <?php
480 endif;
481 }
482
483 /**
484 * Set custom field properties. Can be used by add/edit custom field.
485 * Ignore phpcs nonce issue as we already checked where it is called from.
486 *
487 * @param WPSC_Custom_Field $cf - custom field object.
488 * @param string $field_class - class of field category.
489 * @return void
490 */
491 public static function set_cf_properties( $cf, $field_class ) {
492
493 // tl_width!
494 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
495 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
496 $cf->tl_width = $tl_width ? $tl_width : 100;
497 }
498
499 // save!
500 $cf->save();
501 }
502
503 /**
504 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
505 *
506 * @param WPSC_Custom_Field $cf - custom field object.
507 * @param WPSC_Ticket $ticket - ticket object.
508 * @param string $module - module name.
509 * @return string
510 */
511 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
512
513 return $ticket->{$cf->slug};
514 }
515
516 /**
517 * Print ticket value for given custom field on ticket list
518 *
519 * @param WPSC_Custom_Field $cf - custom field object.
520 * @param WPSC_Ticket $ticket - ticket object.
521 * @return void
522 */
523 public static function print_tl_ticket_field_val( $cf, $ticket ) {
524
525 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
526 }
527
528 /**
529 * Print ticket value for given custom field on widget
530 *
531 * @param WPSC_Custom_Field $cf - custom field object.
532 * @param WPSC_Ticket $ticket - ticket object.
533 * @return void
534 */
535 public static function print_widget_ticket_field_val( $cf, $ticket ) {
536
537 $os = $ticket->{$cf->slug};
538 echo $os ? esc_attr( $os ) : esc_attr__( 'Not Applicable', 'supportcandy' );
539 }
540 }
541 endif;
542
543 WPSC_DF_OS::init();
544