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