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 / _capacity / get_times_fields.php

get_times_fields.php in Booking Calendar 11.7, at includes/_capacity/get_times_fields.php

497 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5
6 /**
7 * Get Time Slots from booking form for Each date: [ '2023-10-25' => [ '12:20 - 12:55', '13:00 - 14:00' ], '2023-10-26' => [ ... ], ... ]
8 *
9 * @param $resource_id // Resource ID (used in case of MultiUser version for getting specific form for regular users)
10 * @param $custom_form_name // Form name for getting time slots configuration
11 * @param $start_date_unix_timestamp // Unix Timestamp to start dates count
12 * @param $days_count // Int number of how many dates to check
13 *
14 * @return array [ '2023-10-25' => [ '12:20 - 12:55', '13:00 - 14:00' ], '2023-10-26' => [ ... ], ... ]
15 *
16 * Example:
17 * $timeslots_arr = wpbc_get_times_fields_configuration(
18 * 1,
19 * 'my_custom_form_name',
20 * strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( 'now' ) ) ),
21 * 365
22 * );
23 */
24 function wpbc_get_times_fields_configuration__by_dates( $resource_id, $custom_form_name, $start_date_unix_timestamp, $days_count ){
25
26 // Get booking form configuration CONTENT.
27 $booking_form_fields = wpbc_get__booking_form_fields__configuration( $resource_id, $custom_form_name );
28 // FixIn: 10.15.8.2.
29 // In BFB mode use the same resolved form source as the front-end renderer. The legacy loader above stays as a fallback.
30 if ( function_exists( 'wpbc_get__booking_form_pair__for_field_names_listing' ) ) {
31
32 $booking_form_pair = wpbc_get__booking_form_pair__for_field_names_listing( $resource_id, $custom_form_name );
33
34 if (
35 is_array( $booking_form_pair )
36 && isset( $booking_form_pair['form'] )
37 && ( '' !== trim( (string) $booking_form_pair['form'] ) )
38 ) {
39 $booking_form_fields = (string) $booking_form_pair['form'];
40 }
41 }
42
43 // -----------------------------------------------------------------------------------------------------------------
44 // Get conditional sections, if exist
45 // -----------------------------------------------------------------------------------------------------------------
46 /**
47 * result=[ weekday-condition=[ 0=[
48 * type = "weekday"
49 * value = "*"
50 * options = false
51 * content = "\r\n Default: [selectbox rangetime "10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00" "13:00 - 14:00" "14:00 - 15:00" "15:00 - 16:00" "16:00 - 17:00" "17:00 - 18:00"]\r\n"
52 * structure = "[condition name="weekday-condition" type="weekday" value="*"]\r\n Default: [selectbox rangetime "10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00" "13:00 - 14:00" "14:00 - 15:00" "15:00 - 16:00" "16:00 - 17:00" "17:00 - 18:00"]\r\n[/condition]"
53 * 1 = [
54 * type = "weekday"
55 * value = "1,2"
56 * options = false
57 * content = " \r\n Monday, Tuesday: [selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]\r\n"
58 * structure = "[condition name="weekday-condition" type="weekday" value="1,2"] \r\n Monday, Tuesday: [selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]\r\n[/condition]"
59 * 2 = [ ... ]
60 */
61 $cached__sql_data__season_filters_arr = array();
62 if ( function_exists( 'wpbc_conditions_form__get_sections' ) ) {
63
64 $conditions = wpbc_conditions_form__get_sections( $booking_form_fields );
65
66 if ( ! empty( $conditions ) ) {
67
68 // ["High season", "Weekend season"]
69 $season_names_arr = wpbc_conditions_get_season_titles_arr( $booking_form_fields );
70
71 // Load all seasons from DB to cache them - for having only one SQL request to DB, if needed
72 $cached__sql_data__season_filters_arr = wpbc_db_get_season_data_arr__for_these_season_names( $season_names_arr );
73 }
74
75 } else {
76 $conditions = array();
77 }
78 // -----------------------------------------------------------------------------------------------------------------
79
80
81 // -----------------------------------------------------------------------------------------------------------------
82 // Get Time Slots [ "10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ] if NO conditional sections - time slot the same for all dates. Get them once here.
83 // -----------------------------------------------------------------------------------------------------------------
84 if ( empty( $conditions ) ) {
85 $time_slots_arr = wpbc_get_timeslots__from_content( $booking_form_fields );
86 } else {
87 $time_slots_arr = array();
88 }
89 // -----------------------------------------------------------------------------------------------------------------
90
91
92 // -----------------------------------------------------------------------------------------------------------------
93 // Here Go for each DATE
94 // -----------------------------------------------------------------------------------------------------------------
95 $dates_times_fields_arr = array();
96 for ( $day_num = 0; $day_num <= $days_count; $day_num ++ ) {
97
98 $my_day_tag = gmdate( 'Y-m-d', $start_date_unix_timestamp ); // '2023-07-19'
99
100 if ( ( ! empty( $conditions ) ) && ( function_exists( 'wpbc_conditions_form__get_section__depend_from_date' ) ) ){
101
102 // Get booking form conditional section depends on DATE
103 $section_content = wpbc_conditions_form__get_section__depend_from_date( $conditions, $my_day_tag , $cached__sql_data__season_filters_arr );
104
105 // Get Time Slots [ "10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ]
106 $time_slots_in_section_arr = wpbc_get_timeslots__from_content( $section_content );
107
108 $dates_times_fields_arr[ $my_day_tag ] = $time_slots_in_section_arr;
109 } else {
110 $dates_times_fields_arr[ $my_day_tag ] = $time_slots_arr;
111 }
112
113 // DEBUG: $dates_times_fields_arr[$my_day_tag] = array( '00:00 - 24:00', '10:00 - 12:00', '12:00 - 14:00', '14:00 - 16:00', '16:00 - 18:00', '18:00 - 20:00' , '09:01 - 09:02' );
114
115 $start_date_unix_timestamp += 24 * 60 * 60; // Go next day + 1 day
116 }
117
118 return $dates_times_fields_arr;
119 }
120
121
122
123 /**
124 * Get Time-Slots (CONVERT READABLE to SECONDS) seconds range array for the specified date from array of dates, where each date include readable time-ranges array
125 *
126 * @param $dates_arr__with__readable_time_slots_arr = [
127 * 2023-10-25 = [],
128 * 2023-10-26 = [
129 * 0 => '00:00 - 24:00',
130 * 1 => '10:00 - 12:00',
131 * 2 => '12:00 - 14:00',
132 * 3 => '14:00 - 16:00',
133 * 4 => '16:00 - 18:00',
134 * 5 => '18:00 - 20:00',
135 * 6 => '09:01 - 09:02',
136 * ],
137 * 2023-10-27 = [], ...
138 * ]
139 * @param $my_day_tag = '2023-10-25'
140 *
141 * @return array|string[] IF ( $my_day_tag == '2023-10-25' ) => []
142 * OR ( $my_day_tag == '2023-10-26' ) => [
143 * 0 => '1 - 86392',
144 * 1 => '36011 - 43192',
145 * 2 => '43211 - 50392',
146 * 3 => '50411 - 57592',
147 * 4 => '57611 - 64792',
148 * 5 => '64811 - 71992',
149 * 6 => '32471 - 32512',
150 * ]
151 */
152 function wpbc_get__time_range_in_seconds_arr__for_date( $dates_arr__with__readable_time_slots_arr, $my_day_tag ) {
153
154 if ( isset( $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] ) ) {
155
156
157 $bookingform__available_timeslots_in_sec__arr = array_map(
158 function ( $time_slot_readable ) {
159 return wpbc_convert__readable_time_slot__to__time_range_in_seconds( $time_slot_readable );
160 }
161 , $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] );
162
163 return $bookingform__available_timeslots_in_sec__arr;
164 }
165
166 return array();
167 }
168
169
170 // =====================================================================================================================
171 // Get Time-Slots from CONTENT (booking form configuration)
172 // =====================================================================================================================
173
174 /**
175 * Get Time-Slots ["10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ] time slot values from booking form content ( or part of booking form configuration)
176 *
177 * @param string $content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
178 *
179 * @return array
180 */
181 function wpbc_get_timeslots__from_content( $content ){
182
183 $time_slots_arr =array();
184
185 // == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] -------------------------------------------------
186 if ( false !== strpos( $content, ' rangetime ' ) ) {
187
188 $time_slots_arr = wpbc_get_timeslots__for_rangetime( $content );
189
190 } else {
191
192 // == START TIME | END TIME == --------------------------------------------------------------------------------
193 if (
194 ( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime]
195 && ( false !== strpos( $content, ' endtime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox endtime and skip this [endtime]
196 ){
197 // Now we need to create the smallest time intervals ( 60 sec. ) for each start / end times.
198
199 // == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ]
200 $start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content , 60 );
201 foreach ( $start_time_slots_arr as $ts ) {
202 $time_slots_arr[] = $ts;
203 }
204
205 // == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ]
206 $end_time_slots_arr = wpbc_get_timeslots__for_endtime( $content , 60 );
207 foreach ( $end_time_slots_arr as $ts ) {
208 $time_slots_arr[] = $ts;
209 }
210 }
211
212 // == START TIME | DURATION TIME == ---------------------------------------------------------------------------
213 if (
214 ( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime]
215 && ( false !== strpos( $content, ' durationtime ' ) )
216 ){
217 // 900 -> e.g.: '00:15'
218 $min_time_duration = wpbc_get__min_time_duration__in_seconds( $content );
219
220 if ( false !== $min_time_duration ) {
221
222 // == START TIME == [ '10:00 - 10:15', '12:00 - 12:15' ... ]
223 $start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content, $min_time_duration );
224 foreach ( $start_time_slots_arr as $ts ) {
225 $time_slots_arr[] = $ts;
226 }
227 }
228
229 }
230 }
231
232 return $time_slots_arr;
233 }
234
235
236 /**
237 * Get values for the first shortcode with this name.
238 *
239 * The legacy parser is intentionally kept as the first path. The fallback handles Booking Form Builder exports
240 * that can include named attributes before option tokens, e.g.:
241 * [selectbox* starttime default="08:00" "08:00" "09:00"].
242 *
243 * @param string $shortcode_name
244 * @param string $bookingform_configuration_content
245 *
246 * @return false|array
247 */
248 function wpbc_get_times_fields__get_first_shortcode_values( $shortcode_name, $bookingform_configuration_content ) {
249
250 $shortcode_values = wpbc_parse_form__get_first_shortcode_values( $shortcode_name, $bookingform_configuration_content );
251
252 if ( ! empty( $shortcode_values ) ) {
253 return $shortcode_values;
254 }
255
256 $shortcode_values = wpbc_get_times_fields__parse_first_shortcode_values__fallback( $shortcode_name, $bookingform_configuration_content );
257
258 return empty( $shortcode_values ) ? false : $shortcode_values;
259 }
260
261
262 /**
263 * Fallback parser for time select shortcodes with named attributes.
264 *
265 * @param string $shortcode_name
266 * @param string $bookingform_configuration_content
267 *
268 * @return array
269 */
270 function wpbc_get_times_fields__parse_first_shortcode_values__fallback( $shortcode_name, $bookingform_configuration_content ) {
271
272 $shortcode_name = trim( (string) $shortcode_name );
273
274 if ( '' === $shortcode_name ) {
275 return array();
276 }
277
278 $regex = "%\\[\\s*([a-zA-Z][0-9a-zA-Z_-]*\\*?)\\s+" . preg_quote( $shortcode_name, '%' ) . "(?=\\s|\\])((?:[^\\]\"']+|\"[^\"]*\"|'[^']*')*)\\]%u";
279
280 if ( ! preg_match( $regex, (string) $bookingform_configuration_content, $shortcode_match ) ) {
281 return array();
282 }
283
284 $shortcode_text = $shortcode_match[0];
285 $consumed_attr_value_offsets = array();
286
287 if ( preg_match_all( '/\b[a-zA-Z][\w:-]*\s*[:=]\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s\]"]+))/u', $shortcode_text, $attr_matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ) {
288 foreach ( $attr_matches as $attr_match ) {
289 if ( isset( $attr_match[1][1] ) && ( $attr_match[1][1] >= 0 ) ) {
290 $consumed_attr_value_offsets[ $attr_match[1][1] ] = true;
291 }
292 if ( isset( $attr_match[2][1] ) && ( $attr_match[2][1] >= 0 ) ) {
293 $consumed_attr_value_offsets[ $attr_match[2][1] ] = true;
294 }
295 }
296 }
297
298 if ( ! preg_match_all( '/"([^"]*)"|\'([^\']*)\'/u', $shortcode_text, $quoted_matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ) {
299 return array();
300 }
301
302 $shortcode_values = array();
303
304 foreach ( $quoted_matches as $quoted_match ) {
305
306 if ( isset( $quoted_match[1][1] ) && ( $quoted_match[1][1] >= 0 ) ) {
307 $raw_value = $quoted_match[1][0];
308 $raw_offset = $quoted_match[1][1];
309 } else {
310 $raw_value = $quoted_match[2][0];
311 $raw_offset = $quoted_match[2][1];
312 }
313
314 if ( isset( $consumed_attr_value_offsets[ $raw_offset ] ) ) {
315 continue;
316 }
317
318 $value_parts = explode( '@@', $raw_value, 2 );
319
320 if ( 2 === count( $value_parts ) ) {
321 $shortcode_values[] = array(
322 'title' => $value_parts[0],
323 'value' => $value_parts[1],
324 );
325 } else {
326 $shortcode_values[] = array(
327 'value' => $value_parts[0],
328 );
329 }
330 }
331
332 return $shortcode_values;
333 }
334
335
336 /**
337 * Get Time-Slots == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] from provided booking form configuration content
338 *
339 * @param $bookingform_configuration_content - '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
340 *
341 * @return array
342 *
343 * $time_slots_arr = [
344 * [0] => 00:00 - 24:00
345 * [1] => 10:00 - 12:00
346 * [2] => 12:00 - 14:00
347 * [3] => 14:00 - 16:00
348 * [4] => 16:00 - 18:00
349 * [5] => 18:00 - 20:00
350 * ]
351 * OR
352 * []
353 */
354 function wpbc_get_timeslots__for_rangetime( $bookingform_configuration_content ) {
355
356 $time_slots_arr = array();
357 $shortcode_values = wpbc_get_times_fields__get_first_shortcode_values( 'rangetime', $bookingform_configuration_content );
358
359 if ( ! empty( $shortcode_values ) ) {
360 foreach ( $shortcode_values as $timeslot_value ) {
361
362 if ( ! empty( $timeslot_value['value'] ) ) {
363 $time_slots_arr[] = $timeslot_value['value'];
364 }
365 }
366 }
367
368 return $time_slots_arr;
369 }
370
371
372 /**
373 * Get Time-Slots from == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ] searching in provided booking form configuration content
374 *
375 * @param string $bookingform_configuration_content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
376 * @param int $time_shift_duration_in_seconds 60
377 *
378 * @return array
379 *
380 * $time_slots_arr = [
381 * [1] => 10:00 - 10:01
382 * [2] => 12:00 - 12:01
383 * [3] => 14:00 - 14:01
384 * [4] => 16:00 - 16:01
385 * [5] => 18:00 - 18:01
386 * ]
387 * OR
388 * []
389 */
390 function wpbc_get_timeslots__for_starttime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) {
391
392 $time_slots_arr = array();
393
394 $shortcode_values = wpbc_get_times_fields__get_first_shortcode_values( 'starttime', $bookingform_configuration_content );
395 if ( ! empty( $shortcode_values ) ) {
396 foreach ( $shortcode_values as $timeslot_value ) {
397
398 if ( ! empty( $timeslot_value['value'] ) ) {
399 // '12:00:00' -> 43190
400 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
401
402 $time_in_seconds += $time_shift_duration_in_seconds;
403
404 // 43250 -> '12:01:00'
405 $time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds );
406 $time_his = substr( $time_his, 0, 5 );
407
408 // '12:00 - 12:01'
409 $time_slots_arr[] = $timeslot_value['value'] . ' - ' . $time_his;
410 }
411 }
412 }
413
414
415 return $time_slots_arr;
416 }
417
418
419 /**
420 * Get Time-Slots from == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ] searching in provided booking form configuration content
421 *
422 * @param string $bookingform_configuration_content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
423 * @param int $time_shift_duration_in_seconds 60
424 *
425 * @return array
426 *
427 * $time_slots_arr = [
428 * [1] => 09:59 - 10:00
429 * [2] => 11:59 - 12:00
430 * [3] => 13:59 - 14:00
431 * [4] => 15:59 - 16:00
432 * [5] => 17:59 - 18:00
433 * ]
434 * OR
435 * []
436 */
437 function wpbc_get_timeslots__for_endtime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) {
438
439 $time_slots_arr = array();
440
441 $shortcode_values = wpbc_get_times_fields__get_first_shortcode_values( 'endtime', $bookingform_configuration_content );
442 if ( ! empty( $shortcode_values ) ) {
443 foreach ( $shortcode_values as $timeslot_value ) {
444
445 if ( ! empty( $timeslot_value['value'] ) ) {
446 // '12:00:00' -> 43190
447 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
448
449 $time_in_seconds -= $time_shift_duration_in_seconds;
450
451 // 43130 -> '11:59:00'
452 $time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds );
453 $time_his = substr( $time_his, 0, 5 );
454
455 // '11:59 - 12:00'
456 $time_slots_arr[] = $time_his . ' - ' . $timeslot_value['value'];
457 }
458 }
459 }
460
461 return $time_slots_arr;
462 }
463
464
465 /**
466 * Get minimum duration of time ( from [selectbox durationtime ... ] ) or FALSE, if no such shortcode or no values.
467 *
468 * @param $bookingform_configuration_content
469 *
470 * @return false|int
471 */
472 function wpbc_get__min_time_duration__in_seconds( $bookingform_configuration_content ){
473
474 $min_time_duration = false;
475 $time_duration_in_seconds_arr = array();
476
477 $shortcode_values = wpbc_get_times_fields__get_first_shortcode_values( 'durationtime', $bookingform_configuration_content );
478 if ( ! empty( $shortcode_values ) ) {
479 foreach ( $shortcode_values as $timeslot_value ) {
480
481 if ( ! empty( $timeslot_value['value'] ) ) {
482 // '00:15' -> 900
483 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
484
485 // [ 900 , ... ]
486 $time_duration_in_seconds_arr[] = $time_in_seconds;
487 }
488 }
489 }
490
491 if ( ! empty( $time_duration_in_seconds_arr ) ) {
492 $min_time_duration = min( $time_duration_in_seconds_arr );
493 }
494
495 return $min_time_duration;
496 }
497