PluginProbe
Booking Calendar / 10.15.7
Booking Calendar v10.15.7
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 10.11.2 10.11.3 10.11.4 All 201 releases
booking / includes / _functions / booking_data__parse.php

booking_data__parse.php in Booking Calendar 10.15.7, at includes/_functions/booking_data__parse.php

1,412 lines 52.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Booking Data Parsing functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 2024-05-14
13 * @file ../includes/_functions/booking_data__parse.php
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
17
18 // =====================================================================================================================
19 // == B o o k i n g F o r m s P a r s i n g ==
20 // =====================================================================================================================
21
22 /**
23 * Parse "text^firstname1^John~..." -> [ 'firstname':[name:"firstname", value:"John" ...] ... ] - Parse (DB) form_data and get fields array values
24 *
25 * @param string $form_data - formatted booking form data from database, like this: 'text^selected_short_dates_hint15^21/02/2023 - 24/02/2023~text^days_number_hint15^4~text^cost_hint15^&amp;#36;400.00~text^deposit_hint15^&amp;#36;40.00~text^standard_bk_cost15^100~...'
26 * @param int $resource_id - ID of booking resource
27 * @param array $params - (optional) default: array() define here what field we need to get array( 'get' => 'value' )
28 *
29 * @return array
30 * [
31 * rangetime = [
32 * type = "selectbox-multiple"
33 * original_name = "rangetime3[]"
34 * name = "rangetime"
35 * value = "12:00 - 14:00"
36 * ]
37 * name = [
38 * type = "text"
39 * original_name = "name3"
40 * name = "name"
41 * value = "test 1"
42 * ]
43 * secondname = [...]
44 * email = [...]
45 * ]
46 *
47 * Example 1:
48 * $structured_booking_data_arr = wpbc_get_parsed_booking_data_arr( $this_booking->form, $resource_id, array( 'get' => 'value' ) );
49 * output: [ "rangetime" : "12:00 - 14:00",
50 * "name": "John" ,
51 * "secondname" : "Smith" , "email" : "test@wpbookingcalendar.com" , "visitors" : "1" ... ]
52 *
53 * Example 2:
54 * $structured_booking_data_arr = wpbc_get_parsed_booking_data_arr( $this_booking->form, $resource_id );
55 * output: [ rangetime = [
56 * type = "selectbox-multiple"
57 * original_name = "rangetime3[]"
58 * name = "rangetime"
59 * value = "12:00 - 14:00"
60 * ]
61 * name = [
62 * type = "text"
63 * original_name = "name3"
64 * name = "name"
65 * value = "test 1"
66 * ]
67 * secondname = [...]
68 * email = [...]
69 * ]
70 */
71 function wpbc_get_parsed_booking_data_arr( $form_data, $resource_id = 1, $params = array( ) ) {
72
73 $booking_data_arr = array();
74
75 if ( ! empty( $form_data ) ) {
76
77 $fields_arr = explode( '~', $form_data );
78
79 foreach ( $fields_arr as $field ) {
80
81 if ( false === strpos( $field, '^' ) ) {
82 break; // FixIn: 9.8.10.2.
83 }
84
85 list( $field_type, $field_original_name, $field_value ) = explode( '^', $field );
86
87 $field_name = $field_original_name;
88
89 // Is this multi select in checkboxes: [checkbox* ConducenteExtra "Si" "No"] => [... 6: "checkbox^ConducenteExtra4[]^Si", 7: "checkbox^ConducenteExtra4[]^" ... ]
90 $is_multi_options = ( '[]' == substr( $field_name, - 2 ) );
91
92 $minus_additional = ( '[]' == substr( $field_name, - 2 ) ) ? 2 : 0;
93 $field_name = ( $minus_additional > 0 ) ? substr( $field_name, 0, - $minus_additional ) : $field_name;
94
95
96 $minus_additional = ( strval( $resource_id ) == substr( $field_name, - 1 * ( strlen( strval( $resource_id ) ) ) ) )
97 ? strlen( strval( $resource_id ) ) : 0;
98 $field_name = ( $minus_additional > 0 ) ? substr( $field_name, 0, - $minus_additional ) : $field_name;
99
100 if ( ( 'checkbox' === $field_type ) && ( 'true' === $field_value ) ) {
101 //$field_value = strtolower( __( 'Yes', 'booking' ) );
102 $field_value = 'true';
103 }
104 if ( ( 'checkbox' === $field_type ) && ( 'false' === $field_value ) ) {
105 //$field_value = strtolower( __( 'No', 'booking' ) );
106 $field_value = 'false';
107 }
108
109 if ( ! isset( $booking_data_arr[ $field_name ] ) ) {
110 $booking_data_arr[ $field_name ] = array(
111 'type' => $field_type,
112 'original_name' => $field_original_name,
113 'name' => $field_name,
114 'value' => array( $field_value )
115 );
116 } else {
117 // All values we save as array values, for situations of MULTI options:
118 // [checkbox* ConducenteExtra "Si" "No"] => [... 6: "checkbox^ConducenteExtra4[]^Si", 7: "checkbox^ConducenteExtra4[]^" ... ]
119 $booking_data_arr[ $field_name ]['value'][] = $field_value;
120 }
121 }
122
123 // Convert arrays to string
124 foreach ( $booking_data_arr as $field_name => $field_structure_arr ) { // FixIn: 9.8.9.1.
125 $field_structure_arr['value'] = array_filter( $field_structure_arr['value'], function ( $v ) {
126 return ( $v !== '' ); // Remove All '' entries
127 } );
128 $booking_data_arr[ $field_name ]['value'] = implode( ', ', $field_structure_arr['value'] );
129 }
130
131
132 if ( isset( $params['get'] ) ) {
133 /**
134 * Now get only values or other fields: [
135 * "rangetime" : "12:00 - 14:00",
136 * "name":"test 1" ,
137 * "secondname" : "test 1" ,
138 * "email" : "test@wpbookingcalendar.com" ,
139 * "visitors" : "1"
140 * ...
141 * ]
142 */
143
144 foreach ( $booking_data_arr as $field_name => $field_structure_arr ) {
145
146 if ( isset( $field_structure_arr[ $params['get'] ] ) ) {
147 $booking_data_arr[ $field_name ] = $field_structure_arr[ $params['get'] ];
148 } else {
149 $booking_data_arr[ $field_name ] = '-';
150 }
151 }
152 }
153 }
154
155 return $booking_data_arr;
156 }
157
158 /**
159 * Convert [ ... ] -> "text^firstname1^John~..." - encoded booking data string (Usually for inserting into DB), from parsed booking data array
160 *
161 * @param array $booking_data_arr - parsed booking data.
162 * @param int $resource_id - ID of booking resource - In case, if we need to RE-UPDATE booking resource, Otherwise skip it
163 *
164 * @return string
165 */
166 function wpbc_encode_booking_data_to_string( $booking_data_arr, $resource_id = 0 ) {
167
168 $fields_arr = array();
169
170 foreach ( $booking_data_arr as $fields ) {
171
172 if ( ! empty( $resource_id ) ) {
173 $fields_arr[] = $fields['type']
174 . '^' . $fields['name'] . $resource_id . ( ( '[]' == substr( $fields['original_name'], - 2 ) ) ? '[]' : '' )
175 . '^' . $fields['value'];
176 } else {
177 $fields_arr[] = $fields['type'] . '^' . $fields['original_name'] . '^' . $fields['value'];
178 }
179
180 }
181
182 $form_data = implode( '~', $fields_arr );
183
184 return $form_data;
185 }
186
187 // -------------------------------------------------------------------------------------------------------------
188
189 /**
190 * Get Time fields in booking form_data
191 *
192 * @param string $form_data - formatted booking form data from database, like this: 'text^selected_short_dates_hint15^21/02/2023 - 24/02/2023~text^days_number_hint15^4~text^cost_hint15^&amp;#36;400.00~text^deposit_hint15^&amp;#36;40.00~text^standard_bk_cost15^100~...'
193 * @param int $resource_id - ID of booking resource
194 * @param array $params - (optional) default: array() define here what field we need to get array( 'get' => 'value' )
195 *
196 * @return array [ "18:00:00", "20:00:00" ] <- default
197 *
198 * Example #1: $times_his_arr = wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id); --> [ "18:00:01", "20:00:02" ]
199 * same: $times_his_arr = wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id, array( 'get' => 'times_his_arr' ) ); --> [ "18:00:01", "20:00:02" ]
200 * Example #2: $time_as_seconds_arr = wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id, array( 'get' => 'time_as_seconds_arr' ) ); --> [ 64800, 72000 ]
201 * Example #4: $booking_data_arr = wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id, array( 'get' => 'structured_booking_data_arr' ) ); --> [ name = "John", secondname = "Smith", email = "john.smith@server.com", visitors = "2",... ]
202 *
203 * Example #5: $booking_data_arr = wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id, array( 'get' => 'all' ) );
204 * --> [
205 * 'structured_booking_data_arr' => [ name = "John", secondname = "Smith", email = "john.smith@server.com", visitors = "2",... ]
206 * 'time_as_seconds_arr' => [ 64800, 72000 ]
207 * 'time_as_his_arr' => [ "18:00:01", "20:00:02" ]
208 * ]
209 */
210 function wpbc_get_times_his_arr__in_form_data( $form_data, $resource_id = 1, $params = array() ) {
211
212 $defaults = array(
213 'get' => 'times_his_arr'
214 );
215 $params = wp_parse_args( $params, $defaults );
216
217
218 // Get Time from booking form
219 $local_params = array();
220 /**
221 * Get parsed booking form: = [ name = "John", secondname = "Smith", email = "john.smith@server.com", visitors = "2",... ]
222 */
223 $local_params['structured_booking_data_arr'] = wpbc_get_parsed_booking_data_arr( $form_data, $resource_id, array( 'get' => 'value' ) );
224 // $local_params['all_booking_data_arr'] = wpbc_get_parsed_booking_data_arr( $form_data, $resource_id );
225
226 // Important! : [ 64800, 72000 ]
227 $local_params['time_as_seconds_arr'] = wpbc_get_in_booking_form__time_to_book_as_seconds_arr( $local_params['structured_booking_data_arr'] );
228
229 // [ "18:00:00", "20:00:00" ]
230 $time_as_seconds_arr = $local_params['time_as_seconds_arr'];
231 $time_as_seconds_arr[0] = ( 0 != $time_as_seconds_arr[0] ) ? $time_as_seconds_arr[0] + 1 : $time_as_seconds_arr[0]; // set check in time with ended 1 second
232 $time_as_seconds_arr[1] = ( ( 24 * 60 * 60 ) != $time_as_seconds_arr[1] ) ? $time_as_seconds_arr[1] + 2 : $time_as_seconds_arr[1]; // set check out time with ended 2 seconds
233 if ( ( 0 != $time_as_seconds_arr[0] ) && ( ( 24 * 60 * 60 ) == $time_as_seconds_arr[1] ) ) {
234 //FixIn: 10.0.0.49 - in case if we have start time != 00:00 and end time as 24:00 then set end time as 23:59:52
235 $time_as_seconds_arr[1] += - 8;
236 }
237 // [ '16:00:01', '18:00:02' ]
238 $local_params['times_his_arr'] = array(
239 wpbc_transform__seconds__in__24_hours_his( $time_as_seconds_arr[0] ),
240 wpbc_transform__seconds__in__24_hours_his( $time_as_seconds_arr[1] )
241 );
242 if (
243 ( isset( $params['get'] ) )
244 && ( isset( $local_params[ $params['get'] ] ) )
245 ) {
246 return $local_params[ $params['get'] ];
247 }
248
249 return $local_params;
250 }
251
252 // -------------------------------------------------------------------------------------------------------------
253
254 /**
255 * Get readable booking form data. Escape values here, as well!
256 *
257 * - 1. <= BS : 'Booking form show' configuration from standard form in versions up to Business Small version ,
258 * - 2 >= BM : If form data has field of custom form, then from custom form configuration,
259 * - 3 >= BM : Otherwise if resource has default custom booking form, then from this default custom booking form
260 * - 4 = MU : specific form of specific WP User
261 * - 5 finally : simple standard form
262 *
263 * @param string $form_data
264 * @param int $resource_id
265 *
266 * @return string
267 */
268 function wpbc_get__booking_form_data__show( $form_data, $resource_id = 1 , $params = array() ) {
269
270 $defaults = array(
271 'is_replace_unknown_shortcodes' => true,
272 'unknown_shortcodes_replace_by' => ''
273 );
274 $params = wp_parse_args( $params, $defaults );
275
276 $booking_form_show = wpbc_get__booking_form_data_configuration( $resource_id, $form_data, $params );
277
278 $booking_data_arr = wpbc_get_parsed_booking_data_arr( $form_data, $resource_id, array( 'get' => 'value' ) );
279
280 foreach ( $booking_data_arr as $key_param => $value_param ) { // FixIn: 6.1.1.4.
281
282 $value_param = esc_html( $value_param ); //FixIn: 9.7.4.1 - escape coded html/xss
283 $value_param = esc_html( html_entity_decode( $value_param ) );
284 $value_param = nl2br($value_param); // Add BR instead if /n elements // FixIn: 9.7.4.2.
285 $value_param = wpbc_string__escape__then_convert__n_amp__html( $value_param );
286
287 $value_param = wpbc_replace__true_false__to__yes_no( $value_param ); // FixIn: 9.8.9.1.
288
289 if (
290 ( gettype( $value_param ) != 'array' )
291 && ( gettype( $value_param ) != 'object' )
292 ) {
293 $booking_form_show = str_replace( '[' . $key_param . ']', $value_param, $booking_form_show );
294 }
295 }
296
297 if ($params['is_replace_unknown_shortcodes']) {
298 // Remove all shortcodes, which is not replaced early.
299 $booking_form_show = preg_replace( '/[\s]{0,}\[[a-zA-Z0-9.,-_]{0,}\][\s]{0,}/', $params['unknown_shortcodes_replace_by'], $booking_form_show ); // FixIn: 6.1.1.4.
300 }
301
302 $booking_form_show = str_replace( "&amp;", '&', $booking_form_show ); // FixIn: 7.1.2.12.
303
304
305
306 return $booking_form_show;
307 }
308
309 /**
310 * Get name of custom booking form, if it was used in form data OR booking resource use this default custom form
311 *
312 * @param int $resource_id
313 * @param string $form_data Form data here, required in >= BM, because such form_data can contain fields about used custom booking form
314 *
315 * @return string
316 */
317 function wpbc_get__custom_booking_form_name( $resource_id = 1, $form_data = '' ) {
318
319 $my_booking_form_name = '';
320
321 if ( class_exists( 'wpdev_bk_biz_m' ) ) {
322
323 if ( false !== strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) ) { // FixIn: 9.4.3.12.
324
325 $custom_booking_form_name = substr( $form_data, strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) + strlen( 'wpbc_custom_booking_form' . $resource_id . '^' ) );
326
327 if ( false !== strpos( $custom_booking_form_name, '~' ) ) {
328 $custom_booking_form_name = substr( $custom_booking_form_name, 0, strpos( $custom_booking_form_name, '~' ) );
329 $my_booking_form_name = $custom_booking_form_name;
330 }
331
332 } else {
333
334 // BM :: Get default Custom Form of Resource
335 $my_booking_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
336 }
337
338 if ( 'standard' == $my_booking_form_name ) {
339 $my_booking_form_name = '';
340 }
341 }
342
343 return $my_booking_form_name;
344 }
345
346 /**
347 * Get configuration of 'BOOKING FORM F I E L D S SHORTCODES' from - Booking > Settings > Form page
348 *
349 * - 1. <= BS : 'Booking form show' configuration from standard form in versions up to Business Small version ,
350 * - 2 >= BM : If form data has field of custom form, then from custom form configuration,
351 * - 3 >= BM : Otherwise if resource has default custom booking form, then from this default custom booking form
352 * - 4 = MU : specific form of specific WP User
353 * - 5 finally : simple standard form
354 *
355 * @param int $resource_id
356 * @param string $form_name Name of custom booking form, required in >= BM
357 *
358 * @return string
359 *
360 * Example 1: $booking_form = wpbc_get__booking_form_fields__configuration( 1 ); <- Load STANDARD booking form
361 * Example 2: $booking_form = wpbc_get__booking_form_fields__configuration( 1 , 'standard' ); <- Load STANDARD booking form
362 * Example 3: $booking_form = wpbc_get__booking_form_fields__configuration( 1 , 'my_custom_form' ); <- Load CUSTOM booking form with name 'my_custom_form'
363 * Example 4: $booking_form = wpbc_get__booking_form_fields__configuration( 1 , '' ); <- Load CUSTOM booking form, which is DEFAULT for RESOURCE
364 * Example 4: $booking_form = wpbc_get__booking_form_fields__configuration( 10 ); <- If resource ID = 10 belong to REGULAR User, then load booking form for this REGULAR USER
365 *
366 */
367 function wpbc_get__booking_form_fields__configuration( $resource_id = 1, $form_name = 'standard' ) {
368
369 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
370 $booking_form_configuration = wpbc_simple_form__get_booking_form__as_shortcodes();
371 } else {
372 $booking_form_configuration = get_bk_option( 'booking_form' );
373
374 if ( class_exists( 'wpdev_bk_biz_m' ) ) {
375
376 if ( $form_name != 'standard' ) {
377
378 $booking_form_configuration = apply_bk_filter( 'wpdev_get_booking_form', $booking_form_configuration, $form_name );
379 }
380
381 // Get default Custom Form for resource, if $form_name == '' wpbc_get__booking_form_fields__configuration(1,'')
382 if ( empty( $form_name ) ) {
383
384 $resource_default_custom_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
385
386 $booking_form_configuration = apply_bk_filter( 'wpdev_get_booking_form', $booking_form_configuration, $resource_default_custom_form_name );
387 }
388
389 //MU :: if resource of "Regular User" - then GET STANDARD user form ( if ( get_bk_option( 'booking_is_custom_forms_for_regular_users' ) !== 'On' ) )
390 $booking_form_configuration = apply_bk_filter( 'wpbc_multiuser_get_booking_form_fields_configuration_of_regular_user', $booking_form_configuration, $resource_id, $form_name ); // FixIn: 8.1.3.19.
391 }
392 }
393
394 // Language
395 $booking_form_configuration = wpbc_lang( $booking_form_configuration );
396
397 return $booking_form_configuration;
398 }
399
400
401 /**
402 * Load BFB preview payload from transient (for booking created inside preview iframe).
403 *
404 * @param array $ctx
405 * @return array|null
406 */
407 function wpbc_bfb_preview__maybe_get_payload_from_context( $ctx ) {
408
409 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
410
411 $user_id = isset( $ctx['user_id'] ) ? absint( $ctx['user_id'] ) : 0;
412 $form_id = isset( $ctx['wpbc_bfb_preview_form_id'] ) ? absint( $ctx['wpbc_bfb_preview_form_id'] ) : 0;
413 $token = isset( $ctx['wpbc_bfb_preview_token'] ) ? sanitize_key( $ctx['wpbc_bfb_preview_token'] ) : '';
414 $nonce = isset( $ctx['wpbc_bfb_preview_nonce'] ) ? sanitize_text_field( $ctx['wpbc_bfb_preview_nonce'] ) : '';
415
416 if ( $user_id <= 0 || $form_id <= 0 || '' === $token ) {
417 return null;
418 }
419
420 // Optional hardening: verify nonce if present.
421 if ( '' !== $nonce ) {
422 if ( ! wp_verify_nonce( $nonce, 'wpbc_bfb_preview_' . $token ) ) {
423 return null;
424 }
425 }
426
427 // Optional hardening: capability check (preview is admin-only anyway).
428 $cap = ( function_exists( 'wpbc_bfb_get_manage_cap' ) ) ? wpbc_bfb_get_manage_cap() : 'manage_options';
429 if ( ! is_user_logged_in() || ! current_user_can( $cap ) ) {
430 return null;
431 }
432
433 $transient_key = 'wpbc_bfb_preview_' . $user_id . '_' . $form_id . '_' . $token;
434
435 $data = get_transient( $transient_key );
436 if ( empty( $data ) || ! is_array( $data ) ) {
437 return null;
438 }
439
440 return $data;
441 }
442
443
444 /**
445 * Get configuration of 'BOOKING FORM DATA' from - Booking > Settings > Form page
446 *
447 * - 1. <= BS : 'Booking form show' configuration from standard form in versions up to Business Small version ,
448 * - 2 >= BM : If form data has field of custom form, then from custom form configuration,
449 * - 3 >= BM : Otherwise if resource has default custom booking form, then from this default custom booking form
450 * - 4 = MU : specific form of specific WP User
451 * - 5 finally : simple standard form
452 *
453 * @param int $resource_id
454 * @param string $form_data Form data here, required in >= BM, because such form_data can contain fields about used custom booking form
455 *
456 * @return string
457 */
458 function wpbc_get__booking_form_data_configuration( $resource_id = 1, $form_data = '', $params = array() ) {
459
460 // FixIn: 2026-02-05 - allow preview booking submissions to use preview "content_form".
461 $ctx = wp_parse_args( ( is_array( $params ) ? $params : array() ), wpbc_get_request_form_context() );
462
463 $form_status = ( isset( $ctx['form_status'] ) ) ? sanitize_key( $ctx['form_status'] ) : 'published';
464
465 if ( 'preview' === $form_status ) {
466
467 $payload = wpbc_bfb_preview__maybe_get_payload_from_context( $ctx );
468
469 // content_form is exactly the "Booking form show" template exported for preview session.
470 if ( ! empty( $payload ) && isset( $payload['content_form'] ) && ( '' !== (string) $payload['content_form'] ) ) {
471 return (string) $payload['content_form'];
472 }
473 }
474
475 $resource_id = (int) $resource_id;
476 $form_data = (string) $form_data;
477 $my_booking_form_name = 'standard';
478
479 // Maybe get name of custom booking form, that was saved in the booking data.
480 if ( false !== strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) ) { // FixIn: 9.4.3.12.
481
482 $custom_booking_form_name = substr( $form_data, strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) + strlen( 'wpbc_custom_booking_form' . $resource_id . '^' ) );
483
484 if ( false !== strpos( $custom_booking_form_name, '~' ) ) {
485 $custom_booking_form_name = substr( $custom_booking_form_name, 0, strpos( $custom_booking_form_name, '~' ) );
486 }
487 if ( ! empty( $custom_booking_form_name ) ) {
488 $my_booking_form_name = $custom_booking_form_name;
489 }
490 }
491
492
493 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
494
495 $booking_form_show = wpbc_simple_form__get_form_show__as_shortcodes();
496 $booking_form_show = wpbc_bf__replace_custom_html_shortcodes( $booking_form_show );
497
498 // Try BFB (if available) even in this branch (safe no-op if not loaded).
499 $booking_form_show = WPBC_BFB_Booking_Data_Content_Resolver::maybe_override_booking_form_show_by_bfb( $booking_form_show, $resource_id, $my_booking_form_name, $form_data, $ctx );
500
501 } else {
502
503 $booking_form_show = get_bk_option( 'booking_form_show' );
504 $booking_form_show = wpbc_bf__replace_custom_html_shortcodes( $booking_form_show );
505
506 if ( class_exists( 'wpdev_bk_biz_m' ) ) {
507
508 if ( false !== strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) ) { // FixIn: 9.4.3.12.
509
510 $custom_booking_form_name = substr(
511 $form_data,
512 strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) + strlen( 'wpbc_custom_booking_form' . $resource_id . '^' )
513 );
514
515 if ( false !== strpos( $custom_booking_form_name, '~' ) ) {
516 $custom_booking_form_name = substr( $custom_booking_form_name, 0, strpos( $custom_booking_form_name, '~' ) );
517 }
518
519 $booking_form_show = apply_bk_filter( 'wpdev_get_booking_form_content', $booking_form_show, $custom_booking_form_name );
520 $my_booking_form_name = $custom_booking_form_name;
521
522 } else {
523
524 // BM :: Get default Custom Form of Resource.
525 $my_booking_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
526 if ( ( $my_booking_form_name != 'standard' ) && ( ! empty( $my_booking_form_name ) ) ) {
527 $booking_form_show = apply_bk_filter( 'wpdev_get_booking_form_content', $booking_form_show, $my_booking_form_name );
528 }
529 }
530
531 // MU :: if resource of "Regular User" - then GET STANDARD user form.
532 $booking_form_show = apply_bk_filter( 'wpbc_multiuser_get_booking_form_show_of_regular_user', $booking_form_show, $resource_id, $my_booking_form_name ); // FixIn: 8.1.3.19.
533
534 /**
535 * IMPORTANT:
536 * Try BFB DB "content_form" (resolver-driven) BEFORE MU regular-user override,
537 * so MU filter can still enforce standard form when required.
538 */
539 $booking_form_show = WPBC_BFB_Booking_Data_Content_Resolver::maybe_override_booking_form_show_by_bfb( $booking_form_show, $resource_id, $my_booking_form_name, $form_data, $ctx );
540
541 } else {
542
543 // Try BFB also for non-BM paid branches (safe).
544 $booking_form_show = WPBC_BFB_Booking_Data_Content_Resolver::maybe_override_booking_form_show_by_bfb( $booking_form_show, $resource_id, $my_booking_form_name, $form_data, $ctx );
545 }
546 }
547
548 // Language.
549 $booking_form_show = wpbc_lang( $booking_form_show );
550
551 return $booking_form_show;
552 }
553
554
555 // -------------------------------------------------------------------------------------------------------------
556
557 /**
558 * Replace resource_ID of booking in 'form_data'
559 * Useful, when we need to save booking from one resource into another.
560 *
561 * @param $booking__form_data__str 'selectbox-multiple^rangetime2[]^18:00 - 20:00~checkbox^fee2[]^true~text^name2^John~text^secondname2^Smith...'
562 * @param $new_resource_id 10
563 * @param $old_resource_id 2
564 *
565 * @return string 'selectbox-multiple^rangetime10[]^18:00 - 20:00~checkbox^fee10[]^true~text^name10^John~text^secondname10^Smith...'
566 */
567 function wpbc_get__form_data__with_replaced_id( $booking__form_data__str, $new_resource_id, $old_resource_id ) {
568
569 $all_booking_data_arr = wpbc_get_parsed_booking_data_arr( $booking__form_data__str, $old_resource_id );
570
571 $new__form_data__str = wpbc_encode_booking_data_to_string( $all_booking_data_arr, $new_resource_id );
572
573 return $new__form_data__str;
574 }
575
576 // FixIn: 10.15.4.1.
577 /**
578 * Extract "advanced_form" from BFB loader result.
579 *
580 * @param mixed $bfb_pair
581 *
582 * @return string
583 */
584 function wpbc_extract__advanced_form_from_bfb_pair__for_field_names_listing( $bfb_pair ) {
585
586 $containers = array();
587
588 if ( ! is_array( $bfb_pair ) ) {
589 return '';
590 }
591
592 /**
593 * Safety:
594 * Do not override if loader already fell back to legacy.
595 */
596 if ( isset( $bfb_pair['source'] ) && ( 'builder' !== $bfb_pair['source'] ) ) {
597 return '';
598 }
599
600 $containers[] = $bfb_pair;
601
602 if ( isset( $bfb_pair['pair'] ) && is_array( $bfb_pair['pair'] ) ) {
603 $containers[] = $bfb_pair['pair'];
604 }
605
606 if ( isset( $bfb_pair['form'] ) && is_array( $bfb_pair['form'] ) ) {
607 $containers[] = $bfb_pair['form'];
608 }
609
610 foreach ( $containers as $container ) {
611
612 if ( isset( $container['form'] ) && is_string( $container['form'] ) ) {
613 return (string) $container['form'];
614 }
615
616 if ( isset( $container['advanced_form'] ) && is_string( $container['advanced_form'] ) ) {
617 return (string) $container['advanced_form'];
618 }
619 }
620
621 return '';
622 }
623
624
625 /**
626 * Extract "content_form" from BFB loader result.
627 *
628 * @param mixed $bfb_pair
629 *
630 * @return string
631 */
632 function wpbc_extract__content_form_from_bfb_pair__for_field_names_listing( $bfb_pair ) {
633
634 $containers = array();
635
636 if ( ! is_array( $bfb_pair ) ) {
637 return '';
638 }
639
640 /**
641 * Safety:
642 * Do not override if loader already fell back to legacy.
643 */
644 if ( isset( $bfb_pair['source'] ) && ( 'builder' !== $bfb_pair['source'] ) ) {
645 return '';
646 }
647
648 $containers[] = $bfb_pair;
649
650 if ( isset( $bfb_pair['pair'] ) && is_array( $bfb_pair['pair'] ) ) {
651 $containers[] = $bfb_pair['pair'];
652 }
653
654 if ( isset( $bfb_pair['form'] ) && is_array( $bfb_pair['form'] ) ) {
655 $containers[] = $bfb_pair['form'];
656 }
657
658 foreach ( $containers as $container ) {
659
660 if ( isset( $container['content'] ) && is_string( $container['content'] ) ) {
661 return (string) $container['content'];
662 }
663
664 if ( isset( $container['content_form'] ) && is_string( $container['content_form'] ) ) {
665 return (string) $container['content_form'];
666 }
667 }
668
669 return '';
670 }
671
672
673 /**
674 * Maybe get booking form pair from BFB using the same resolver chain as front-end rendering.
675 *
676 * @param int $resource_id
677 * @param string $form_name
678 * @param array $ctx
679 *
680 * @return array
681 */
682 function wpbc_get__bfb_booking_form_pair__for_field_names_listing( $resource_id = 1, $form_name = 'standard', $ctx = array() ) {
683
684 $resource_id = absint( $resource_id );
685 $form_name = sanitize_text_field( (string) $form_name );
686 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
687
688 if ( '' === $form_name ) {
689 $form_name = 'standard';
690 }
691
692 $owner_user_id = isset( $ctx['owner_user_id'] ) ? absint( $ctx['owner_user_id'] ) : null;
693
694 if ( ! class_exists( 'WPBC_FE_Form_Source_Resolver' ) ) {
695 return array();
696 }
697
698 if ( ! function_exists( 'wpbc_bfb_get_booking_form_pair' ) ) {
699 return array();
700 }
701
702 $form_status = isset( $ctx['form_status'] ) ? sanitize_key( $ctx['form_status'] ) : 'published';
703
704 if ( in_array( $form_status, array( 'publish', 'published' ), true ) ) {
705 $form_status = 'published';
706 }
707 if ( 'preview' !== $form_status ) {
708 $form_status = 'published';
709 }
710
711 /**
712 * Preview payload shortcut.
713 */
714 if (
715 ( 'preview' === $form_status )
716 && function_exists( 'wpbc_bfb_preview__maybe_get_payload_from_context' )
717 ) {
718 $payload = wpbc_bfb_preview__maybe_get_payload_from_context( $ctx );
719
720 if ( ! empty( $payload ) && is_array( $payload ) ) {
721
722 $pair = array(
723 'form' => isset( $payload['advanced_form'] ) ? (string) $payload['advanced_form'] : '',
724 'content' => isset( $payload['content_form'] ) ? (string) $payload['content_form'] : '',
725 );
726
727 if ( ( '' !== trim( $pair['form'] ) ) || ( '' !== trim( $pair['content'] ) ) ) {
728 return $pair;
729 }
730 }
731 }
732
733 $req = array(
734 'resource_id' => $resource_id,
735 'form_slug' => $form_name,
736 'form_status' => $form_status,
737 'custom_params' => array(),
738 'legacy_instance' => null,
739 'ctx' => $ctx,
740 );
741
742 if ( null !== $owner_user_id ) {
743 $req['owner_user_id'] = $owner_user_id;
744 }
745
746 $resolved = WPBC_FE_Form_Source_Resolver::resolve( $req );
747
748 // Same behavior as your content resolver: preview can fallback to published.
749 if (
750 ( empty( $resolved['engine'] ) || ( 'bfb_db' !== $resolved['engine'] ) )
751 && ( 'preview' === $form_status )
752 ) {
753 $req['form_status'] = 'published';
754 $resolved = WPBC_FE_Form_Source_Resolver::resolve( $req );
755 }
756
757 if ( empty( $resolved['engine'] ) || ( 'bfb_db' !== $resolved['engine'] ) ) {
758 return array();
759 }
760
761 $bfb_loader_args = array();
762
763 if ( ! empty( $resolved['bfb_loader_args'] ) && is_array( $resolved['bfb_loader_args'] ) ) {
764 $bfb_loader_args = $resolved['bfb_loader_args'];
765 }
766
767 $bfb_pair = wpbc_bfb_get_booking_form_pair( $bfb_loader_args );
768
769 $pair = array(
770 'form' => wpbc_extract__advanced_form_from_bfb_pair__for_field_names_listing( $bfb_pair ),
771 'content' => wpbc_extract__content_form_from_bfb_pair__for_field_names_listing( $bfb_pair ),
772 );
773
774 if ( '' === trim( $pair['form'] ) && '' === trim( $pair['content'] ) ) {
775 return array();
776 }
777
778 return $pair;
779 }
780
781
782 /**
783 * Get booking form pair (fields + show template) for listing/inspection purposes.
784 *
785 * - Legacy mode: returns legacy form + content.
786 * - BFB mode: uses resolver and returns advanced_form + content_form.
787 *
788 * @param int $resource_id
789 * @param string $form_name
790 * @param array $ctx
791 *
792 * @return array
793 */
794 function wpbc_get__booking_form_pair__for_field_names_listing( $resource_id = 1, $form_name = 'standard', $ctx = array() ) {
795
796 $resource_id = absint( $resource_id );
797 $form_name = sanitize_text_field( (string) $form_name );
798 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
799
800 if ( '' === $form_name ) {
801 $form_name = 'standard';
802 }
803
804 /**
805 * ---------------------------------------------------------------------
806 * Legacy baseline pair.
807 * ---------------------------------------------------------------------
808 */
809 $pair = array(
810 'form' => '',
811 'content' => '',
812 );
813
814 // Legacy "fields" configuration.
815 $pair['form'] = wpbc_get__booking_form_fields__configuration( $resource_id, $form_name );
816 $pair['form'] = wpbc_bf__replace_custom_html_shortcodes( $pair['form'] );
817
818 // Legacy "show/content" configuration.
819 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
820
821 $pair['content'] = wpbc_simple_form__get_form_show__as_shortcodes();
822 $pair['content'] = wpbc_bf__replace_custom_html_shortcodes( $pair['content'] );
823
824 } else {
825
826 $pair['content'] = get_bk_option( 'booking_form_show' );
827 $pair['content'] = wpbc_bf__replace_custom_html_shortcodes( $pair['content'] );
828
829 if ( class_exists( 'wpdev_bk_biz_m' ) ) {
830
831 $my_booking_form_name = $form_name;
832
833 if ( empty( $my_booking_form_name ) ) {
834 $my_booking_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
835 }
836
837 if ( ( 'standard' !== $my_booking_form_name ) && ( '' !== $my_booking_form_name ) ) {
838 $pair['content'] = apply_bk_filter( 'wpdev_get_booking_form_content', $pair['content'], $my_booking_form_name );
839 }
840
841 $pair['content'] = apply_bk_filter(
842 'wpbc_multiuser_get_booking_form_show_of_regular_user',
843 $pair['content'],
844 $resource_id,
845 $my_booking_form_name
846 );
847 }
848 }
849
850 /**
851 * ---------------------------------------------------------------------
852 * BFB override.
853 * ---------------------------------------------------------------------
854 */
855 $bfb_pair = wpbc_get__bfb_booking_form_pair__for_field_names_listing( $resource_id, $form_name, $ctx );
856
857 if ( ! empty( $bfb_pair ) ) {
858
859 if ( isset( $bfb_pair['form'] ) && ( '' !== trim( (string) $bfb_pair['form'] ) ) ) {
860 $pair['form'] = (string) $bfb_pair['form'];
861 }
862
863 if ( isset( $bfb_pair['content'] ) && ( '' !== trim( (string) $bfb_pair['content'] ) ) ) {
864 $pair['content'] = (string) $bfb_pair['content'];
865 }
866 }
867
868 $pair['form'] = wpbc_bf__replace_custom_html_shortcodes( (string) $pair['form'] );
869 $pair['content'] = wpbc_bf__replace_custom_html_shortcodes( (string) $pair['content'] );
870
871 $pair['form'] = wpbc_lang( (string) $pair['form'] );
872 $pair['content'] = wpbc_lang( (string) $pair['content'] );
873
874
875 return $pair;
876 }
877
878 /**
879 * Get arr of all fields names from all booking forms (including custom).
880 *
881 * Legacy mode:
882 * - Keep previous behavior exactly as before.
883 *
884 * BFB mode:
885 * - Load form names via helper.
886 * - Load each pair through BFB-aware resolver logic.
887 *
888 * @return array
889 */
890 function wpbc_get__in_all_forms__field_names_arr() {
891
892 $booking_form_fields_arr = array();
893
894 $is_bfb_enabled = (
895 class_exists( 'WPBC_Frontend_Settings' )
896 && method_exists( 'WPBC_Frontend_Settings', 'is_bfb_enabled' )
897 && WPBC_Frontend_Settings::is_bfb_enabled( null )
898 );
899
900 // ---------------------------------------------------------------------
901 // Legacy mode: keep previous behavior exactly.
902 // ---------------------------------------------------------------------
903 if ( ! $is_bfb_enabled ) {
904
905 $booking_form_fields_arr[] = array(
906 'name' => 'standard',
907 'form' => wpbc_bf__replace_custom_html_shortcodes( get_bk_option( 'booking_form' ) ),
908 'content' => wpbc_bf__replace_custom_html_shortcodes( get_bk_option( 'booking_form_show' ) ),
909 );
910
911 $is_can = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'only_super_admin' );
912
913 if ( ( $is_can ) || ( get_bk_option( 'booking_is_custom_forms_for_regular_users' ) === 'On' ) ) {
914
915 $booking_forms_extended = get_bk_option( 'booking_forms_extended' );
916 $booking_forms_extended = maybe_unserialize( $booking_forms_extended );
917
918 if ( is_array( $booking_forms_extended ) ) {
919 foreach ( $booking_forms_extended as $form_extended ) {
920 if ( is_array( $form_extended ) ) {
921 $booking_form_fields_arr[] = $form_extended;
922 }
923 }
924 }
925 }
926
927 // ---------------------------------------------------------------------
928 // BFB mode: use new resolver-aware logic.
929 // ---------------------------------------------------------------------
930 } else {
931
932 $ctx = wpbc_get_request_form_context();
933 $ctx['form_status'] = 'published';
934
935 $form_names = array( 'standard' );
936
937 if (
938 class_exists( 'WPBC_FE_Custom_Form_Helper' )
939 && method_exists( 'WPBC_FE_Custom_Form_Helper', 'get_custom_booking_forms_list' )
940 ) {
941 $owner_user_id = 0;
942
943 if ( method_exists( 'WPBC_FE_Custom_Form_Helper', 'wpbc_mu__get_current__owner_user_id' ) ) {
944 $owner_user_id = absint( WPBC_FE_Custom_Form_Helper::wpbc_mu__get_current__owner_user_id() );
945 }
946
947 $ctx['owner_user_id'] = $owner_user_id;
948
949 $forms_list = WPBC_FE_Custom_Form_Helper::get_custom_booking_forms_list(
950 array(
951 'include_standard' => false,
952 'owner_user_id' => $owner_user_id,
953 'statuses' => array( 'published' ),
954 'list_mode' => 'auto',
955 )
956 );
957
958 if ( is_array( $forms_list ) ) {
959 foreach ( $forms_list as $form_data ) {
960
961 if ( empty( $form_data['name'] ) ) {
962 continue;
963 }
964
965 $form_name = sanitize_text_field( (string) $form_data['name'] );
966
967 if ( ( '' !== $form_name ) && ( 'standard' !== $form_name ) ) {
968 $form_names[] = $form_name;
969 }
970 }
971 }
972 } else {
973
974 // Fallback to legacy list only if helper is unavailable.
975 $is_can = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'only_super_admin' );
976
977 if ( ( $is_can ) || ( get_bk_option( 'booking_is_custom_forms_for_regular_users' ) === 'On' ) ) {
978
979 $booking_forms_extended = get_bk_option( 'booking_forms_extended' );
980 $booking_forms_extended = maybe_unserialize( $booking_forms_extended );
981
982 if ( is_array( $booking_forms_extended ) ) {
983 foreach ( $booking_forms_extended as $form_extended_key => $form_extended ) {
984
985 $form_name = '';
986
987 if ( is_array( $form_extended ) && ! empty( $form_extended['name'] ) ) {
988 $form_name = sanitize_text_field( (string) $form_extended['name'] );
989 } elseif ( is_string( $form_extended_key ) ) {
990 $form_name = sanitize_text_field( (string) $form_extended_key );
991 }
992
993 if ( ( '' !== $form_name ) && ( 'standard' !== $form_name ) ) {
994 $form_names[] = $form_name;
995 }
996 }
997 }
998 }
999 }
1000
1001 $form_names = array_values( array_unique( $form_names ) );
1002
1003 foreach ( $form_names as $form_name ) {
1004
1005 $pair = wpbc_get__booking_form_pair__for_field_names_listing(
1006 wpbc_get_default_resource(),
1007 $form_name,
1008 $ctx
1009 );
1010
1011 $booking_form_fields_arr[] = array(
1012 'name' => $form_name,
1013 'form' => $pair['form'],
1014 'content' => $pair['content'],
1015 );
1016 }
1017 }
1018
1019 // ---------------------------------------------------------------------
1020 // Common parser for both legacy and BFB mode.
1021 // ---------------------------------------------------------------------
1022 foreach ( $booking_form_fields_arr as $form_key => $booking_form_element ) {
1023
1024 $booking_form = isset( $booking_form_element['form'] ) ? (string) $booking_form_element['form'] : '';
1025
1026 $types = 'text[*]?|email[*]?|time[*]?|textarea[*]?|select[*]?|selectbox[*]?|checkbox[*]?|radio|acceptance|captchac|captchar|file[*]?|quiz';
1027 $regex = '%\[\s*(' . $types . ')(\s+[a-zA-Z][0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
1028 $regex2 = '%\[\s*(country[*]?|starttime[*]?|endtime[*]?)(\s*[a-zA-Z]*[0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
1029
1030 $fields_matches = array();
1031 $fields_matches2 = array();
1032
1033 $fields_count = preg_match_all( $regex, $booking_form, $fields_matches );
1034 $fields_count2 = preg_match_all( $regex2, $booking_form, $fields_matches2 );
1035
1036 foreach ( $fields_matches2 as $key => $value ) {
1037
1038 if ( 2 === (int) $key ) {
1039 $value = $fields_matches2[1];
1040 }
1041
1042 if ( ! isset( $fields_matches[ $key ] ) || ! is_array( $fields_matches[ $key ] ) ) {
1043 $fields_matches[ $key ] = array();
1044 }
1045
1046 foreach ( $value as $single_value ) {
1047 $fields_matches[ $key ][] = $single_value;
1048 }
1049 }
1050
1051 $fields_count += $fields_count2;
1052
1053 $booking_form_fields_arr[ $form_key ]['num'] = $fields_count;
1054 $booking_form_fields_arr[ $form_key ]['listing'] = array();
1055
1056 if ( ! isset( $fields_matches[1] ) ) {
1057 $fields_matches[1] = array();
1058 }
1059 if ( ! isset( $fields_matches[2] ) ) {
1060 $fields_matches[2] = array();
1061 }
1062
1063 $fields_matches[1] = array_map( 'trim', $fields_matches[1] );
1064 $fields_matches[2] = array_map( 'trim', $fields_matches[2] );
1065
1066 $booking_form_fields_arr[ $form_key ]['listing']['labels'] = array_map( 'ucfirst', $fields_matches[2] );
1067 $booking_form_fields_arr[ $form_key ]['listing']['fields'] = $fields_matches[2];
1068
1069 foreach ( $fields_matches[1] as $key_fm => $value_fm ) {
1070 $fields_matches[1][ $key_fm ] = trim( str_replace( '*', '', $value_fm ) );
1071 }
1072
1073 $booking_form_fields_arr[ $form_key ]['listing']['fields_type'] = $fields_matches[1];
1074
1075 unset( $booking_form_fields_arr[ $form_key ]['form'] );
1076 unset( $booking_form_fields_arr[ $form_key ]['content'] );
1077 }
1078
1079 return $booking_form_fields_arr;
1080 }
1081
1082 /**
1083 * Get options for billing field assignment from all available booking forms.
1084 *
1085 * Important:
1086 * - Collect fields from standard, legacy custom, and BFB forms.
1087 * - Keep option VALUE as plain field name for backwards compatibility.
1088 * - Use the first found label as option title.
1089 *
1090 * @return array
1091 */
1092 function wpbc_get__field_options__for_billing_assignment() {
1093
1094 $field_options = array(
1095 '' => __( 'Please select', 'booking' ),
1096 );
1097
1098 $booking_forms = wpbc_get__in_all_forms__field_names_arr();
1099
1100 if ( empty( $booking_forms ) || ! is_array( $booking_forms ) ) {
1101 return $field_options;
1102 }
1103
1104 $unique_fields = array();
1105
1106 foreach ( $booking_forms as $single_booking_form ) {
1107
1108 if (
1109 empty( $single_booking_form['listing'] )
1110 || ! is_array( $single_booking_form['listing'] )
1111 || empty( $single_booking_form['listing']['fields'] )
1112 || ! is_array( $single_booking_form['listing']['fields'] )
1113 ) {
1114 continue;
1115 }
1116
1117 $field_labels = array();
1118
1119 if (
1120 isset( $single_booking_form['listing']['labels'] )
1121 && is_array( $single_booking_form['listing']['labels'] )
1122 ) {
1123 $field_labels = $single_booking_form['listing']['labels'];
1124 }
1125
1126 foreach ( $single_booking_form['listing']['fields'] as $field_index => $field_name ) {
1127
1128 $field_name = trim( (string) $field_name );
1129
1130 if ( '' === $field_name ) {
1131 continue;
1132 }
1133
1134 // Keep first found label/value pair.
1135 if ( isset( $unique_fields[ $field_name ] ) ) {
1136 continue;
1137 }
1138
1139 $field_label = isset( $field_labels[ $field_index ] ) ? trim( (string) $field_labels[ $field_index ] ) : '';
1140
1141 if ( '' === $field_label ) {
1142 $field_label = $field_name;
1143 }
1144
1145 $unique_fields[ $field_name ] = $field_label;
1146 }
1147 }
1148
1149 foreach ( $unique_fields as $field_name => $field_label ) {
1150 $field_options[ $field_name ] = $field_label;
1151 }
1152
1153 return $field_options;
1154 }
1155
1156 // -------------------------------------------------------------------------------------------------------------
1157
1158 /**
1159 * Get arr with booking form fields values, after parsing form_data. Avoid to use this function in a future.
1160 *
1161 * @param $formdata
1162 * @param $bktype
1163 * @param $booking_form_show
1164 * @param $extended_params
1165 *
1166 * @return array
1167 */
1168 function wpbc__legacy__get_form_content_arr ( $formdata, $bktype =-1, $booking_form_show ='', $extended_params = array() ) {
1169
1170 if ( $bktype == -1 ) {
1171 $bktype = ( function_exists( 'get__default_type' ) ) ? get__default_type() : 1;
1172 }
1173
1174 if ( $booking_form_show === '' ) {
1175
1176 $booking_form_show = wpbc_get__booking_form_data_configuration( $bktype, $formdata );
1177 }
1178
1179 $formdata_array = explode('~',$formdata);
1180 $formdata_array_count = count($formdata_array);
1181 $email_adress='';
1182 $name_of_person = '';
1183 $coupon_code = '';
1184 $secondname_of_person = '';
1185 $visitors_count = 1;
1186 $select_box_selected_items = array();
1187 $check_box_selected_items = array();
1188 $all_fields_array = array();
1189 $all_fields_array_without_types = array();
1190 $checkbox_value=array();
1191
1192 for ( $i=0 ; $i < $formdata_array_count ; $i++) {
1193
1194 if ( empty( $formdata_array[$i] ) ) {
1195 continue;
1196 }
1197
1198 $elemnts = explode('^',$formdata_array[$i]);
1199
1200 $type = $elemnts[0];
1201 $element_name = $elemnts[1];
1202 $value = $elemnts[2];
1203
1204 //FixIn: 9.7.4.1 - escape coded html/xss
1205 $value = esc_html( $value );
1206 $value = esc_html( html_entity_decode( $value ) );
1207 $value = nl2br($value); // Add BR instead if /n elements // FixIn: 9.7.4.2.
1208 // Escaping for timeline popovers and for other places
1209 $value = wpbc_string__escape__then_convert__n_amp__html( $value );
1210
1211 $count_pos = strlen( $bktype );
1212
1213 $type_name = $elemnts[1];
1214 $type_name = str_replace('[]','',$type_name);
1215 if ($bktype == substr( $type_name, -1*$count_pos ) ) $type_name = substr( $type_name, 0, -1*$count_pos ); // $type_name = str_replace($bktype,'',$elemnts[1]);
1216
1217 if ( ( ($type_name == 'email') || ($type == 'email') ) && ( empty($email_adress) ) ) $email_adress = $value; // FixIn: 6.0.1.9.
1218 if ( ($type_name == 'coupon') || ($type == 'coupon') ) $coupon_code = $value;
1219 if ( ($type_name == 'name') || ($type == 'name') ) $name_of_person = $value;
1220 if ( ($type_name == 'secondname') || ($type == 'secondname') ) $secondname_of_person = $value;
1221 if ( ($type_name == 'visitors') || ($type == 'visitors') ) $visitors_count = $value;
1222
1223
1224 if ($type == 'checkbox') {
1225 if ($value == 'true') {
1226 $value = strtolower( __( 'yes', 'booking' ) );
1227 }
1228
1229 if ($value == 'false') {
1230 $value = strtolower( __( 'no', 'booking' ) );
1231 }
1232
1233 if ( $value !='' )
1234 if ( ( isset($checkbox_value[ str_replace('[]','',(string) $element_name) ]) ) && ( is_array($checkbox_value[ str_replace('[]','',(string) $element_name) ]) ) ) {
1235 $checkbox_value[ str_replace('[]','',(string) $element_name) ][] = $value;
1236 } else {
1237 $checkbox_value[ str_replace('[]','',(string) $element_name) ] = array($value);
1238 }
1239
1240 $value = '['. $type_name .']'; // FixIn: 6.1.1.14.
1241 }
1242
1243 if ( ( $type == 'select-one') || ( $type == 'selectbox-one') || ( $type == 'select-multiple' ) || ( $type == 'selectbox-multiple' ) || ( $type == 'radio' ) ) { // add all select box selected items to return array
1244 $select_box_selected_items[$type_name] = $value;
1245 }
1246
1247 if ( ($type == 'checkbox') && (isset($checkbox_value)) ) {
1248 if (isset( $checkbox_value[ str_replace('[]','',(string) $element_name) ] )) {
1249 if (is_array( $checkbox_value[ str_replace('[]','',(string) $element_name) ] ))
1250 $current_checkbox_value = implode(', ', $checkbox_value[ str_replace('[]','',(string) $element_name) ] );
1251 else
1252 $current_checkbox_value = $checkbox_value[ str_replace('[]','',(string) $element_name) ] ;
1253 } else {
1254 $current_checkbox_value = '';
1255 }
1256 $all_fields_array[ str_replace('[]','',(string) $element_name) ] = $current_checkbox_value;
1257 $all_fields_array_without_types[ substr( str_replace('[]','',(string) $element_name), 0 , -1*strlen( $bktype ) ) ] = $current_checkbox_value;
1258
1259 $check_box_selected_items[$type_name] = $current_checkbox_value;
1260 } else {
1261
1262 // FixIn: 8.4.2.11.
1263 $all_fields_array_without_types[ substr( str_replace('[]','',(string) $element_name), 0 , -1*strlen( $bktype ) ) ] = $value;
1264 /**
1265 ['_all_'] => $all_fields_array, CONVERT to " AM/PM "
1266 ['_all_fields_'] => $all_fields_array_without_types => in " 24 hour " format - for ability correct calculate Booking > Resources > Advanced cost page.
1267 */
1268 if ( ( $type_name == 'rangetime' ) || ( $type == 'rangetime' ) ) {
1269 $value = wpbc_time_slot_in_format( $value );
1270 }
1271 $all_fields_array[ str_replace('[]','',(string) $element_name) ] = $value;
1272 // FixIn: 8.4.2.11.
1273
1274 }
1275 $is_skip_replace = false; // FixIn: 7.0.1.45.
1276 if ( ( $type == 'radio' ) && empty( $value ) )
1277 $is_skip_replace = true;
1278 if ( ! $is_skip_replace ) {
1279 $booking_form_show = str_replace( '[' . $type_name . ']', $value, $booking_form_show );
1280 }
1281 }
1282
1283 if (! isset($all_fields_array_without_types[ 'booking_resource_id' ])) $all_fields_array_without_types[ 'booking_resource_id' ] = $bktype;
1284 if (! isset($all_fields_array_without_types[ 'resource_id' ])) $all_fields_array_without_types[ 'resource_id' ] = $bktype;
1285 if (! isset($all_fields_array_without_types[ 'type_id' ])) $all_fields_array_without_types[ 'type_id' ] = $bktype;
1286
1287 if (! isset($all_fields_array_without_types[ 'type' ])) $all_fields_array_without_types[ 'type' ] = $bktype;
1288 if (! isset($all_fields_array_without_types[ 'resource' ])) $all_fields_array_without_types[ 'resource' ] = $bktype;
1289
1290 foreach ($extended_params as $key_param=>$value_param) {
1291 if (! isset($all_fields_array_without_types[ $key_param ])) $all_fields_array_without_types[ $key_param ] = $value_param;
1292 }
1293
1294 foreach ( $all_fields_array_without_types as $key_param=>$value_param) { // FixIn: 6.1.1.4.
1295 if ( ( gettype ( $value_param ) != 'array' )
1296 && ( gettype ( $value_param ) != 'object' )
1297 ) {
1298 $booking_form_show = str_replace( '['. $key_param .']', $value_param ,$booking_form_show);
1299
1300 $all_fields_array_without_types[ $key_param ] = str_replace( "&amp;", '&', $value_param ); // FixIn: 7.1.2.12.
1301 }
1302
1303
1304 }
1305 // Remove all shortcodes, which is not replaced early.
1306 $booking_form_show = preg_replace ('/[\s]{0,}\[[a-zA-Z0-9.,-_]{0,}\][\s]{0,}/', '', $booking_form_show); // FixIn: 6.1.1.4.
1307
1308 $booking_form_show = str_replace( "&amp;", '&', $booking_form_show ); // FixIn: 7.1.2.12.
1309
1310 $return_array = array(
1311 'content' => $booking_form_show,
1312 'email' => $email_adress,
1313 'name' => $name_of_person,
1314 'secondname' => $secondname_of_person,
1315 'visitors' => $visitors_count,
1316 'coupon' => $coupon_code,
1317 '_all_' => $all_fields_array,
1318 '_all_fields_' => $all_fields_array_without_types
1319 );
1320
1321 foreach ( $select_box_selected_items as $key => $value ) {
1322 if ( ! isset( $return_array[ $key ] ) ) {
1323 $return_array[ $key ] = $value;
1324 }
1325 }
1326 foreach ( $check_box_selected_items as $key => $value ) {
1327 if ( ! isset( $return_array[ $key ] ) ) {
1328 $return_array[ $key ] = $value;
1329 }
1330 }
1331
1332 return $return_array;
1333 }
1334
1335
1336 /**
1337 * Check whether custom booking forms are enabled for the current user.
1338 *
1339 * This function determines if the current user is allowed to access custom booking forms
1340 * based on plugin version and user permissions. It checks:
1341 * - If the business version of the plugin is active (`wpdev_bk_biz_m` class exists).
1342 * - If the user is a super admin (via `multiuser_is_user_can_be_here` filter).
1343 * - If regular users are allowed to use custom forms (via `booking_is_custom_forms_for_regular_users` option).
1344 *
1345 * @return bool True if custom forms are enabled for the current user; false otherwise.
1346 */
1347 function wpbc_is_custom_forms_enabled() {
1348
1349 if ( ! class_exists( 'wpdev_bk_biz_m' ) ) {
1350 return false;
1351 }
1352
1353 $is_super_admin = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'only_super_admin' );
1354 $custom_forms_allowed = ( 'On' === get_bk_option( 'booking_is_custom_forms_for_regular_users' ) );
1355
1356 return $is_super_admin || $custom_forms_allowed;
1357 }
1358
1359 // ---------------------------------------------------------------------------------------------------------------------
1360 // == Shortcode Replacers ==
1361 // ---------------------------------------------------------------------------------------------------------------------
1362 /**
1363 * Replace folowing shortcodes in the booking form at Booking > Settings > Fields page:
1364 * [bookingresource show='id'] - to booking resource ID
1365 * [bookingresource show='title'] - to booking resource Title
1366 * [bookingresource show='cost'] - to booking resource Cost
1367 * [bookingresource show='capacity'] - to booking resource Capacity
1368 *
1369 * @param string $return_form
1370 * @param int $resource_id
1371 *
1372 * @return string
1373 */
1374 function wpbc_replace_bookingresource_info_in_form( $return_form, $resource_id ) { // FixIn: 5.4.5.4.
1375
1376 $patterns = array();
1377
1378 $parameters = array( 'id', 'title', 'cost', 'capacity' );
1379 foreach ( $parameters as $parameter ) {
1380 $patterns[] = '/\[bookingresource\s*show=\'' . $parameter . '\'\\s*]/';
1381 }
1382
1383 $replaced_form = $return_form;
1384
1385 $replacements = array( $resource_id );
1386
1387 if ( function_exists( 'get_booking_resource_attr' ) ) {
1388 $booking_resource_attr = get_booking_resource_attr( $resource_id );
1389
1390 if ( ! empty( $booking_resource_attr ) ) {
1391
1392 if ( isset( $booking_resource_attr->title ) ) {
1393 $bk_res_title = wpbc_lang( $booking_resource_attr->title );
1394 $replacements[] = $bk_res_title;
1395 } else {
1396 $replacements[] = '';
1397 }
1398
1399 if ( ( class_exists( 'wpdev_bk_biz_s' ) ) && ( isset ( $booking_resource_attr->cost ) ) ) {
1400
1401 $replacements[] = wpbc_get_cost_with_currency_for_user( $booking_resource_attr->cost, $resource_id );
1402
1403 } else {
1404 $replacements[] = '';
1405 }
1406 }
1407 $replaced_form = preg_replace( $patterns, $replacements, $return_form );
1408 }
1409
1410
1411 return $replaced_form;
1412 }