PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.9
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.3.9, at includes/admin/tickets/widgets/class-wpsc-itw-assigned-agents.php

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