PluginProbe
Booking Calendar / 11.3
Booking Calendar v11.3
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 10.11 All 204 releases
booking / includes / page-form-builder / field-packs / _shared / hint-shortcode-wptpl.php

hint-shortcode-wptpl.php in Booking Calendar 11.3, at includes/page-form-builder/field-packs/_shared/hint-shortcode-wptpl.php

403 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared helpers for generic BFB hint shortcode field packs.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Check whether the configured pack requirement is available.
14 *
15 * @param array $cfg Pack config.
16 *
17 * @return bool
18 */
19 function wpbc_bfb_hint_shortcode_is_supported( $cfg ) {
20
21 return empty( $cfg['required_class'] ) || class_exists( $cfg['required_class'] );
22 }
23
24 /**
25 * Get localized sample values for date/time hint previews.
26 *
27 * These examples intentionally use the same formatting helpers as the booking
28 * form hints, so the Builder library and canvas follow the site's configured
29 * date and time formats.
30 *
31 * @param string $hint_name Hint shortcode token.
32 * @param string $fallback Fallback preview text.
33 *
34 * @return string
35 */
36 function wpbc_bfb_hint_shortcode_preview_value( $hint_name, $fallback = '' ) {
37
38 $site_now_timestamp = function_exists( 'current_time' ) ? current_time( 'timestamp' ) : time();
39 $sample_check_in_time = strtotime( '+1 week', $site_now_timestamp );
40
41 if ( false === $sample_check_in_time ) {
42 return $fallback;
43 }
44
45 $sample_middle_date_time = strtotime( '+1 day', $sample_check_in_time );
46 $sample_check_out_time = strtotime( '+2 days', $sample_check_in_time );
47 $sample_check_out_plusone = strtotime( '+1 day', $sample_check_out_time );
48 $sample_cancel_time = strtotime( '-14 days', $sample_check_in_time );
49 $sample_start = '14:00:01';
50 $sample_end = '12:00:02';
51
52 if ( false === $sample_middle_date_time || false === $sample_check_out_time || false === $sample_check_out_plusone || false === $sample_cancel_time ) {
53 return $fallback;
54 }
55
56 $sample_check_in = gmdate( 'Y-m-d', $sample_check_in_time );
57 $sample_middle_date = gmdate( 'Y-m-d', $sample_middle_date_time );
58 $sample_check_out = gmdate( 'Y-m-d', $sample_check_out_time );
59
60 if (
61 ! function_exists( 'wpbc_get_dates_comma_string_localized' )
62 || ! function_exists( 'wpbc_get_dates_short_format' )
63 || ! function_exists( 'wpbc_time_localized' )
64 ) {
65 return $fallback;
66 }
67
68 $only_full_days = array(
69 $sample_check_in . ' 00:00:00',
70 $sample_middle_date . ' 00:00:00',
71 $sample_check_out . ' 00:00:00',
72 );
73
74 $days_and_times = array(
75 $sample_check_in . ' ' . $sample_start,
76 $sample_middle_date . ' 00:00:00',
77 $sample_check_out . ' ' . $sample_end,
78 );
79
80 switch ( $hint_name ) {
81 case 'cancel_date_hint':
82 return wpbc_get_dates_comma_string_localized( gmdate( 'Y-m-d H:i:s', $sample_cancel_time ) );
83
84 case 'check_in_date_hint':
85 return wpbc_get_dates_comma_string_localized( $only_full_days[0] );
86
87 case 'check_out_date_hint':
88 return wpbc_get_dates_comma_string_localized( $only_full_days[2] );
89
90 case 'check_out_plus1day_hint':
91 return wpbc_get_dates_comma_string_localized( gmdate( 'Y-m-d H:i:s', $sample_check_out_plusone ) );
92
93 case 'pre_checkin_date_hint':
94 $days_before_check_in = 14;
95 if ( function_exists( 'get_bk_option' ) ) {
96 $pre_checkin_days = get_bk_option( 'booking_number_for_pre_checkin_date_hint' );
97 if ( '' !== $pre_checkin_days ) {
98 $days_before_check_in = intval( $pre_checkin_days );
99 }
100 }
101 return wpbc_get_dates_comma_string_localized(
102 gmdate( 'Y-m-d H:i:s', strtotime( '-' . $days_before_check_in . ' days', $sample_check_in_time ) )
103 );
104
105 case 'selected_dates_hint':
106 return wpbc_get_dates_comma_string_localized( implode( ',', $only_full_days ) );
107
108 case 'selected_short_dates_hint':
109 return wpbc_get_dates_short_format( implode( ',', $only_full_days ) );
110
111 case 'selected_timedates_hint':
112 return wpbc_get_dates_comma_string_localized( implode( ',', $days_and_times ) );
113
114 case 'selected_short_timedates_hint':
115 return wpbc_get_dates_short_format( implode( ',', $days_and_times ) );
116
117 case 'start_time_hint':
118 return wpbc_time_localized( $sample_start );
119
120 case 'end_time_hint':
121 return wpbc_time_localized( $sample_end );
122 }
123
124 return $fallback;
125 }
126
127 /**
128 * Register a generic hint shortcode field pack.
129 *
130 * @param array $packs Accumulated packs.
131 * @param array $cfg Pack config.
132 *
133 * @return array
134 */
135 function wpbc_bfb_hint_shortcode_register_pack( $packs, $cfg ) {
136
137 $packs[ $cfg['token'] ] = array(
138 'kind' => 'field',
139 'type' => $cfg['token'],
140 'label' => '',
141 'icon' => $cfg['icon'],
142 'usage_key' => $cfg['token'],
143
144 'schema' => array(
145 'props' => array(
146 'prefix_text' => array( 'type' => 'string', 'default' => $cfg['prefix'] ),
147 'html_id' => array( 'type' => 'string', 'default' => '' ),
148 'cssclass' => array( 'type' => 'string', 'default' => '' ),
149 'help' => array( 'type' => 'string', 'default' => '' ),
150 ),
151 ),
152
153 'templates_printer' => $cfg['templates_printer'],
154 );
155
156 return $packs;
157 }
158
159 /**
160 * Enqueue a generic hint shortcode pack script.
161 *
162 * @param string $page Current admin page slug.
163 * @param array $cfg Pack config.
164 *
165 * @return void
166 */
167 function wpbc_bfb_hint_shortcode_enqueue_js( $page, $cfg ) {
168
169 if ( WPBC_BFB_BUILDER_PAGE_SLUG !== $page ) {
170 return;
171 }
172
173 wp_enqueue_script(
174 'wpbc-bfb_hint_shortcode_wptpl_shared',
175 wpbc_plugin_url( '/includes/page-form-builder/field-packs/_shared/_out/hint-shortcode-wptpl.js' ),
176 array( 'wp-util', 'wpbc-bfb' ),
177 WP_BK_VERSION_NUM,
178 true
179 );
180
181 wp_enqueue_script(
182 $cfg['handle'],
183 wpbc_plugin_url( '/includes/page-form-builder/field-packs/' . $cfg['folder'] . '/_out/' . $cfg['script_file'] ),
184 array( 'wpbc-bfb_hint_shortcode_wptpl_shared' ),
185 WP_BK_VERSION_NUM,
186 true
187 );
188
189 wp_localize_script(
190 $cfg['handle'],
191 $cfg['boot_var'],
192 array(
193 'is_supported' => wpbc_bfb_hint_shortcode_is_supported( $cfg ) ? 1 : 0,
194 'upgrade_text' => $cfg['upgrade_text'],
195 'preview_value' => $cfg['preview_value'],
196 )
197 );
198 }
199
200 /**
201 * Print wp.template() blocks for a generic hint shortcode pack.
202 *
203 * @param string $page Current admin page slug.
204 * @param array $cfg Pack config.
205 *
206 * @return void
207 */
208 function wpbc_bfb_hint_shortcode_print_templates( $page, $cfg ) {
209
210 if ( WPBC_BFB_BUILDER_PAGE_SLUG !== $page ) {
211 return;
212 }
213
214 $token = $cfg['token'];
215 $shortcode_display = $cfg['shortcode_display'];
216 $template_id = 'wpbc-bfb-field-' . $token;
217 $inspector_id = 'wpbc-bfb-inspector-' . $token;
218 $exported = sprintf(
219 /* translators: 1: Field prefix text. 2: Shortcode text. */
220 __( 'Exported output: %1$s <strong>[%2$s]</strong>', 'booking' ),
221 $cfg['prefix'],
222 $shortcode_display
223 );
224
225 ?>
226 <script type="text/html" id="tmpl-<?php echo esc_attr( $template_id ); ?>">
227 <span class="wpbc_bfb__noaction wpbc_bfb__no-drag-zone" inert="">
228 <span class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_line {{ data.cssclass || '' }}"
229 <# if ( data.html_id ) { #> id="{{ data.html_id }}" <# } #>>
230 <# if ( data.prefix_text ) { #>
231 <span class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_prefix">{{ data.prefix_text }}</span>
232 <# } #>
233 <# if ( data.prefix_text ) { #>&nbsp;<# } #>
234 <strong class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_shortcode">{{{ data.preview_value || '<?php echo esc_js( $cfg['preview_value'] ); ?>' }}}</strong>
235 </span>
236
237 <# if ( ! data.is_supported && data.upgrade_text ) { #>
238 <div class="wpbc_bfb__help">
239 {{ data.upgrade_text }}
240 </div>
241 <# } #>
242
243 <# if ( data.help ) { #>
244 <div class="wpbc_bfb__help">{{ data.help }}</div>
245 <# } #>
246 </span>
247 </script>
248
249 <script type="text/html" id="tmpl-<?php echo esc_attr( $inspector_id ); ?>">
250 <div class="wpbc_bfb__inspector__head">
251 <div class="header_container">
252 <div class="header_title_content">
253 <h3 class="title"><?php echo esc_html( $cfg['inspector_title'] ); ?></h3>
254 <div class="desc">
255 <?php echo esc_html( $cfg['description'] ); ?>
256 </div>
257 </div>
258 <div class="actions wpbc_ajx_toolbar wpbc_no_borders">
259 <div class="ui_container ui_container_small">
260 <div class="ui_group">
261 <div class="ui_element">
262 <button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button"
263 data-action="deselect" aria-label="<?php echo esc_attr__( 'Deselect', 'booking' ); ?>">
264 <i class="menu_icon icon-1x wpbc_icn_remove_done"></i>
265 </button>
266 <button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button"
267 data-action="scrollto" aria-label="<?php echo esc_attr__( 'Scroll to field', 'booking' ); ?>">
268 <i class="menu_icon icon-1x wpbc_icn_ads_click filter_center_focus"></i>
269 </button>
270 </div>
271 <div class="ui_element">
272 <button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button"
273 data-action="move-up" aria-label="<?php echo esc_attr__( 'Move up', 'booking' ); ?>">
274 <i class="menu_icon icon-1x wpbc_icn_arrow_upward"></i>
275 </button>
276 <button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button"
277 data-action="move-down" aria-label="<?php echo esc_attr__( 'Move down', 'booking' ); ?>">
278 <i class="menu_icon icon-1x wpbc_icn_arrow_downward"></i>
279 </button>
280 </div>
281 <div class="ui_element">
282 <button data-action="delete"
283 aria-label="<?php echo esc_attr__( 'Delete', 'booking' ); ?>"
284 type="button"
285 class="button button-secondary wpbc_ui_control wpbc_ui_button wpbc_ui_button_danger button-link-delete">
286 <span class="in-button-text"><?php echo esc_html__( 'Delete', 'booking' ); ?></span>&nbsp;&nbsp;
287 <i class="menu_icon icon-1x wpbc_icn_delete_outline"></i>
288 </button>
289 </div>
290 </div>
291 </div>
292 </div>
293 </div>
294 </div>
295
296 <div class="wpbc_bfb__inspector__body">
297 <section class="wpbc_bfb__inspector__group wpbc_ui__collapsible_group is-open" data-group="basic">
298 <button type="button" class="group__header">
299 <h3><?php echo esc_html__( 'Basic', 'booking' ); ?></h3>
300 <i class="wpbc_ui_el__vert_menu_root_section_icon menu_icon icon-1x wpbc-bi-chevron-right"></i>
301 </button>
302 <div class="group__fields">
303 <div class="inspector__row">
304 <label class="inspector__label"><?php echo esc_html__( 'Text before shortcode', 'booking' ); ?></label>
305 <div class="inspector__control">
306 <input type="text"
307 class="inspector__input"
308 data-inspector-key="prefix_text"
309 value="{{ data.prefix_text || '<?php echo esc_js( $cfg['prefix'] ); ?>' }}">
310 </div>
311 </div>
312
313 <div class="inspector__row">
314 <label class="inspector__label"><?php echo esc_html__( 'Help text', 'booking' ); ?></label>
315 <div class="inspector__control">
316 <textarea class="inspector__textarea" rows="3" data-inspector-key="help">{{ data.help || '' }}</textarea>
317 </div>
318 </div>
319 </div>
320 </section>
321
322 <section class="wpbc_bfb__inspector__group wpbc_ui__collapsible_group" data-group="appearance">
323 <button type="button" class="group__header">
324 <h3><?php echo esc_html__( 'Appearance', 'booking' ); ?></h3>
325 <i class="wpbc_ui_el__vert_menu_root_section_icon menu_icon icon-1x wpbc-bi-chevron-right"></i>
326 </button>
327 <div class="group__fields">
328 <div class="inspector__row">
329 <label class="inspector__label"><?php echo esc_html__( 'CSS class', 'booking' ); ?></label>
330 <div class="inspector__control">
331 <input type="text"
332 class="inspector__input"
333 data-inspector-key="cssclass"
334 value="{{ data.cssclass || '' }}">
335 </div>
336 </div>
337
338 <div class="inspector__row">
339 <label class="inspector__label"><?php echo esc_html__( 'HTML ID', 'booking' ); ?></label>
340 <div class="inspector__control">
341 <input type="text"
342 class="inspector__input"
343 data-inspector-key="html_id"
344 value="{{ data.html_id || '' }}">
345 </div>
346 </div>
347
348 <div class="wpbc_bfb__inspector__note">
349 <?php echo esc_html( $exported ); ?>
350 </div>
351
352 <?php if ( ! empty( $cfg['upgrade_text'] ) ) { ?>
353 <div class="wpbc_bfb__inspector__note">
354 <?php echo esc_html( $cfg['upgrade_text'] ); ?>
355 </div>
356 <?php } ?>
357 </div>
358 </section>
359 </div>
360 </script>
361 <?php
362 }
363
364 /**
365 * Print a generic hint shortcode palette item.
366 *
367 * @param string $group Palette group.
368 * @param string $position Position.
369 * @param array $cfg Pack config.
370 *
371 * @return void
372 */
373 function wpbc_bfb_hint_shortcode_palette_item( $group, $position, $cfg ) {
374
375 if ( ! empty( $cfg['group'] ) ) {
376 if ( $cfg['group'] !== $group || 'top' !== $position ) {
377 return;
378 }
379 } else if ( 'hints' !== $group || 'top' !== $position ) {
380 return;
381 }
382
383 ?>
384 <li class="wpbc_bfb__field"
385 data-id="<?php echo esc_attr( $cfg['token'] ); ?>"
386 data-type="<?php echo esc_attr( $cfg['token'] ); ?>"
387 data-usage_key="<?php echo esc_attr( $cfg['token'] ); ?>"
388 data-prefix_text="<?php echo esc_attr( $cfg['prefix'] ); ?>"
389 data-preview_value="<?php echo esc_attr( $cfg['preview_value'] ); ?>"
390 data-help=""
391 data-label="">
392 <i class="menu_icon icon-1x <?php echo esc_attr( $cfg['palette_icon'] ); ?>"></i>
393 <span class="wpbc_bfb__field-label"><?php echo esc_html( $cfg['palette_label'] ); ?></span>
394 <?php
395 if ( ! wpbc_bfb_hint_shortcode_is_supported( $cfg ) && ! empty( $cfg['pro_label'] ) ) {
396 echo '<span class="wpbc_pro_label">' . esc_html( $cfg['pro_label'] ) . '</span>';
397 }
398 ?>
399 <span class="wpbc_bfb__field-type"><?php echo esc_html( $cfg['preview_value'] ); ?></span>
400 </li>
401 <?php
402 }
403