PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.8
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.8
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-user-type.php

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

459 lines 13.0 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_User_Type' ) ) :
7
8 final class WPSC_DF_User_Type {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_user_type';
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(100) 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 = 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 // create ticket data for rest api.
171 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
172
173 // WP user chagnes (add/delete).
174 add_action( 'user_register', array( __CLASS__, 'wp_user_register' ), 11, 2 );
175 add_action( 'delete_user', array( __CLASS__, 'wp_user_delete' ), 10, 3 );
176 }
177
178 /**
179 * Load current class to reference classes
180 *
181 * @param array $classes - Associative array of class names indexed by its slug.
182 * @return array
183 */
184 public static function load_ref_class( $classes ) {
185
186 $classes[ self::$slug ] = array(
187 'class' => __CLASS__,
188 'save-key' => 'id',
189 );
190 return $classes;
191 }
192
193 /**
194 * Print operators for ticket form filter
195 *
196 * @param WPSC_Custom_Field $cf - custom field object.
197 * @param array $filter - Existing filters (if any).
198 * @return void
199 */
200 public static function get_operators( $cf, $filter = array() ) {
201 ?>
202
203 <div class="item conditional">
204 <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' ) ); ?>');">
205 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
206 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
207 </select>
208 </div>
209 <?php
210 }
211
212 /**
213 * Print operators for ticket form filter
214 *
215 * @param string $operator - condition operator on which operands should be returned.
216 * @param WPSC_Custom_Field $cf - custom field object.
217 * @param array $filter - Exising functions (if any).
218 * @return void
219 */
220 public static function get_operands( $operator, $cf, $filter = array() ) {
221
222 $unique_id = uniqid( 'wpsc_' );
223 ?>
224 <div class="item conditional operand single">
225 <select
226 class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>">
227 <option <?php isset( $filter['operand_val_1'] ) && selected( $filter['operand_val_1'], 'registered' ); ?> value="registered"><?php esc_attr_e( 'Registered', 'supportcandy' ); ?></option>
228 <option <?php isset( $filter['operand_val_1'] ) && selected( $filter['operand_val_1'], 'guest' ); ?> value="guest"><?php esc_attr_e( 'Guest', 'supportcandy' ); ?></option>
229 </select>
230 </div>
231 <?php
232 }
233
234 /**
235 * Parse filter and return sql query to be merged in ticket model query builder
236 *
237 * @param WPSC_Custom_Field $cf - custom field of this type.
238 * @param mixed $compare - comparison operator.
239 * @param mixed $val - value to compare.
240 * @return string
241 */
242 public static function parse_filter( $cf, $compare, $val ) {
243
244 $str = '';
245
246 switch ( $compare ) {
247
248 case '=':
249 $str = 't.' . $cf->slug . '=\'' . esc_sql( $val ) . '\'';
250 break;
251
252 default:
253 $str = '1=1';
254 }
255
256 return $str;
257 }
258
259 /**
260 * Check ticket condition
261 *
262 * @param array $condition - array with condition data.
263 * @param WPSC_Custom_Field $cf - custom field object.
264 * @param WPSC_Ticket $ticket - ticket object.
265 * @return boolean
266 */
267 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
268
269 $flag = true;
270
271 switch ( $condition['operator'] ) {
272
273 case '=':
274 $flag = $condition['operand_val_1'] == $ticket->{$cf->slug} ? true : false;
275 break;
276
277 default:
278 $flag = true;
279 }
280
281 return $flag;
282 }
283
284 /**
285 * Check and return custom field value for new ticket to be created.
286 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
287 * Ignore phpcs nonce issue as we already checked where it is called from.
288 *
289 * @param array $data - Array of values to to stored in ticket in an insert function.
290 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
291 * @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.
292 * @return array
293 */
294 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
295
296 $customer = new WPSC_Customer( $data['customer'] );
297 $data['user_type'] = $customer->user ? 'registered' : 'guest';
298 return $data;
299 }
300
301 /**
302 * Set create ticket data for rest api request
303 *
304 * @param array $data - create ticket data array.
305 * @param WP_REST_Request $request - rest request object.
306 * @param array $custom_fields - custom field objects indexed by unique custom field types.
307 * @return array
308 */
309 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
310
311 $customer = new WPSC_Customer( $data['customer'] );
312 $data['user_type'] = $customer->user ? 'registered' : 'guest';
313 return $data;
314 }
315
316 /**
317 * Return val field for meta query of this type of custom field
318 *
319 * @param array $condition - condition data.
320 * @return mixed
321 */
322 public static function get_meta_value( $condition ) {
323
324 return $condition['operand_val_1'];
325 }
326
327 /**
328 * Change user type from "guest" to "registered" for all his exising guest tickets (if any)
329 *
330 * @param int $user_id - user id of user being registered.
331 * @param array $user_data - user data such as name, email, etc.
332 * @return void
333 */
334 public static function wp_user_register( $user_id, $user_data ) {
335
336 global $wpdb;
337
338 $customer = WPSC_Customer::get_by_user_id( $user_id );
339
340 // update customer_type in all of his tickets.
341 $success = $wpdb->update(
342 $wpdb->prefix . 'psmsc_tickets',
343 array( 'user_type' => 'registered' ),
344 array( 'customer' => $customer->id )
345 );
346 }
347
348 /**
349 * Change user type from "registered" to "guest" for all his exising guest tickets (if any)
350 *
351 * @param int $user_id - user id of user being deleted.
352 * @param int $reassign - user id of user this user being reassigned to.
353 * @param WP_User $user - wp user object.
354 * @return void
355 */
356 public static function wp_user_delete( $user_id, $reassign, $user ) {
357
358 global $wpdb;
359
360 $customer = WPSC_Customer::get_by_email( $user->user_email );
361 if ( $customer->id ) {
362
363 $customer->user = 0;
364 $customer->save();
365
366 // update customer_type in all of his tickets.
367 $success = $wpdb->update(
368 $wpdb->prefix . 'psmsc_tickets',
369 array( 'user_type' => 'guest' ),
370 array( 'customer' => $customer->id )
371 );
372 }
373 }
374
375 /**
376 * Return data for this custom field while creating duplicate ticket
377 *
378 * @param WPSC_Custom_Field $cf - custom field object.
379 * @param WPSC_Ticket $ticket - ticket object.
380 * @return mixed
381 */
382 public static function get_duplicate_ticket_data( $cf, $ticket ) {
383
384 return $ticket->user_type;
385 }
386
387 /**
388 * Print edit custom field properties
389 *
390 * @param WPSC_Custom_Fields $cf - custom field object.
391 * @param string $field_class - class name of field category.
392 * @return void
393 */
394 public static function get_edit_custom_field_properties( $cf, $field_class ) {
395
396 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
397 ?>
398 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
399 <div class="label-container">
400 <label for="">
401 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
402 </label>
403 </div>
404 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
405 </div>
406 <?php
407 endif;
408 }
409
410 /**
411 * Set custom field properties. Can be used by add/edit custom field.
412 * Ignore phpcs nonce issue as we already checked where it is called from.
413 *
414 * @param WPSC_Custom_Field $cf - custom field object.
415 * @param string $field_class - class of field category.
416 * @return void
417 */
418 public static function set_cf_properties( $cf, $field_class ) {
419
420 // tl_width!
421 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
422 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
423 $cf->tl_width = $tl_width ? $tl_width : 100;
424 }
425
426 // save!
427 $cf->save();
428 }
429
430 /**
431 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
432 *
433 * @param WPSC_Custom_Field $cf - custom field object.
434 * @param WPSC_Ticket $ticket - ticket object.
435 * @param string $module - module name.
436 * @return string
437 */
438 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
439
440 $value = $ticket->{$cf->slug} == 'registered' ? esc_attr__( 'Registered', 'supportcandy' ) : esc_attr__( 'Guest', 'supportcandy' );
441 return apply_filters( 'wpsc_ticket_field_val_user_type', $value, $cf, $ticket, $module );
442 }
443
444 /**
445 * Print ticket value for given custom field on ticket list
446 *
447 * @param WPSC_Custom_Field $cf - custom field object.
448 * @param WPSC_Ticket $ticket - ticket object.
449 * @return void
450 */
451 public static function print_tl_ticket_field_val( $cf, $ticket ) {
452
453 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
454 }
455 }
456 endif;
457
458 WPSC_DF_User_Type::init();
459