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-booking-resources / class-wpbc-setup-wizard-booking-resources-module.php

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

157 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module adapter for Setup Wizard Booking Resources.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Expose Booking Resource creation and editing through the common module contract.
14 */
15 final class WPBC_Setup_Wizard_Booking_Resources_Module implements WPBC_Setup_Wizard_Step_Module {
16
17 /** @var WPBC_Setup_Wizard_Booking_Resources */
18 private $booking_resources;
19
20 /**
21 * Build the adapter around the Resource read and validation service.
22 *
23 * @param WPBC_Setup_Wizard_Booking_Resources|null $booking_resources Optional service for tests.
24 */
25 public function __construct( $booking_resources = null ) {
26 $this->booking_resources = $booking_resources instanceof WPBC_Setup_Wizard_Booking_Resources ? $booking_resources : new WPBC_Setup_Wizard_Booking_Resources();
27 }
28
29 /** @return string Stable step ID. */
30 public function get_step_id() {
31 return 'booking_resources';
32 }
33
34 /** @return array<string,string> Step definition. */
35 public function get_step_definition() {
36 return array(
37 'id' => $this->get_step_id(),
38 'label' => __( 'Booking resources', 'booking' ),
39 'template' => 'step-booking-resources',
40 'footer_note' => __( 'Create or update these Booking Resources when you continue. Existing Resources cannot be deleted here.', 'booking' ),
41 );
42 }
43
44 /** @return string Absolute template path. */
45 public function get_template_path() {
46 return __DIR__ . '/template.php';
47 }
48
49 /** @return string[] Field allow-list. */
50 public function get_field_names() {
51 return array( 'booking_resources', 'existing_booking_resources' );
52 }
53
54 /** @return string[] Required field identifiers. */
55 public function get_required_fields() {
56 return array( 'booking_resources' );
57 }
58
59 /** @return array<string,mixed> Initial values. */
60 public function get_initial_values() {
61 return array(
62 'booking_resources' => $this->booking_resources->get_initial_resource_drafts(),
63 'existing_booking_resources' => array(),
64 );
65 }
66
67 /** @return string[] Context dependency IDs. */
68 public function get_context_dependencies() {
69 return array();
70 }
71
72 /**
73 * Build presentation context and remove proposals already created by a saved result.
74 *
75 * @param array<string,mixed> $field_values Validated fields.
76 * @param array<string,mixed> $consumer_context Read-only step results.
77 *
78 * @return array<string,mixed> Presentation context.
79 */
80 public function get_context( array $field_values, array $consumer_context = array() ) {
81 $result = isset( $consumer_context['_step_results']['booking_resources'] ) && is_array( $consumer_context['_step_results']['booking_resources'] ) ? $consumer_context['_step_results']['booking_resources'] : array();
82 $created_keys = array();
83 foreach ( isset( $result['created_ids'] ) ? (array) $result['created_ids'] : array() as $mapping ) {
84 if ( is_scalar( $mapping ) && preg_match( '/^resource:([a-z0-9_-]{1,64}):[0-9]+$/', (string) $mapping, $matches ) ) {
85 $created_keys[] = $matches[1];
86 }
87 }
88 if ( ! empty( $created_keys ) && isset( $field_values['booking_resources'] ) && is_array( $field_values['booking_resources'] ) ) {
89 $field_values['booking_resources'] = array_values(
90 array_filter(
91 $field_values['booking_resources'],
92 static function ( $resource ) use ( $created_keys ) {
93 return is_array( $resource ) && ! in_array( sanitize_key( isset( $resource['draft_id'] ) ? (string) $resource['draft_id'] : '' ), $created_keys, true );
94 }
95 )
96 );
97 }
98 $field_values['existing_booking_resources'] = array();
99
100 return $this->booking_resources->get_context( $field_values );
101 }
102
103 /**
104 * Validate the staged Resource transport.
105 *
106 * @param string $field_id Stable field ID.
107 * @param mixed $raw_value Untrusted proposals.
108 * @param bool $is_required Whether the field is required.
109 *
110 * @return array<int,array<string,mixed>>|WP_Error Normalized value or error.
111 */
112 public function validate_field( $field_id, $raw_value, $is_required ) {
113 if ( 'existing_booking_resources' === $field_id ) {
114 return $this->booking_resources->validate_existing_resource_updates( $raw_value );
115 }
116 if ( 'booking_resources' !== $field_id ) {
117 return new WP_Error( 'wpbc_setup_wizard_resource_field_unknown', __( 'The Booking Resources editor received an unsupported field.', 'booking' ) );
118 }
119
120 return $this->booking_resources->validate_resource_drafts( $raw_value, $is_required );
121 }
122
123 /**
124 * Enqueue compiled Resource editor assets.
125 *
126 * @param string $module_url Setup Wizard module URL.
127 * @param string|false $asset_version Plugin asset version.
128 * @param string $shared_style_handle Shared shell style handle.
129 * @param string $shared_script_handle Shared shell script handle.
130 *
131 * @return void
132 */
133 public function enqueue_assets( $module_url, $asset_version, $shared_style_handle, $shared_script_handle ) {
134 $module_url = trailingslashit( $module_url );
135 if ( function_exists( 'wpbc_load_js__required_for_media_upload' ) ) {
136 wpbc_load_js__required_for_media_upload();
137 }
138 wp_enqueue_style( 'wpbc-setup-wizard-booking-resources', $module_url . 'step-booking-resources/_out/step-booking-resources.css', array( $shared_style_handle ), $asset_version );
139 wp_enqueue_script( 'wpbc-setup-wizard-booking-resources', $module_url . 'step-booking-resources/_out/step-booking-resources.js', array( 'jquery', $shared_script_handle ), $asset_version, true );
140 wp_localize_script(
141 'wpbc-setup-wizard-booking-resources',
142 'wpbc_setup_wizard_booking_resources',
143 array(
144 'i18n' => array(
145 'new_resource' => __( 'New Booking Resource', 'booking' ),
146 'resource_title_required' => __( 'Enter a name for every Booking Resource.', 'booking' ),
147 'resource_price_invalid' => __( 'Enter a valid non-negative base cost.', 'booking' ),
148 'resource_limit' => __( 'The Booking Resource limit for this account has been reached.', 'booking' ),
149 /* translators: %s: Booking Resource title. */
150 'remove_resource' => __( 'Remove %s', 'booking' ),
151 'resource_removed' => __( 'Unsaved Booking Resource removed from this setup.', 'booking' ),
152 ),
153 )
154 );
155 }
156 }
157