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-module.php

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

195 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Reusable module adapter for the Working Hours setup editor.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Expose the Working Hours editor through the common setup-module contract.
14 */
15 final class WPBC_Setup_Wizard_Working_Hours_Module implements WPBC_Setup_Wizard_Step_Module, WPBC_Setup_Wizard_Current_Values_Module {
16
17 /** @var WPBC_Setup_Wizard_Working_Hours */
18 private $working_hours;
19
20 /**
21 * Build the module around the Working Hours read and validation service.
22 *
23 * @param WPBC_Setup_Wizard_Working_Hours|null $working_hours Optional domain service for testing or composition.
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 stable module and step identifier.
33 *
34 * @return string Stable identifier.
35 */
36 public function get_step_id() {
37 return 'working_hours';
38 }
39
40 /**
41 * Return the rail and template metadata contributed by this module.
42 *
43 * @return array{id:string,label:string,template:string,footer_note:string} Step definition.
44 */
45 public function get_step_definition() {
46 return array(
47 'id' => $this->get_step_id(),
48 'label' => __( 'Working hours', 'booking' ),
49 'template' => 'step-working-hours',
50 'footer_note' => __( 'Apply working hours & continue updates the default weekly schedule immediately.', 'booking' ),
51 );
52 }
53
54 /**
55 * Return the absolute server-owned template path.
56 *
57 * @return string Absolute template path.
58 */
59 public function get_template_path() {
60 return __DIR__ . '/template.php';
61 }
62
63 /**
64 * Return the field names accepted by the Working Hours editor.
65 *
66 * @return string[] Field allow-list.
67 */
68 public function get_field_names() {
69 return array( 'working_hours' );
70 }
71
72 /**
73 * Return the fields required when advancing from Working Hours.
74 *
75 * @return string[] Required field identifiers.
76 */
77 public function get_required_fields() {
78 return array( 'working_hours' );
79 }
80
81 /**
82 * Return the authorized canonical schedule or a safe starter proposal.
83 *
84 * @return array<string,mixed> Initial field values.
85 */
86 public function get_initial_values() {
87 return array(
88 'working_hours' => $this->working_hours->get_initial_working_hours(),
89 );
90 }
91
92 /**
93 * Refresh the weekly schedule from canonical Working Time settings.
94 *
95 * @return string[] Current-value field identifiers.
96 */
97 public function get_current_value_field_names() {
98 return $this->get_field_names();
99 }
100
101 /**
102 * Return dependencies used for time labels and journey-owned defaults.
103 *
104 * @return string[] Stable dependency step identifiers.
105 */
106 public function get_context_dependencies() {
107 return array( 'date_time_formats', 'customer_journey' );
108 }
109
110 /**
111 * Build the Working Hours editor template context.
112 *
113 * @param array<string,mixed> $field_values Validated Working Hours field values.
114 * @param array<string,mixed> $consumer_context Read-only values supplied by the consumer shell.
115 *
116 * @return array<string,mixed> Authorized presentation context.
117 */
118 public function get_context( array $field_values, array $consumer_context = array() ) {
119 $time_format = isset( $consumer_context['date_time_formats']['time_format'] ) && is_scalar( $consumer_context['date_time_formats']['time_format'] )
120 ? (string) $consumer_context['date_time_formats']['time_format']
121 : 'H:i';
122 $customer_journey_id = isset( $consumer_context['customer_journey']['customer_journey'] )
123 && is_scalar( $consumer_context['customer_journey']['customer_journey'] )
124 ? sanitize_key( (string) $consumer_context['customer_journey']['customer_journey'] )
125 : '';
126 $has_saved_values = ! empty( $consumer_context['_step_state']['has_saved_values'] );
127 $is_journey_transition = WPBC_Setup_Wizard_Customer_Journey_Policy::has_pending_transition_for_step(
128 isset( $consumer_context['_step_results'] ) && is_array( $consumer_context['_step_results'] )
129 ? $consumer_context['_step_results']
130 : array(),
131 $this->get_step_id()
132 );
133
134 // A new route owns the initial Working Hours status. Ordinary revisits use
135 // the current canonical status supplied through the current-values contract.
136 if ( ! $has_saved_values || $is_journey_transition ) {
137 $field_values['working_hours']['enabled'] = WPBC_Setup_Wizard_Customer_Journey_Policy::uses_disabled_working_hours_default( $customer_journey_id )
138 ? 'Off'
139 : 'On';
140 }
141
142 return $this->working_hours->get_context( $field_values, $time_format );
143 }
144
145 /**
146 * Validate the Working Hours proposal transport field.
147 *
148 * @param string $field_id Stable field identifier.
149 * @param mixed $raw_value Untrusted weekly schedule.
150 * @param bool $is_required Whether at least one interval is required.
151 *
152 * @return array<string,mixed>|WP_Error Normalized schedule or an error.
153 */
154 public function validate_field( $field_id, $raw_value, $is_required ) {
155 if ( 'working_hours' !== $field_id ) {
156 return new WP_Error( 'wpbc_setup_wizard_working_hours_field_unknown', __( 'The Working Hours editor received an unsupported field.', 'booking' ) );
157 }
158
159 return $this->working_hours->validate_working_hours( $raw_value, $is_required );
160 }
161
162 /**
163 * Enqueue compiled Working Hours assets and presentation translations.
164 *
165 * @param string $module_url Absolute URL to the Setup Wizard module root.
166 * @param string|false $asset_version Plugin version used for cache busting.
167 * @param string $shared_style_handle Consumer shell style handle.
168 * @param string $shared_script_handle Consumer shell script handle.
169 *
170 * @return void
171 */
172 public function enqueue_assets( $module_url, $asset_version, $shared_style_handle, $shared_script_handle ) {
173 $module_url = trailingslashit( $module_url );
174
175 wp_enqueue_style( 'wpbc-setup-wizard-working-hours', $module_url . 'step-working-hours/_out/step-working-hours.css', array( $shared_style_handle ), $asset_version );
176 wp_enqueue_script( 'wpbc-setup-wizard-working-hours', $module_url . 'step-working-hours/_out/step-working-hours.js', array( $shared_script_handle ), $asset_version, true );
177 wp_localize_script(
178 'wpbc-setup-wizard-working-hours',
179 'wpbc_setup_wizard_working_hours',
180 array(
181 'i18n' => array(
182 'working_hours_required' => __( 'Enable at least one weekday and add its working hours.', 'booking' ),
183 'working_hours_invalid' => __( 'Every working interval must end after it starts.', 'booking' ),
184 'working_hours_overlap' => __( 'Working intervals on the same day cannot overlap or repeat.', 'booking' ),
185 'working_hours_limit' => __( 'No additional one-hour interval is available for this day.', 'booking' ),
186 'copy_target_required' => __( 'Select at least one target day.', 'booking' ),
187 'schedule_copied' => __( 'The working-hours schedule was copied to the selected days.', 'booking' ),
188 /* translators: %s: Weekday name. */
189 'remove_working_interval' => __( 'Remove %s working interval', 'booking' ),
190 ),
191 )
192 );
193 }
194 }
195