| 1 |
<?php |
| 2 |
/** |
| 3 |
* AJAX persistence for Booking Calendar administration modes. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.5.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Validate and persist one owner-scoped mode change. |
| 15 |
* |
| 16 |
* The browser supplies its current same-site URL and fragment as destination |
| 17 |
* hints. The server signs those hints together with the real user, effective |
| 18 |
* owner, site, target mode, and expiry, then returns the intent only after the |
| 19 |
* preference saves. A full administration landing request revalidates the |
| 20 |
* route against normally contributed metadata. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
function wpbc_booking_modes_ajax_switch_mode() { |
| 25 |
|
| 26 |
$mode_id = isset( $_POST['mode_id'] ) && is_scalar( $_POST['mode_id'] ) ? sanitize_key( wp_unslash( $_POST['mode_id'] ) ) : ''; |
| 27 |
$nonce = isset( $_POST['nonce'] ) && is_scalar( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 28 |
$origin_url = isset( $_POST['origin_url'] ) && is_scalar( $_POST['origin_url'] ) ? esc_url_raw( wp_unslash( $_POST['origin_url'] ) ) : ''; |
| 29 |
$origin_fragment = isset( $_POST['fragment'] ) && is_scalar( $_POST['fragment'] ) ? sanitize_text_field( wp_unslash( $_POST['fragment'] ) ) : ''; |
| 30 |
$validation = wpbc_booking_modes_validate_switch_request( $mode_id, $nonce ); |
| 31 |
|
| 32 |
if ( is_wp_error( $validation ) ) { |
| 33 |
wp_send_json_error( |
| 34 |
array( |
| 35 |
'code' => $validation->get_error_code(), |
| 36 |
'message' => $validation->get_error_message(), |
| 37 |
), |
| 38 |
403 |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
if ( '' === $origin_url ) { |
| 43 |
$origin_url = wp_get_referer(); |
| 44 |
} |
| 45 |
$landing_url = WPBC_Booking_Modes_V3_Switch_Intent::create_landing_url( $mode_id, $origin_url, $origin_fragment ); |
| 46 |
if ( is_wp_error( $landing_url ) ) { |
| 47 |
wp_send_json_error( |
| 48 |
array( |
| 49 |
'code' => $landing_url->get_error_code(), |
| 50 |
'message' => $landing_url->get_error_message(), |
| 51 |
), |
| 52 |
500 |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
$save_result = wpbc_booking_modes_set_selected_mode_id( $mode_id ); |
| 57 |
if ( is_wp_error( $save_result ) ) { |
| 58 |
wp_send_json_error( |
| 59 |
array( |
| 60 |
'code' => $save_result->get_error_code(), |
| 61 |
'message' => $save_result->get_error_message(), |
| 62 |
), |
| 63 |
500 |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$mode = wpbc_booking_modes_get_mode( $mode_id ); |
| 68 |
/* translators: %s: Activated Booking Calendar administration mode. */ |
| 69 |
$success_message = sprintf( __( '%s mode activated.', 'booking' ), $mode['label'] ); |
| 70 |
|
| 71 |
wp_send_json_success( |
| 72 |
array( |
| 73 |
'mode_id' => $mode_id, |
| 74 |
'label' => $mode['label'], |
| 75 |
'redirect_url' => $landing_url, |
| 76 |
'message' => $success_message, |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
add_action( 'wp_ajax_WPBC_AJX_BOOKING_MODE_SWITCH', 'wpbc_booking_modes_ajax_switch_mode' ); |
| 81 |
|