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-agent-created.php

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

684 lines 19.1 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_Agent_Created' ) ) :
7
8 final class WPSC_DF_Agent_Created {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_agent_created';
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 = 'INT 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 = true;
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 = 'wpsc_agent';
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 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 = 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 // create ticket data from ticket form.
168 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 5, 3 );
169
170 // create ticket data for rest api.
171 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 2, 3 );
172
173 // agent autocomplete filter access only.
174 add_action( 'wp_ajax_wpsc_agent_autocomplete_agent_created', array( __CLASS__, 'agent_autocomplete_agent_created' ) );
175 add_action( 'wp_ajax_nopriv_wpsc_agent_autocomplete_agent_created', array( __CLASS__, 'agent_autocomplete_agent_created' ) );
176
177 // rest api.
178 add_filter( 'wpsc_rest_prevent_ticket_data', array( __CLASS__, 'rest_prevent_ticket_data' ) );
179
180 // Set custom field type.
181 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
182 }
183
184 /**
185 * Load current class to reference classes
186 *
187 * @param array $classes - Associative array of class names indexed by its slug.
188 * @return array
189 */
190 public static function load_ref_class( $classes ) {
191
192 $classes[ self::$slug ] = array(
193 'class' => __CLASS__,
194 'save-key' => 'id',
195 );
196 return $classes;
197 }
198
199 /**
200 * Add default custom field type to list
201 *
202 * @param array $default_cf_types - default custom field types array.
203 * @return array
204 */
205 public static function default_cf_types( $default_cf_types ) {
206 $default_cf_types[ self::$slug ] = array(
207 'label' => esc_attr__( 'Agent Created', 'supportcandy' ),
208 'class' => __CLASS__,
209 );
210 return $default_cf_types;
211 }
212
213 /**
214 * Print operators for ticket form filter
215 *
216 * @param WPSC_Custom_Field $cf - custom field object.
217 * @param array $filter - Existing filters (if any).
218 * @return void
219 */
220 public static function get_operators( $cf, $filter = array() ) {
221 ?>
222
223 <div class="item conditional">
224 <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' ) ); ?>');">
225 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
226 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
227 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
228 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
229 </select>
230 </div>
231 <?php
232 }
233
234 /**
235 * Print operators for ticket form filter
236 *
237 * @param string $operator - condition operator on which operands should be returned.
238 * @param WPSC_Custom_Field $cf - custom field object.
239 * @param array $filter - Exising functions (if any).
240 * @return void
241 */
242 public static function get_operands( $operator, $cf, $filter = array() ) {
243
244 $is_multiple = $operator !== '=' ? true : false;
245 $unique_id = uniqid( 'wpsc_' );
246 ?>
247
248 <div class="item conditional operand single">
249
250 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
251
252 <?php
253 if ( $is_multiple && isset( $filter['operand_val_1'] ) ) {
254
255 foreach ( $filter['operand_val_1'] as $agent_id ) {
256
257 if ( $agent_id == '0' ) {
258
259 ?>
260 <option selected="selected" value="0"><?php esc_attr_e( 'None', 'supportcandy' ); ?></option>
261 <?php
262
263 } else {
264
265 $agent = new WPSC_Agent( intval( $agent_id ) )
266 ?>
267 <option selected="selected" value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
268 <?php
269 }
270 }
271 }
272
273 if ( ! $is_multiple && isset( $filter['operand_val_1'] ) ) {
274
275 if ( $filter['operand_val_1'] == '0' ) {
276
277 ?>
278 <option selected="selected" value="0"><?php esc_attr_e( 'None', 'supportcandy' ); ?></option>
279 <?php
280
281 } else {
282
283 $agent = new WPSC_Agent( intval( $filter['operand_val_1'] ) )
284 ?>
285 <option selected="selected" value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
286 <?php
287
288 }
289 }
290 ?>
291
292 </select>
293
294 </div>
295
296 <script>
297
298 jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
299 ajax: {
300 url: supportcandy.ajax_url,
301 dataType: 'json',
302 delay: 250,
303 data: function (params) {
304 return {
305 q: params.term, // search term
306 page: params.page,
307 action: 'wpsc_agent_autocomplete_agent_created',
308 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_agent_created' ) ); ?>',
309 isAgentgroup: 0
310 };
311 },
312 processResults: function (data, params) {
313 var terms = [];
314 if ( data ) {
315 jQuery.each( data, function( id, text ) {
316 terms.push( { id: text.id, text: text.title } );
317 });
318 }
319 return {
320 results: terms
321 };
322 },
323 cache: true
324 },
325 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
326 minimumInputLength: 0
327 });
328
329 </script>
330 <?php
331 }
332
333 /**
334 * Parse filter and return sql query to be merged in ticket model query builder
335 *
336 * @param WPSC_Custom_Field $cf - custom field of this type.
337 * @param mixed $compare - comparison operator.
338 * @param mixed $val - value to compare.
339 * @return string
340 */
341 public static function parse_filter( $cf, $compare, $val ) {
342
343 global $wpdb;
344 $column = 't.' . sanitize_key( $cf->slug );
345 switch ( $compare ) {
346
347 case '=':
348 $val = absint( $val );
349 if ( ! $val ) {
350 return '1=0';
351 }
352
353 return $wpdb->prepare(
354 "{$column} = %d",
355 $val
356 );
357
358 case 'IN':
359 case 'NOT IN':
360 if ( ! is_array( $val ) ) {
361 return '1=0';
362 }
363
364 $ids = array_filter( array_map( 'absint', $val ) );
365 if ( empty( $ids ) ) {
366 return '1=0';
367 }
368
369 $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
370 $operator = ( $compare === 'IN' ) ? 'IN' : 'NOT IN';
371
372 return $wpdb->prepare(
373 "{$column} {$operator} ({$placeholders})",
374 $ids
375 );
376
377 default:
378 return '1=1';
379 }
380 }
381
382 /**
383 * Check ticket condition
384 *
385 * @param array $condition - array with condition data.
386 * @param WPSC_Custom_Field $cf - custom field object.
387 * @param WPSC_Ticket $ticket - ticket object.
388 * @return boolean
389 */
390 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
391
392 $flag = true;
393
394 switch ( $condition['operator'] ) {
395
396 case '=':
397 if ( ! $ticket->{$cf->slug} ) {
398 $flag = $condition['operand_val_1'] == '0' ? true : false;
399 } else {
400 $flag = $condition['operand_val_1'] == $ticket->{$cf->slug}->id ? true : false;
401 }
402 break;
403
404 case 'IN':
405 if ( ! $ticket->{$cf->slug} ) {
406 $flag = in_array( '0', $condition['operand_val_1'] ) ? true : false;
407 } else {
408 $flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ) ? true : false;
409 }
410 break;
411
412 case 'NOT IN':
413 if ( ! $ticket->{$cf->slug} ) {
414 $flag = ! in_array( '0', $condition['operand_val_1'] ) ? true : false;
415 } else {
416 $flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ) ? true : false;
417 }
418 break;
419
420 default:
421 $flag = true;
422 }
423
424 return $flag;
425 }
426
427 /**
428 * Return default value for custom field of this type
429 *
430 * @param WPSC_Custom_Field $cf - custom field object.
431 * @return mixed
432 */
433 public static function get_default_value( $cf ) {
434
435 return 0;
436 }
437
438 /**
439 * Check and return custom field value for new ticket to be created.
440 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
441 * Ignore phpcs nonce issue as we already checked where it is called from.
442 *
443 * @param array $data - Array of values to to stored in ticket in an insert function.
444 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
445 * @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.
446 * @return array
447 */
448 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
449
450 if ( $is_my_profile ) {
451 return;
452 }
453
454 $current_user = WPSC_Current_User::$current_user;
455 $name = WPSC_DF_Customer_Name::get_tff_value( 'name' );
456 $email = WPSC_DF_Customer_Email::get_tff_value( 'email' );
457
458 if ( ! $name || ! $email ) {
459 wp_send_json_error( new WP_Error( 'WPSC_DF_Agent_Created', 'customer name or email not given!' ), 400 );
460 }
461
462 if ( $current_user->is_customer ) {
463
464 $is_create_as = $current_user->customer->email != $email ? true : false;
465 if (
466 $is_create_as &&
467 (
468 ! $current_user->is_agent || ! $current_user->agent->has_cap( 'create-as' )
469 )
470 ) {
471 wp_send_json_error( new WP_Error( 'WPSC_DF_Agent_Created', 'Unauthorized!' ), 401 );
472 }
473
474 $data['agent_created'] = $is_create_as ? $current_user->agent->id : 0;
475
476 } else {
477
478 $data['agent_created'] = 0;
479 }
480
481 return $data;
482 }
483
484 /**
485 * Set create ticket data for rest api request
486 *
487 * @param array $data - create ticket data array.
488 * @param WP_REST_Request $request - rest request object.
489 * @param array $custom_fields - custom field objects indexed by unique custom field types.
490 * @return array
491 */
492 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
493
494 $current_user = WPSC_Current_User::$current_user;
495 $is_create_as = $current_user->customer->id != $data['customer'] ? true : false;
496
497 if (
498 $is_create_as && (
499 ! $current_user->is_agent || ! $current_user->agent->has_cap( 'create-as' )
500 )
501 ) {
502 $data['errors']->add( 'unauthorized', 'You are not authorized to create ticket for other customer!' );
503 return $data;
504 }
505
506 $data['agent_created'] = $is_create_as ? $current_user->agent->id : 0;
507 return $data;
508 }
509
510 /**
511 * Return val field for meta query of this type of custom field
512 *
513 * @param array $condition - condition data.
514 * @return mixed
515 */
516 public static function get_meta_value( $condition ) {
517
518 $operator = $condition['operator'];
519 switch ( $operator ) {
520
521 case '=':
522 case 'IN':
523 case 'NOT IN':
524 return $condition['operand_val_1'];
525 }
526 return false;
527 }
528
529 /**
530 * Return data for this custom field while creating duplicate ticket
531 *
532 * @param WPSC_Custom_Field $cf - custom field object.
533 * @param WPSC_Ticket $ticket - ticket object.
534 * @return mixed
535 */
536 public static function get_duplicate_ticket_data( $cf, $ticket ) {
537
538 return WPSC_Current_User::$current_user->agent->id;
539 }
540
541 /**
542 * Print edit custom field properties
543 *
544 * @param WPSC_Custom_Fields $cf - custom field object.
545 * @param string $field_class - class name of field category.
546 * @return void
547 */
548 public static function get_edit_custom_field_properties( $cf, $field_class ) {
549
550 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
551 ?>
552 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
553 <div class="label-container">
554 <label for="">
555 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
556 </label>
557 </div>
558 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
559 </div>
560 <?php
561 endif;
562 }
563
564 /**
565 * Set custom field properties. Can be used by add/edit custom field.
566 * Ignore phpcs nonce issue as we already checked where it is called from.
567 *
568 * @param WPSC_Custom_Field $cf - custom field object.
569 * @param string $field_class - class of field category.
570 * @return void
571 */
572 public static function set_cf_properties( $cf, $field_class ) {
573
574 // tl_width!
575 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
576
577 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
578 $cf->tl_width = $tl_width ? $tl_width : 100;
579 }
580
581 // save!
582 $cf->save();
583 }
584
585 /**
586 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
587 *
588 * @param WPSC_Custom_Field $cf - custom field object.
589 * @param WPSC_Ticket $ticket - ticket object.
590 * @param string $module - module name.
591 * @return string
592 */
593 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
594
595 $agent = $ticket->{$cf->slug};
596 $value = is_object( $agent ) && $agent->id ? $agent->name : '';
597 return apply_filters( 'wpsc_ticket_field_val_agent_created', $value, $cf, $ticket, $module );
598 }
599
600 /**
601 * Print ticket value for given custom field on ticket list
602 *
603 * @param WPSC_Custom_Field $cf - custom field object.
604 * @param WPSC_Ticket $ticket - ticket object.
605 * @return void
606 */
607 public static function print_tl_ticket_field_val( $cf, $ticket ) {
608
609 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
610 }
611
612 /**
613 * Print ticket value for given custom field on widget
614 *
615 * @param WPSC_Custom_Field $cf - custom field object.
616 * @param WPSC_Ticket $ticket - ticket object.
617 * @return void
618 */
619 public static function print_widget_ticket_field_val( $cf, $ticket ) {
620
621 $agent = $ticket->{$cf->slug};
622 echo $agent->id ? esc_attr( $agent->name ) : esc_attr__( 'Not Applicable', 'supportcandy' );
623 }
624
625 /**
626 * Agent autocomplete for filter access
627 *
628 * @return void
629 */
630 public static function agent_autocomplete_agent_created() {
631
632 if ( check_ajax_referer( 'wpsc_agent_autocomplete_agent_created', '_ajax_nonce', false ) !== 1 ) {
633 wp_send_json_error( 'Unauthorized request!', 401 );
634 }
635
636 $current_user = WPSC_Current_User::$current_user;
637 if ( $current_user->is_agent ) {
638 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
639 } elseif ( $current_user->is_customer && ! $current_user->is_agent ) {
640 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
641 }
642
643 if ( ! ( in_array( 'agent_created', $filter_items ) || WPSC_Functions::is_site_admin() ) ) {
644 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
645 }
646
647 $filters = array(
648 'term' => isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '',
649 'filter_by' => 'all',
650 'sort_by' => 'name',
651 'isMultiple' => 0,
652 );
653
654 $filters['isAgentgroup'] = 0;
655 if ( class_exists( 'WPSC_Agentgroups' ) ) {
656 $filters['isAgentgroup'] = isset( $_GET['isAgentgroup'] ) ? intval( $_GET['isAgentgroup'] ) : null;
657 }
658
659 $response = WPSC_Agent::agent_autocomplete( $filters );
660 wp_send_json( $response );
661 }
662
663 /**
664 * Rest api filter for ticket data to prevent from sending it to client
665 *
666 * @param array $data - array of slugs to prevent from.
667 * @return array
668 */
669 public static function rest_prevent_ticket_data( $data ) {
670
671 $current_user = WPSC_Current_User::$current_user;
672 if ( ! $current_user->is_agent ) {
673 $list_items = $current_user->get_tl_list_items();
674 if ( ! in_array( 'agent_created', $list_items ) ) {
675 $data[] = 'agent_created';
676 }
677 }
678 return $data;
679 }
680 }
681 endif;
682
683 WPSC_DF_Agent_Created::init();
684