PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.7
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 / agent-settings / class-wpsc-agent-settings.php

class-wpsc-agent-settings.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.7, at includes/admin/agent-settings/class-wpsc-agent-settings.php

609 lines 16.7 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_Agent_Settings' ) ) :
7
8 final class WPSC_Agent_Settings {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // Get agent list ajax register.
18 add_action( 'wp_ajax_wpsc_get_agent_list', array( __CLASS__, 'get_agent_list' ) );
19
20 // Get add agent modal.
21 add_action( 'wp_ajax_wpsc_get_add_agent', array( __CLASS__, 'get_add_agent' ) );
22 add_action( 'wp_ajax_wpsc_set_add_agent', array( __CLASS__, 'set_add_agent' ) );
23
24 // Get edit agent modal.
25 add_action( 'wp_ajax_wpsc_get_edit_agent', array( __CLASS__, 'get_edit_agent' ) );
26 add_action( 'wp_ajax_wpsc_set_edit_agent', array( __CLASS__, 'set_edit_agent' ) );
27
28 // Get delete agent modal.
29 add_action( 'wp_ajax_wpsc_delete_agent', array( __CLASS__, 'delete_agent' ) );
30
31 // WP user autocomplete.
32 add_action( 'wp_ajax_wpsc_search_wp_users', array( __CLASS__, 'search_wp_users' ) );
33
34 // WP User ADD/Delete.
35 add_action( 'delete_user', array( __CLASS__, 'wp_user_delete' ), 10, 3 );
36 add_action( 'user_register', array( __CLASS__, 'wp_user_register' ), 11, 2 );
37 }
38
39 /**
40 * Get agent list ajax callback function
41 *
42 * @return void
43 */
44 public static function get_agent_list() {
45
46 if ( ! WPSC_Functions::is_site_admin() ) {
47 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
48 }
49
50 $args = array(
51 'items_per_page' => 0,
52 'meta_query' => array(
53 'relation' => 'AND',
54 array(
55 'slug' => 'is_active',
56 'compare' => '=',
57 'val' => 1,
58 ),
59 array(
60 'slug' => 'is_agentgroup',
61 'compare' => '=',
62 'val' => 0,
63 ),
64 ),
65 );
66 $agents = WPSC_Agent::find( $args )['results'];
67 $roles = get_option( 'wpsc-agent-roles', array() );
68 $unique_id = uniqid( 'wpsc_' );?>
69 <div class="wpsc-setting-header">
70 <h2><?php esc_attr_e( 'Agents', 'supportcandy' ); ?></h2>
71 </div>
72
73 <div class="wpsc-setting-section-body">
74 <div class="wpsc-dock-container">
75 <?php
76 printf(
77 /* translators: Click here to see the documentation */
78 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
79 '<a href="https://supportcandy.net/docs/add-new-agent/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
80 );
81 ?>
82 </div>
83 <div class="wpsc-setting-cards-container">
84 <div style="width: 100%;">
85 <table class="agent-role-table wpsc-setting-tbl">
86 <thead>
87 <tr>
88 <th><?php esc_attr_e( 'Agent name', 'supportcandy' ); ?></th>
89 <th><?php esc_attr_e( 'Agent Email', 'supportcandy' ); ?></th>
90 <th><?php esc_attr_e( 'Role', 'supportcandy' ); ?></th>
91 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
92 </tr>
93 </thead>
94 <tbody>
95 <?php
96 if ( $agents ) :
97 foreach ( $agents as $agent ) {
98 ?>
99 <tr>
100 <td><?php echo esc_attr( $agent->name ); ?></td>
101 <td><?php echo esc_attr( $agent->customer->email ); ?></td>
102 <td><?php echo esc_attr( $roles[ $agent->role ]['label'] ); ?></td>
103 <td>
104 <span class="wpsc-link" onclick="wpsc_get_edit_agent(<?php echo esc_attr( $agent->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent' ) ); ?>')"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span> |
105 <span class="wpsc-link" onclick="wpsc_get_delete_agent(<?php echo esc_attr( $agent->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_delete_agent' ) ); ?>' )"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span>
106 </td>
107 </tr>
108 <?php
109 }
110 endif;
111 ?>
112 </tbody>
113 </table>
114 <script>
115 jQuery('table.agent-role-table').DataTable({
116 ordering: false,
117 pageLength: 20,
118 bLengthChange: false,
119 columnDefs: [
120 { targets: -1, searchable: false },
121 { targets: '_all', className: 'dt-left' }
122 ],
123 layout: {
124 topStart: {
125 buttons: [
126 {
127 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
128 className: 'wpsc-button small primary',
129 action: function ( e, dt, node, config ) {
130 wpsc_show_modal();
131 var data = { action: 'wpsc_get_add_agent' };
132 jQuery.post(
133 supportcandy.ajax_url,
134 data,
135 function (response) {
136
137 jQuery( '.wpsc-modal-header' ).text( response.title );
138 jQuery( '.wpsc-modal-body' ).html( response.body );
139 jQuery( '.wpsc-modal-footer' ).html( response.footer );
140 wpsc_show_modal_inner_container();
141 }
142 );
143 }
144 }
145 ]
146 }
147 },
148 language: supportcandy.translations.datatables
149 });
150 </script>
151 </div>
152 </div>
153 </div>
154 <?php
155 wp_die();
156 }
157
158 /**
159 * Add agent modal popup UI
160 *
161 * @return void
162 */
163 public static function get_add_agent() {
164
165 if ( ! WPSC_Functions::is_site_admin() ) {
166 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
167 }
168 $title = esc_attr__( 'Add new', 'supportcandy' );
169 $roles = get_option( 'wpsc-agent-roles', array() );
170 ob_start();
171 ?>
172 <form action="#" onsubmit="return false;" class="wpsc-frm-add-agent">
173 <div class="wpsc-input-group">
174 <div class="label-container">
175 <label for=""><?php esc_attr_e( 'Select users', 'supportcandy' ); ?></label>
176 </div>
177 <select id="wpsc-select-user-input" name="users[]" multiple></select>
178 <script>
179 jQuery('#wpsc-select-user-input').selectWoo({
180 ajax: {
181 url: supportcandy.ajax_url,
182 dataType: 'json',
183 delay: 250,
184 data: function (params) {
185 return {
186 q: params.term, // search term.
187 page: params.page,
188 action: 'wpsc_search_wp_users',
189 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_search_wp_users' ) ); ?>',
190 };
191 },
192 processResults: function (data, params) {
193 var terms = [];
194 if ( data ) {
195 jQuery.each( data, function( id, text ) {
196 terms.push( { id: text.id, text: text.title+' ('+text.email+')' } );
197 });
198 }
199 return {
200 results: terms
201 };
202 },
203 cache: true
204 },
205 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
206 minimumInputLength: 1,
207 allowClear: false,
208 placeholder: ""
209 });
210 </script>
211 </div>
212 <div class="wpsc-input-group">
213 <div class="label-container">
214 <label for=""><?php esc_attr_e( 'Select role', 'supportcandy' ); ?></label>
215 </div>
216 <select name="role">
217 <?php
218 foreach ( $roles as $key => $role ) {
219 ?>
220 <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
221 <?php
222 }
223 ?>
224 </select>
225 </div>
226 <?php do_action( 'wpsc_get_add_agent_body' ); ?>
227 <input type="hidden" name="action" value="wpsc_set_add_agent">
228 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent' ) ); ?>">
229 </form>
230 <?php
231 $body = ob_get_clean();
232
233 ob_start();
234 ?>
235 <button class="wpsc-button small primary" onclick="wpsc_set_add_agent(this);">
236 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
237 </button>
238 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
239 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
240 </button>
241 <?php
242 do_action( 'wpsc_get_add_agent_footer' );
243 $footer = ob_get_clean();
244
245 $response = array(
246 'title' => $title,
247 'body' => $body,
248 'footer' => $footer,
249 );
250 wp_send_json( $response );
251 }
252
253 /**
254 * Create an agent
255 *
256 * @return void
257 */
258 public static function set_add_agent() {
259
260 if ( check_ajax_referer( 'wpsc_set_add_agent', '_ajax_nonce', false ) != 1 ) {
261 wp_send_json_error( 'Unauthorized request!', 401 );
262 }
263
264 if ( ! WPSC_Functions::is_site_admin() ) {
265 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
266 }
267
268 $users = isset( $_POST['users'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['users'] ) ) ) : array();
269 if ( ! $users ) {
270 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
271 }
272
273 $roles = get_option( 'wpsc-agent-roles', array() );
274
275 $role = isset( $_POST['role'] ) ? intval( $_POST['role'] ) : 0;
276 if ( ! $role || ! isset( $roles[ $role ] ) ) {
277 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
278 }
279
280 foreach ( $users as $user_id ) {
281
282 $user = new WP_User( $user_id );
283 if ( ! $user->ID ) {
284 continue;
285 }
286
287 $agent = WPSC_Agent::get_by_user_id( $user->ID );
288
289 // skip if agent is already active.
290 if ( $agent->id && $agent->is_active ) {
291 continue;
292 }
293
294 if ( $agent->id ) {
295
296 $customer = WPSC_Customer::get_by_user_id( $user->ID );
297 $agent->is_active = 1;
298 $agent->role = $role;
299 $agent->customer = $customer->id;
300 $agent->save();
301 $agent->user->add_cap( 'wpsc_agent' );
302
303 } elseif ( ! $agent->id ) {
304
305 $agent = self::insert_new_record( $user, $role );
306 }
307
308 // reset agent counts.
309 $agent->reset_workload();
310 $agent->reset_unresolved_count();
311 }
312
313 do_action( 'after_set_add_agent', $agent );
314
315 wp_die();
316 }
317
318 /**
319 * Edit agent modal popup UI
320 *
321 * @return void
322 */
323 public static function get_edit_agent() {
324
325 if ( check_ajax_referer( 'wpsc_get_edit_agent', '_ajax_nonce', false ) != 1 ) {
326 wp_send_json_error( 'Unauthorized request!', 401 );
327 }
328
329 if ( ! WPSC_Functions::is_site_admin() ) {
330 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
331 }
332
333 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
334 if ( ! $id ) {
335 wp_send_json_error( __( 'Unauthorized access', 'supportcandy' ), 401 );
336 }
337
338 $agent = new WPSC_Agent( $id );
339 if ( ! $agent->id ) {
340 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
341 }
342
343 $title = $agent->name;
344 $roles = get_option( 'wpsc-agent-roles', array() );
345 ob_start();
346 ?>
347 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-agent">
348 <div class="wpsc-input-group">
349 <div class="label-container">
350 <label for=""><?php esc_attr_e( 'Role', 'supportcandy' ); ?></label>
351 </div>
352 <select name="role">
353 <?php
354 foreach ( $roles as $key => $role ) {
355 ?>
356 <option <?php selected( $key, $agent->role ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
357 <?php
358 }
359 ?>
360 </select>
361 </div>
362 <?php do_action( 'wpsc_get_edit_agent_body', $agent ); ?>
363 <input type="hidden" name="action" value="wpsc_set_edit_agent">
364 <input type="hidden" name="id" value="<?php echo esc_attr( $agent->id ); ?>">
365 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent' ) ); ?>">
366 </form>
367 <?php
368 $body = ob_get_clean();
369
370 ob_start();
371 ?>
372 <button class="wpsc-button small primary" onclick="wpsc_set_edit_agent(this);">
373 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
374 </button>
375 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
376 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
377 </button>
378 <?php
379 do_action( 'wpsc_get_edit_agent_footer' );
380 $footer = ob_get_clean();
381
382 $response = array(
383 'title' => $title,
384 'body' => $body,
385 'footer' => $footer,
386 );
387 wp_send_json( $response );
388 }
389
390 /**
391 * Update an agent
392 */
393 public static function set_edit_agent() {
394
395 if ( check_ajax_referer( 'wpsc_set_edit_agent', '_ajax_nonce', false ) != 1 ) {
396 wp_send_json_error( 'Unauthorized request!', 401 );
397 }
398
399 if ( ! WPSC_Functions::is_site_admin() ) {
400 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
401 }
402
403 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
404 if ( ! $id ) {
405 wp_send_json_error( __( 'Unauthorized access', 'supportcandy' ), 401 );
406 }
407
408 $roles = get_option( 'wpsc-agent-roles', array() );
409
410 $role = isset( $_POST['role'] ) ? intval( $_POST['role'] ) : 0;
411 if ( ! $role || ! isset( $roles[ $role ] ) ) {
412 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
413 }
414
415 $agent = new WPSC_Agent( $id );
416 if ( ! $agent->id ) {
417 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
418 }
419
420 if ( $agent->role != $role ) {
421 $agent->role = $role;
422 if ( $agent->has_cap( 'backend-access' ) ) {
423 $agent->user->add_cap( 'wpsc_agent' );
424 } else {
425 $agent->user->remove_cap( 'wpsc_agent' );
426 }
427 }
428
429 $agent = apply_filters( 'wpsc_set_edit_agent', $agent );
430 $agent->save();
431 wp_die();
432 }
433
434 /**
435 * Delete an agent
436 *
437 * @return void
438 */
439 public static function delete_agent() {
440
441 if ( check_ajax_referer( 'wpsc_get_delete_agent', '_ajax_nonce', false ) != 1 ) {
442 wp_send_json_error( 'Unauthorized request!', 401 );
443 }
444
445 if ( ! WPSC_Functions::is_site_admin() ) {
446 wp_send_json_error( 'Unauthorized!', 401 );
447 }
448
449 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
450 if ( ! $id ) {
451 wp_send_json_error( 'Bad Request', 400 );
452 }
453
454 $agent = new WPSC_Agent( $id );
455 if ( ! $agent->id ) {
456 wp_send_json_error( 'Bad Request', 400 );
457 }
458
459 $agent->is_active = 0;
460 $success = $agent->save();
461
462 // Remove agent capability.
463 if ( $agent->has_cap( 'backend-access' ) ) {
464 $agent->user->remove_cap( 'wpsc_agent' );
465 }
466
467 do_action( 'wpsc_delete_agent', $agent );
468
469 wp_die();
470 }
471
472 /**
473 * Search WordPress users for autocomplete purposes
474 *
475 * @return void
476 */
477 public static function search_wp_users() {
478
479 if ( check_ajax_referer( 'wpsc_search_wp_users', '_ajax_nonce', false ) != 1 ) {
480 wp_send_json_error( 'Unauthorized request!', 400 );
481 }
482
483 global $wpdb;
484
485 if ( ! WPSC_Functions::is_site_admin() ) {
486 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
487 }
488 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
489
490 // search in wp user table.
491 $users = ( new WP_User_Query(
492 array(
493 'search' => '*' . esc_attr( $term ) . '*',
494 'search_columns' => array(
495 'user_login',
496 'user_nicename',
497 'user_email',
498 'display_name',
499 ),
500 'number' => 10,
501 )
502 ) )->get_results();
503
504 $response = array();
505 foreach ( $users as $user ) {
506 $response[] = array(
507 'id' => $user->ID,
508 'title' => $user->display_name,
509 'email' => $user->user_email,
510 );
511 }
512 wp_send_json( $response );
513 }
514
515 /**
516 * Create new agent
517 *
518 * @param object $user - user name.
519 * @param integer $role - role id.
520 * @return WPSC_Agent
521 */
522 private static function insert_new_record( $user, $role ) {
523
524 $customer = WPSC_Customer::get_by_user_id( $user->ID );
525
526 // Create agent record.
527 $data = array(
528 'user' => $user->ID,
529 'customer' => $customer->id,
530 'role' => $role,
531 'name' => $user->display_name,
532 'is_active' => 1,
533 );
534 $agent = WPSC_Agent::insert( $data );
535
536 // Give agent capability to this WP user.
537 if ( $agent->has_cap( 'backend-access' ) ) {
538 $user->add_cap( 'wpsc_agent' );
539 }
540
541 // working hrs.
542 $company_whs = WPSC_Working_Hour::get();
543 for ( $i = 1; $i <= 7; $i++ ) {
544 $data = array(
545 'agent' => $agent->id,
546 'day' => $i,
547 'start_time' => $company_whs[ $i ]->start_time,
548 'end_time' => $company_whs[ $i ]->end_time,
549 );
550 WPSC_Working_Hour::insert( $data );
551 }
552
553 return $agent;
554 }
555
556 /**
557 * After user deleted
558 *
559 * @param integer $user_id - user id.
560 * @param integer $reassign - reassign.
561 * @param object $user -user info.
562 * @return void
563 */
564 public static function wp_user_delete( $user_id, $reassign, $user ) {
565
566 global $wpdb;
567 $agent = WPSC_Agent::get_by_user_id( $user_id );
568 if ( ! $agent->id || ! $agent->is_active ) {
569 return;
570 }
571
572 $agent->is_active = 0;
573 $agent->user = 0;
574 $agent->save();
575
576 do_action( 'wpsc_delete_agent', $agent );
577 }
578
579 /**
580 * After new user added
581 *
582 * @param integer $user_id - user id.
583 * @param array $user_data - user data.
584 * @return void
585 */
586 public static function wp_user_register( $user_id, $user_data ) {
587
588 $customer = WPSC_Customer::get_by_email( $user_data['user_email'] );
589 if ( ! $customer->id ) {
590 return;
591 }
592
593 $agent = WPSC_Agent::get_by_customer( $customer );
594 if ( ! $agent->id ) {
595 return;
596 }
597
598 $name = $user_data['first_name'] . ' ' . $user_data['last_name'];
599 $name = trim( $name ) ? $name : $user_data['user_login'];
600
601 $agent->user = $user_id;
602 $agent->name = $name;
603 $agent->save();
604 }
605 }
606 endif;
607
608 WPSC_Agent_Settings::init();
609