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-change-status.php

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

492 lines 18.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_Change_Status' ) ) :
7
8 final class WPSC_ITW_Change_Status {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // get edit ticket status.
18 add_action( 'wp_ajax_wpsc_it_get_edit_ticket_status', array( __CLASS__, 'it_get_edit_ticket_status' ) );
19 add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_ticket_status', array( __CLASS__, 'it_get_edit_ticket_status' ) );
20 add_action( 'wp_ajax_wpsc_it_set_edit_ticket_status', array( __CLASS__, 'it_set_edit_ticket_status' ) );
21 add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_ticket_status', array( __CLASS__, 'it_set_edit_ticket_status' ) );
22
23 // ticket status widget.
24 add_action( 'wp_ajax_wpsc_get_tw_ticket_status', array( __CLASS__, 'get_tw_ticket_status' ) );
25 add_action( 'wp_ajax_wpsc_set_tw_ticket_status', array( __CLASS__, 'set_tw_ticket_status' ) );
26 }
27
28 /**
29 * Prints body of current widget
30 *
31 * @param WPSC_Ticket $ticket - ticket object.
32 * @param array $settings - widget settings.
33 * @return void
34 */
35 public static function print_widget( $ticket, $settings ) {
36
37 $current_user = WPSC_Current_User::$current_user;
38 if ( ! (
39 (
40 (
41 WPSC_Individual_Ticket::$view_profile == 'customer' ||
42 $ticket->customer->id == $current_user->customer->id
43 ) &&
44 $settings['allow-customer']
45 ) ||
46 ( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
47 ) ) {
48 return;
49 }
50
51 $show_edit = (
52 $ticket->is_active &&
53 WPSC_Individual_Ticket::$view_profile == 'agent' &&
54 WPSC_Individual_Ticket::has_ticket_cap( 'cs' ) &&
55 ! WPSC_Individual_Ticket::$is_restricted
56 );
57 self::render_widget( $ticket, $settings, $show_edit, false );
58 }
59
60 /**
61 * Prints body of current widget
62 *
63 * @param WPSC_Ticket $ticket - ticket object.
64 * @param array $settings - widget settings.
65 * @return void
66 */
67 public static function print_archive_widget( $ticket, $settings ) {
68
69 $current_user = WPSC_Current_User::$current_user;
70 if ( ! (
71 (
72 (
73 WPSC_Individual_Archive_Ticket::$view_profile == 'customer' ||
74 $ticket->customer->id == $current_user->customer->id
75 ) &&
76 $settings['allow-customer']
77 ) ||
78 ( WPSC_Individual_Archive_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
79 ) ) {
80 return;
81 }
82 self::render_widget( $ticket, $settings, false, true );
83 }
84
85 /**
86 * Render widget
87 *
88 * @param WPSC_Ticket|WPSC_Archive_Ticket $ticket - ticket object.
89 * @param array $settings - widget settings.
90 * @param bool $show_edit - show edit button.
91 * @param bool $is_archive - is archive ticket.
92 * @return void
93 */
94 private static function render_widget( $ticket, $settings, $show_edit, $is_archive ) {
95 $status = WPSC_Custom_Field::get_cf_by_slug( 'status' );
96 $category = WPSC_Custom_Field::get_cf_by_slug( 'category' );
97 $priority = WPSC_Custom_Field::get_cf_by_slug( 'priority' );
98
99 $title = $settings['title']
100 ? WPSC_Translations::get( 'wpsc-twt-change-status', stripslashes( $settings['title'] ) )
101 : stripslashes( $settings['title'] );
102
103 $open_ticket_class = $is_archive ? 'WPSC_Individual_Archive_Ticket' : 'WPSC_Individual_Ticket';
104 ?>
105 <div class="wpsc-it-widget wpsc-itw-ticket-status">
106 <div class="wpsc-widget-header">
107 <h2><?php echo esc_attr( $title ); ?></h2>
108 <?php
109 if ( $show_edit ) :
110 ?>
111 <span onclick="wpsc_it_get_edit_ticket_status(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_ticket_status' ) ); ?>' )"><?php WPSC_Icons::get( 'edit' ); ?></span>
112 <?php
113 endif;
114 ?>
115 <span class="wpsc-itw-toggle" data-widget="wpsc-itw-ticket-status"><?php WPSC_Icons::get( 'chevron-up' ); ?></span>
116 </div>
117 <div class="wpsc-widget-body">
118 <div class="info-list-item">
119 <div class="info-label"><?php echo esc_attr( $status->name ); ?>:</div>
120 <div class="info-val"><?php $status->type::print_widget_ticket_field_val( $category, $ticket ); ?></div>
121 </div>
122 <div class="info-list-item">
123 <div class="info-label"><?php echo esc_attr( $category->name ); ?>:</div>
124 <div class="info-val"><?php $category->type::print_widget_ticket_field_val( $category, $ticket ); ?></div>
125 </div>
126 <?php
127 if (
128 $open_ticket_class::$view_profile == 'agent' ||
129 ( $open_ticket_class::$view_profile == 'customer' && $settings['show-priority-to-customer'] )
130 ) :
131 ?>
132 <div class="info-list-item">
133 <div class="info-label"><?php echo esc_attr( $priority->name ); ?>:</div>
134 <div class="info-val"><?php $priority->type::print_widget_ticket_field_val( $priority, $ticket ); ?></div>
135 </div>
136 <?php
137 endif;
138 do_action( 'wpsc_itw_change_status', $ticket )
139 ?>
140 </div>
141 </div>
142 <?php
143 }
144
145 /**
146 * Get edit ticket status,category and priority
147 *
148 * @return void
149 */
150 public static function it_get_edit_ticket_status() {
151
152 if ( check_ajax_referer( 'wpsc_it_get_edit_ticket_status', '_ajax_nonce', false ) != 1 ) {
153 wp_send_json_error( 'Unauthorised request!', 401 );
154 }
155
156 WPSC_Individual_Ticket::load_current_ticket();
157 if ( WPSC_Individual_Ticket::$is_restricted ) {
158 wp_send_json_error( 'Unauthorised request!', 401 );
159 }
160
161 $current_user = WPSC_Current_User::$current_user;
162 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'cs' ) ) ) {
163 wp_send_json_error( 'Something went wrong!', 400 );
164 }
165
166 $ticket = WPSC_Individual_Ticket::$ticket;
167
168 $widgets = get_option( 'wpsc-ticket-widget' );
169 $gs = get_option( 'wpsc-gs-general' );
170 $tl_advanced = get_option( 'wpsc-tl-ms-advanced' );
171 $title = $widgets['change-status']['title'];
172
173 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
174 $categories = WPSC_Category::find( array( 'items_per_page' => 0 ) )['results'];
175 $priorities = WPSC_Priority::find( array( 'items_per_page' => 0 ) )['results'];
176
177 $close_flag = in_array( $current_user->agent->role, $gs['allow-close-ticket'] ) ? true : false;
178
179 $allow_status = true;
180 if (
181 ( $ticket->status->id == $gs['close-ticket-status'] || in_array( $ticket->status->id, $tl_advanced['closed-ticket-statuses'] ) ) &&
182 ! $close_flag
183 ) {
184 $allow_status = false;
185 }
186 ob_start();
187 ?>
188 <form action="#" onsubmit="return false;" class="frm-edit-ticket-status">
189 <?php
190 if ( $allow_status ) {
191 ?>
192 <div class="wpsc-input-group">
193 <?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'status' ); ?>
194 <div class="label-container">
195 <label for=""><?php echo esc_attr( $cf->name ); ?></label>
196 </div>
197 <select id="wpsc-select-it-ticket-status" name="status_id">
198 <option value=""></option>
199 <?php
200 foreach ( $statuses as $status ) :
201 if (
202 ( $status->id == $gs['close-ticket-status'] || in_array( $status->id, $tl_advanced['closed-ticket-statuses'] ) ) &&
203 ! $close_flag
204 ) {
205 continue;
206 }
207 ?>
208 <option value="<?php echo esc_attr( $status->id ); ?>" <?php selected( $ticket->status->id, $status->id ); ?>><?php echo esc_attr( $status->name ); ?></option>
209 <?php
210 endforeach;
211 ?>
212 </select>
213 <script>
214 jQuery('#wpsc-select-it-ticket-status').selectWoo({
215 allowClear: true,
216 placeholder: ""
217 });
218 </script>
219 </div>
220 <?php
221 }
222 ?>
223 <div class="wpsc-input-group">
224 <?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'category' ); ?>
225 <div class="label-container">
226 <label for=""><?php echo esc_attr( $cf->name ); ?></label>
227 </div>
228 <select id="wpsc-select-it-ticket-category" name="cat_id">
229 <?php
230 foreach ( $categories as $category ) :
231 ?>
232 <option value="<?php echo esc_attr( $category->id ); ?>" <?php selected( $ticket->category->id, $category->id ); ?>><?php echo esc_attr( $category->name ); ?></option>
233 <?php
234 endforeach;
235 ?>
236 </select>
237 <script>
238 jQuery('#wpsc-select-it-ticket-category').selectWoo({
239 allowClear: true,
240 placeholder: ""
241 });
242 </script>
243 </div>
244 <div class="wpsc-input-group">
245 <?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'priority' ); ?>
246 <div class="label-container">
247 <label for=""><?php echo esc_attr( $cf->name ); ?></label>
248 </div>
249 <select id="wpsc-select-it-ticket-priority" name="priority_id">
250 <?php
251 foreach ( $priorities as $priority ) :
252 ?>
253 <option value="<?php echo esc_attr( $priority->id ); ?>" <?php selected( $ticket->priority->id, $priority->id ); ?>><?php echo esc_attr( $priority->name ); ?></option>
254 <?php
255 endforeach;
256 ?>
257 </select>
258 <script>
259 jQuery('#wpsc-select-it-ticket-priority').selectWoo({
260 allowClear: true,
261 placeholder: ""
262 });
263 </script>
264 </div>
265 <?php do_action( 'wpsc_get_tw_ticket_status_body', $ticket ); ?>
266 <input type="hidden" name="action" value="wpsc_it_set_edit_ticket_status">
267 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
268 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_ticket_status' ) ); ?>">
269 </form>
270 <?php
271 do_action( 'wpsc_get_edit_status_footer', $ticket );
272 $body = ob_get_clean();
273
274 ob_start();
275 ?>
276 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_ticket_status(this, <?php echo esc_attr( $ticket->id ); ?>);">
277 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
278 </button>
279 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
280 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
281 </button>
282 <?php
283 $footer = ob_get_clean();
284
285 $response = array(
286 'title' => $title,
287 'body' => $body,
288 'footer' => $footer,
289 );
290 wp_send_json( $response );
291 }
292
293 /**
294 * Set change status/category/priorities
295 *
296 * @return void
297 */
298 public static function it_set_edit_ticket_status() {
299
300 if ( check_ajax_referer( 'wpsc_it_set_edit_ticket_status', '_ajax_nonce', false ) != 1 ) {
301 wp_send_json_error( 'Unauthorized request!', 401 );
302 }
303
304 WPSC_Individual_Ticket::load_current_ticket();
305 if ( WPSC_Individual_Ticket::$is_restricted ) {
306 wp_send_json_error( 'Unauthorised request!', 401 );
307 }
308
309 $current_user = WPSC_Current_User::$current_user;
310 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'cs' ) ) ) {
311 wp_send_json_error( 'Something went wrong!', 400 );
312 }
313
314 $ticket = WPSC_Individual_Ticket::$ticket;
315 if ( ! $ticket->is_active ) {
316 wp_send_json_error( 'Something went wrong!', 400 );
317 }
318
319 $status_id = isset( $_POST['status_id'] ) && intval( $_POST['status_id'] ) ? intval( $_POST['status_id'] ) : $ticket->status->id;
320 $cat_id = isset( $_POST['cat_id'] ) && intval( $_POST['cat_id'] ) ? intval( $_POST['cat_id'] ) : $ticket->category->id;
321 $priority_id = isset( $_POST['priority_id'] ) && intval( $_POST['priority_id'] ) ? intval( $_POST['priority_id'] ) : $ticket->priority->id;
322
323 $gs = get_option( 'wpsc-gs-general' );
324 $tl_advanced = get_option( 'wpsc-tl-ms-advanced' );
325 $close_flag = in_array( $current_user->agent->role, $gs['allow-close-ticket'] ) ? true : false;
326
327 // Check status.
328 if (
329 $ticket->status->id != $status_id ||
330 (
331 ( $status_id == $gs['close-ticket-status'] || in_array( $status_id, $tl_advanced['closed-ticket-statuses'] ) ) &&
332 $close_flag && $ticket->status->id != $status_id
333 )
334 ) {
335 WPSC_Individual_Ticket::change_status( $ticket->status->id, $status_id, $current_user->customer->id );
336 }
337
338 // Check category.
339 if ( $ticket->category->id != $cat_id ) {
340 WPSC_Individual_Ticket::change_category( $ticket->category->id, $cat_id, $current_user->customer->id );
341 }
342
343 // Check priority.
344 if ( $ticket->priority->id != $priority_id ) {
345 WPSC_Individual_Ticket::change_priority( $ticket->priority->id, $priority_id, $current_user->customer->id );
346 }
347
348 do_action( 'wpsc_change_status', WPSC_Individual_Ticket::$ticket );
349
350 wp_die();
351 }
352
353 /**
354 * Get Ticket Status
355 */
356 public static function get_tw_ticket_status() {
357
358 if ( ! WPSC_Functions::is_site_admin() ) {
359 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
360 }
361
362 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
363 $change_status = $ticket_widgets['change-status'];
364 $title = $change_status['title'];
365 $roles = get_option( 'wpsc-agent-roles', array() );
366 ob_start();
367 ?>
368
369 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-ticket-status">
370 <div class="wpsc-input-group">
371 <div class="label-container">
372 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
373 </div>
374 <input name="label" type="text" value="<?php echo esc_attr( $change_status['title'] ); ?>" autocomplete="off">
375 </div>
376 <div class="wpsc-input-group">
377 <div class="label-container">
378 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
379 </div>
380 <select name="is_enable">
381 <option <?php selected( $change_status['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
382 <option <?php selected( $change_status['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
383 </select>
384 </div>
385 <div class="wpsc-input-group">
386 <div class="label-container">
387 <label for=""><?php esc_attr_e( 'Allowed for customer', 'supportcandy' ); ?></label>
388 </div>
389 <select id="allow-customer" name="allow-customer">
390 <option <?php selected( $change_status['allow-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
391 <option <?php selected( $change_status['allow-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
392 </select>
393 </div>
394 <div class="wpsc-input-group">
395 <div class="label-container">
396 <label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label>
397 </div>
398 <select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent...">
399 <?php
400 foreach ( $roles as $key => $role ) :
401 $selected = in_array( $key, $change_status['allowed-agent-roles'] ) ? 'selected="selected"' : ''
402 ?>
403 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
404 <?php
405 endforeach;
406 ?>
407 </select>
408 </div>
409 <script>
410 jQuery('#wpsc-select-agents').selectWoo({
411 allowClear: false,
412 placeholder: ""
413 });
414 </script>
415 <div class="wpsc-input-group">
416 <div class="label-container">
417 <label for=""><?php esc_attr_e( 'Show priority to customer', 'supportcandy' ); ?></label>
418 </div>
419 <select id="show-priority-to-customer" name="show-priority-to-customer">
420 <option <?php selected( $change_status['show-priority-to-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
421 <option <?php selected( $change_status['show-priority-to-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
422 </select>
423 </div>
424 <?php do_action( 'wpsc_get_ticket_status_body' ); ?>
425 <input type="hidden" name="action" value="wpsc_set_tw_ticket_status">
426 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_ticket_status' ) ); ?>">
427 </form>
428 <?php
429 $body = ob_get_clean();
430
431 ob_start();
432 ?>
433 <button class="wpsc-button small primary" onclick="wpsc_set_tw_ticket_status(this);">
434 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
435 </button>
436 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
437 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
438 </button>
439 <?php
440 do_action( 'wpsc_get_tw_ticket_status_widget_footer' );
441 $footer = ob_get_clean();
442
443 $response = array(
444 'title' => $title,
445 'body' => $body,
446 'footer' => $footer,
447 );
448 wp_send_json( $response );
449 }
450
451 /**
452 * Set Ticket Status Widget
453 */
454 public static function set_tw_ticket_status() {
455
456 if ( check_ajax_referer( 'wpsc_set_tw_ticket_status', '_ajax_nonce', false ) != 1 ) {
457 wp_send_json_error( 'Unauthorized request!', 401 );
458 }
459
460 if ( ! WPSC_Functions::is_site_admin() ) {
461 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
462 }
463
464 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
465 if ( ! $label ) {
466 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
467 }
468
469 $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
470 $allow_for_customer = isset( $_POST['allow-customer'] ) ? intval( $_POST['allow-customer'] ) : 0;
471 $agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array();
472 $show_priority_to_customer = isset( $_POST['show-priority-to-customer'] ) ? intval( $_POST['show-priority-to-customer'] ) : 0;
473
474 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
475
476 $ticket_widgets['change-status']['title'] = $label;
477 $ticket_widgets['change-status']['is_enable'] = $is_enable;
478 $ticket_widgets['change-status']['allow-customer'] = $allow_for_customer;
479 $ticket_widgets['change-status']['allowed-agent-roles'] = $agents;
480 $ticket_widgets['change-status']['show-priority-to-customer'] = $show_priority_to_customer;
481 update_option( 'wpsc-ticket-widget', $ticket_widgets );
482
483 // remove string translations.
484 WPSC_Translations::remove( 'wpsc-twt-change-status' );
485 WPSC_Translations::add( 'wpsc-twt-change-status', stripslashes( $label ) );
486 wp_die();
487 }
488 }
489 endif;
490
491 WPSC_ITW_Change_Status::init();
492