PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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 / settings / class-wpsc-ticket-statuses.php

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

702 lines 21.9 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_Ticket_Statuses' ) ) :
7
8 final class WPSC_Ticket_Statuses {
9
10 /**
11 * Initialize this class
12 */
13 public static function init() {
14
15 // List.
16 add_action( 'wp_ajax_wpsc_get_ticket_statuses', array( __CLASS__, 'get_statuses' ) );
17
18 // Add new.
19 add_action( 'wp_ajax_wpsc_get_add_new_status', array( __CLASS__, 'get_add_new_status' ) );
20 add_action( 'wp_ajax_wpsc_set_add_status', array( __CLASS__, 'set_add_status' ) );
21
22 // Edit.
23 add_action( 'wp_ajax_wpsc_get_edit_status', array( __CLASS__, 'get_edit_status' ) );
24 add_action( 'wp_ajax_wpsc_set_edit_status', array( __CLASS__, 'set_edit_status' ) );
25
26 // Delete.
27 add_action( 'wp_ajax_wpsc_get_delete_status', array( __CLASS__, 'get_delete_status' ) );
28 add_action( 'wp_ajax_wpsc_set_delete_status', array( __CLASS__, 'set_delete_status' ) );
29
30 // Sorting.
31 add_action( 'wp_ajax_wpsc_set_status_load_order', array( __CLASS__, 'set_status_load_order' ) );
32 }
33
34 /**
35 * Get status settings
36 *
37 * @return void
38 */
39 public static function get_statuses() {
40
41 if ( ! WPSC_Functions::is_site_admin() ) {
42 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
43 }
44
45 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
46 $cf = WPSC_Custom_Field::get_cf_by_slug( 'status' );
47 $general_settings = get_option( 'wpsc-gs-general' );
48 $default_statuses = array( 1, 2, 3, 4, $cf->default_value[0], $general_settings['ticket-status-after-customer-reply'], $general_settings['ticket-status-after-agent-reply'], $general_settings['close-ticket-status'] );
49 $default_statuses = array_unique( $default_statuses );?>
50
51 <div class="wpsc-setting-header">
52 <h2><?php esc_attr_e( 'Ticket Statuses', 'supportcandy' ); ?></h2>
53 </div>
54 <div class="wpsc-setting-section-body">
55 <div class="wpsc-dock-container">
56 <?php
57 printf(
58 /* translators: Click here to see the documentation */
59 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
60 '<a href="https://supportcandy.net/docs/ticket-statuses/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
61 );
62 ?>
63 </div>
64 <table class="wpsc-ticket-statuses wpsc-setting-tbl">
65 <thead>
66 <tr>
67 <th style="width: 30px;"><?php esc_attr_e( 'Sort', 'supportcandy' ); ?></th>
68 <th><?php esc_attr_e( 'Name', 'supportcandy' ); ?></th>
69 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
70 </tr>
71 </thead>
72 <tbody>
73 <?php
74 foreach ( $statuses as $status ) {
75 ?>
76 <tr data-id="<?php echo esc_attr( $status->id ); ?>">
77 <td class="sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></td>
78 <td><span class="wpsc-tag" style="color: <?php echo esc_attr( $status->color ); ?>; background-color: <?php echo esc_attr( $status->bg_color ); ?>;" ><?php echo esc_attr( $status->name ); ?></span></td>
79 <td>
80 <div class="actions">
81 <span class="wpsc-link" onclick="wpsc_get_edit_status(<?php echo esc_attr( $status->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_status' ) ); ?>');" ><?php echo esc_attr__( 'Edit', 'supportcandy' ); ?></span>
82 <?php if ( ! in_array( $status->id, $default_statuses ) ) : ?>
83 | <span class="wpsc-link" onclick="wpsc_get_delete_status(<?php echo esc_attr( $status->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_delete_status' ) ); ?>');" ><?php echo esc_attr__( 'Delete', 'supportcandy' ); ?></span>
84 <?php
85 endif;
86 ?>
87 </div>
88 </td>
89 </tr>
90 <?php
91 }
92 ?>
93 </tbody>
94 </table>
95 </div>
96 <script>
97 jQuery('table.wpsc-ticket-statuses').DataTable({
98 ordering: false,
99 autoWidth: false,
100 pageLength: 20,
101 bLengthChange: false,
102 columnDefs: [
103 { targets: 0, width: '30px', searchable: false },
104 { targets: -1, searchable: false },
105 { targets: '_all', className: 'dt-left' }
106 ],
107 layout: {
108 topStart: {
109 buttons: [
110 {
111 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
112 className: 'wpsc-button small primary',
113 action: function ( e, dt, node, config ) {
114 wpsc_show_modal();
115 var data = { action: 'wpsc_get_add_new_status' };
116 jQuery.post(
117 supportcandy.ajax_url,
118 data,
119 function (response) {
120
121 // Set to modal.
122 jQuery( '.wpsc-modal-header' ).text( response.title );
123 jQuery( '.wpsc-modal-body' ).html( response.body );
124 jQuery( '.wpsc-modal-footer' ).html( response.footer );
125 // Display modal.
126 wpsc_show_modal_inner_container();
127 }
128 );
129 }
130 }
131 ],
132 },
133 },
134 language: supportcandy.translations.datatables
135 });
136
137 jQuery(function() {
138 // Enable sorting with jQuery UI.
139 jQuery('table.wpsc-ticket-statuses tbody').sortable({
140 handle: '.sort-handle',
141 helper: function(e, tr) {
142 var $originals = tr.children();
143 var $helper = tr.clone();
144 $helper.children().each(function(index) {
145 jQuery(this).width($originals.eq(index).width());
146 });
147 return $helper;
148 },
149 update: function(event, ui) {
150 var ids = jQuery(this).sortable('toArray', { attribute: 'data-id' });
151 jQuery.post(
152 supportcandy.ajax_url,
153 {
154 action: 'wpsc_set_status_load_order',
155 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_status_load_order' ) ); ?>',
156 ids: ids
157 },
158 function(response) {
159
160 }
161 );
162 }
163 });
164 });
165 </script>
166 <?php
167 wp_die();
168 }
169
170 /**
171 * Get add new status
172 */
173 public static function get_add_new_status() {
174
175 if ( ! WPSC_Functions::is_site_admin() ) {
176 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
177 }
178 $title = esc_attr__( 'Add new', 'supportcandy' );
179
180 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
181 ob_start();
182 ?>
183 <form action="#" onsubmit="return false;" class="wpsc-frm-add-status">
184 <div class="wpsc-input-group">
185 <div class="label-container">
186 <label for=""><?php esc_attr_e( 'Name', 'supportcandy' ); ?></label>
187 <span class="required-char">*</span>
188 </div>
189 <input name="name" type="text" autocomplete="off">
190 </div>
191 <div class="wpsc-input-group">
192 <div class="label-container">
193 <label for=""><?php esc_attr_e( 'Color', 'supportcandy' ); ?></label>
194 </div>
195 <input class="wpsc-color-picker" name="color" value="#ffffff" />
196 </div>
197 <div class="wpsc-input-group">
198 <div class="label-container">
199 <label for=""><?php esc_attr_e( 'Background color', 'supportcandy' ); ?></label>
200 </div>
201 <input class="wpsc-color-picker" name="bg-color" value="#1E90FF" />
202 </div>
203
204 <div data-type="" data-required="true" class="wpsc-input-group load-after">
205 <div class="label-container">
206 <label for="">
207 <?php esc_attr_e( 'Load after', 'supportcandy' ); ?>
208 </label>
209 </div>
210 <select name="load-after" class="load-after">
211 <option value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
212 <?php
213 foreach ( $statuses as $status ) {
214 ?>
215 <option value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option>
216 <?php
217 }
218 ?>
219 <option selected value="__END__">-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
220 </select>
221 <script>jQuery('select.load-after').selectWoo();</script>
222 </div>
223
224
225 <script>jQuery('.wpsc-color-picker').wpColorPicker();</script>
226 <?php do_action( 'wpsc_get_add_status_body' ); ?>
227 <input type="hidden" name="action" value="wpsc_set_add_status">
228 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_status' ) ); ?>">
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_status(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_status' );
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 * Set add new status
255 */
256 public static function set_add_status() {
257
258 if ( check_ajax_referer( 'wpsc_set_add_status', '_ajax_nonce', false ) != 1 ) {
259 wp_send_json_error( 'Unauthorized request!', 401 );
260 }
261
262 if ( ! WPSC_Functions::is_site_admin() ) {
263 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
264 }
265
266 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
267 if ( ! $name ) {
268 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
269 }
270
271 $color = isset( $_POST['color'] ) ? sanitize_text_field( wp_unslash( $_POST['color'] ) ) : '';
272 $color = ( 'transparent' === $color ) ? $color : sanitize_hex_color( $color );
273 if ( ! $color ) {
274 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
275 }
276
277 $bgcolor = isset( $_POST['bg-color'] ) ? sanitize_text_field( wp_unslash( $_POST['bg-color'] ) ) : '';
278 $bgcolor = ( 'transparent' === $bgcolor ) ? $bgcolor : sanitize_hex_color( $bgcolor );
279 if ( ! $bgcolor ) {
280 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
281 }
282
283 $data = array(
284 'name' => $name,
285 'color' => $color,
286 'bg_color' => $bgcolor,
287 );
288 $status = WPSC_Status::insert( $data );
289
290 // set laod order.
291 $statuses_list = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
292 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
293
294 if ( $load_after != '__END__' ) {
295
296 $count = 1;
297
298 if ( $load_after == '__TOP__' ) {
299 $status->load_order = $count++;
300 $status->save();
301 }
302
303 foreach ( $statuses_list as $stat ) {
304
305 if ( $stat->id == $status->id ) {
306 continue;
307 }
308
309 $stat->load_order = $count++;
310 $stat->save();
311
312 if ( $stat->id == $load_after ) {
313 $status->load_order = $count++;
314 $status->save();
315 }
316 }
317 }
318
319 wp_die();
320 }
321
322 /**
323 * Get edit status
324 */
325 public static function get_edit_status() {
326
327 global $wpdb;
328
329 if ( check_ajax_referer( 'wpsc_get_edit_status', '_ajax_nonce', false ) != 1 ) {
330 wp_send_json_error( 'Unauthorized request!', 401 );
331 }
332
333 if ( ! WPSC_Functions::is_site_admin() ) {
334 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
335 }
336
337 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
338 if ( ! $id ) {
339 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
340 }
341
342 $status = new WPSC_Status( $id );
343 if ( ! $status->id ) {
344 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
345 }
346
347 $title = esc_attr( $status->name );
348
349 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
350 ob_start();
351 ?>
352 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-status">
353 <div class="wpsc-input-group">
354 <div class="label-container">
355 <label for=""><?php esc_attr_e( 'Name', 'supportcandy' ); ?></label>
356 <span class="required-char">*</span>
357 </div>
358 <input name="name" type="text" value="<?php echo esc_attr( $status->name ); ?>" autocomplete="off">
359 </div>
360 <div class="wpsc-input-group">
361 <div class="label-container">
362 <label for=""><?php esc_attr_e( 'Color', 'supportcandy' ); ?></label>
363 </div>
364 <input class="wpsc-color-picker" name="color" value="<?php echo esc_attr( $status->color ); ?>" />
365 </div>
366 <div class="wpsc-input-group">
367 <div class="label-container">
368 <label for=""><?php esc_attr_e( 'Background color', 'supportcandy' ); ?></label>
369 </div>
370 <input class="wpsc-color-picker" name="bg-color" value="<?php echo esc_attr( $status->bg_color ); ?>" />
371 </div>
372
373 <div data-type="" data-required="true" class="wpsc-input-group load-after">
374 <div class="label-container">
375 <label for="">
376 <?php esc_attr_e( 'Load after', 'supportcandy' ); ?>
377 </label>
378 </div>
379 <select name="load-after" class="load-after">
380 <option value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
381 <?php
382 $load_after = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}psmsc_statuses WHERE load_order < {$status->load_order} ORDER BY load_order DESC LIMIT 1" );
383 foreach ( $statuses as $stat ) {
384 if ( $status == $stat ) {
385 continue;
386 }
387 ?>
388 <option <?php selected( $load_after, $stat->id ); ?> value="<?php echo esc_attr( $stat->id ); ?>"><?php echo esc_attr( $stat->name ); ?></option>
389 <?php
390 }
391 ?>
392 <option value="__END__">-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
393 </select>
394 <script>jQuery('select.load-after').selectWoo();</script>
395 </div>
396
397
398 <script>jQuery('.wpsc-color-picker').wpColorPicker();</script>
399 <?php do_action( 'wpsc_get_edit_status_body' ); ?>
400 <input type="hidden" name="action" value="wpsc_set_edit_status">
401 <input type="hidden" name= "id" value= "<?php echo esc_attr( $status->id ); ?>">
402 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_status' ) ); ?>">
403 </form>
404 <?php
405 $body = ob_get_clean();
406
407 ob_start();
408 ?>
409 <button class="wpsc-button small primary" onclick="wpsc_set_edit_status(this);">
410 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
411 </button>
412 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
413 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
414 </button>
415 <?php
416 do_action( 'wpsc_get_edit_status' );
417 $footer = ob_get_clean();
418
419 $response = array(
420 'title' => $title,
421 'body' => $body,
422 'footer' => $footer,
423 );
424 wp_send_json( $response );
425 }
426
427 /**
428 * Set edit status
429 */
430 public static function set_edit_status() {
431
432 global $wpdb;
433
434 if ( check_ajax_referer( 'wpsc_set_edit_status', '_ajax_nonce', false ) != 1 ) {
435 wp_send_json_error( 'Unauthorized request!', 401 );
436 }
437
438 if ( ! WPSC_Functions::is_site_admin() ) {
439 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
440 }
441
442 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
443 if ( ! $id ) {
444 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
445 }
446
447 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
448 if ( ! $name ) {
449 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
450 }
451
452 $color = isset( $_POST['color'] ) ? sanitize_text_field( wp_unslash( $_POST['color'] ) ) : '';
453 $color = ( 'transparent' === $color ) ? $color : sanitize_hex_color( $color );
454 if ( ! $color ) {
455 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
456 }
457
458 $bgcolor = isset( $_POST['bg-color'] ) ? sanitize_text_field( wp_unslash( $_POST['bg-color'] ) ) : '';
459 $bgcolor = ( 'transparent' === $bgcolor ) ? $bgcolor : sanitize_hex_color( $bgcolor );
460 if ( ! $bgcolor ) {
461 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
462 }
463
464 $status = new WPSC_Status( $id );
465 if ( ! $status->id ) {
466 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
467 }
468
469 $status->name = $name;
470 $status->color = $color;
471 $status->bg_color = $bgcolor;
472 $status->save();
473
474 // set load order.
475 $statuses_list = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
476 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
477
478 if ( $load_after != '__END__' ) {
479
480 $count = 1;
481
482 if ( $load_after == '__TOP__' ) {
483 $status->load_order = $count++;
484 $status->save();
485 }
486
487 foreach ( $statuses_list as $cat ) {
488
489 if ( $cat->id == $status->id ) {
490 continue;
491 }
492
493 $cat->load_order = $count++;
494 $cat->save();
495
496 if ( $cat->id == $load_after ) {
497 $status->load_order = $count++;
498 $status->save();
499 }
500 }
501 } else {
502
503 $max_load_order = (int) $wpdb->get_var( "SELECT max(load_order) FROM {$wpdb->prefix}psmsc_statuses" );
504 $status->load_order = ++$max_load_order;
505 $status->save();
506 }
507
508 wp_die();
509 }
510
511 /**
512 * Delete status modal
513 *
514 * @return void
515 */
516 public static function get_delete_status() {
517
518 if ( ! WPSC_Functions::is_site_admin() ) {
519 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
520 }
521
522 if ( check_ajax_referer( 'wpsc_get_delete_status', '_ajax_nonce', false ) != 1 ) {
523 wp_send_json_error( 'Unauthorized request!', 401 );
524 }
525
526 $title = esc_attr__( 'Delete status', 'supportcandy' );
527
528 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
529 if ( ! $id ) {
530 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
531 }
532
533 $status = new WPSC_Status( $id );
534 if ( ! $status->id ) {
535 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
536 }
537
538 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
539 ob_start();
540 ?>
541 <form action="#" onsubmit="return false;" class="wpsc-frm-delete-status">
542 <div class="wpsc-input-group">
543 <div class="label-container">
544 <label for=""><?php esc_attr_e( 'Replace with', 'supportcandy' ); ?></label>
545 </div>
546 <select name="replace_id">
547 <?php
548 foreach ( $statuses as $st ) {
549 if ( $st->id == $status->id ) {
550 continue;
551 }
552 ?>
553 <option value="<?php echo esc_attr( $st->id ); ?>"><?php echo esc_attr( $st->name ); ?></option>
554 <?php
555 }
556 ?>
557 </select>
558 </div>
559 <input type="hidden" name="id" value="<?php echo esc_attr( $status->id ); ?>">
560 <input type="hidden" name="action" value="wpsc_set_delete_status">
561 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_delete_status' ) ); ?>">
562 </form>
563 <?php
564 $body = ob_get_clean();
565
566 ob_start();
567 ?>
568 <button class="wpsc-button small primary" onclick="wpsc_set_delete_status(this);">
569 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
570 </button>
571 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
572 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
573 </button>
574 <?php
575 $footer = ob_get_clean();
576
577 $response = array(
578 'title' => $title,
579 'body' => $body,
580 'footer' => $footer,
581 );
582
583 wp_send_json( $response );
584 }
585
586 /**
587 * Delete status
588 */
589 public static function set_delete_status() {
590
591 global $wpdb;
592
593 if ( ! WPSC_Functions::is_site_admin() ) {
594 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
595 }
596
597 if ( check_ajax_referer( 'wpsc_set_delete_status', '_ajax_nonce', false ) != 1 ) {
598 wp_send_json_error( 'Unauthorized request!', 401 );
599 }
600
601 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
602 if ( ! $id ) {
603 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
604 }
605
606 $status = new WPSC_Status( $id );
607 if ( ! $status->id ) {
608 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
609 }
610
611 $replace_id = isset( $_POST['replace_id'] ) ? intval( $_POST['replace_id'] ) : 0;
612 if ( ! $replace_id ) {
613 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
614 }
615
616 $replace = new WPSC_Status( $replace_id );
617 if ( ! $replace->id ) {
618 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
619 }
620
621 $cf = WPSC_Custom_Field::get_cf_by_slug( 'status' );
622 $general_settings = get_option( 'wpsc-gs-general' );
623
624 $statuses = array( 1, 2, 3, 4, $cf->default_value[0], $general_settings['ticket-status-after-customer-reply'], $general_settings['ticket-status-after-agent-reply'], $general_settings['close-ticket-status'] );
625 $statuses = array_unique( $statuses );
626 if ( in_array( $id, $statuses ) ) {
627 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
628 }
629
630 // replace in ticket table.
631 $success = $wpdb->update(
632 $wpdb->prefix . 'psmsc_tickets',
633 array( 'status' => $replace->id ),
634 array( 'status' => $status->id )
635 );
636
637 // replace in logs.
638 $results = $wpdb->get_results(
639 "SELECT * FROM {$wpdb->prefix}psmsc_threads WHERE
640 (type = 'log') AND
641 JSON_VALID(body) AND
642 JSON_EXTRACT(body, '$.slug') = 'status' AND
643 (
644 JSON_UNQUOTE(JSON_EXTRACT(body, '$.new')) = '" . $status->id . "' OR
645 JSON_UNQUOTE(JSON_EXTRACT(body, '$.prev')) = '" . $status->id . "'
646 )"
647 );
648 foreach ( $results as $log ) {
649 $body = json_decode( $log->body );
650 $body->prev = $body->prev == $status->id ? $replace->id : $body->prev;
651 $body->new = $body->new == $status->id ? $replace->id : $body->new;
652 $body = wp_json_encode( $body );
653 $wpdb->update(
654 $wpdb->prefix . 'psmsc_threads',
655 array( 'body' => $body ),
656 array( 'id' => $log->id )
657 );
658 }
659
660 $status->destroy( $status );
661 wp_die();
662 }
663
664 /**
665 * Set load order for ticket statuses
666 *
667 * @return void
668 */
669 public static function set_status_load_order() {
670
671 if ( check_ajax_referer( 'wpsc_set_status_load_order', '_ajax_nonce', false ) != 1 ) {
672 wp_send_json_error( 'Unauthorized request!', 400 );
673 }
674
675 if ( ! WPSC_Functions::is_site_admin() ) {
676 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
677 }
678
679 $ids = isset( $_POST['ids'] ) ? array_map( 'intval', wp_unslash( $_POST['ids'] ) ) : array();
680 if ( ! $ids ) {
681 wp_send_json_error( 'Bad Request', 400 );
682 }
683
684 $count = 1;
685 foreach ( $ids as $id ) {
686
687 $status = new WPSC_Status( $id );
688 if ( ! $status->id ) {
689 continue;
690 }
691
692 $status->load_order = $count++;
693 $status->save();
694 }
695
696 wp_send_json( array( 'success' => true ), 200 );
697 }
698 }
699 endif;
700
701 WPSC_Ticket_Statuses::init();
702