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 / admin / tickets / widgets / class-wpsc-itw-assigned-agents.php

class-wpsc-itw-assigned-agents.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.2, at includes/admin/tickets/widgets/class-wpsc-itw-assigned-agents.php

498 lines 17.0 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_ITW_Assigned_Agents' ) ) :
7
8 final class WPSC_ITW_Assigned_Agents {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // get edit assigned agents.
18 add_action( 'wp_ajax_wpsc_it_get_edit_assigned_agents', array( __CLASS__, 'it_get_edit_assigned_agents' ) );
19 add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_assigned_agents', array( __CLASS__, 'it_get_edit_assigned_agents' ) );
20 add_action( 'wp_ajax_wpsc_it_set_edit_assigned_agents', array( __CLASS__, 'set_edit_assigned_agents' ) );
21 add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_assigned_agents', array( __CLASS__, 'set_edit_assigned_agents' ) );
22
23 // assigned agents.
24 add_action( 'wp_ajax_wpsc_get_tw_agents', array( __CLASS__, 'get_tw_agents' ) );
25 add_action( 'wp_ajax_wpsc_set_tw_agents', array( __CLASS__, 'set_tw_agents' ) );
26
27 // agent autocomplete assign cap access check.
28 add_action( 'wp_ajax_wpsc_agent_autocomplete_itw_assign', array( __CLASS__, 'agent_autocomplete_itw_assign' ) );
29 add_action( 'wp_ajax_nopriv_wpsc_agent_autocomplete_itw_assign', array( __CLASS__, 'agent_autocomplete_itw_assign' ) );
30 }
31
32 /**
33 * Prints body of current widget
34 *
35 * @param WPSC_Ticket $ticket - ticket object.
36 * @param array $settings - widget settings.
37 * @return void
38 */
39 public static function print_widget( $ticket, $settings ) {
40
41 $current_user = WPSC_Current_User::$current_user;
42 if ( ! (
43 (
44 (
45 WPSC_Individual_Ticket::$view_profile == 'customer' ||
46 $ticket->customer->id == $current_user->customer->id
47 ) &&
48 $settings['allow-customer']
49 ) ||
50 ( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
51 ) ) {
52 return;
53 }
54
55 $show_edit = (
56 $ticket->is_active &&
57 WPSC_Individual_Ticket::$view_profile == 'agent' &&
58 WPSC_Individual_Ticket::has_ticket_cap( 'aa' ) &&
59 ! WPSC_Individual_Ticket::$is_restricted
60 );
61 self::render_widget( $ticket, $settings, $show_edit, false );
62 }
63
64 /**
65 * Prints body of current widget
66 *
67 * @param WPSC_Ticket $ticket - ticket object.
68 * @param array $settings - widget settings.
69 * @return void
70 */
71 public static function print_archive_widget( $ticket, $settings ) {
72
73 $current_user = WPSC_Current_User::$current_user;
74 if ( ! (
75 (
76 (
77 WPSC_Individual_Archive_Ticket::$view_profile == 'customer' ||
78 $ticket->customer->id == $current_user->customer->id
79 ) &&
80 $settings['allow-customer']
81 ) ||
82 ( WPSC_Individual_Archive_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
83 ) ) {
84 return;
85 }
86 self::render_widget( $ticket, $settings, false, true );
87 }
88
89 /**
90 * Render widget
91 *
92 * @param WPSC_Ticket|WPSC_Archive_Ticket $ticket - ticket object.
93 * @param array $settings - widget settings.
94 * @param bool $show_edit - show edit button.
95 * @param bool $is_archive - is archive ticket.
96 * @return void
97 */
98 private static function render_widget( $ticket, $settings, $show_edit, $is_archive ) {
99 $title = $settings['title']
100 ? WPSC_Translations::get( 'wpsc-twt-assignee', stripslashes( $settings['title'] ) )
101 : stripslashes( $settings['title'] );
102 ?>
103 <div class="wpsc-it-widget wpsc-itw-assignee">
104 <div class="wpsc-widget-header">
105 <h2><?php echo esc_attr( $title ); ?></h2>
106 <?php
107 if ( $show_edit ) :
108 ?>
109 <span onclick="wpsc_it_get_edit_assigned_agents(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_assigned_agents' ) ); ?>')"><?php WPSC_Icons::get( 'edit' ); ?></span>
110 <?php
111 endif;
112 ?>
113 <span class="wpsc-itw-toggle" data-widget="wpsc-itw-assignee"><?php WPSC_Icons::get( 'chevron-up' ); ?></span>
114 </div>
115 <div class="wpsc-widget-body">
116 <?php
117 $cf = WPSC_Custom_Field::get_cf_by_slug( 'assigned_agent' );
118 $cf->type::print_widget_ticket_field_val( $cf, $ticket );
119 do_action( 'wpsc_itw_assigned_agents', $ticket )
120 ?>
121 </div>
122 </div>
123 <?php
124 }
125
126 /**
127 * Get edit assign agents
128 *
129 * @return void
130 */
131 public static function it_get_edit_assigned_agents() {
132
133 if ( check_ajax_referer( 'wpsc_it_get_edit_assigned_agents', '_ajax_nonce', false ) != 1 ) {
134 wp_send_json_error( 'Unauthorized request!', 401 );
135 }
136
137 WPSC_Individual_Ticket::load_current_ticket();
138 if ( WPSC_Individual_Ticket::$is_restricted ) {
139 wp_send_json_error( 'Unauthorised request!', 401 );
140 }
141
142 $current_user = WPSC_Current_User::$current_user;
143 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'aa' ) ) ) {
144 wp_send_json_error( 'Something went wrong!', 401 );
145 }
146
147 $ticket = WPSC_Individual_Ticket::$ticket;
148
149 $widgets = get_option( 'wpsc-ticket-widget' );
150 $title = $widgets['assignee']['title'];
151
152 ob_start();
153 $unique_id = uniqid()
154 ?>
155 <form action="#" onsubmit="return false;" class="change-assignee <?php echo esc_attr( $unique_id ); ?>">
156 <div class="wpsc-input-group" style="flex-direction:row; flex-wrap:nowrap;">
157 <div class="wpsc-input-group">
158 <div class="label-container">
159 <label for=""><?php esc_attr_e( 'Filter by', 'supportcandy' ); ?></label>
160 </div>
161 <select class="agent-filter-by">
162 <?php
163 $filter_by = apply_filters(
164 'wpsc_assignee_filter_by',
165 array(
166 'all' => esc_attr__( 'All', 'supportcandy' ),
167 )
168 );
169 foreach ( $filter_by as $key => $label ) :
170 ?>
171 <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $label ); ?></option>
172 <?php
173 endforeach
174 ?>
175 </select>
176 </div>
177 <div style="margin:5px;"></div>
178 <div class="wpsc-input-group">
179 <div class="label-container">
180 <label for=""><?php esc_attr_e( 'Sort by', 'supportcandy' ); ?></label>
181 </div>
182 <select class="agent-sort-by">
183 <option value="workload"><?php esc_attr_e( 'Workload', 'supportcandy' ); ?></option>
184 <option value="name"><?php esc_attr_e( 'Name', 'supportcandy' ); ?></option>
185 </select>
186 </div>
187 </div>
188 <div class="wpsc-input-group">
189 <div class="label-container">
190 <label for=""><?php esc_attr_e( 'Select agent(s)', 'supportcandy' ); ?></label>
191 </div>
192 <select class="<?php echo esc_attr( $unique_id ); ?>" multiple name="assignee[]">
193 <?php
194 $assignees = $ticket->assigned_agent;
195 foreach ( $assignees as $assignee ) :
196 ?>
197 <option selected value="<?php echo esc_attr( $assignee->id ); ?>"><?php echo esc_attr( $assignee->name ); ?></option>
198 <?php
199 endforeach
200 ?>
201 </select>
202 <script>
203 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
204 ajax: {
205 url: supportcandy.ajax_url,
206 dataType: 'json',
207 delay: 250,
208 data: function (params) {
209 return {
210 q: params.term, // search term
211 action: 'wpsc_agent_autocomplete_itw_assign',
212 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_itw_assign' ) ); ?>',
213 filter_by: jQuery('select.agent-filter-by').val(),
214 sort_by: jQuery('select.agent-sort-by').val(),
215 isMultiple: 1,
216 ticket_id: <?php echo esc_attr( $ticket->id ); ?>
217 };
218 },
219 processResults: function (data, params) {
220 var terms = [];
221 if ( data ) {
222 jQuery.each( data, function( id, text ) {
223 terms.push( { id: text.id, text: text.title } );
224 });
225 }
226 return {
227 results: terms
228 };
229 },
230 cache: true
231 },
232 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
233 minimumInputLength: 0,
234 allowClear: false,
235 placeholder: ""
236 });
237 </script>
238 </div>
239 <?php do_action( 'wpsc_get_edit_assigned_agent_body', $ticket ); ?>
240 <input type="hidden" name="action" value="wpsc_it_set_edit_assigned_agents">
241 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
242 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_assigned_agents' ) ); ?>">
243 </form>
244 <?php
245 do_action( 'wpsc_get_edit_assigned_agent_footer' );
246 $body = ob_get_clean();
247
248 ob_start();
249 ?>
250 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_assigned_agents(this, <?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( $unique_id ); ?>');">
251 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
252 </button>
253 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
254 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
255 </button>
256 <?php
257 $footer = ob_get_clean();
258
259 $response = array(
260 'title' => $title,
261 'body' => $body,
262 'footer' => $footer,
263 );
264 wp_send_json( $response );
265 }
266
267 /**
268 * Change assigned agent
269 *
270 * @return void
271 */
272 public static function set_edit_assigned_agents() {
273
274 if ( check_ajax_referer( 'wpsc_it_set_edit_assigned_agents', '_ajax_nonce', false ) != 1 ) {
275 wp_send_json_error( 'Unauthorized request!', 401 );
276 }
277
278 WPSC_Individual_Ticket::load_current_ticket();
279 if ( WPSC_Individual_Ticket::$is_restricted ) {
280 wp_send_json_error( 'Unauthorised request!', 401 );
281 }
282
283 $current_user = WPSC_Current_User::$current_user;
284 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'aa' ) ) ) {
285 wp_send_json_error( 'Something went wrong!', 401 );
286 }
287
288 $ticket = WPSC_Individual_Ticket::$ticket;
289 if ( ! $ticket->is_active ) {
290 wp_send_json_error( 'Something went wrong!', 400 );
291 }
292
293 $prev = $ticket->assigned_agent;
294 $new_ids = isset( $_POST['assignee'] ) ? array_filter( array_map( 'intval', $_POST['assignee'] ) ) : array();
295
296 // Check whether all new agets exists.
297 $new = array();
298 foreach ( $new_ids as $id ) {
299 $agent = new WPSC_Agent( $id );
300 $new[] = $agent;
301 if ( ! $agent->id ) {
302 wp_send_json_error( 'Something went wrong!', 400 );
303 }
304 }
305
306 $prev_ids = array();
307 foreach ( $prev as $agent ) {
308 $prev_ids[] = $agent->id;
309 }
310
311 // Exit if there is no change.
312 if (
313 count( array_diff( $new_ids, $prev_ids ) ) === 0 &&
314 count( array_diff( $prev_ids, $new_ids ) ) === 0
315 ) {
316 wp_die();
317 }
318
319 // Change assignee.
320 WPSC_Individual_Ticket::change_assignee( $prev, $new, $current_user->customer->id );
321
322 $response = array( 'viewPermission' => WPSC_Individual_Ticket::has_ticket_cap( 'view' ) ? 1 : 0 );
323 wp_send_json( $response, 200 );
324 }
325
326 /**
327 * Get agent
328 */
329 public static function get_tw_agents() {
330
331 if ( ! WPSC_Functions::is_site_admin() ) {
332 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
333 }
334
335 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
336 $agents = $ticket_widgets['assignee'];
337 $title = $agents['title'];
338 $roles = get_option( 'wpsc-agent-roles', array() );
339 ob_start();
340 ?>
341
342 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-agents">
343 <div class="wpsc-input-group">
344 <div class="label-container">
345 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
346 </div>
347 <input name="label" type="text" value="<?php echo esc_attr( $agents['title'] ); ?>" autocomplete="off">
348 </div>
349 <div class="wpsc-input-group">
350 <div class="label-container">
351 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
352 </div>
353 <select name="is_enable">
354 <option <?php selected( $agents['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
355 <option <?php selected( $agents['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
356 </select>
357 </div>
358 <div class="wpsc-input-group">
359 <div class="label-container">
360 <label for=""><?php esc_attr_e( 'Allowed for customer', 'supportcandy' ); ?></label>
361 </div>
362 <select id="allow-customer" name="allow-customer">
363 <option <?php selected( $agents['allow-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
364 <option <?php selected( $agents['allow-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
365 </select>
366 </div>
367 <div class="wpsc-input-group">
368 <div class="label-container">
369 <label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label>
370 </div>
371 <select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent...">
372 <?php
373 foreach ( $roles as $key => $role ) :
374 $selected = in_array( $key, $agents['allowed-agent-roles'] ) ? 'selected="selected"' : ''
375 ?>
376 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
377 <?php
378 endforeach;
379 ?>
380 </select>
381 </div>
382 <script>
383 jQuery('#wpsc-select-agents').selectWoo({
384 allowClear: false,
385 placeholder: ""
386 });
387 </script>
388 <?php do_action( 'wpsc_get_ticket_info_body' ); ?>
389 <input type="hidden" name="action" value="wpsc_set_tw_agents">
390 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_agents' ) ); ?>">
391 </form>
392 <?php
393 $body = ob_get_clean();
394
395 ob_start();
396 ?>
397 <button class="wpsc-button small primary" onclick="wpsc_set_tw_agents(this);">
398 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
399 </button>
400 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
401 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
402 </button>
403 <?php
404 do_action( 'wpsc_get_tw_agents_widget_footer' );
405 $footer = ob_get_clean();
406
407 $response = array(
408 'title' => $title,
409 'body' => $body,
410 'footer' => $footer,
411 );
412 wp_send_json( $response );
413 }
414
415 /**
416 * Set Agents
417 */
418 public static function set_tw_agents() {
419
420 if ( check_ajax_referer( 'wpsc_set_tw_agents', '_ajax_nonce', false ) != 1 ) {
421 wp_send_json_error( 'Unauthorized request!', 401 );
422 }
423
424 if ( ! WPSC_Functions::is_site_admin() ) {
425 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
426 }
427
428 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
429 if ( ! $label ) {
430 wp_die();
431 }
432
433 $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
434 $allow_for_customer = isset( $_POST['allow-customer'] ) ? intval( $_POST['allow-customer'] ) : 0;
435 $agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array();
436
437 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
438
439 $ticket_widgets['assignee']['title'] = $label;
440 $ticket_widgets['assignee']['is_enable'] = $is_enable;
441 $ticket_widgets['assignee']['allow-customer'] = $allow_for_customer;
442 $ticket_widgets['assignee']['allowed-agent-roles'] = $agents;
443 update_option( 'wpsc-ticket-widget', $ticket_widgets );
444
445 // update custom field.
446 $cf = WPSC_Custom_Field::get_cf_by_slug( 'assigned_agent' );
447 $cf->name = $label;
448 $cf->save();
449
450 // remove string translations.
451 WPSC_Translations::remove( 'wpsc-twt-assignee' );
452 WPSC_Translations::add( 'wpsc-twt-assignee', stripslashes( $label ) );
453 wp_die();
454 }
455
456 /**
457 * Agent autocomplete assign agents
458 *
459 * @return void
460 */
461 public static function agent_autocomplete_itw_assign() {
462
463 if ( check_ajax_referer( 'wpsc_agent_autocomplete_itw_assign', '_ajax_nonce', false ) !== 1 ) {
464 wp_send_json_error( 'Unauthorized request!', 401 );
465 }
466
467 $ticket_id = isset( $_GET['ticket_id'] ) ? intval( $_GET['ticket_id'] ) : 0;
468 $ticket = new WPSC_Ticket( $ticket_id );
469 if ( ! $ticket->id ) {
470 wp_send_json_error( 'Unauthorized request!', 401 );
471 }
472 WPSC_Individual_Ticket::$ticket = $ticket;
473
474 $current_user = WPSC_Current_User::$current_user;
475 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'aa' ) ) ) {
476 wp_send_json_error( 'Unauthorized request!', 401 );
477 }
478
479 $filters = array();
480
481 $filters['term'] = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
482 $filters['filter_by'] = isset( $_GET['filter_by'] ) ? sanitize_text_field( wp_unslash( $_GET['filter_by'] ) ) : 'all';
483 $filters['sort_by'] = isset( $_GET['sort_by'] ) ? sanitize_text_field( wp_unslash( $_GET['sort_by'] ) ) : 'name';
484 $filters['isMultiple'] = isset( $_GET['isMultiple'] ) ? intval( wp_unslash( $_GET['isMultiple'] ) ) : 0;
485
486 $filters['isAgentgroup'] = 0;
487 if ( class_exists( 'WPSC_Agentgroups' ) ) {
488 $filters['isAgentgroup'] = isset( $_GET['isAgentgroup'] ) ? intval( $_GET['isAgentgroup'] ) : null;
489 }
490
491 $response = WPSC_Agent::agent_autocomplete( $filters );
492 wp_send_json( $response );
493 }
494 }
495 endif;
496
497 WPSC_ITW_Assigned_Agents::init();
498