booking
/
includes
/
page-setup-wizard
/
step-days-off
/
class-wpbc-setup-wizard-days-off-save-handler.php
class-wpbc-setup-wizard-days-off-save-handler.php in Booking Calendar 11.9, at includes/page-setup-wizard/step-days-off/class-wpbc-setup-wizard-days-off-save-handler.php
| 1 | <?php |
| 2 | /** |
| 3 | * Progressive save handler for Setup Wizard Days Off. |
| 4 | * |
| 5 | * @package Booking Calendar |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Apply the complete staged Days Off proposal at one explicit save boundary. |
| 14 | */ |
| 15 | final class WPBC_Setup_Wizard_Days_Off_Save_Handler implements WPBC_Setup_Wizard_Step_Save_Handler { |
| 16 | |
| 17 | /** @var WPBC_Setup_Wizard_Days_Off */ |
| 18 | private $days_off; |
| 19 | |
| 20 | /** |
| 21 | * Build the handler around the Availability domain service. |
| 22 | * |
| 23 | * @param WPBC_Setup_Wizard_Days_Off|null $days_off Optional domain service for tests. |
| 24 | */ |
| 25 | public function __construct( $days_off = null ) { |
| 26 | $this->days_off = $days_off instanceof WPBC_Setup_Wizard_Days_Off ? $days_off : new WPBC_Setup_Wizard_Days_Off(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return the owned step identifier. |
| 31 | * |
| 32 | * @return string Stable step ID. |
| 33 | */ |
| 34 | public function get_step_id() { |
| 35 | return 'days_off'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Return the ordinary scalar/domain action label. |
| 40 | * |
| 41 | * @param array<string,mixed> $checkpoint Current checkpoint. |
| 42 | * @param array<string,mixed> $step_context Authorized context. |
| 43 | * |
| 44 | * @return string Translated label. |
| 45 | */ |
| 46 | public function get_primary_action_label( array $checkpoint, array $step_context = array() ) { |
| 47 | unset( $checkpoint, $step_context ); |
| 48 | return __( 'Save & continue', 'booking' ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Save and verify staged unavailable dates. |
| 53 | * |
| 54 | * @param array<string,mixed> $validated_fields Validated fields. |
| 55 | * @param array<string,mixed> $checkpoint Current checkpoint. |
| 56 | * @param array<string,mixed> $operation_context Operation metadata. |
| 57 | * |
| 58 | * @return array<string,mixed>|WP_Error Save result or error. |
| 59 | */ |
| 60 | public function save( array $validated_fields, array $checkpoint, array $operation_context ) { |
| 61 | unset( $checkpoint, $operation_context ); |
| 62 | |
| 63 | $staged = isset( $validated_fields['days_off'] ) ? $validated_fields['days_off'] : array(); |
| 64 | $staged = $this->days_off->validate_staged_values( $staged, true ); |
| 65 | if ( is_wp_error( $staged ) ) { |
| 66 | return $staged; |
| 67 | } |
| 68 | |
| 69 | $can_manage = $this->days_off->current_user_can_manage(); |
| 70 | $saved_state = $this->days_off->save_staged_values( $staged ); |
| 71 | if ( is_wp_error( $saved_state ) ) { |
| 72 | return $saved_state; |
| 73 | } |
| 74 | |
| 75 | $canonical_dates = isset( $saved_state['unavailable_dates'] ) && is_array( $saved_state['unavailable_dates'] ) ? $saved_state['unavailable_dates'] : array(); |
| 76 | return array( |
| 77 | 'status' => 'saved', |
| 78 | 'summary' => $can_manage ? __( 'Days Off saved.', 'booking' ) : __( 'Days Off was skipped because date availability is not editable in this environment.', 'booking' ), |
| 79 | 'canonical_fingerprint' => hash( 'sha256', wp_json_encode( $canonical_dates ) ), |
| 80 | 'created_ids' => array(), |
| 81 | 'updated_ids' => $can_manage ? array( 'booking_date_avalability' ) : array(), |
| 82 | 'warnings' => $can_manage ? array() : array( __( 'Date availability was not changed.', 'booking' ) ), |
| 83 | 'writes_canonical_settings' => $can_manage, |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Days Off is not the terminal route step. |
| 89 | * |
| 90 | * @return bool False. |
| 91 | */ |
| 92 | public function is_terminal() { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 |