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 / booking_modes_v3 / booking_modes-toolbar.php

booking_modes-toolbar.php in Booking Calendar 11.9, at includes/booking_modes_v3/booking_modes-toolbar.php

267 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Booking Modes toolbar selector and assets.
4 *
5 * @package Booking Calendar
6 * @since 11.5.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Enqueue the mode selector client on Booking Calendar administration pages.
15 *
16 * @return void
17 */
18 function wpbc_booking_modes_enqueue_toolbar_script() {
19
20 $context = wpbc_booking_modes_get_context();
21
22 if ( ! $context['is_wpbc_page'] || ! wpbc_booking_modes_current_user_can_switch() ) {
23 return;
24 }
25
26 $script_handle = 'wpbc-booking-modes-toolbar';
27 $script_url = trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/booking_modes.js';
28 $selected_mode_id = wpbc_booking_modes_get_selected_mode_id();
29 $selected_mode = wpbc_booking_modes_get_mode( $selected_mode_id );
30 /* translators: %s: Activated Booking Calendar administration mode. */
31 $switch_success_message = is_array( $selected_mode )
32 ? sprintf( __( '%s mode activated.', 'booking' ), $selected_mode['label'] )
33 : __( 'Booking Calendar mode activated.', 'booking' );
34
35 wp_enqueue_script( $script_handle, $script_url, array( 'jquery', 'wpbc_all' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) );
36 wp_localize_script(
37 $script_handle,
38 'wpbc_booking_modes_config',
39 array(
40 'ajax_url' => admin_url( 'admin-ajax.php' ),
41 'action' => 'WPBC_AJX_BOOKING_MODE_SWITCH',
42 'nonce' => wp_create_nonce( 'wpbc_booking_modes_switch_nonce' ),
43 'selected_mode_id' => $selected_mode_id,
44 'quickstart' => function_exists( 'wpbc_booking_modes_run_quickstart' )
45 ? array(
46 'action' => 'WPBC_AJX_BOOKING_MODE_QUICKSTART',
47 'nonce' => wp_create_nonce( 'wpbc_booking_modes_quickstart_nonce' ),
48 )
49 : array(),
50 'i18n' => array(
51 'saving' => __( 'Switching Booking Calendar mode...', 'booking' ),
52 'error' => __( 'The administration mode could not be changed.', 'booking' ),
53 'quickstart_error' => __( 'QuickStart could not be completed.', 'booking' ),
54 'quickstart_confirm' => __( 'QuickStart can create a public booking page and configure mode defaults. Continue?', 'booking' ),
55 'test_page' => __( 'Test booking page', 'booking' ),
56 'switched' => __( 'Booking Calendar mode activated.', 'booking' ),
57 'switch_success' => $switch_success_message,
58 ),
59 )
60 );
61 }
62 add_action( 'wpbc_load_js_on_admin_page', 'wpbc_booking_modes_enqueue_toolbar_script', 30 );
63
64 /**
65 * Enqueue the scoped mode selector stylesheet on Booking Calendar pages.
66 *
67 * @return void
68 */
69 function wpbc_booking_modes_enqueue_toolbar_style() {
70
71 $context = wpbc_booking_modes_get_context();
72
73 if ( ! $context['is_wpbc_page'] || ! wpbc_booking_modes_current_user_can_switch() ) {
74 return;
75 }
76
77 wp_enqueue_style( 'wpbc-booking-modes-toolbar', trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/booking_modes.css', array(), WP_BK_VERSION_NUM );
78 }
79 add_action( 'wpbc_load_css_on_admin_page', 'wpbc_booking_modes_enqueue_toolbar_style', 30 );
80
81 /**
82 * Render the administration mode selector in the shared top toolbar.
83 *
84 * @param string $page_tag Current Booking Calendar page slug.
85 * @param string $active_page_tab Current active tab slug.
86 * @param string $active_page_subtab Current active subtab slug.
87 *
88 * @return void
89 */
90 function wpbc_booking_modes_render_toolbar_selector( $page_tag, $active_page_tab, $active_page_subtab ) {
91
92 unset( $page_tag, $active_page_tab, $active_page_subtab );
93
94 if ( ! wpbc_booking_modes_current_user_can_switch() ) {
95 return;
96 }
97
98 $selected_mode_id = wpbc_booking_modes_get_selected_mode_id();
99 $selected_mode = wpbc_booking_modes_get_mode( $selected_mode_id );
100 $selector_items = array();
101
102 foreach ( wpbc_booking_modes_get_allowed_mode_ids() as $mode_id ) {
103 $mode = wpbc_booking_modes_get_mode( $mode_id );
104
105 if ( ! is_array( $mode ) ) {
106 continue;
107 }
108
109 $item_attributes = array(
110 'class' => 'wpbc_booking_mode_option' . ( $selected_mode_id === $mode_id ? ' is-current' : '' ),
111 'data-mode-id' => $mode_id,
112 );
113
114 if ( $selected_mode_id === $mode_id ) {
115 $item_attributes['aria-current'] = 'true';
116 }
117
118 $selector_items[] = array(
119 'type' => 'link',
120 'title' => $mode['label'],
121 'url' => '#',
122 'attr' => $item_attributes,
123 );
124 }
125
126 if ( empty( $selector_items ) || ! is_array( $selected_mode ) ) {
127 return;
128 }
129
130 wpbc_ui_el__divider_vertical( array( 'class' => 'wpbc_booking_modes_toolbar_divider wpbc_ui_el__vertical_line' ) );
131 wpbc_ui_el__dropdown_menu(
132 array(
133 /* translators: %s: Selected Booking Calendar administration mode. */
134 'title' => sprintf( __( 'Mode: %s', 'booking' ), $selected_mode['label'] ),
135 'has_border' => false,
136 'class' => 'wpbc_booking_modes_toggle',
137 'container_class' => 'wpbc_booking_modes_selector',
138 'attr' => array( 'aria-label' => __( 'Booking Calendar administration mode', 'booking' ) ),
139 'items' => $selector_items,
140 )
141 );
142 }
143 add_action( 'wpbc_ui_el__top_nav__content_start', 'wpbc_booking_modes_render_toolbar_selector', 5, 3 );
144
145 /**
146 * Check whether the QuickStart notice belongs on the current administration route.
147 *
148 * Form Builder has its own focused editing workflow and action toolbar. Keeping
149 * the content-provisioning notice out of that route prevents it from covering
150 * the Builder canvas while leaving QuickStart available on other eligible
151 * Booking Calendar screens.
152 *
153 * @param string $page_tag Current Booking Calendar page slug.
154 * @param string $active_page_tab Current active tab slug.
155 *
156 * @return bool True when the current route may render QuickStart.
157 */
158 function wpbc_booking_modes_is_quickstart_route_allowed( $page_tag, $active_page_tab ) {
159 if ( 'wpbc-setup' === (string) $page_tag ) {
160 return false;
161 }
162
163 return ! ( 'wpbc-settings' === (string) $page_tag && 'builder_booking_form' === (string) $active_page_tab );
164 }
165
166 /**
167 * Render the explicit QuickStart notice in the floating admin-message stack.
168 *
169 * Mode selection remains a presentation-only operation. QuickStart uses a
170 * standard WordPress notice with standard buttons because it can create site
171 * content. Each mode has a separate user-specific dismissal key, so dismissing
172 * one prompt does not suppress the other. Live demos omit these prompts because
173 * their guided sample content is already provisioned separately.
174 *
175 * @param string $page_tag Current Booking Calendar page slug.
176 * @param string $active_page_tab Current active tab slug.
177 * @param string $active_page_subtab Current active subtab slug.
178 *
179 * @return void
180 */
181 function wpbc_booking_modes_render_quickstart_action( $page_tag, $active_page_tab, $active_page_subtab ) {
182
183 if ( ! wpbc_booking_modes_is_quickstart_route_allowed( $page_tag, $active_page_tab ) ) {
184 return;
185 }
186
187 unset( $active_page_subtab );
188
189 if ( function_exists( 'wpbc_is_this_demo' ) && wpbc_is_this_demo() ) {
190 return;
191 }
192
193 $mode_id = wpbc_booking_modes_get_selected_mode_id();
194 $mode = wpbc_booking_modes_get_mode( $mode_id );
195
196 if ( ! is_array( $mode ) || empty( $mode['quickstart_id'] ) || 'classic' === $mode_id ) {
197 return;
198 }
199
200 $notice_id = 'wpbc_booking_modes_quickstart_notice_' . $mode_id;
201
202 if ( function_exists( 'wpbc_is_dismissed_panel_visible' ) && ! wpbc_is_dismissed_panel_visible( $notice_id ) ) {
203 return;
204 }
205
206 $notice_title = 'appointment' === $mode_id
207 ? __( 'Appointment QuickStart', 'booking' )
208 : __( 'Rental QuickStart', 'booking' );
209 $notice_text = 'appointment' === $mode_id
210 ? __( 'Create or reuse the first Service, Provider assignment, suitable Booking Form, and Appointment booking page.', 'booking' )
211 : __( 'Configure date ranges and changeover times, then create or reuse the first Property booking page.', 'booking' );
212
213 if ( ! function_exists( 'wpbc_booking_modes_current_user_can_quickstart' ) || ! wpbc_booking_modes_current_user_can_quickstart() ) {
214 return;
215 }
216
217 $test_url = wpbc_booking_modes_get_quickstart_test_url( $mode_id );
218 ?>
219 <div>
220 <div class="wpbc_alert_message">
221 <div id="<?php echo esc_attr( $notice_id ); ?>" class="wpbc_inner_message notice notice-info wpbc_booking_modes_quickstart_notice" data-wpbc-quickstart-mode="<?php echo esc_attr( $mode_id ); ?>">
222 <?php wpbc_booking_modes_render_quickstart_dismiss_button( $notice_id ); ?>
223 <p><strong><?php echo esc_html( $notice_title ); ?></strong></p>
224 <p><?php echo esc_html( $notice_text ); ?></p>
225 <p class="wpbc_booking_modes_quickstart_actions">
226 <button type="button" class="button button-primary wpbc_booking_modes_quickstart_button" data-mode-id="<?php echo esc_attr( $mode_id ); ?>">
227 <?php esc_html_e( 'Run QuickStart', 'booking' ); ?>
228 </button>
229 <a class="button button-secondary wpbc_booking_modes_test_page<?php echo $test_url ? '' : ' is-hidden'; ?>" href="<?php echo esc_url( $test_url ? $test_url : '#' ); ?>" target="_blank" rel="noopener noreferrer">
230 <?php esc_html_e( 'Test booking page', 'booking' ); ?>
231 </a>
232 </p>
233 </div>
234 </div>
235 </div>
236 <?php
237 }
238
239 /**
240 * Render the existing persistent Booking Calendar dismiss control.
241 *
242 * The shared dismissal API saves an owner-specific user option through its
243 * nonce-protected AJAX endpoint. The screen-reader label keeps the visual
244 * close symbol understandable to assistive technology.
245 *
246 * @param string $notice_id HTML ID and persistent dismissal key.
247 *
248 * @return void
249 */
250 function wpbc_booking_modes_render_quickstart_dismiss_button( $notice_id ) {
251
252 if ( ! function_exists( 'wpbc_is_dismissed' ) ) {
253 return;
254 }
255
256 wpbc_is_dismissed(
257 $notice_id,
258 array(
259 'title' => '<span aria-hidden="true">&times;</span><span class="screen-reader-text">' . esc_html__( 'Dismiss', 'booking' ) . '</span>',
260 'hint' => __( 'Dismiss', 'booking' ),
261 'class' => 'close',
262 'is_apply_in_demo' => true,
263 )
264 );
265 }
266 add_action( 'wpbc_inside_ui__admin_messages', 'wpbc_booking_modes_render_quickstart_action', 10, 3 );
267