| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reviewed permanent-deletion endpoints for Appointment Services. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Return a signed, non-mutating deletion preview for selected Services. |
| 15 |
* |
| 16 |
* @return void Terminates with JSON. |
| 17 |
*/ |
| 18 |
function wpbc_appointment_services_ajax_delete_preview() { |
| 19 |
wpbc_appointment_services_ajax_authorize(); |
| 20 |
$service_ids = wpbc_appointment_services_catalog_decode_json_field( 'ids' ); |
| 21 |
if ( is_wp_error( $service_ids ) ) { |
| 22 |
wpbc_appointment_services_send_catalog_edit_error( $service_ids ); |
| 23 |
} |
| 24 |
|
| 25 |
$result = ( new WPBC_Appointment_Services_Catalog_Deleter() )->preview( $service_ids ); |
| 26 |
if ( is_wp_error( $result ) ) { |
| 27 |
wpbc_appointment_services_send_catalog_edit_error( $result ); |
| 28 |
} |
| 29 |
|
| 30 |
wp_send_json_success( $result ); |
| 31 |
} |
| 32 |
add_action( 'wp_ajax_WPBC_AJX_APPOINTMENT_SERVICES_DELETE_PREVIEW', 'wpbc_appointment_services_ajax_delete_preview' ); |
| 33 |
|
| 34 |
/** |
| 35 |
* Apply one acknowledged, signed Service deletion after revalidation. |
| 36 |
* |
| 37 |
* @return void Terminates with JSON. |
| 38 |
*/ |
| 39 |
function wpbc_appointment_services_ajax_delete_apply() { |
| 40 |
wpbc_appointment_services_ajax_authorize(); |
| 41 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- The catalog nonce was verified above. |
| 42 |
$acknowledged = isset( $_POST['acknowledged'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['acknowledged'] ) ); |
| 43 |
if ( ! $acknowledged ) { |
| 44 |
wpbc_appointment_services_send_catalog_edit_error( |
| 45 |
new WP_Error( |
| 46 |
'wpbc_service_delete_not_acknowledged', |
| 47 |
__( 'Confirm that the selected Services will be permanently deleted before continuing.', 'booking' ) |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
$plan = wpbc_appointment_services_catalog_decode_json_field( 'plan' ); |
| 53 |
if ( is_wp_error( $plan ) ) { |
| 54 |
wpbc_appointment_services_send_catalog_edit_error( $plan ); |
| 55 |
} |
| 56 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- The catalog nonce was verified above. |
| 57 |
$token = isset( $_POST['token'] ) && is_scalar( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : ''; |
| 58 |
$result = ( new WPBC_Appointment_Services_Catalog_Deleter() )->apply( $plan, $token ); |
| 59 |
if ( is_wp_error( $result ) ) { |
| 60 |
wpbc_appointment_services_send_catalog_edit_error( $result ); |
| 61 |
} |
| 62 |
|
| 63 |
$result['message'] = sprintf( |
| 64 |
/* translators: %s: Number of permanently deleted Services. */ |
| 65 |
_n( '%s Service permanently deleted.', '%s Services permanently deleted.', absint( $result['deleted_count'] ), 'booking' ), |
| 66 |
number_format_i18n( absint( $result['deleted_count'] ) ) |
| 67 |
); |
| 68 |
wp_send_json_success( $result ); |
| 69 |
} |
| 70 |
add_action( 'wp_ajax_WPBC_AJX_APPOINTMENT_SERVICES_DELETE_APPLY', 'wpbc_appointment_services_ajax_delete_apply' ); |
| 71 |
|