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

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

676 lines 19.4 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_Customer' ) ) :
7
8 final class WPSC_DF_Customer {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_customer';
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 field type is system default (no fields can be created from it).
33 *
34 * @var boolean
35 */
36 public static $is_default = true;
37
38 /**
39 * Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc.
40 *
41 * @var boolean
42 */
43 public static $has_extra_info = true;
44
45 /**
46 * Set whether this custom field type can accept personal info.
47 *
48 * @var boolean
49 */
50 public static $has_personal_info = false;
51
52 /**
53 * Set whether this custom field type has multiple values
54 *
55 * @var boolean
56 */
57 public static $has_multiple_val = false;
58
59 /**
60 * Set whether this custom field type has reference to other class
61 *
62 * @var boolean
63 */
64 public static $has_ref = true;
65
66 /**
67 * 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.
68 *
69 * @var string
70 */
71 public static $ref_class = 'wpsc_customer';
72
73 /**
74 * Data type for column created in customer table
75 *
76 * @var string
77 */
78 public static $data_type = 'BIGINT NOT NULL';
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 = false;
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 = 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 * Set ignore customer info custom field types.
159 *
160 * @var array
161 */
162 public static $ignore_customer_info_cft = array();
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 // create ticket data from ticket form.
175 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 6, 3 );
176
177 // create ticket data for rest api.
178 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 1, 3 );
179
180 // Ticket model.
181 add_filter( 'wpsc_ticket_joins', array( __CLASS__, 'ticket_join' ), 8, 2 );
182 add_filter( 'wpsc_archive_ticket_joins', array( __CLASS__, 'ticket_join' ), 8, 2 );
183
184 // ticket search query.
185 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
186 add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
187
188 // Ignore customer info cft.
189 add_action( 'init', array( __CLASS__, 'ignore_customer_info_cft' ) );
190
191 // Customer filter autocomplete.
192 add_action( 'wp_ajax_wpsc_customer_filter_autocomplete', array( __CLASS__, 'customer_filter_autocomplete' ) );
193 }
194
195 /**
196 * Load current class to reference classes
197 *
198 * @param array $classes - Associative array of class names indexed by its slug.
199 * @return array
200 */
201 public static function load_ref_class( $classes ) {
202
203 $classes[ self::$slug ] = array(
204 'class' => __CLASS__,
205 'save-key' => 'id',
206 );
207 return $classes;
208 }
209
210 /**
211 * Print operators for ticket form filter
212 *
213 * @param WPSC_Custom_Field $cf - custom field object.
214 * @param array $filter - Existing filters (if any).
215 * @return void
216 */
217 public static function get_operators( $cf, $filter = array() ) {
218 ?>
219
220 <div class="item conditional">
221 <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' ) ); ?>');">
222 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
223 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
224 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
225 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
226 </select>
227 </div>
228 <?php
229 }
230
231 /**
232 * Print operators for ticket form filter
233 *
234 * @param string $operator - condition operator on which operands should be returned.
235 * @param WPSC_Custom_Field $cf - custom field object.
236 * @param array $filter - Exising functions (if any).
237 * @return void
238 */
239 public static function get_operands( $operator, $cf, $filter = array() ) {
240
241 $is_multiple = $operator !== '=' ? true : false;
242 $unique_id = uniqid( 'wpsc_' );
243 ?>
244 <div class="item conditional operand single">
245 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
246 <?php
247
248 if ( $is_multiple && isset( $filter['operand_val_1'] ) ) {
249
250 foreach ( $filter['operand_val_1'] as $customer_id ) {
251 $customer = new WPSC_Customer( intval( $customer_id ) )
252 ?>
253 <option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option>
254 <?php
255 }
256 }
257
258 if ( ! $is_multiple && isset( $filter['operand_val_1'] ) ) {
259
260 $customer = new WPSC_Customer( intval( $filter['operand_val_1'] ) )
261 ?>
262 <option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option>
263 <?php
264 }
265 ?>
266
267 </select>
268 </div>
269 <script>
270 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
271 ajax: {
272 url: supportcandy.ajax_url,
273 dataType: 'json',
274 delay: 250,
275 data: function (params) {
276 return {
277 q: params.term, // search term
278 page: params.page,
279 action: 'wpsc_customer_filter_autocomplete',
280 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_customer_filter_autocomplete' ) ); ?>'
281 };
282 },
283 processResults: function (data, params) {
284 var terms = [];
285 if ( data ) {
286 jQuery.each( data, function( id, text ) {
287 terms.push( { id: text.id, text: text.title } );
288 });
289 }
290 return {
291 results: terms
292 };
293 },
294 cache: true
295 },
296 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
297 minimumInputLength: 1
298 });
299 </script>
300 <?php
301 }
302
303 /**
304 * Parse filter and return sql query to be merged in ticket model query builder
305 *
306 * @param WPSC_Custom_Field $cf - custom field of this type.
307 * @param mixed $compare - comparison operator.
308 * @param mixed $val - value to compare.
309 * @return string
310 */
311 public static function parse_filter( $cf, $compare, $val ) {
312
313 $str = '';
314
315 switch ( $compare ) {
316
317 case '=':
318 $str = 't.' . $cf->slug . '=\'' . esc_sql( $val ) . '\'';
319 break;
320
321 case 'IN':
322 $str = 't.' . $cf->slug . ' IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
323 break;
324
325 case 'NOT IN':
326 $str = 't.' . $cf->slug . ' NOT IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
327 break;
328
329 default:
330 $str = '1=1';
331 }
332
333 return $str;
334 }
335
336 /**
337 * Check ticket condition
338 *
339 * @param array $condition - array with condition data.
340 * @param WPSC_Custom_Field $cf - custom field object.
341 * @param WPSC_Ticket $ticket - ticket object.
342 * @return boolean
343 */
344 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
345
346 if ( ! is_object( $ticket->{$cf->slug} ) ) {
347 return false;
348 }
349
350 $flag = true;
351
352 switch ( $condition['operator'] ) {
353
354 case '=':
355 $flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false;
356 break;
357
358 case 'IN':
359 $flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] );
360 break;
361
362 case 'NOT IN':
363 $flag = ! in_array( $ticket->{$cf->slug}->id, $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 $current_user = WPSC_Current_User::$current_user;
386 $name = WPSC_DF_Customer_Name::get_tff_value( 'name' );
387 $email = WPSC_DF_Customer_Email::get_tff_value( 'email' );
388
389 $customer = self::get_customer_record( $name, $email );
390 if ( ! $customer->id ) {
391 wp_send_json_error( new WP_Error( '001', 'something went wrong!' ), 500 );
392 }
393
394 $data['customer'] = $customer->id;
395 return $data;
396 }
397
398 /**
399 * Set create ticket data for rest api request
400 *
401 * @param array $data - create ticket data array.
402 * @param WP_REST_Request $request - rest request object.
403 * @param array $custom_fields - custom field objects indexed by unique custom field types.
404 * @return array
405 */
406 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
407
408 $name = $request->get_param( 'name' );
409 $email = $request->get_param( 'email' );
410
411 // check name and email are present.
412 if ( ! $name || ! $email ) {
413 $data['errors']->add( 'req_fields_missing', 'name or email is missing!', 'name, email' );
414 return $data;
415 }
416
417 // validate email address.
418 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
419 $data['errors']->add( 'invalid_data', 'invalid email address!', 'email' );
420 return $data;
421 }
422
423 // get customer object.
424 $customer = self::get_customer_record( $name, $email );
425 if ( ! $customer->id ) {
426 $data['errors']->add( 'unknown', 'Something went wrong!' );
427 return $data;
428 }
429
430 $data['customer'] = $customer->id;
431 return $data;
432 }
433
434 /**
435 * Add SQL joins to ticket model for this field type
436 *
437 * @param array $joins - array of join string that can be imploded later.
438 * @param array $filter - user filter.
439 * @return array
440 */
441 public static function ticket_join( $joins, $filter ) {
442
443 global $wpdb;
444 $joins[] = 'LEFT JOIN ' . $wpdb->prefix . 'psmsc_customers c ON t.customer = c.id';
445 return $joins;
446 }
447
448 /**
449 * Add ticket search compatibility for fields of this custom field type.
450 *
451 * @param array $sql - Array of sql peices that can be joined later.
452 * @param array $filter - User filter.
453 * @param array $custom_fields - Custom fields array applicable for search.
454 * @param string $search - search string.
455 * @param array $allowed_search_fields - Allowed search fields.
456 * @return array
457 */
458 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
459
460 if ( in_array( 'customer', $allowed_search_fields ) ) {
461 $sql[] = 'CONVERT(c.name USING utf8) LIKE \'%' . $search . '%\'';
462 $sql[] = 'CONVERT(c.email USING utf8) LIKE \'%' . $search . '%\'';
463 }
464 return $sql;
465 }
466
467 /**
468 * Return val field for meta query of this type of custom field
469 *
470 * @param array $condition - condition data.
471 * @return mixed
472 */
473 public static function get_meta_value( $condition ) {
474
475 $operator = $condition['operator'];
476 switch ( $operator ) {
477
478 case '=':
479 case 'IN':
480 case 'NOT IN':
481 return $condition['operand_val_1'];
482 }
483 return false;
484 }
485
486 /**
487 * Return data for this custom field while creating duplicate ticket
488 *
489 * @param WPSC_Custom_Field $cf - custom field object.
490 * @param WPSC_Ticket $ticket - ticket object.
491 * @return mixed
492 */
493 public static function get_duplicate_ticket_data( $cf, $ticket ) {
494
495 return $ticket->customer->id;
496 }
497
498 /**
499 * Print edit custom field properties
500 *
501 * @param WPSC_Custom_Fields $cf - custom field object.
502 * @param string $field_class - class name of field category.
503 * @return void
504 */
505 public static function get_edit_custom_field_properties( $cf, $field_class ) {
506
507 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
508 ?>
509 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
510 <div class="label-container">
511 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
512 </div>
513 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
514 </div>
515 <?php
516 endif;
517 }
518
519 /**
520 * Set custom field properties. Can be used by add/edit custom field.
521 * Ignore phpcs nonce issue as we already checked where it is called from.
522 *
523 * @param WPSC_Custom_Field $cf - custom field object.
524 * @param string $field_class - class of field category.
525 * @return void
526 */
527 public static function set_cf_properties( $cf, $field_class ) {
528
529 // extra info.
530 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
531 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
532 }
533
534 // save!
535 $cf->save();
536 }
537
538 /**
539 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
540 *
541 * @param WPSC_Custom_Field $cf - custom field object.
542 * @param WPSC_Ticket $ticket - ticket object.
543 * @param string $module - module name.
544 * @return string
545 */
546 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
547
548 return apply_filters( 'wpsc_ticket_field_val_customer', $ticket->customer->name, $cf, $ticket, $module );
549 }
550
551 /**
552 * Print given value for custom field
553 *
554 * @param WPSC_Custom_Field $cf - custom field object.
555 * @param mixed $val - value to convert and print.
556 * @return void
557 */
558 public static function print_val( $cf, $val ) {
559
560 $customer = is_object( $val ) ? $val : new WPSC_Customer( $val );
561 echo $customer->id ? esc_attr( $customer->name ) : esc_attr__( 'None', 'supportcandy' );
562 }
563
564 /**
565 * Return printable value for history log macro
566 *
567 * @param WPSC_Custom_Field $cf - custom field object.
568 * @param mixed $val - value to convert and return.
569 * @return string
570 */
571 public static function get_history_log_val( $cf, $val ) {
572
573 ob_start();
574 self::print_val( $cf, $val );
575 return ob_get_clean();
576 }
577
578 /**
579 * Set ignore customer info custom field type.
580 *
581 * @return void
582 */
583 public static function ignore_customer_info_cft() {
584
585 self::$ignore_customer_info_cft = apply_filters( 'wpsc_ignore_customer_info_cft', array( 'name', 'email', 'usergroups' ) );
586 }
587
588 /**
589 * Customer autocomplete callback
590 *
591 * @return void
592 */
593 public static function customer_filter_autocomplete() {
594
595 if ( check_ajax_referer( 'wpsc_customer_filter_autocomplete', '_ajax_nonce', false ) != 1 ) {
596 wp_send_json_error( 'Unauthorized request!', 401 );
597 }
598
599 $current_user = WPSC_Current_User::$current_user;
600 if ( $current_user->is_agent ) {
601 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
602 } elseif ( $current_user->is_customer && ! $current_user->is_agent ) {
603 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
604 }
605
606 if ( ! ( in_array( 'customer', $filter_items ) || WPSC_Functions::is_site_admin() ) ) {
607 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
608 }
609
610 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
611
612 $customers = WPSC_Customer::customer_autocomplete( $term );
613 wp_send_json( $customers );
614 }
615
616 /**
617 * Get customer record for creating new ticket. Apply setting like register user if not exists.
618 *
619 * @param string $name - name of the customer.
620 * @param string $email - email of the customer.
621 * @return WPSC_Customer
622 */
623 public static function get_customer_record( $name, $email ) {
624
625 $advanced = get_option( 'wpsc-ms-advanced-settings' );
626 $customer = WPSC_Customer::get_by_email( $email );
627 $user = get_user_by( 'email', $email );
628
629 if ( ! $user && $advanced['register-user-if-not-exist'] ) {
630
631 $user_id = wp_insert_user(
632 array(
633 'user_pass' => null,
634 'user_login' => $email,
635 'user_email' => $email,
636 'display_name' => $name,
637 )
638 );
639
640 if ( ! is_wp_error( $user_id ) ) {
641 wp_new_user_notification( $user_id, null, 'both' );
642 $customer = WPSC_Customer::get_by_email( $email );
643 }
644 }
645
646 if ( ! $customer->id ) {
647
648 $customer = WPSC_Customer::insert(
649 array(
650 'user' => $user ? $user->ID : 0,
651 'name' => $name,
652 'email' => $email,
653 )
654 );
655 }
656
657 return $customer;
658 }
659
660 /**
661 * Return given value for custom field
662 *
663 * @param WPSC_Custom_Field $cf - custom field object.
664 * @param mixed $val - value to convert and print.
665 * @return string
666 */
667 public static function get_field_value( $cf, $val ) {
668
669 $customer = is_object( $val ) ? $val : new WPSC_Customer( $val );
670 return $customer->id ? esc_attr( $customer->name ) : esc_attr__( 'None', 'supportcandy' );
671 }
672 }
673 endif;
674
675 WPSC_DF_Customer::init();
676