| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking Form Builder - helpers for dedicated live-demo templates. |
| 4 |
* |
| 5 |
* Demo records deliberately reuse proven Builder structures, while keeping |
| 6 |
* stable template keys that can evolve independently from the public presets. |
| 7 |
* |
| 8 |
* @package Booking Calendar |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! function_exists( 'wpbc_bfb_demo_template__walk_fields' ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* Apply a callback to every BFB field in a decoded structure. |
| 19 |
* |
| 20 |
* @param array $node Structure node. |
| 21 |
* @param callable $callback Field callback. |
| 22 |
* |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
function wpbc_bfb_demo_template__walk_fields( $node, $callback ) { |
| 26 |
if ( ! is_array( $node ) ) { |
| 27 |
return $node; |
| 28 |
} |
| 29 |
|
| 30 |
if ( isset( $node['type'], $node['data'] ) && 'field' === $node['type'] && is_array( $node['data'] ) ) { |
| 31 |
$node['data'] = call_user_func( $callback, $node['data'] ); |
| 32 |
} |
| 33 |
|
| 34 |
foreach ( $node as $key => $value ) { |
| 35 |
if ( is_array( $value ) ) { |
| 36 |
$node[ $key ] = wpbc_bfb_demo_template__walk_fields( $value, $callback ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $node; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! function_exists( 'wpbc_bfb_demo_template__remove_field_types' ) ) { |
| 45 |
|
| 46 |
/** |
| 47 |
* Remove selected BFB field nodes from a decoded structure. |
| 48 |
* |
| 49 |
* @param mixed $node Structure node. |
| 50 |
* @param array $field_types Field types to remove. |
| 51 |
* |
| 52 |
* @return mixed|null |
| 53 |
*/ |
| 54 |
function wpbc_bfb_demo_template__remove_field_types( $node, $field_types ) { |
| 55 |
if ( ! is_array( $node ) ) { |
| 56 |
return $node; |
| 57 |
} |
| 58 |
$is_list = ( empty( $node ) || array_keys( $node ) === range( 0, count( $node ) - 1 ) ); |
| 59 |
|
| 60 |
if ( |
| 61 |
isset( $node['type'], $node['data']['type'] ) && |
| 62 |
'field' === $node['type'] && |
| 63 |
in_array( (string) $node['data']['type'], $field_types, true ) |
| 64 |
) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
foreach ( $node as $key => $value ) { |
| 69 |
if ( ! is_array( $value ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$filtered = wpbc_bfb_demo_template__remove_field_types( $value, $field_types ); |
| 74 |
if ( null === $filtered ) { |
| 75 |
unset( $node[ $key ] ); |
| 76 |
} else { |
| 77 |
$node[ $key ] = $filtered; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( $is_list ) { |
| 82 |
$node = array_values( $node ); |
| 83 |
} |
| 84 |
|
| 85 |
return $node; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! function_exists( 'wpbc_bfb_demo_template__node_has_static_texts' ) ) { |
| 90 |
|
| 91 |
/** |
| 92 |
* Check whether a Builder node contains every requested static-text value. |
| 93 |
* |
| 94 |
* @param mixed $node Structure node. |
| 95 |
* @param array $texts Static-text values. |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
function wpbc_bfb_demo_template__node_has_static_texts( $node, $texts ) { |
| 100 |
$found = array_fill_keys( $texts, false ); |
| 101 |
$walk = static function ( $value ) use ( &$walk, &$found ) { |
| 102 |
if ( ! is_array( $value ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
if ( |
| 106 |
isset( $value['type'], $value['data']['type'], $value['data']['text'] ) && |
| 107 |
'field' === $value['type'] && |
| 108 |
'static_text' === $value['data']['type'] && |
| 109 |
array_key_exists( (string) $value['data']['text'], $found ) |
| 110 |
) { |
| 111 |
$found[ (string) $value['data']['text'] ] = true; |
| 112 |
} |
| 113 |
foreach ( $value as $child ) { |
| 114 |
if ( is_array( $child ) ) { |
| 115 |
$walk( $child ); |
| 116 |
} |
| 117 |
} |
| 118 |
}; |
| 119 |
$walk( $node ); |
| 120 |
|
| 121 |
return ! in_array( false, $found, true ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! function_exists( 'wpbc_bfb_demo_template__remove_advanced_row' ) ) { |
| 126 |
|
| 127 |
/** |
| 128 |
* Remove the balanced outer Advanced Form row containing a marker. |
| 129 |
* |
| 130 |
* @param string $form Advanced Form markup. |
| 131 |
* @param string $marker Unique marker within the row. |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
function wpbc_bfb_demo_template__remove_advanced_row( $form, $marker ) { |
| 136 |
$marker_position = strpos( $form, $marker ); |
| 137 |
if ( false === $marker_position ) { |
| 138 |
return $form; |
| 139 |
} |
| 140 |
|
| 141 |
$row_start = strrpos( substr( $form, 0, $marker_position ), '<r>' ); |
| 142 |
if ( false === $row_start ) { |
| 143 |
return $form; |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! preg_match_all( '/<\/?r>/', substr( $form, $row_start ), $matches, PREG_OFFSET_CAPTURE ) ) { |
| 147 |
return $form; |
| 148 |
} |
| 149 |
|
| 150 |
$depth = 0; |
| 151 |
foreach ( $matches[0] as $tag ) { |
| 152 |
$depth += ( '<r>' === $tag[0] ) ? 1 : -1; |
| 153 |
if ( 0 === $depth ) { |
| 154 |
$row_end = $row_start + $tag[1] + strlen( $tag[0] ); |
| 155 |
return substr( $form, 0, $row_start ) . substr( $form, $row_end ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return $form; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! function_exists( 'wpbc_bfb_demo_template__medium_layout' ) ) { |
| 164 |
|
| 165 |
/** |
| 166 |
* Arrange the Business Medium demo like its canonical two-step Builder export. |
| 167 |
* |
| 168 |
* @param array $record Template record. |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
function wpbc_bfb_demo_template__medium_layout( $record ) { |
| 173 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 174 |
if ( ! isset( $structure[0]['content'], $structure[1]['content'] ) ) { |
| 175 |
return $record; |
| 176 |
} |
| 177 |
|
| 178 |
$calendar_section = null; |
| 179 |
$summary_section = null; |
| 180 |
foreach ( $structure[0]['content'] as $node ) { |
| 181 |
if ( isset( $node['type'], $node['data']['columns'][0]['items'] ) && 'section' === $node['type'] ) { |
| 182 |
$encoded = wp_json_encode( $node ); |
| 183 |
if ( false !== strpos( $encoded, '"type":"calendar"' ) ) { |
| 184 |
$calendar_section = $node; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
foreach ( $structure[1]['content'] as &$node ) { |
| 189 |
if ( wpbc_bfb_demo_template__node_has_static_texts( $node, array( 'Booking Data', 'Booking Cost' ) ) ) { |
| 190 |
$node['data']['columns'][0]['width'] = '76.056%'; |
| 191 |
$node['data']['columns'][1]['width'] = '20.944%'; |
| 192 |
$summary_section = $node; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
unset( $node ); |
| 197 |
|
| 198 |
if ( null === $calendar_section || null === $summary_section ) { |
| 199 |
return $record; |
| 200 |
} |
| 201 |
|
| 202 |
$resource_title = null; |
| 203 |
$find_resource = static function ( $node ) use ( &$find_resource, &$resource_title ) { |
| 204 |
if ( ! is_array( $node ) || null !== $resource_title ) { |
| 205 |
return; |
| 206 |
} |
| 207 |
if ( isset( $node['type'], $node['data']['type'] ) && 'field' === $node['type'] && 'resource_title_hint' === $node['data']['type'] ) { |
| 208 |
$resource_title = $node; |
| 209 |
return; |
| 210 |
} |
| 211 |
foreach ( $node as $child ) { |
| 212 |
if ( is_array( $child ) ) { |
| 213 |
$find_resource( $child ); |
| 214 |
} |
| 215 |
} |
| 216 |
}; |
| 217 |
$find_resource( $calendar_section ); |
| 218 |
|
| 219 |
$calendar_section['data']['columns'] = array( $calendar_section['data']['columns'][0] ); |
| 220 |
$calendar_section['data']['col_styles'] = ''; |
| 221 |
$calendar_section['data']['columns'][0]['width'] = '100%'; |
| 222 |
|
| 223 |
$first_items = &$summary_section['data']['columns'][0]['items']; |
| 224 |
if ( isset( $first_items[0]['data']['text'] ) && 'Booking Data' === $first_items[0]['data']['text'] ) { |
| 225 |
array_shift( $first_items ); |
| 226 |
} |
| 227 |
if ( null !== $resource_title ) { |
| 228 |
array_unshift( $first_items, $resource_title ); |
| 229 |
} |
| 230 |
$suffix_step_one_ids = static function ( $node ) use ( &$suffix_step_one_ids ) { |
| 231 |
if ( ! is_array( $node ) ) { |
| 232 |
return $node; |
| 233 |
} |
| 234 |
if ( isset( $node['type'], $node['data'] ) && is_array( $node['data'] ) ) { |
| 235 |
if ( isset( $node['data']['id'] ) ) { |
| 236 |
$node['data']['id'] .= '-step1'; |
| 237 |
} |
| 238 |
if ( 'field' === $node['type'] && isset( $node['data']['name'] ) ) { |
| 239 |
$node['data']['name'] .= '-step1'; |
| 240 |
} |
| 241 |
} |
| 242 |
foreach ( $node as $key => $value ) { |
| 243 |
if ( is_array( $value ) ) { |
| 244 |
$node[ $key ] = $suffix_step_one_ids( $value ); |
| 245 |
} |
| 246 |
} |
| 247 |
return $node; |
| 248 |
}; |
| 249 |
$summary_section = $suffix_step_one_ids( $summary_section ); |
| 250 |
|
| 251 |
$structure[0]['content'] = array( |
| 252 |
$structure[0]['content'][0], |
| 253 |
$calendar_section, |
| 254 |
$summary_section, |
| 255 |
$structure[0]['content'][ count( $structure[0]['content'] ) - 1 ], |
| 256 |
); |
| 257 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 258 |
|
| 259 |
$form = wpbc_bfb_demo_template__remove_advanced_row( (string) $record['advanced_form'], '[calendar]' ); |
| 260 |
$replacement = <<<'WPBC_BFB_MEDIUM_STEP_ONE' |
| 261 |
<r> |
| 262 |
<c style="flex-basis: 100%; --wpbc-col-min: 0px"><item><l>Select Date</l><br>[calendar]</item></c> |
| 263 |
</r> |
| 264 |
<r> |
| 265 |
<c style="flex-basis: 76.056%; --wpbc-bfb-col-dir: row;--wpbc-bfb-col-wrap: wrap;--wpbc-bfb-col-ai: flex-start;--wpbc-bfb-col-gap: 10px;--wpbc-col-min: 0px"> |
| 266 |
<item>Booking: <strong>[resource_title_hint]</strong></item> |
| 267 |
<r> |
| 268 |
<c style="flex-basis: 31.3333%"><item>Check-in: <strong>[check_in_date_hint]</strong></item></c> |
| 269 |
<c style="flex-basis: 31.3333%"><item>Check-out: <strong>[check_out_date_hint]</strong></item></c> |
| 270 |
<c style="flex-basis: 31.3333%"><item>Nights: <strong>[nights_number_hint]</strong></item></c> |
| 271 |
</r> |
| 272 |
</c> |
| 273 |
<c style="flex-basis: 20.944%; --wpbc-bfb-col-dir: row;--wpbc-bfb-col-wrap: wrap;--wpbc-bfb-col-ai: flex-start;--wpbc-bfb-col-gap: 10px;--wpbc-col-min: 0px"> |
| 274 |
<item><label name="static-text-blyy7-step1" class="wpbc_static_text" style="text-align:left;font-weight:bold">Booking Cost</label></item> |
| 275 |
<r><c style="flex-basis: 100%"><item>Total Cost: <strong>[cost_hint]</strong></item></c></r> |
| 276 |
</c> |
| 277 |
</r> |
| 278 |
WPBC_BFB_MEDIUM_STEP_ONE; |
| 279 |
$timeline_end = strpos( $form, '</r>', strpos( $form, 'active_step="1"' ) ); |
| 280 |
if ( false !== $timeline_end ) { |
| 281 |
$timeline_end += 4; |
| 282 |
$form = substr( $form, 0, $timeline_end ) . "\n" . $replacement . substr( $form, $timeline_end ); |
| 283 |
} |
| 284 |
$form = str_replace( array( '71.7186%', '25.2814%' ), array( '76.056%', '20.944%' ), $form ); |
| 285 |
$record['advanced_form'] = $form; |
| 286 |
|
| 287 |
return $record; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! function_exists( 'wpbc_bfb_demo_template__large_layout' ) ) { |
| 292 |
|
| 293 |
/** |
| 294 |
* Place the Business Large calendar above its compact date and cost hints. |
| 295 |
* |
| 296 |
* @param array $record Template record. |
| 297 |
* |
| 298 |
* @return array |
| 299 |
*/ |
| 300 |
function wpbc_bfb_demo_template__large_layout( $record ) { |
| 301 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 302 |
if ( ! isset( $structure[0]['content'] ) || ! is_array( $structure[0]['content'] ) ) { |
| 303 |
return $record; |
| 304 |
} |
| 305 |
|
| 306 |
$calendar_index = null; |
| 307 |
$calendar_section = null; |
| 308 |
foreach ( $structure[0]['content'] as $index => $node ) { |
| 309 |
if ( false !== strpos( wp_json_encode( $node ), '"type":"calendar"' ) ) { |
| 310 |
$calendar_index = $index; |
| 311 |
$calendar_section = $node; |
| 312 |
break; |
| 313 |
} |
| 314 |
} |
| 315 |
if ( null === $calendar_section || empty( $calendar_section['data']['columns'][0] ) ) { |
| 316 |
return $record; |
| 317 |
} |
| 318 |
|
| 319 |
$hint_fields = array(); |
| 320 |
$collect = static function ( $node ) use ( &$collect, &$hint_fields ) { |
| 321 |
if ( ! is_array( $node ) ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
if ( isset( $node['type'], $node['data']['type'] ) && 'field' === $node['type'] ) { |
| 325 |
$hint_fields[ $node['data']['type'] ] = $node; |
| 326 |
} |
| 327 |
foreach ( $node as $child ) { |
| 328 |
if ( is_array( $child ) ) { |
| 329 |
$collect( $child ); |
| 330 |
} |
| 331 |
} |
| 332 |
}; |
| 333 |
$collect( $calendar_section ); |
| 334 |
|
| 335 |
$required_types = array( 'capacity_hint', 'check_in_date_hint', 'check_out_date_hint', 'nights_number_hint', 'cost_hint' ); |
| 336 |
foreach ( $required_types as $required_type ) { |
| 337 |
if ( ! isset( $hint_fields[ $required_type ] ) ) { |
| 338 |
return $record; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
$calendar_section['data']['columns'] = array( $calendar_section['data']['columns'][0] ); |
| 343 |
$calendar_section['data']['col_styles'] = ''; |
| 344 |
$calendar_section['data']['columns'][0]['width'] = '100%'; |
| 345 |
$hint_fields['capacity_hint']['data']['preview_value'] = 4; |
| 346 |
|
| 347 |
$divider_field = static function ( $id ) { |
| 348 |
return array( |
| 349 |
'type' => 'field', |
| 350 |
'data' => array( |
| 351 |
'id' => $id, |
| 352 |
'type' => 'divider', |
| 353 |
'usage_key' => 'divider', |
| 354 |
'orientation' => 'horizontal', |
| 355 |
'line_style' => 'solid', |
| 356 |
'thickness_px' => 1, |
| 357 |
'length' => '100%', |
| 358 |
'align' => 'center', |
| 359 |
'color' => '#e0e0e0', |
| 360 |
'label' => 'Divider_horizontal', |
| 361 |
'name' => $id, |
| 362 |
'margin_top_px' => 2, |
| 363 |
'margin_bottom_px' => 2, |
| 364 |
'margin_left_px' => 2, |
| 365 |
'margin_right_px' => 2, |
| 366 |
'cssclass_extra' => '', |
| 367 |
'html_id' => '', |
| 368 |
), |
| 369 |
); |
| 370 |
}; |
| 371 |
|
| 372 |
$summary_section = array( |
| 373 |
'type' => 'section', |
| 374 |
'data' => array( |
| 375 |
'id' => 'demo-business-large-booking-summary', |
| 376 |
'label' => 'Section', |
| 377 |
'html_id' => '', |
| 378 |
'cssclass' => '', |
| 379 |
'col_styles' => '[{"dir":"row","wrap":"wrap","ai":"flex-start","gap":"10px"},{"dir":"row","wrap":"wrap","ai":"flex-start","gap":"10px"}]', |
| 380 |
'columns' => array( |
| 381 |
array( |
| 382 |
'width' => '76.056%', |
| 383 |
'items' => array( |
| 384 |
array( |
| 385 |
'type' => 'section', |
| 386 |
'data' => array( |
| 387 |
'id' => 'demo-business-large-date-hints', |
| 388 |
'label' => 'Section', |
| 389 |
'html_id' => '', |
| 390 |
'cssclass' => '', |
| 391 |
'col_styles' => '', |
| 392 |
'columns' => array( |
| 393 |
array( 'width' => '31.3333%', 'items' => array( $hint_fields['check_in_date_hint'] ) ), |
| 394 |
array( 'width' => '31.3333%', 'items' => array( $hint_fields['check_out_date_hint'] ) ), |
| 395 |
array( 'width' => '31.3333%', 'items' => array( $hint_fields['nights_number_hint'] ) ), |
| 396 |
), |
| 397 |
), |
| 398 |
), |
| 399 |
), |
| 400 |
), |
| 401 |
array( |
| 402 |
'width' => '20.944%', |
| 403 |
'items' => array( |
| 404 |
array( |
| 405 |
'type' => 'section', |
| 406 |
'data' => array( |
| 407 |
'id' => 'demo-business-large-cost-hint', |
| 408 |
'label' => 'Section', |
| 409 |
'html_id' => '', |
| 410 |
'cssclass' => '', |
| 411 |
'col_styles' => '', |
| 412 |
'columns' => array( |
| 413 |
array( 'width' => '100%', 'items' => array( $hint_fields['cost_hint'] ) ), |
| 414 |
), |
| 415 |
), |
| 416 |
), |
| 417 |
), |
| 418 |
), |
| 419 |
), |
| 420 |
), |
| 421 |
); |
| 422 |
|
| 423 |
array_splice( |
| 424 |
$structure[0]['content'], |
| 425 |
$calendar_index, |
| 426 |
1, |
| 427 |
array( |
| 428 |
$calendar_section, |
| 429 |
$divider_field( 'divider_horizontal-r8s' ), |
| 430 |
$hint_fields['capacity_hint'], |
| 431 |
$summary_section, |
| 432 |
$divider_field( 'divider_horizontal' ), |
| 433 |
) |
| 434 |
); |
| 435 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 436 |
|
| 437 |
$form = wpbc_bfb_demo_template__remove_advanced_row( (string) $record['advanced_form'], '[calendar]' ); |
| 438 |
$replacement = <<<'WPBC_BFB_LARGE_HINTS' |
| 439 |
<r> |
| 440 |
<c style="flex-basis: 100%"> |
| 441 |
<item> |
| 442 |
<l>Select Date</l> |
| 443 |
<br>[calendar] |
| 444 |
</item> |
| 445 |
</c> |
| 446 |
</r> |
| 447 |
<r> |
| 448 |
<c style="flex-basis: 100%"><item><div class="wpbc_bfb_divider_wrap" data-bfb-type="divider" data-orientation="horizontal" style="margin:2px 2px 2px 2px"><hr name="divider_horizontal-r8s" class="wpbc_bfb_divider wpbc_bfb_divider--h" style="border:none; height:0; border-top:1px solid #e0e0e0; width:100%; margin-left:auto; margin-right:auto"></div></item></c> |
| 449 |
</r> |
| 450 |
<r> |
| 451 |
<c style="flex-basis: 100%"><item>Availability: <strong>[capacity_hint]</strong></item></c> |
| 452 |
</r> |
| 453 |
<r> |
| 454 |
<c style="flex-basis: 76.056%; --wpbc-bfb-col-dir: row;--wpbc-bfb-col-wrap: wrap;--wpbc-bfb-col-ai: flex-start;--wpbc-bfb-col-gap: 10px;--wpbc-col-min: 0px"> |
| 455 |
<r> |
| 456 |
<c style="flex-basis: 31.3333%"><item>Check-in: <strong>[check_in_date_hint]</strong></item></c> |
| 457 |
<c style="flex-basis: 31.3333%"><item>Check-out: <strong>[check_out_date_hint]</strong></item></c> |
| 458 |
<c style="flex-basis: 31.3333%"><item>Nights: <strong>[nights_number_hint]</strong></item></c> |
| 459 |
</r> |
| 460 |
</c> |
| 461 |
<c style="flex-basis: 20.944%; --wpbc-bfb-col-dir: row;--wpbc-bfb-col-wrap: wrap;--wpbc-bfb-col-ai: flex-start;--wpbc-bfb-col-gap: 10px;--wpbc-col-min: 0px"> |
| 462 |
<r> |
| 463 |
<c style="flex-basis: 100%"><item>Total Cost: <strong>[cost_hint]</strong></item></c> |
| 464 |
</r> |
| 465 |
</c> |
| 466 |
</r> |
| 467 |
<r> |
| 468 |
<c style="flex-basis: 100%"><item><div class="wpbc_bfb_divider_wrap" data-bfb-type="divider" data-orientation="horizontal" style="margin:2px 2px 2px 2px"><hr name="divider_horizontal" class="wpbc_bfb_divider wpbc_bfb_divider--h" style="border:none; height:0; border-top:1px solid #e0e0e0; width:100%; margin-left:auto; margin-right:auto"></div></item></c> |
| 469 |
</r> |
| 470 |
WPBC_BFB_LARGE_HINTS; |
| 471 |
$form_position = strpos( $form, '<div class="wpbc_bfb_form' ); |
| 472 |
if ( false !== $form_position ) { |
| 473 |
$form_open_end = strpos( $form, '>', $form_position ); |
| 474 |
if ( false !== $form_open_end ) { |
| 475 |
$form = substr( $form, 0, $form_open_end + 1 ) . "\n" . $replacement . substr( $form, $form_open_end + 1 ); |
| 476 |
} |
| 477 |
} |
| 478 |
$record['advanced_form'] = $form; |
| 479 |
|
| 480 |
return $record; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
if ( ! function_exists( 'wpbc_bfb_demo_template__build' ) ) { |
| 485 |
|
| 486 |
/** |
| 487 |
* Build a dedicated demo template from an existing bundled BFB record. |
| 488 |
* |
| 489 |
* @param string $base_file Absolute base-template path. |
| 490 |
* @param array $args Demo template configuration. |
| 491 |
* |
| 492 |
* @return array |
| 493 |
*/ |
| 494 |
function wpbc_bfb_demo_template__build( $base_file, $args ) { |
| 495 |
$args = wp_parse_args( |
| 496 |
$args, |
| 497 |
array( |
| 498 |
'template_key' => '', |
| 499 |
'title' => '', |
| 500 |
'description' => '', |
| 501 |
'picture_url' => '', |
| 502 |
'heading' => '', |
| 503 |
'service_options' => array(), |
| 504 |
'remove_field_types' => array(), |
| 505 |
'remove_review_summary' => false, |
| 506 |
'use_medium_demo_layout' => false, |
| 507 |
'use_large_demo_layout' => false, |
| 508 |
) |
| 509 |
); |
| 510 |
|
| 511 |
if ( ! is_string( $base_file ) || ! file_exists( $base_file ) ) { |
| 512 |
return array(); |
| 513 |
} |
| 514 |
|
| 515 |
$template = require $base_file; |
| 516 |
if ( empty( $template['record'] ) || ! is_array( $template['record'] ) ) { |
| 517 |
return array(); |
| 518 |
} |
| 519 |
|
| 520 |
$template_key = sanitize_key( (string) $args['template_key'] ); |
| 521 |
if ( '' === $template_key ) { |
| 522 |
return array(); |
| 523 |
} |
| 524 |
|
| 525 |
$record = $template['record']; |
| 526 |
$record['form_slug'] = $template_key; |
| 527 |
$record['status'] = 'template'; |
| 528 |
$record['scope'] = 'global'; |
| 529 |
$record['owner_user_id'] = 0; |
| 530 |
$record['is_default'] = 0; |
| 531 |
$record['title'] = (string) $args['title']; |
| 532 |
$record['description'] = (string) $args['description']; |
| 533 |
if ( '' !== (string) $args['picture_url'] ) { |
| 534 |
$record['picture_url'] = (string) $args['picture_url']; |
| 535 |
} |
| 536 |
|
| 537 |
$service_options = array_values( array_filter( (array) $args['service_options'], 'is_array' ) ); |
| 538 |
if ( ! empty( $service_options ) && ! empty( $record['structure_json'] ) ) { |
| 539 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 540 |
if ( is_array( $structure ) ) { |
| 541 |
$structure = wpbc_bfb_demo_template__walk_fields( |
| 542 |
$structure, |
| 543 |
static function ( $field ) use ( $service_options ) { |
| 544 |
if ( isset( $field['type'] ) && 'durationtime' === $field['type'] ) { |
| 545 |
$field['options'] = $service_options; |
| 546 |
} |
| 547 |
return $field; |
| 548 |
} |
| 549 |
); |
| 550 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 551 |
} |
| 552 |
|
| 553 |
$shortcode_options = array(); |
| 554 |
foreach ( $service_options as $option ) { |
| 555 |
if ( ! isset( $option['label'], $option['value'] ) ) { |
| 556 |
continue; |
| 557 |
} |
| 558 |
$shortcode_options[] = '"' . str_replace( '"', '"', (string) $option['label'] ) . '@@' . sanitize_text_field( (string) $option['value'] ) . '"'; |
| 559 |
} |
| 560 |
if ( ! empty( $shortcode_options ) ) { |
| 561 |
$replacement = '[selectbox* durationtime class:wpbc_service_duration ' . implode( ' ', $shortcode_options ) . ']'; |
| 562 |
$record['advanced_form'] = preg_replace( '/\[selectbox\*\s+durationtime\b[^\]]*\]/', $replacement, (string) $record['advanced_form'] ); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
$remove_field_types = array_values( array_filter( array_map( 'sanitize_key', (array) $args['remove_field_types'] ) ) ); |
| 567 |
if ( ! empty( $remove_field_types ) && ! empty( $record['structure_json'] ) ) { |
| 568 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 569 |
if ( is_array( $structure ) ) { |
| 570 |
$structure = wpbc_bfb_demo_template__remove_field_types( $structure, $remove_field_types ); |
| 571 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if ( in_array( 'capacity_hint', $remove_field_types, true ) ) { |
| 576 |
$record['advanced_form'] = preg_replace( |
| 577 |
'/\s*<r>\s*<c[^>]*>\s*<item>\s*Availability:\s* \s*<strong>\[capacity_hint\]<\/strong>\s*<\/item>\s*<\/c>\s*<\/r>/i', |
| 578 |
'', |
| 579 |
(string) $record['advanced_form'] |
| 580 |
); |
| 581 |
} |
| 582 |
|
| 583 |
if ( ! empty( $args['remove_review_summary'] ) && ! empty( $record['structure_json'] ) ) { |
| 584 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 585 |
if ( isset( $structure[0]['content'] ) && is_array( $structure[0]['content'] ) ) { |
| 586 |
$structure[0]['content'] = array_values( |
| 587 |
array_filter( |
| 588 |
$structure[0]['content'], |
| 589 |
static function ( $node ) { |
| 590 |
return ! wpbc_bfb_demo_template__node_has_static_texts( $node, array( 'Booking Data', 'Booking Cost' ) ); |
| 591 |
} |
| 592 |
) |
| 593 |
); |
| 594 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 595 |
} |
| 596 |
$record['advanced_form'] = wpbc_bfb_demo_template__remove_advanced_row( (string) $record['advanced_form'], '>Booking Data</label>' ); |
| 597 |
} |
| 598 |
|
| 599 |
if ( ! empty( $args['use_medium_demo_layout'] ) ) { |
| 600 |
$record = wpbc_bfb_demo_template__medium_layout( $record ); |
| 601 |
} |
| 602 |
|
| 603 |
if ( ! empty( $args['use_large_demo_layout'] ) ) { |
| 604 |
$record = wpbc_bfb_demo_template__large_layout( $record ); |
| 605 |
} |
| 606 |
|
| 607 |
$heading = trim( (string) $args['heading'] ); |
| 608 |
if ( '' !== $heading && ! empty( $record['structure_json'] ) ) { |
| 609 |
$structure = json_decode( (string) $record['structure_json'], true ); |
| 610 |
if ( isset( $structure[0]['content'] ) && is_array( $structure[0]['content'] ) ) { |
| 611 |
$heading_section = array( |
| 612 |
'type' => 'section', |
| 613 |
'data' => array( |
| 614 |
'id' => $template_key . '-heading-section', |
| 615 |
'label' => 'Section', |
| 616 |
'html_id' => '', |
| 617 |
'cssclass' => '', |
| 618 |
'col_styles' => '', |
| 619 |
'columns' => array( |
| 620 |
array( |
| 621 |
'width' => '100%', |
| 622 |
'items' => array( |
| 623 |
array( |
| 624 |
'type' => 'field', |
| 625 |
'data' => array( |
| 626 |
'id' => $template_key . '-heading', |
| 627 |
'type' => 'static_text', |
| 628 |
'usage_key' => 'static_text', |
| 629 |
'text' => $heading, |
| 630 |
'tag' => 'h3', |
| 631 |
'align' => 'left', |
| 632 |
'bold' => 0, |
| 633 |
'italic' => 0, |
| 634 |
'html_allowed' => 0, |
| 635 |
'nl2br' => 1, |
| 636 |
'label' => 'Static_text', |
| 637 |
'name' => $template_key . '-heading', |
| 638 |
'html_id' => '', |
| 639 |
'cssclass_extra' => '', |
| 640 |
), |
| 641 |
), |
| 642 |
), |
| 643 |
), |
| 644 |
), |
| 645 |
), |
| 646 |
); |
| 647 |
array_unshift( $structure[0]['content'], $heading_section ); |
| 648 |
$record['structure_json'] = wp_json_encode( $structure ); |
| 649 |
} |
| 650 |
|
| 651 |
$heading_html = '<r><c style="flex-basis: 100%"><item><h3 class="wpbc_static_text" style="text-align:left">' . esc_html( $heading ) . '</h3></item></c></r>'; |
| 652 |
$record['advanced_form'] = preg_replace( '/(<div class="wpbc_wizard_step[^>]*>)/', '$1' . "\n\t\t" . $heading_html, (string) $record['advanced_form'], 1 ); |
| 653 |
} |
| 654 |
|
| 655 |
return array( |
| 656 |
'template_key' => $template_key, |
| 657 |
'seed_version' => '11.4.4', |
| 658 |
'sync_mode' => 'upsert', |
| 659 |
'record' => $record, |
| 660 |
); |
| 661 |
} |
| 662 |
} |
| 663 |
|