PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / page-setup-wizard / step-working-hours / class-wpbc-setup-wizard-working-hours-save-handler.php

class-wpbc-setup-wizard-working-hours-save-handler.php in Booking Calendar 11.9, at includes/page-setup-wizard/step-working-hours/class-wpbc-setup-wizard-working-hours-save-handler.php

164 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Progressive save handler for Setup Wizard Working Hours.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Persist the default weekly Working Time schedule for routes that expose it.
14 */
15 final class WPBC_Setup_Wizard_Working_Hours_Save_Handler implements WPBC_Setup_Wizard_Step_Save_Handler {
16
17 /** @var WPBC_Setup_Wizard_Working_Hours */
18 private $working_hours;
19
20 /**
21 * Build the handler around the Working Hours validator.
22 *
23 * @param WPBC_Setup_Wizard_Working_Hours|null $working_hours Optional domain service for tests.
24 */
25 public function __construct( $working_hours = null ) {
26 $this->working_hours = $working_hours instanceof WPBC_Setup_Wizard_Working_Hours
27 ? $working_hours
28 : new WPBC_Setup_Wizard_Working_Hours();
29 }
30
31 /**
32 * Return the owned step identifier.
33 *
34 * @return string Stable step ID.
35 */
36 public function get_step_id() {
37 return 'working_hours';
38 }
39
40 /**
41 * Return the explicit Working Hours action label.
42 *
43 * @param array<string,mixed> $checkpoint Current checkpoint.
44 * @param array<string,mixed> $step_context Authorized presentation context.
45 *
46 * @return string Translated action label.
47 */
48 public function get_primary_action_label( array $checkpoint, array $step_context = array() ) {
49 unset( $checkpoint, $step_context );
50
51 return __( 'Apply working hours & continue', 'booking' );
52 }
53
54 /**
55 * Save, verify, and compensate the canonical default weekly schedule.
56 *
57 * Resource-specific Working Time overrides are intentionally retained because
58 * the wizard owns only the default schedule exposed by this page.
59 *
60 * @param array<string,mixed> $validated_fields Validated current-step fields.
61 * @param array<string,mixed> $checkpoint Current checkpoint.
62 * @param array<string,mixed> $operation_context Stable operation metadata.
63 *
64 * @return array<string,mixed>|WP_Error Verified result or error.
65 */
66 public function save( array $validated_fields, array $checkpoint, array $operation_context ) {
67 unset( $operation_context );
68
69 if ( ! WPBC_Setup_Wizard_Customer_Journey_Policy::uses_working_hours_configuration( $this->get_customer_journey_id( $checkpoint ) ) ) {
70 return new WP_Error( 'wpbc_setup_wizard_working_hours_journey_invalid', __( 'Working Hours cannot be saved for the selected customer journey.', 'booking' ) );
71 }
72 if ( ! $this->working_hours->current_user_can_manage() ) {
73 return new WP_Error( 'wpbc_setup_wizard_working_hours_forbidden', __( 'You are not allowed to change global Working Hours.', 'booking' ) );
74 }
75 if ( ! function_exists( 'wpbc_working_time__get_settings' ) || ! function_exists( 'wpbc_working_time__update_settings' ) ) {
76 return new WP_Error( 'wpbc_setup_wizard_working_hours_unavailable', __( 'Working Hours storage is not available.', 'booking' ) );
77 }
78
79 $submitted_schedule = isset( $validated_fields['working_hours'] ) ? $validated_fields['working_hours'] : array();
80 $submitted_schedule = $this->working_hours->validate_working_hours( $submitted_schedule, true );
81 if ( is_wp_error( $submitted_schedule ) ) {
82 return $submitted_schedule;
83 }
84
85 $before_settings = wpbc_working_time__get_settings();
86 if ( ! is_array( $before_settings ) ) {
87 return new WP_Error( 'wpbc_setup_wizard_working_hours_read_failed', __( 'Current Working Hours could not be read safely.', 'booking' ) );
88 }
89
90 $next_settings = $before_settings;
91 $next_settings['enabled'] = $submitted_schedule['enabled'];
92 $next_settings['default'] = isset( $next_settings['default'] ) && is_array( $next_settings['default'] ) ? $next_settings['default'] : array();
93 $next_settings['default']['weekdays'] = $submitted_schedule['weekdays'];
94 $expected_settings = function_exists( 'wpbc_working_time__normalize_settings' )
95 ? wpbc_working_time__normalize_settings( $next_settings )
96 : $next_settings;
97
98 $save_result = wpbc_working_time__update_settings( $next_settings );
99 if ( false === $save_result || ! $this->settings_match( $expected_settings, wpbc_working_time__get_settings() ) ) {
100 wpbc_working_time__update_settings( $before_settings );
101 $was_restored = $this->settings_match( $before_settings, wpbc_working_time__get_settings() );
102 $message = $was_restored
103 ? __( 'Working Hours could not be saved. The previous schedule was restored.', 'booking' )
104 : __( 'Working Hours could not be saved or restored completely. Review General Availability before continuing.', 'booking' );
105
106 return new WP_Error( 'wpbc_setup_wizard_working_hours_not_saved', $message );
107 }
108
109 return array(
110 'status' => 'saved',
111 'summary' => 'On' === $submitted_schedule['enabled']
112 ? __( 'Working Hours applied.', 'booking' )
113 : __( 'Working Hours disabled.', 'booking' ),
114 'canonical_fingerprint' => hash( 'sha256', wp_json_encode( $submitted_schedule ) ),
115 'created_ids' => array(),
116 'updated_ids' => array( 'booking_working_time_enabled', 'booking_working_time_rules' ),
117 'warnings' => array(),
118 'writes_canonical_settings' => true,
119 );
120 }
121
122 /**
123 * Compare complete normalized Working Time settings with a canonical readback.
124 *
125 * @param array<string,mixed> $expected_settings Expected normalized settings.
126 * @param mixed $canonical_settings Canonical readback candidate.
127 *
128 * @return bool True when the default schedule and preserved resource overrides match.
129 */
130 private function settings_match( array $expected_settings, $canonical_settings ) {
131 if ( ! is_array( $canonical_settings ) ) {
132 return false;
133 }
134
135 $canonical_settings = function_exists( 'wpbc_working_time__normalize_settings' )
136 ? wpbc_working_time__normalize_settings( $canonical_settings )
137 : $canonical_settings;
138
139 return $expected_settings === $canonical_settings;
140 }
141
142 /**
143 * Read the saved Customer Journey.
144 *
145 * @param array<string,mixed> $checkpoint Current checkpoint.
146 *
147 * @return string Journey ID.
148 */
149 private function get_customer_journey_id( array $checkpoint ) {
150 return isset( $checkpoint['values']['customer_journey']['customer_journey'] )
151 ? sanitize_key( (string) $checkpoint['values']['customer_journey']['customer_journey'] )
152 : '';
153 }
154
155 /**
156 * Working Hours do not complete the route.
157 *
158 * @return bool False.
159 */
160 public function is_terminal() {
161 return false;
162 }
163 }
164