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

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