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

734 lines 20.7 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 // Set custom field type.
195 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
196 }
197
198 /**
199 * Load current class to reference classes
200 *
201 * @param array $classes - Associative array of class names indexed by its slug.
202 * @return array
203 */
204 public static function load_ref_class( $classes ) {
205
206 $classes[ self::$slug ] = array(
207 'class' => __CLASS__,
208 'save-key' => 'id',
209 );
210 return $classes;
211 }
212
213 /**
214 * Add default custom field type to list
215 *
216 * @param array $default_cf_types - default custom field types array.
217 * @return array
218 */
219 public static function default_cf_types( $default_cf_types ) {
220 $default_cf_types[ self::$slug ] = array(
221 'label' => esc_attr__( 'Customer', 'supportcandy' ),
222 'class' => __CLASS__,
223 );
224 return $default_cf_types;
225 }
226
227 /**
228 * Print operators for ticket form filter
229 *
230 * @param WPSC_Custom_Field $cf - custom field object.
231 * @param array $filter - Existing filters (if any).
232 * @return void
233 */
234 public static function get_operators( $cf, $filter = array() ) {
235 ?>
236
237 <div class="item conditional">
238 <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' ) ); ?>');">
239 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
240 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
241 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
242 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
243 </select>
244 </div>
245 <?php
246 }
247
248 /**
249 * Print operators for ticket form filter
250 *
251 * @param string $operator - condition operator on which operands should be returned.
252 * @param WPSC_Custom_Field $cf - custom field object.
253 * @param array $filter - Exising functions (if any).
254 * @return void
255 */
256 public static function get_operands( $operator, $cf, $filter = array() ) {
257
258 $is_multiple = $operator !== '=' ? true : false;
259 $unique_id = uniqid( 'wpsc_' );
260 ?>
261 <div class="item conditional operand single">
262 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
263 <?php
264
265 if ( $is_multiple && isset( $filter['operand_val_1'] ) ) {
266
267 foreach ( $filter['operand_val_1'] as $customer_id ) {
268 $customer = new WPSC_Customer( intval( $customer_id ) )
269 ?>
270 <option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option>
271 <?php
272 }
273 }
274
275 if ( ! $is_multiple && isset( $filter['operand_val_1'] ) ) {
276
277 $customer = new WPSC_Customer( intval( $filter['operand_val_1'] ) )
278 ?>
279 <option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option>
280 <?php
281 }
282 ?>
283
284 </select>
285 </div>
286 <script>
287 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
288 ajax: {
289 url: supportcandy.ajax_url,
290 dataType: 'json',
291 delay: 250,
292 data: function (params) {
293 return {
294 q: params.term, // search term
295 page: params.page,
296 action: 'wpsc_customer_filter_autocomplete',
297 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_customer_filter_autocomplete' ) ); ?>'
298 };
299 },
300 processResults: function (data, params) {
301 var terms = [];
302 if ( data ) {
303 jQuery.each( data, function( id, text ) {
304 terms.push( { id: text.id, text: text.title } );
305 });
306 }
307 return {
308 results: terms
309 };
310 },
311 cache: true
312 },
313 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
314 minimumInputLength: 1
315 });
316 </script>
317 <?php
318 }
319
320 /**
321 * Parse filter and return sql query to be merged in ticket model query builder
322 *
323 * @param WPSC_Custom_Field $cf - custom field of this type.
324 * @param mixed $compare - comparison operator.
325 * @param mixed $val - value to compare.
326 * @return string
327 */
328 public static function parse_filter( $cf, $compare, $val ) {
329
330 global $wpdb;
331 $column = 't.' . sanitize_key( $cf->slug );
332
333 /**
334 * Normalize IDs (INT only)
335 */
336 $normalize_ids = static function ( $values ) {
337
338 $values = is_array( $values ) ? $values : array( $values );
339 $ids = array();
340
341 foreach ( $values as $v ) {
342 if ( is_numeric( $v ) && (int) $v > 0 ) {
343 $ids[] = (int) $v;
344 }
345 }
346
347 return array_unique( $ids );
348 };
349
350 switch ( $compare ) {
351
352 case '=':
353 $ids = $normalize_ids( $val );
354 if ( empty( $ids ) ) {
355 return '1=0';
356 }
357
358 return $wpdb->prepare(
359 "{$column} RLIKE %s",
360 '(^|[|])' . $ids[0] . '($|[|])'
361 );
362
363 case 'IN':
364 $ids = $normalize_ids( $val );
365 if ( empty( $ids ) ) {
366 return '1=0';
367 }
368
369 $regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])';
370
371 return $wpdb->prepare(
372 "{$column} RLIKE %s",
373 $regex
374 );
375
376 case 'NOT IN':
377 $ids = $normalize_ids( $val );
378 if ( empty( $ids ) ) {
379 return '1=1';
380 }
381
382 $regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])';
383
384 return $wpdb->prepare(
385 "{$column} NOT RLIKE %s",
386 $regex
387 );
388
389 default:
390 return '1=1';
391 }
392 }
393
394 /**
395 * Check ticket condition
396 *
397 * @param array $condition - array with condition data.
398 * @param WPSC_Custom_Field $cf - custom field object.
399 * @param WPSC_Ticket $ticket - ticket object.
400 * @return boolean
401 */
402 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
403
404 if ( ! is_object( $ticket->{$cf->slug} ) ) {
405 return false;
406 }
407
408 $flag = true;
409
410 switch ( $condition['operator'] ) {
411
412 case '=':
413 $flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false;
414 break;
415
416 case 'IN':
417 $flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] );
418 break;
419
420 case 'NOT IN':
421 $flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] );
422 break;
423
424 default:
425 $flag = true;
426 }
427
428 return $flag;
429 }
430
431 /**
432 * Check and return custom field value for new ticket to be created.
433 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
434 * Ignore phpcs nonce issue as we already checked where it is called from.
435 *
436 * @param array $data - Array of values to to stored in ticket in an insert function.
437 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
438 * @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.
439 * @return array
440 */
441 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
442
443 $current_user = WPSC_Current_User::$current_user;
444 $name = WPSC_DF_Customer_Name::get_tff_value( 'name' );
445 $email = WPSC_DF_Customer_Email::get_tff_value( 'email' );
446
447 $customer = self::get_customer_record( $name, $email );
448 if ( ! $customer->id ) {
449 wp_send_json_error( new WP_Error( '001', 'something went wrong!' ), 500 );
450 }
451
452 $data['customer'] = $customer->id;
453 return $data;
454 }
455
456 /**
457 * Set create ticket data for rest api request
458 *
459 * @param array $data - create ticket data array.
460 * @param WP_REST_Request $request - rest request object.
461 * @param array $custom_fields - custom field objects indexed by unique custom field types.
462 * @return array
463 */
464 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
465
466 $name = $request->get_param( 'name' );
467 $email = $request->get_param( 'email' );
468
469 // check name and email are present.
470 if ( ! $name || ! $email ) {
471 $data['errors']->add( 'req_fields_missing', 'name or email is missing!', 'name, email' );
472 return $data;
473 }
474
475 // validate email address.
476 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
477 $data['errors']->add( 'invalid_data', 'invalid email address!', 'email' );
478 return $data;
479 }
480
481 // get customer object.
482 $customer = self::get_customer_record( $name, $email );
483 if ( ! $customer->id ) {
484 $data['errors']->add( 'unknown', 'Something went wrong!' );
485 return $data;
486 }
487
488 $data['customer'] = $customer->id;
489 return $data;
490 }
491
492 /**
493 * Add SQL joins to ticket model for this field type
494 *
495 * @param array $joins - array of join string that can be imploded later.
496 * @param array $filter - user filter.
497 * @return array
498 */
499 public static function ticket_join( $joins, $filter ) {
500
501 global $wpdb;
502 $joins[] = 'LEFT JOIN ' . $wpdb->prefix . 'psmsc_customers c ON t.customer = c.id';
503 return $joins;
504 }
505
506 /**
507 * Add ticket search compatibility for fields of this custom field type.
508 *
509 * @param array $sql - Array of sql peices that can be joined later.
510 * @param array $filter - User filter.
511 * @param array $custom_fields - Custom fields array applicable for search.
512 * @param string $search - search string.
513 * @param array $allowed_search_fields - Allowed search fields.
514 * @return array
515 */
516 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
517
518 if ( in_array( 'customer', $allowed_search_fields ) ) {
519 $sql[] = 'CONVERT(c.name USING utf8) LIKE \'%' . $search . '%\'';
520 $sql[] = 'CONVERT(c.email USING utf8) LIKE \'%' . $search . '%\'';
521 }
522 return $sql;
523 }
524
525 /**
526 * Return val field for meta query of this type of custom field
527 *
528 * @param array $condition - condition data.
529 * @return mixed
530 */
531 public static function get_meta_value( $condition ) {
532
533 $operator = $condition['operator'];
534 switch ( $operator ) {
535
536 case '=':
537 case 'IN':
538 case 'NOT IN':
539 return $condition['operand_val_1'];
540 }
541 return false;
542 }
543
544 /**
545 * Return data for this custom field while creating duplicate ticket
546 *
547 * @param WPSC_Custom_Field $cf - custom field object.
548 * @param WPSC_Ticket $ticket - ticket object.
549 * @return mixed
550 */
551 public static function get_duplicate_ticket_data( $cf, $ticket ) {
552
553 return $ticket->customer->id;
554 }
555
556 /**
557 * Print edit custom field properties
558 *
559 * @param WPSC_Custom_Fields $cf - custom field object.
560 * @param string $field_class - class name of field category.
561 * @return void
562 */
563 public static function get_edit_custom_field_properties( $cf, $field_class ) {
564
565 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
566 ?>
567 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
568 <div class="label-container">
569 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
570 </div>
571 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
572 </div>
573 <?php
574 endif;
575 }
576
577 /**
578 * Set custom field properties. Can be used by add/edit custom field.
579 * Ignore phpcs nonce issue as we already checked where it is called from.
580 *
581 * @param WPSC_Custom_Field $cf - custom field object.
582 * @param string $field_class - class of field category.
583 * @return void
584 */
585 public static function set_cf_properties( $cf, $field_class ) {
586
587 // extra info.
588 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
589 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
590 }
591
592 // save!
593 $cf->save();
594 }
595
596 /**
597 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
598 *
599 * @param WPSC_Custom_Field $cf - custom field object.
600 * @param WPSC_Ticket $ticket - ticket object.
601 * @param string $module - module name.
602 * @return string
603 */
604 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
605
606 return apply_filters( 'wpsc_ticket_field_val_customer', $ticket->customer->name, $cf, $ticket, $module );
607 }
608
609 /**
610 * Print given value for custom field
611 *
612 * @param WPSC_Custom_Field $cf - custom field object.
613 * @param mixed $val - value to convert and print.
614 * @return void
615 */
616 public static function print_val( $cf, $val ) {
617
618 $customer = is_object( $val ) ? $val : new WPSC_Customer( $val );
619 echo $customer->id ? esc_attr( $customer->name ) : esc_attr__( 'None', 'supportcandy' );
620 }
621
622 /**
623 * Return printable value for history log macro
624 *
625 * @param WPSC_Custom_Field $cf - custom field object.
626 * @param mixed $val - value to convert and return.
627 * @return string
628 */
629 public static function get_history_log_val( $cf, $val ) {
630
631 ob_start();
632 self::print_val( $cf, $val );
633 return ob_get_clean();
634 }
635
636 /**
637 * Set ignore customer info custom field type.
638 *
639 * @return void
640 */
641 public static function ignore_customer_info_cft() {
642
643 self::$ignore_customer_info_cft = apply_filters( 'wpsc_ignore_customer_info_cft', array( 'name', 'email', 'usergroups' ) );
644 }
645
646 /**
647 * Customer autocomplete callback
648 *
649 * @return void
650 */
651 public static function customer_filter_autocomplete() {
652
653 if ( check_ajax_referer( 'wpsc_customer_filter_autocomplete', '_ajax_nonce', false ) != 1 ) {
654 wp_send_json_error( 'Unauthorized request!', 401 );
655 }
656
657 $current_user = WPSC_Current_User::$current_user;
658 if ( $current_user->is_agent ) {
659 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
660 } elseif ( $current_user->is_customer && ! $current_user->is_agent ) {
661 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
662 }
663
664 if ( ! ( in_array( 'customer', $filter_items ) || WPSC_Functions::is_site_admin() ) ) {
665 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
666 }
667
668 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
669
670 $customers = WPSC_Customer::customer_autocomplete( $term );
671 wp_send_json( $customers );
672 }
673
674 /**
675 * Get customer record for creating new ticket. Apply setting like register user if not exists.
676 *
677 * @param string $name - name of the customer.
678 * @param string $email - email of the customer.
679 * @return WPSC_Customer
680 */
681 public static function get_customer_record( $name, $email ) {
682
683 $advanced = get_option( 'wpsc-ms-advanced-settings' );
684 $customer = WPSC_Customer::get_by_email( $email );
685 $user = get_user_by( 'email', $email );
686
687 if ( ! $user && $advanced['register-user-if-not-exist'] ) {
688
689 $user_id = wp_insert_user(
690 array(
691 'user_pass' => null,
692 'user_login' => $email,
693 'user_email' => $email,
694 'display_name' => $name,
695 )
696 );
697
698 if ( ! is_wp_error( $user_id ) ) {
699 wp_new_user_notification( $user_id, null, 'both' );
700 $customer = WPSC_Customer::get_by_email( $email );
701 }
702 }
703
704 if ( ! $customer->id ) {
705
706 $customer = WPSC_Customer::insert(
707 array(
708 'user' => $user ? $user->ID : 0,
709 'name' => $name,
710 'email' => $email,
711 )
712 );
713 }
714
715 return $customer;
716 }
717
718 /**
719 * Return given value for custom field
720 *
721 * @param WPSC_Custom_Field $cf - custom field object.
722 * @param mixed $val - value to convert and print.
723 * @return string
724 */
725 public static function get_field_value( $cf, $val ) {
726
727 $customer = is_object( $val ) ? $val : new WPSC_Customer( $val );
728 return $customer->id ? esc_attr( $customer->name ) : esc_attr__( 'None', 'supportcandy' );
729 }
730 }
731 endif;
732
733 WPSC_DF_Customer::init();
734