PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.7
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.3.7, at includes/custom-field-types/class-wpsc-df-id.php

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