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

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