| 1 |
<?php |
| 2 |
/** |
| 3 |
* Public AJAX endpoint for resolving Booking Resource selector stages. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Resolve a Booking Resource selection and return the native form stage. |
| 14 |
* |
| 15 |
* The endpoint is read-only. It verifies the public nonce and signed shortcode |
| 16 |
* configuration, then rebuilds the current resource catalogue before render. |
| 17 |
* |
| 18 |
* @return void Terminates with a JSON response. |
| 19 |
*/ |
| 20 |
function wpbc_booking_resource_selector_ajax_resolve() { |
| 21 |
if ( false === check_ajax_referer( 'wpbc_booking_resource_selector_ajax', 'nonce', false ) ) { |
| 22 |
wp_send_json_error( array( 'message' => __( 'Security check failed. Reload the page and try again.', 'booking' ) ), 403 ); |
| 23 |
} |
| 24 |
|
| 25 |
$config_token = isset( $_POST['config_token'] ) && ! is_array( $_POST['config_token'] ) ? sanitize_text_field( wp_unslash( $_POST['config_token'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 26 |
$config = wpbc_booking_resource_selector_decode_config( $config_token ); |
| 27 |
if ( is_wp_error( $config ) ) { |
| 28 |
wp_send_json_error( |
| 29 |
array( |
| 30 |
'message' => $config->get_error_message(), |
| 31 |
'code' => $config->get_error_code(), |
| 32 |
), |
| 33 |
400 |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
$resource_id = isset( $_POST['resource_id'] ) && ! is_array( $_POST['resource_id'] ) ? absint( wp_unslash( $_POST['resource_id'] ) ) : 0; |
| 38 |
$result = wpbc_booking_resource_selector_resolve_stage( $config, $resource_id, 0 < $resource_id ); |
| 39 |
if ( is_wp_error( $result ) ) { |
| 40 |
wp_send_json_error( |
| 41 |
array( |
| 42 |
'message' => $result->get_error_message(), |
| 43 |
'code' => $result->get_error_code(), |
| 44 |
), |
| 45 |
400 |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
wp_send_json_success( $result ); |
| 50 |
} |
| 51 |
add_action( 'wp_ajax_nopriv_WPBC_AJX_BOOKING_RESOURCE_SELECTOR_RESOLVE', 'wpbc_booking_resource_selector_ajax_resolve' ); |
| 52 |
add_action( 'wp_ajax_WPBC_AJX_BOOKING_RESOURCE_SELECTOR_RESOLVE', 'wpbc_booking_resource_selector_ajax_resolve' ); |
| 53 |
|