PluginProbe
Booking Calendar / 11.7
Booking Calendar v11.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 11.7, at includes/_functions/booking_data__parse.php

1,220 lines 45.2 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 $pair = wpbc_get__bfb_booking_form_pair__for_field_names_listing( $resource_id, $form_name, wpbc_get_request_form_context() );
370
371 // Keep the complete BFB source intact. Languages use separate custom forms.
372 $booking_form_configuration = isset( $pair['form'] ) ? (string) $pair['form'] : '';
373
374 return $booking_form_configuration;
375 }
376
377
378 /**
379 * Load BFB preview payload from transient (for booking created inside preview iframe).
380 *
381 * @param array $ctx
382 * @return array|null
383 */
384 function wpbc_bfb_preview__maybe_get_payload_from_context( $ctx ) {
385
386 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
387
388 $user_id = isset( $ctx['user_id'] ) ? absint( $ctx['user_id'] ) : 0;
389 $form_id = isset( $ctx['wpbc_bfb_preview_form_id'] ) ? absint( $ctx['wpbc_bfb_preview_form_id'] ) : 0;
390 $token = isset( $ctx['wpbc_bfb_preview_token'] ) ? sanitize_key( $ctx['wpbc_bfb_preview_token'] ) : '';
391 $nonce = isset( $ctx['wpbc_bfb_preview_nonce'] ) ? sanitize_text_field( $ctx['wpbc_bfb_preview_nonce'] ) : '';
392
393 if ( $user_id <= 0 || $form_id <= 0 || '' === $token ) {
394 return null;
395 }
396
397 // Optional hardening: verify nonce if present.
398 if ( '' !== $nonce ) {
399 if ( ! wp_verify_nonce( $nonce, 'wpbc_bfb_preview_' . $token ) ) {
400 return null;
401 }
402 }
403
404 // Optional hardening: capability check (preview is admin-only anyway).
405 $cap = ( function_exists( 'wpbc_bfb_get_manage_cap' ) ) ? wpbc_bfb_get_manage_cap() : 'manage_options';
406 if ( ! is_user_logged_in() || ! current_user_can( $cap ) ) {
407 return null;
408 }
409
410 $transient_key = 'wpbc_bfb_preview_' . $user_id . '_' . $form_id . '_' . $token;
411
412 $data = get_transient( $transient_key );
413 if ( empty( $data ) || ! is_array( $data ) ) {
414 return null;
415 }
416
417 return $data;
418 }
419
420
421 /**
422 * Get configuration of 'BOOKING FORM DATA' from - Booking > Settings > Form page
423 *
424 * - 1. <= BS : 'Booking form show' configuration from standard form in versions up to Business Small version ,
425 * - 2 >= BM : If form data has field of custom form, then from custom form configuration,
426 * - 3 >= BM : Otherwise if resource has default custom booking form, then from this default custom booking form
427 * - 4 = MU : specific form of specific WP User
428 * - 5 finally : simple standard form
429 *
430 * @param int $resource_id
431 * @param string $form_data Form data here, required in >= BM, because such form_data can contain fields about used custom booking form
432 *
433 * @return string
434 */
435 function wpbc_get__booking_form_data_configuration( $resource_id = 1, $form_data = '', $params = array() ) {
436
437 // FixIn: 2026-02-05 - allow preview booking submissions to use preview "content_form".
438 $ctx = wp_parse_args( ( is_array( $params ) ? $params : array() ), wpbc_get_request_form_context() );
439
440 $form_status = ( isset( $ctx['form_status'] ) ) ? sanitize_key( $ctx['form_status'] ) : 'published';
441
442 if ( 'preview' === $form_status ) {
443
444 $payload = wpbc_bfb_preview__maybe_get_payload_from_context( $ctx );
445
446 // content_form is exactly the "Booking form show" template exported for preview session.
447 if ( ! empty( $payload ) && isset( $payload['content_form'] ) && ( '' !== (string) $payload['content_form'] ) ) {
448 return (string) $payload['content_form'];
449 }
450 }
451
452 $resource_id = (int) $resource_id;
453 $form_data = (string) $form_data;
454 $my_booking_form_name = 'standard';
455
456 // Maybe get name of custom booking form, that was saved in the booking data.
457 if ( false !== strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) ) { // FixIn: 9.4.3.12.
458
459 $custom_booking_form_name = substr( $form_data, strpos( $form_data, 'wpbc_custom_booking_form' . $resource_id . '^' ) + strlen( 'wpbc_custom_booking_form' . $resource_id . '^' ) );
460
461 if ( false !== strpos( $custom_booking_form_name, '~' ) ) {
462 $custom_booking_form_name = substr( $custom_booking_form_name, 0, strpos( $custom_booking_form_name, '~' ) );
463 }
464 if ( ! empty( $custom_booking_form_name ) ) {
465 $my_booking_form_name = $custom_booking_form_name;
466 }
467 }
468
469
470 if ( 'standard' === $my_booking_form_name || '' === $my_booking_form_name ) {
471 $resource_default_custom_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
472 if ( ! empty( $resource_default_custom_form_name ) ) {
473 $my_booking_form_name = $resource_default_custom_form_name;
474 }
475 }
476
477 $booking_form_show = WPBC_BFB_Booking_Data_Content_Resolver::maybe_override_booking_form_show_by_bfb( '', $resource_id, $my_booking_form_name, $form_data, $ctx );
478
479 // Keep the complete BFB content template intact. Languages use separate custom forms.
480 return $booking_form_show;
481 }
482
483
484 // -------------------------------------------------------------------------------------------------------------
485
486 /**
487 * Replace resource_ID of booking in 'form_data'
488 * Useful, when we need to save booking from one resource into another.
489 *
490 * @param $booking__form_data__str 'selectbox-multiple^rangetime2[]^18:00 - 20:00~checkbox^fee2[]^true~text^name2^John~text^secondname2^Smith...'
491 * @param $new_resource_id 10
492 * @param $old_resource_id 2
493 *
494 * @return string 'selectbox-multiple^rangetime10[]^18:00 - 20:00~checkbox^fee10[]^true~text^name10^John~text^secondname10^Smith...'
495 */
496 function wpbc_get__form_data__with_replaced_id( $booking__form_data__str, $new_resource_id, $old_resource_id ) {
497
498 $all_booking_data_arr = wpbc_get_parsed_booking_data_arr( $booking__form_data__str, $old_resource_id );
499
500 $new__form_data__str = wpbc_encode_booking_data_to_string( $all_booking_data_arr, $new_resource_id );
501
502 return $new__form_data__str;
503 }
504
505 // FixIn: 10.15.4.1.
506 /**
507 * Extract "advanced_form" from BFB loader result.
508 *
509 * @param mixed $bfb_pair
510 *
511 * @return string
512 */
513 function wpbc_extract__advanced_form_from_bfb_pair__for_field_names_listing( $bfb_pair ) {
514
515 $containers = array();
516
517 if ( ! is_array( $bfb_pair ) ) {
518 return '';
519 }
520
521 /**
522 * Safety:
523 * Do not override if loader already fell back to legacy.
524 */
525 if ( isset( $bfb_pair['source'] ) && ( 'builder' !== $bfb_pair['source'] ) ) {
526 return '';
527 }
528
529 $containers[] = $bfb_pair;
530
531 if ( isset( $bfb_pair['pair'] ) && is_array( $bfb_pair['pair'] ) ) {
532 $containers[] = $bfb_pair['pair'];
533 }
534
535 if ( isset( $bfb_pair['form'] ) && is_array( $bfb_pair['form'] ) ) {
536 $containers[] = $bfb_pair['form'];
537 }
538
539 foreach ( $containers as $container ) {
540
541 if ( isset( $container['form'] ) && is_string( $container['form'] ) ) {
542 return (string) $container['form'];
543 }
544
545 if ( isset( $container['advanced_form'] ) && is_string( $container['advanced_form'] ) ) {
546 return (string) $container['advanced_form'];
547 }
548 }
549
550 return '';
551 }
552
553
554 /**
555 * Extract "content_form" from BFB loader result.
556 *
557 * @param mixed $bfb_pair
558 *
559 * @return string
560 */
561 function wpbc_extract__content_form_from_bfb_pair__for_field_names_listing( $bfb_pair ) {
562
563 $containers = array();
564
565 if ( ! is_array( $bfb_pair ) ) {
566 return '';
567 }
568
569 /**
570 * Safety:
571 * Do not override if loader already fell back to legacy.
572 */
573 if ( isset( $bfb_pair['source'] ) && ( 'builder' !== $bfb_pair['source'] ) ) {
574 return '';
575 }
576
577 $containers[] = $bfb_pair;
578
579 if ( isset( $bfb_pair['pair'] ) && is_array( $bfb_pair['pair'] ) ) {
580 $containers[] = $bfb_pair['pair'];
581 }
582
583 if ( isset( $bfb_pair['form'] ) && is_array( $bfb_pair['form'] ) ) {
584 $containers[] = $bfb_pair['form'];
585 }
586
587 foreach ( $containers as $container ) {
588
589 if ( isset( $container['content'] ) && is_string( $container['content'] ) ) {
590 return (string) $container['content'];
591 }
592
593 if ( isset( $container['content_form'] ) && is_string( $container['content_form'] ) ) {
594 return (string) $container['content_form'];
595 }
596 }
597
598 return '';
599 }
600
601
602 /**
603 * Maybe get booking form pair from BFB using the same resolver chain as front-end rendering.
604 *
605 * @param int $resource_id
606 * @param string $form_name
607 * @param array $ctx
608 *
609 * @return array
610 */
611 function wpbc_get__bfb_booking_form_pair__for_field_names_listing( $resource_id = 1, $form_name = 'standard', $ctx = array() ) {
612
613 $resource_id = absint( $resource_id );
614 $form_name = sanitize_text_field( (string) $form_name );
615 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
616
617 if ( '' === $form_name ) {
618 $form_name = 'standard';
619 }
620
621 $owner_user_id = isset( $ctx['owner_user_id'] ) ? absint( $ctx['owner_user_id'] ) : null;
622
623 if ( ! class_exists( 'WPBC_FE_Form_Source_Resolver' ) ) {
624 return array();
625 }
626
627 if ( ! function_exists( 'wpbc_bfb_get_booking_form_pair' ) ) {
628 return array();
629 }
630
631 $form_status = isset( $ctx['form_status'] ) ? sanitize_key( $ctx['form_status'] ) : 'published';
632
633 if ( in_array( $form_status, array( 'publish', 'published' ), true ) ) {
634 $form_status = 'published';
635 }
636 if ( 'preview' !== $form_status ) {
637 $form_status = 'published';
638 }
639
640 /**
641 * Preview payload shortcut.
642 */
643 if (
644 ( 'preview' === $form_status )
645 && function_exists( 'wpbc_bfb_preview__maybe_get_payload_from_context' )
646 ) {
647 $payload = wpbc_bfb_preview__maybe_get_payload_from_context( $ctx );
648
649 if ( ! empty( $payload ) && is_array( $payload ) ) {
650
651 $pair = array(
652 'form' => isset( $payload['advanced_form'] ) ? (string) $payload['advanced_form'] : '',
653 'content' => isset( $payload['content_form'] ) ? (string) $payload['content_form'] : '',
654 );
655
656 if ( ( '' !== trim( $pair['form'] ) ) || ( '' !== trim( $pair['content'] ) ) ) {
657 return $pair;
658 }
659 }
660 }
661
662 $req = array(
663 'resource_id' => $resource_id,
664 'form_slug' => $form_name,
665 'form_status' => $form_status,
666 'custom_params' => array(),
667 'legacy_instance' => null,
668 'ctx' => $ctx,
669 );
670
671 if ( null !== $owner_user_id ) {
672 $req['owner_user_id'] = $owner_user_id;
673 }
674
675 $resolved = WPBC_FE_Form_Source_Resolver::resolve( $req );
676
677 // Same behavior as your content resolver: preview can fallback to published.
678 if (
679 ( empty( $resolved['engine'] ) || ( 'bfb_db' !== $resolved['engine'] ) )
680 && ( 'preview' === $form_status )
681 ) {
682 $req['form_status'] = 'published';
683 $resolved = WPBC_FE_Form_Source_Resolver::resolve( $req );
684 }
685
686 if ( empty( $resolved['engine'] ) || ( 'bfb_db' !== $resolved['engine'] ) ) {
687 return array();
688 }
689
690 $bfb_loader_args = array();
691
692 if ( ! empty( $resolved['bfb_loader_args'] ) && is_array( $resolved['bfb_loader_args'] ) ) {
693 $bfb_loader_args = $resolved['bfb_loader_args'];
694 }
695
696 $bfb_pair = wpbc_bfb_get_booking_form_pair( $bfb_loader_args );
697
698 $pair = array(
699 'form' => wpbc_extract__advanced_form_from_bfb_pair__for_field_names_listing( $bfb_pair ),
700 'content' => wpbc_extract__content_form_from_bfb_pair__for_field_names_listing( $bfb_pair ),
701 );
702
703 if ( '' === trim( $pair['form'] ) && '' === trim( $pair['content'] ) ) {
704 return array();
705 }
706
707 return $pair;
708 }
709
710
711 /**
712 * Get booking form pair (fields + show template) for listing/inspection purposes.
713 *
714 * Uses BFB resolver and returns advanced_form + content_form.
715 *
716 * @param int $resource_id
717 * @param string $form_name
718 * @param array $ctx
719 *
720 * @return array
721 */
722 function wpbc_get__booking_form_pair__for_field_names_listing( $resource_id = 1, $form_name = 'standard', $ctx = array() ) {
723
724 $resource_id = absint( $resource_id );
725 $form_name = sanitize_text_field( (string) $form_name );
726 $ctx = ( is_array( $ctx ) ) ? $ctx : array();
727
728 if ( '' === $form_name ) {
729 $form_name = 'standard';
730 }
731
732 $bfb_pair = wpbc_get__bfb_booking_form_pair__for_field_names_listing( $resource_id, $form_name, $ctx );
733 $pair = is_array( $bfb_pair ) ? $bfb_pair : array();
734 $pair = wp_parse_args( $pair, array( 'form' => '', 'content' => '' ) );
735
736 $pair['form'] = wpbc_bf__replace_custom_html_shortcodes( (string) $pair['form'] );
737 $pair['content'] = wpbc_bf__replace_custom_html_shortcodes( (string) $pair['content'] );
738
739 // Do not interpret inline [lang=LOCALE] markers as whole-form language blocks.
740 return $pair;
741 }
742
743 /**
744 * Get arr of all fields names from all booking forms (including custom).
745 *
746 * Load form names through BFB helpers and inspect each stored form pair.
747 *
748 * @return array
749 */
750 function wpbc_get__in_all_forms__field_names_arr() {
751
752 $booking_form_fields_arr = array();
753
754 $ctx = wpbc_get_request_form_context();
755 $ctx['form_status'] = 'published';
756
757 $form_names = array( 'standard' );
758
759 if (
760 class_exists( 'WPBC_FE_Custom_Form_Helper' )
761 && method_exists( 'WPBC_FE_Custom_Form_Helper', 'get_custom_booking_forms_list' )
762 ) {
763 $owner_user_id = 0;
764
765 if ( method_exists( 'WPBC_FE_Custom_Form_Helper', 'wpbc_mu__get_current__owner_user_id' ) ) {
766 $owner_user_id = absint( WPBC_FE_Custom_Form_Helper::wpbc_mu__get_current__owner_user_id() );
767 }
768
769 $ctx['owner_user_id'] = $owner_user_id;
770
771 $forms_list = WPBC_FE_Custom_Form_Helper::get_custom_booking_forms_list(
772 array(
773 'include_standard' => false,
774 'owner_user_id' => $owner_user_id,
775 'statuses' => array( 'published' ),
776 'list_mode' => 'bfb',
777 )
778 );
779
780 if ( is_array( $forms_list ) ) {
781 foreach ( $forms_list as $form_data ) {
782
783 if ( empty( $form_data['name'] ) ) {
784 continue;
785 }
786
787 $form_name = sanitize_text_field( (string) $form_data['name'] );
788
789 if ( ( '' !== $form_name ) && ( 'standard' !== $form_name ) ) {
790 $form_names[] = $form_name;
791 }
792 }
793 }
794 }
795
796 $form_names = array_values( array_unique( $form_names ) );
797
798 foreach ( $form_names as $form_name ) {
799
800 $pair = wpbc_get__booking_form_pair__for_field_names_listing(
801 wpbc_get_default_resource(),
802 $form_name,
803 $ctx
804 );
805
806 $booking_form_fields_arr[] = array(
807 'name' => $form_name,
808 'form' => $pair['form'],
809 'content' => $pair['content'],
810 );
811 }
812
813 // ---------------------------------------------------------------------
814 // Common parser.
815 // ---------------------------------------------------------------------
816 foreach ( $booking_form_fields_arr as $form_key => $booking_form_element ) {
817
818 $booking_form = isset( $booking_form_element['form'] ) ? (string) $booking_form_element['form'] : '';
819
820 $types = 'text[*]?|email[*]?|time[*]?|textarea[*]?|select[*]?|selectbox[*]?|checkbox[*]?|radio|acceptance|captchac|captchar|file[*]?|quiz';
821 $regex = '%\[\s*(' . $types . ')(\s+[a-zA-Z][0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
822 $regex2 = '%\[\s*(country[*]?|starttime[*]?|endtime[*]?)(\s*[a-zA-Z]*[0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
823
824 $fields_matches = array();
825 $fields_matches2 = array();
826
827 $fields_count = preg_match_all( $regex, $booking_form, $fields_matches );
828 $fields_count2 = preg_match_all( $regex2, $booking_form, $fields_matches2 );
829
830 foreach ( $fields_matches2 as $key => $value ) {
831
832 if ( 2 === (int) $key ) {
833 $value = $fields_matches2[1];
834 }
835
836 if ( ! isset( $fields_matches[ $key ] ) || ! is_array( $fields_matches[ $key ] ) ) {
837 $fields_matches[ $key ] = array();
838 }
839
840 foreach ( $value as $single_value ) {
841 $fields_matches[ $key ][] = $single_value;
842 }
843 }
844
845 $fields_count += $fields_count2;
846
847 $booking_form_fields_arr[ $form_key ]['num'] = $fields_count;
848 $booking_form_fields_arr[ $form_key ]['listing'] = array();
849
850 if ( ! isset( $fields_matches[1] ) ) {
851 $fields_matches[1] = array();
852 }
853 if ( ! isset( $fields_matches[2] ) ) {
854 $fields_matches[2] = array();
855 }
856
857 $fields_matches[1] = array_map( 'trim', $fields_matches[1] );
858 $fields_matches[2] = array_map( 'trim', $fields_matches[2] );
859
860 $booking_form_fields_arr[ $form_key ]['listing']['labels'] = array_map( 'ucfirst', $fields_matches[2] );
861 $booking_form_fields_arr[ $form_key ]['listing']['fields'] = $fields_matches[2];
862
863 foreach ( $fields_matches[1] as $key_fm => $value_fm ) {
864 $fields_matches[1][ $key_fm ] = trim( str_replace( '*', '', $value_fm ) );
865 }
866
867 $booking_form_fields_arr[ $form_key ]['listing']['fields_type'] = $fields_matches[1];
868
869 unset( $booking_form_fields_arr[ $form_key ]['form'] );
870 unset( $booking_form_fields_arr[ $form_key ]['content'] );
871 }
872
873 return $booking_form_fields_arr;
874 }
875
876 /**
877 * Get options for billing field assignment from all available booking forms.
878 *
879 * Important:
880 * - Collect fields from standard, legacy custom, and BFB forms.
881 * - Keep option VALUE as plain field name for backwards compatibility.
882 * - Use the first found label as option title.
883 *
884 * @return array
885 */
886 function wpbc_get__field_options__for_billing_assignment() {
887
888 $field_options = array(
889 '' => __( 'Please select', 'booking' ),
890 );
891
892 $booking_forms = wpbc_get__in_all_forms__field_names_arr();
893
894 if ( empty( $booking_forms ) || ! is_array( $booking_forms ) ) {
895 return $field_options;
896 }
897
898 $unique_fields = array();
899
900 foreach ( $booking_forms as $single_booking_form ) {
901
902 if (
903 empty( $single_booking_form['listing'] )
904 || ! is_array( $single_booking_form['listing'] )
905 || empty( $single_booking_form['listing']['fields'] )
906 || ! is_array( $single_booking_form['listing']['fields'] )
907 ) {
908 continue;
909 }
910
911 $field_labels = array();
912
913 if (
914 isset( $single_booking_form['listing']['labels'] )
915 && is_array( $single_booking_form['listing']['labels'] )
916 ) {
917 $field_labels = $single_booking_form['listing']['labels'];
918 }
919
920 foreach ( $single_booking_form['listing']['fields'] as $field_index => $field_name ) {
921
922 $field_name = trim( (string) $field_name );
923
924 if ( '' === $field_name ) {
925 continue;
926 }
927
928 // Keep first found label/value pair.
929 if ( isset( $unique_fields[ $field_name ] ) ) {
930 continue;
931 }
932
933 $field_label = isset( $field_labels[ $field_index ] ) ? trim( (string) $field_labels[ $field_index ] ) : '';
934
935 if ( '' === $field_label ) {
936 $field_label = $field_name;
937 }
938
939 $unique_fields[ $field_name ] = $field_label;
940 }
941 }
942
943 foreach ( $unique_fields as $field_name => $field_label ) {
944 $field_options[ $field_name ] = $field_label;
945 }
946
947 return $field_options;
948 }
949
950 // -------------------------------------------------------------------------------------------------------------
951
952 /**
953 * Get arr with booking form fields values, after parsing form_data. Avoid to use this function in a future.
954 *
955 * @param $formdata
956 * @param $bktype
957 * @param $booking_form_show
958 * @param $extended_params
959 *
960 * @return array
961 */
962 function wpbc__legacy__get_form_content_arr ( $formdata, $bktype =-1, $booking_form_show ='', $extended_params = array() ) {
963
964 if ( $bktype == -1 ) {
965 $bktype = ( function_exists( 'get__default_type' ) ) ? get__default_type() : 1;
966 }
967
968 if ( $booking_form_show === '' ) {
969
970 $booking_form_show = wpbc_get__booking_form_data_configuration( $bktype, $formdata );
971 }
972
973 $formdata_array = explode('~',$formdata);
974 $formdata_array_count = count($formdata_array);
975 $email_adress='';
976 $name_of_person = '';
977 $coupon_code = '';
978 $secondname_of_person = '';
979 $visitors_count = 1;
980 $select_box_selected_items = array();
981 $check_box_selected_items = array();
982 $all_fields_array = array();
983 $all_fields_array_without_types = array();
984 $checkbox_value=array();
985
986 for ( $i=0 ; $i < $formdata_array_count ; $i++) {
987
988 if ( empty( $formdata_array[$i] ) ) {
989 continue;
990 }
991
992 $elemnts = explode('^',$formdata_array[$i]);
993
994 $type = $elemnts[0];
995 $element_name = $elemnts[1];
996 $value = $elemnts[2];
997
998 //FixIn: 9.7.4.1 - escape coded html/xss
999 $value = esc_html( $value );
1000 $value = esc_html( html_entity_decode( $value ) );
1001 $value = nl2br($value); // Add BR instead if /n elements // FixIn: 9.7.4.2.
1002 // Escaping for timeline popovers and for other places
1003 $value = wpbc_string__escape__then_convert__n_amp__html( $value );
1004
1005 $count_pos = strlen( $bktype );
1006
1007 $type_name = $elemnts[1];
1008 $type_name = str_replace('[]','',$type_name);
1009 if ($bktype == substr( $type_name, -1*$count_pos ) ) $type_name = substr( $type_name, 0, -1*$count_pos ); // $type_name = str_replace($bktype,'',$elemnts[1]);
1010
1011 if ( ( ($type_name == 'email') || ($type == 'email') ) && ( empty($email_adress) ) ) $email_adress = $value; // FixIn: 6.0.1.9.
1012 if ( ($type_name == 'coupon') || ($type == 'coupon') ) $coupon_code = $value;
1013 if ( ($type_name == 'name') || ($type == 'name') ) $name_of_person = $value;
1014 if ( ($type_name == 'secondname') || ($type == 'secondname') ) $secondname_of_person = $value;
1015 if ( ($type_name == 'visitors') || ($type == 'visitors') ) $visitors_count = $value;
1016
1017
1018 if ($type == 'checkbox') {
1019 if ($value == 'true') {
1020 $value = strtolower( __( 'yes', 'booking' ) );
1021 }
1022
1023 if ($value == 'false') {
1024 $value = strtolower( __( 'no', 'booking' ) );
1025 }
1026
1027 if ( $value !='' )
1028 if ( ( isset($checkbox_value[ str_replace('[]','',(string) $element_name) ]) ) && ( is_array($checkbox_value[ str_replace('[]','',(string) $element_name) ]) ) ) {
1029 $checkbox_value[ str_replace('[]','',(string) $element_name) ][] = $value;
1030 } else {
1031 $checkbox_value[ str_replace('[]','',(string) $element_name) ] = array($value);
1032 }
1033
1034 $value = '['. $type_name .']'; // FixIn: 6.1.1.14.
1035 }
1036
1037 if ( ( $type == 'select-one') || ( $type == 'selectbox-one') || ( $type == 'select-multiple' ) || ( $type == 'selectbox-multiple' ) || ( $type == 'radio' ) ) { // add all select box selected items to return array
1038 $select_box_selected_items[$type_name] = $value;
1039 }
1040
1041 if ( ($type == 'checkbox') && (isset($checkbox_value)) ) {
1042 if (isset( $checkbox_value[ str_replace('[]','',(string) $element_name) ] )) {
1043 if (is_array( $checkbox_value[ str_replace('[]','',(string) $element_name) ] ))
1044 $current_checkbox_value = implode(', ', $checkbox_value[ str_replace('[]','',(string) $element_name) ] );
1045 else
1046 $current_checkbox_value = $checkbox_value[ str_replace('[]','',(string) $element_name) ] ;
1047 } else {
1048 $current_checkbox_value = '';
1049 }
1050 $all_fields_array[ str_replace('[]','',(string) $element_name) ] = $current_checkbox_value;
1051 $all_fields_array_without_types[ substr( str_replace('[]','',(string) $element_name), 0 , -1*strlen( $bktype ) ) ] = $current_checkbox_value;
1052
1053 $check_box_selected_items[$type_name] = $current_checkbox_value;
1054 } else {
1055
1056 // FixIn: 8.4.2.11.
1057 $all_fields_array_without_types[ substr( str_replace('[]','',(string) $element_name), 0 , -1*strlen( $bktype ) ) ] = $value;
1058 /**
1059 ['_all_'] => $all_fields_array, CONVERT to " AM/PM "
1060 ['_all_fields_'] => $all_fields_array_without_types => in " 24 hour " format - for ability correct calculate Booking > Resources > Advanced cost page.
1061 */
1062 if ( ( $type_name == 'rangetime' ) || ( $type == 'rangetime' ) ) {
1063 $value = wpbc_time_slot_in_format( $value );
1064 }
1065 $all_fields_array[ str_replace('[]','',(string) $element_name) ] = $value;
1066 // FixIn: 8.4.2.11.
1067
1068 }
1069 $is_skip_replace = false; // FixIn: 7.0.1.45.
1070 if ( ( $type == 'radio' ) && empty( $value ) )
1071 $is_skip_replace = true;
1072 if ( ! $is_skip_replace ) {
1073 $booking_form_show = str_replace( '[' . $type_name . ']', $value, $booking_form_show );
1074 }
1075 }
1076
1077 if ( ! isset( $all_fields_array_without_types['booking_resource_id'] ) ) {
1078 $all_fields_array_without_types['booking_resource_id'] = $bktype;
1079 }
1080 if ( ! isset( $all_fields_array_without_types['resource_id'] ) ) {
1081 $all_fields_array_without_types['resource_id'] = $bktype;
1082 }
1083 if ( ! isset( $all_fields_array_without_types['type_id'] ) ) {
1084 $all_fields_array_without_types['type_id'] = $bktype;
1085 }
1086
1087 if ( ! isset( $all_fields_array_without_types['type'] ) ) {
1088 $all_fields_array_without_types['type'] = $bktype;
1089 }
1090 if ( ! isset( $all_fields_array_without_types['resource'] ) ) {
1091 $all_fields_array_without_types['resource'] = wpbc_get_resource_title( $bktype );
1092 }
1093 if ( ! isset( $all_fields_array_without_types['resource_title'] ) ) {
1094 $all_fields_array_without_types['resource_title'] = wpbc_get_resource_title( $bktype );
1095 }
1096
1097 foreach ($extended_params as $key_param=>$value_param) {
1098 if (! isset($all_fields_array_without_types[ $key_param ])) $all_fields_array_without_types[ $key_param ] = $value_param;
1099 }
1100
1101 foreach ( $all_fields_array_without_types as $key_param=>$value_param) { // FixIn: 6.1.1.4.
1102 if ( ( gettype ( $value_param ) != 'array' )
1103 && ( gettype ( $value_param ) != 'object' )
1104 ) {
1105 $booking_form_show = str_replace( '['. $key_param .']', $value_param ,$booking_form_show);
1106
1107 $all_fields_array_without_types[ $key_param ] = str_replace( "&amp;", '&', $value_param ); // FixIn: 7.1.2.12.
1108 }
1109
1110
1111 }
1112 // Remove all shortcodes, which is not replaced early.
1113 $booking_form_show = preg_replace ('/[\s]{0,}\[[a-zA-Z0-9.,-_]{0,}\][\s]{0,}/', '', $booking_form_show); // FixIn: 6.1.1.4.
1114
1115 $booking_form_show = str_replace( "&amp;", '&', $booking_form_show ); // FixIn: 7.1.2.12.
1116
1117 $return_array = array(
1118 'content' => $booking_form_show,
1119 'email' => $email_adress,
1120 'name' => $name_of_person,
1121 'secondname' => $secondname_of_person,
1122 'visitors' => $visitors_count,
1123 'coupon' => $coupon_code,
1124 '_all_' => $all_fields_array,
1125 '_all_fields_' => $all_fields_array_without_types
1126 );
1127
1128 foreach ( $select_box_selected_items as $key => $value ) {
1129 if ( ! isset( $return_array[ $key ] ) ) {
1130 $return_array[ $key ] = $value;
1131 }
1132 }
1133 foreach ( $check_box_selected_items as $key => $value ) {
1134 if ( ! isset( $return_array[ $key ] ) ) {
1135 $return_array[ $key ] = $value;
1136 }
1137 }
1138
1139 return $return_array;
1140 }
1141
1142
1143 /**
1144 * Check whether custom booking forms are enabled for the current user.
1145 *
1146 * This function determines if the current user is allowed to access custom booking forms
1147 * based on plugin version and user permissions. It checks:
1148 * - If the business version of the plugin is active (`wpdev_bk_biz_m` class exists).
1149 * - If the user is a super admin (via `multiuser_is_user_can_be_here` filter).
1150 * - If regular users are allowed to use custom forms (via `booking_is_custom_forms_for_regular_users` option).
1151 *
1152 * @return bool True if custom forms are enabled for the current user; false otherwise.
1153 */
1154 function wpbc_is_custom_forms_enabled() {
1155
1156 if ( ! class_exists( 'wpdev_bk_biz_m' ) ) {
1157 return false;
1158 }
1159
1160 $is_super_admin = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'only_super_admin' );
1161 $custom_forms_allowed = ( 'On' === get_bk_option( 'booking_is_custom_forms_for_regular_users' ) );
1162
1163 return $is_super_admin || $custom_forms_allowed;
1164 }
1165
1166 // ---------------------------------------------------------------------------------------------------------------------
1167 // == Shortcode Replacers ==
1168 // ---------------------------------------------------------------------------------------------------------------------
1169 /**
1170 * Replace folowing shortcodes in the booking form at Booking > Settings > Fields page:
1171 * [bookingresource show='id'] - to booking resource ID
1172 * [bookingresource show='title'] - to booking resource Title
1173 * [bookingresource show='cost'] - to booking resource Cost
1174 * [bookingresource show='capacity'] - to booking resource Capacity
1175 *
1176 * @param string $return_form
1177 * @param int $resource_id
1178 *
1179 * @return string
1180 */
1181 function wpbc_replace_bookingresource_info_in_form( $return_form, $resource_id ) { // FixIn: 5.4.5.4.
1182
1183 $patterns = array();
1184
1185 $parameters = array( 'id', 'title', 'cost', 'capacity' );
1186 foreach ( $parameters as $parameter ) {
1187 $patterns[] = '/\[bookingresource\s*show=\'' . $parameter . '\'\\s*]/';
1188 }
1189
1190 $replaced_form = $return_form;
1191
1192 $replacements = array( $resource_id );
1193
1194 if ( function_exists( 'get_booking_resource_attr' ) ) {
1195 $booking_resource_attr = get_booking_resource_attr( $resource_id );
1196
1197 if ( ! empty( $booking_resource_attr ) ) {
1198
1199 if ( isset( $booking_resource_attr->title ) ) {
1200 $bk_res_title = wpbc_lang( $booking_resource_attr->title );
1201 $replacements[] = $bk_res_title;
1202 } else {
1203 $replacements[] = '';
1204 }
1205
1206 if ( ( class_exists( 'wpdev_bk_biz_s' ) ) && ( isset ( $booking_resource_attr->cost ) ) ) {
1207
1208 $replacements[] = wpbc_get_cost_with_currency_for_user( $booking_resource_attr->cost, $resource_id );
1209
1210 } else {
1211 $replacements[] = '';
1212 }
1213 }
1214 $replaced_form = preg_replace( $patterns, $replacements, $return_form );
1215 }
1216
1217
1218 return $replaced_form;
1219 }
1220