| 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 |
|
| 219 |
?> |
| 220 |
<script type="text/html" id="tmpl-<?php echo esc_attr( $template_id ); ?>"> |
| 221 |
<span class="wpbc_bfb__noaction wpbc_bfb__no-drag-zone" inert=""> |
| 222 |
<span class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_line {{ data.cssclass || '' }}" |
| 223 |
<# if ( data.html_id ) { #> id="{{ data.html_id }}" <# } #>> |
| 224 |
<# if ( data.prefix_text ) { #> |
| 225 |
<span class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_prefix">{{ data.prefix_text }}</span> |
| 226 |
<# } #> |
| 227 |
<# if ( data.prefix_text ) { #> <# } #> |
| 228 |
<strong class="wpbc_bfb__<?php echo esc_attr( $token ); ?>_shortcode">{{{ data.preview_value || '<?php echo esc_js( $cfg['preview_value'] ); ?>' }}}</strong> |
| 229 |
</span> |
| 230 |
|
| 231 |
<# if ( ! data.is_supported && data.upgrade_text ) { #> |
| 232 |
<div class="wpbc_bfb__help"> |
| 233 |
{{ data.upgrade_text }} |
| 234 |
</div> |
| 235 |
<# } #> |
| 236 |
|
| 237 |
<# if ( data.help ) { #> |
| 238 |
<div class="wpbc_bfb__help">{{ data.help }}</div> |
| 239 |
<# } #> |
| 240 |
</span> |
| 241 |
</script> |
| 242 |
|
| 243 |
<script type="text/html" id="tmpl-<?php echo esc_attr( $inspector_id ); ?>"> |
| 244 |
<div class="wpbc_bfb__inspector__head"> |
| 245 |
<div class="header_container"> |
| 246 |
<div class="header_title_content"> |
| 247 |
<h3 class="title"><?php echo esc_html( $cfg['inspector_title'] ); ?></h3> |
| 248 |
<div class="desc"> |
| 249 |
<?php echo esc_html( $cfg['description'] ); ?> |
| 250 |
</div> |
| 251 |
</div> |
| 252 |
<div class="actions wpbc_ajx_toolbar wpbc_no_borders"> |
| 253 |
<div class="ui_container ui_container_small"> |
| 254 |
<div class="ui_group"> |
| 255 |
<div class="ui_element"> |
| 256 |
<button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button" |
| 257 |
data-action="deselect" aria-label="<?php echo esc_attr__( 'Deselect', 'booking' ); ?>"> |
| 258 |
<i class="menu_icon icon-1x wpbc_icn_remove_done"></i> |
| 259 |
</button> |
| 260 |
<button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button" |
| 261 |
data-action="scrollto" aria-label="<?php echo esc_attr__( 'Scroll to field', 'booking' ); ?>"> |
| 262 |
<i class="menu_icon icon-1x wpbc_icn_ads_click filter_center_focus"></i> |
| 263 |
</button> |
| 264 |
</div> |
| 265 |
<div class="ui_element"> |
| 266 |
<button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button" |
| 267 |
data-action="move-up" aria-label="<?php echo esc_attr__( 'Move up', 'booking' ); ?>"> |
| 268 |
<i class="menu_icon icon-1x wpbc_icn_arrow_upward"></i> |
| 269 |
</button> |
| 270 |
<button type="button" class="button button-secondary wpbc_ui_control wpbc_ui_button" |
| 271 |
data-action="move-down" aria-label="<?php echo esc_attr__( 'Move down', 'booking' ); ?>"> |
| 272 |
<i class="menu_icon icon-1x wpbc_icn_arrow_downward"></i> |
| 273 |
</button> |
| 274 |
</div> |
| 275 |
<div class="ui_element"> |
| 276 |
<button data-action="delete" |
| 277 |
aria-label="<?php echo esc_attr__( 'Delete', 'booking' ); ?>" |
| 278 |
type="button" |
| 279 |
class="button button-secondary wpbc_ui_control wpbc_ui_button wpbc_ui_button_danger button-link-delete"> |
| 280 |
<span class="in-button-text"><?php echo esc_html__( 'Delete', 'booking' ); ?></span> |
| 281 |
<i class="menu_icon icon-1x wpbc_icn_delete_outline"></i> |
| 282 |
</button> |
| 283 |
</div> |
| 284 |
</div> |
| 285 |
</div> |
| 286 |
</div> |
| 287 |
</div> |
| 288 |
</div> |
| 289 |
|
| 290 |
<div class="wpbc_bfb__inspector__body"> |
| 291 |
<section class="wpbc_bfb__inspector__group wpbc_ui__collapsible_group is-open" data-group="basic"> |
| 292 |
<button type="button" class="group__header"> |
| 293 |
<h3><?php echo esc_html__( 'Basic', 'booking' ); ?></h3> |
| 294 |
<i class="wpbc_ui_el__vert_menu_root_section_icon menu_icon icon-1x wpbc-bi-chevron-right"></i> |
| 295 |
</button> |
| 296 |
<div class="group__fields"> |
| 297 |
<div class="inspector__row"> |
| 298 |
<label class="inspector__label"><?php echo esc_html__( 'Text before shortcode', 'booking' ); ?></label> |
| 299 |
<div class="inspector__control"> |
| 300 |
<input type="text" |
| 301 |
class="inspector__input" |
| 302 |
data-inspector-key="prefix_text" |
| 303 |
value="{{ ( typeof data.prefix_text !== 'undefined' && data.prefix_text !== null ) ? data.prefix_text : '<?php echo esc_js( $cfg['prefix'] ); ?>' }}"> |
| 304 |
</div> |
| 305 |
</div> |
| 306 |
|
| 307 |
<div class="inspector__row"> |
| 308 |
<label class="inspector__label"><?php echo esc_html__( 'Help text', 'booking' ); ?></label> |
| 309 |
<div class="inspector__control"> |
| 310 |
<textarea class="inspector__textarea" rows="3" data-inspector-key="help">{{ data.help || '' }}</textarea> |
| 311 |
</div> |
| 312 |
</div> |
| 313 |
</div> |
| 314 |
</section> |
| 315 |
|
| 316 |
<section class="wpbc_bfb__inspector__group wpbc_ui__collapsible_group" data-group="appearance"> |
| 317 |
<button type="button" class="group__header"> |
| 318 |
<h3><?php echo esc_html__( 'Appearance', 'booking' ); ?></h3> |
| 319 |
<i class="wpbc_ui_el__vert_menu_root_section_icon menu_icon icon-1x wpbc-bi-chevron-right"></i> |
| 320 |
</button> |
| 321 |
<div class="group__fields"> |
| 322 |
<div class="inspector__row"> |
| 323 |
<label class="inspector__label"><?php echo esc_html__( 'CSS class', 'booking' ); ?></label> |
| 324 |
<div class="inspector__control"> |
| 325 |
<input type="text" |
| 326 |
class="inspector__input" |
| 327 |
data-inspector-key="cssclass" |
| 328 |
value="{{ data.cssclass || '' }}"> |
| 329 |
</div> |
| 330 |
</div> |
| 331 |
|
| 332 |
<div class="inspector__row"> |
| 333 |
<label class="inspector__label"><?php echo esc_html__( 'HTML ID', 'booking' ); ?></label> |
| 334 |
<div class="inspector__control"> |
| 335 |
<input type="text" |
| 336 |
class="inspector__input" |
| 337 |
data-inspector-key="html_id" |
| 338 |
value="{{ data.html_id || '' }}"> |
| 339 |
</div> |
| 340 |
</div> |
| 341 |
|
| 342 |
<div class="wpbc_bfb__inspector__note"> |
| 343 |
<?php echo esc_html__( 'Exported output:', 'booking' ); ?> |
| 344 |
<# |
| 345 |
var exported_prefix_text = ( typeof data.prefix_text === 'string' ) |
| 346 |
? data.prefix_text.trim() |
| 347 |
: '<?php echo esc_js( $cfg['prefix'] ); ?>'; |
| 348 |
#> |
| 349 |
<# if ( exported_prefix_text ) { #>{{ exported_prefix_text }} <# } #><strong>[<?php echo esc_html( $shortcode_display ); ?>]</strong> |
| 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 |
|