| 1 |
<?php |
| 2 |
/** |
| 3 |
* Progressive save handler for the Setup Wizard Booking Mode page. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Persist Booking Mode through the released owner-scoped Booking Modes API. |
| 14 |
*/ |
| 15 |
final class WPBC_Setup_Wizard_Booking_Mode_Save_Handler implements WPBC_Setup_Wizard_Step_Save_Handler { |
| 16 |
|
| 17 |
/** |
| 18 |
* Return the owned step identifier. |
| 19 |
* |
| 20 |
* @return string Stable step identifier. |
| 21 |
*/ |
| 22 |
public function get_step_id() { |
| 23 |
return 'booking_experience'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Return the scalar-settings save label. |
| 28 |
* |
| 29 |
* @param array<string,mixed> $checkpoint Current checkpoint. |
| 30 |
* @param array<string,mixed> $step_context Authorized presentation context. |
| 31 |
* |
| 32 |
* @return string Translated action label. |
| 33 |
*/ |
| 34 |
public function get_primary_action_label( array $checkpoint, array $step_context = array() ) { |
| 35 |
unset( $checkpoint, $step_context ); |
| 36 |
|
| 37 |
return __( 'Save & continue', 'booking' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Revalidate and save one allowed owner-scoped Booking Mode. |
| 42 |
* |
| 43 |
* @param array<string,mixed> $validated_fields Validated current-step fields. |
| 44 |
* @param array<string,mixed> $checkpoint Current checkpoint. |
| 45 |
* @param array<string,mixed> $operation_context Stable operation metadata. |
| 46 |
* |
| 47 |
* @return array<string,mixed>|WP_Error Verified result or bounded error. |
| 48 |
*/ |
| 49 |
public function save( array $validated_fields, array $checkpoint, array $operation_context ) { |
| 50 |
unset( $checkpoint, $operation_context ); |
| 51 |
|
| 52 |
if ( |
| 53 |
! WPBC_Setup_Wizard_Access::current_user_can_access() |
| 54 |
|| ! function_exists( 'wpbc_booking_modes_current_user_can_switch' ) |
| 55 |
|| ! wpbc_booking_modes_current_user_can_switch() |
| 56 |
) { |
| 57 |
return new WP_Error( 'wpbc_setup_wizard_booking_mode_forbidden', __( 'You are not allowed to change the Booking Calendar administration mode.', 'booking' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( |
| 61 |
! function_exists( 'wpbc_booking_modes_get_allowed_mode_ids' ) |
| 62 |
|| ! function_exists( 'wpbc_booking_modes_get_selected_mode_id' ) |
| 63 |
|| ! function_exists( 'wpbc_booking_modes_set_selected_mode_id' ) |
| 64 |
) { |
| 65 |
return new WP_Error( 'wpbc_setup_wizard_booking_mode_unavailable', __( 'Booking Calendar administration modes are unavailable. Reload the page and try again.', 'booking' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
$mode_id = isset( $validated_fields['booking_experience'] ) && is_scalar( $validated_fields['booking_experience'] ) |
| 69 |
? sanitize_key( (string) $validated_fields['booking_experience'] ) |
| 70 |
: ''; |
| 71 |
$allowed_mode_ids = wpbc_booking_modes_get_allowed_mode_ids(); |
| 72 |
if ( ! is_array( $allowed_mode_ids ) || ! in_array( $mode_id, $allowed_mode_ids, true ) ) { |
| 73 |
return new WP_Error( 'wpbc_setup_wizard_booking_mode_invalid', __( 'The selected Booking Calendar administration mode is not available.', 'booking' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
$previous_mode_id = wpbc_booking_modes_get_selected_mode_id(); |
| 77 |
$updated_ids = array(); |
| 78 |
if ( $mode_id !== $previous_mode_id ) { |
| 79 |
$save_result = wpbc_booking_modes_set_selected_mode_id( $mode_id ); |
| 80 |
if ( is_wp_error( $save_result ) ) { |
| 81 |
return $save_result; |
| 82 |
} |
| 83 |
|
| 84 |
if ( $mode_id !== wpbc_booking_modes_get_selected_mode_id() ) { |
| 85 |
$restored = false; |
| 86 |
if ( in_array( $previous_mode_id, $allowed_mode_ids, true ) ) { |
| 87 |
$restore_result = wpbc_booking_modes_set_selected_mode_id( $previous_mode_id ); |
| 88 |
$restored = ! is_wp_error( $restore_result ) && $previous_mode_id === wpbc_booking_modes_get_selected_mode_id(); |
| 89 |
} |
| 90 |
|
| 91 |
return new WP_Error( |
| 92 |
'wpbc_setup_wizard_booking_mode_not_saved', |
| 93 |
$restored |
| 94 |
? __( 'The Booking Calendar administration mode could not be verified after saving. The previous mode was restored.', 'booking' ) |
| 95 |
: __( 'The Booking Calendar administration mode could not be verified after saving, and the previous mode could not be confirmed. Review Booking Mode before continuing.', 'booking' ) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
$updated_ids[] = 'booking_admin_mode'; |
| 100 |
} |
| 101 |
|
| 102 |
return array( |
| 103 |
'status' => 'saved', |
| 104 |
'summary' => empty( $updated_ids ) |
| 105 |
? __( 'Booking Mode already matched this setup.', 'booking' ) |
| 106 |
: __( 'Booking Mode saved.', 'booking' ), |
| 107 |
'canonical_fingerprint' => hash( 'sha256', wp_json_encode( array( 'booking_experience' => $mode_id ) ) ), |
| 108 |
'created_ids' => array(), |
| 109 |
'updated_ids' => $updated_ids, |
| 110 |
'warnings' => array(), |
| 111 |
'writes_canonical_settings' => true, |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Booking Mode does not complete the route. |
| 117 |
* |
| 118 |
* @return bool False. |
| 119 |
*/ |
| 120 |
public function is_terminal() { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|