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-customer.php

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

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