| 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( date( '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 |
|
| 29 |
// ----------------------------------------------------------------------------------------------------------------- |
| 30 |
// Get conditional sections, if exist |
| 31 |
// ----------------------------------------------------------------------------------------------------------------- |
| 32 |
/** |
| 33 |
* result=[ weekday-condition=[ 0=[ |
| 34 |
* type = "weekday" |
| 35 |
* value = "*" |
| 36 |
* options = false |
| 37 |
* 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" |
| 38 |
* 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]" |
| 39 |
* 1 = [ |
| 40 |
* type = "weekday" |
| 41 |
* value = "1,2" |
| 42 |
* options = false |
| 43 |
* content = " \r\n Monday, Tuesday: [selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]\r\n" |
| 44 |
* 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]" |
| 45 |
* 2 = [ ... ] |
| 46 |
*/ |
| 47 |
$cached__sql_data__season_filters_arr = array(); |
| 48 |
if ( function_exists( 'wpbc_conditions_form__get_sections' ) ) { |
| 49 |
|
| 50 |
$conditions = wpbc_conditions_form__get_sections( $booking_form_fields ); |
| 51 |
|
| 52 |
if ( ! empty( $conditions ) ) { |
| 53 |
|
| 54 |
// ["High season", "Weekend season"] |
| 55 |
$season_names_arr = wpbc_conditions_get_season_titles_arr( $booking_form_fields ); |
| 56 |
|
| 57 |
// Load all seasons from DB to cache them - for having only one SQL request to DB, if needed |
| 58 |
$cached__sql_data__season_filters_arr = wpbc_db_get_season_data_arr__for_these_season_names( $season_names_arr ); |
| 59 |
} |
| 60 |
|
| 61 |
} else { |
| 62 |
$conditions = array(); |
| 63 |
} |
| 64 |
// ----------------------------------------------------------------------------------------------------------------- |
| 65 |
|
| 66 |
|
| 67 |
// ----------------------------------------------------------------------------------------------------------------- |
| 68 |
// 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. |
| 69 |
// ----------------------------------------------------------------------------------------------------------------- |
| 70 |
if ( empty( $conditions ) ) { |
| 71 |
$time_slots_arr = wpbc_get_timeslots__from_content( $booking_form_fields ); |
| 72 |
} else { |
| 73 |
$time_slots_arr = array(); |
| 74 |
} |
| 75 |
// ----------------------------------------------------------------------------------------------------------------- |
| 76 |
|
| 77 |
|
| 78 |
// ----------------------------------------------------------------------------------------------------------------- |
| 79 |
// Here Go for each DATE |
| 80 |
// ----------------------------------------------------------------------------------------------------------------- |
| 81 |
$dates_times_fields_arr = array(); |
| 82 |
for ( $day_num = 0; $day_num <= $days_count; $day_num ++ ) { |
| 83 |
|
| 84 |
$my_day_tag = date( 'Y-m-d', $start_date_unix_timestamp ); // '2023-07-19' |
| 85 |
|
| 86 |
if ( ( ! empty( $conditions ) ) && ( function_exists( 'wpbc_conditions_form__get_section__depend_from_date' ) ) ){ |
| 87 |
|
| 88 |
// Get booking form conditional section depends on DATE |
| 89 |
$section_content = wpbc_conditions_form__get_section__depend_from_date( $conditions, $my_day_tag , $cached__sql_data__season_filters_arr ); |
| 90 |
|
| 91 |
// Get Time Slots [ "10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ] |
| 92 |
$time_slots_in_section_arr = wpbc_get_timeslots__from_content( $section_content ); |
| 93 |
|
| 94 |
$dates_times_fields_arr[ $my_day_tag ] = $time_slots_in_section_arr; |
| 95 |
} else { |
| 96 |
$dates_times_fields_arr[ $my_day_tag ] = $time_slots_arr; |
| 97 |
} |
| 98 |
|
| 99 |
// 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' ); |
| 100 |
|
| 101 |
$start_date_unix_timestamp += 24 * 60 * 60; // Go next day + 1 day |
| 102 |
} |
| 103 |
|
| 104 |
return $dates_times_fields_arr; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* 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 |
| 111 |
* |
| 112 |
* @param $dates_arr__with__readable_time_slots_arr = [ |
| 113 |
* 2023-10-25 = [], |
| 114 |
* 2023-10-26 = [ |
| 115 |
* 0 => '00:00 - 24:00', |
| 116 |
* 1 => '10:00 - 12:00', |
| 117 |
* 2 => '12:00 - 14:00', |
| 118 |
* 3 => '14:00 - 16:00', |
| 119 |
* 4 => '16:00 - 18:00', |
| 120 |
* 5 => '18:00 - 20:00', |
| 121 |
* 6 => '09:01 - 09:02', |
| 122 |
* ], |
| 123 |
* 2023-10-27 = [], ... |
| 124 |
* ] |
| 125 |
* @param $my_day_tag = '2023-10-25' |
| 126 |
* |
| 127 |
* @return array|string[] IF ( $my_day_tag == '2023-10-25' ) => [] |
| 128 |
* OR ( $my_day_tag == '2023-10-26' ) => [ |
| 129 |
* 0 => '1 - 86392', |
| 130 |
* 1 => '36011 - 43192', |
| 131 |
* 2 => '43211 - 50392', |
| 132 |
* 3 => '50411 - 57592', |
| 133 |
* 4 => '57611 - 64792', |
| 134 |
* 5 => '64811 - 71992', |
| 135 |
* 6 => '32471 - 32512', |
| 136 |
* ] |
| 137 |
*/ |
| 138 |
function wpbc_get__time_range_in_seconds_arr__for_date( $dates_arr__with__readable_time_slots_arr, $my_day_tag ) { |
| 139 |
|
| 140 |
if ( isset( $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] ) ) { |
| 141 |
|
| 142 |
|
| 143 |
$bookingform__available_timeslots_in_sec__arr = array_map( |
| 144 |
function ( $time_slot_readable ) { |
| 145 |
return wpbc_convert__readable_time_slot__to__time_range_in_seconds( $time_slot_readable ); |
| 146 |
} |
| 147 |
, $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] ); |
| 148 |
|
| 149 |
return $bookingform__available_timeslots_in_sec__arr; |
| 150 |
} |
| 151 |
|
| 152 |
return array(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
// ===================================================================================================================== |
| 157 |
// Get Time-Slots from CONTENT (booking form configuration) |
| 158 |
// ===================================================================================================================== |
| 159 |
|
| 160 |
/** |
| 161 |
* 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) |
| 162 |
* |
| 163 |
* @param string $content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...' |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
function wpbc_get_timeslots__from_content( $content ){ |
| 168 |
|
| 169 |
$time_slots_arr =array(); |
| 170 |
|
| 171 |
// == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] ------------------------------------------------- |
| 172 |
if ( false !== strpos( $content, ' rangetime ' ) ) { |
| 173 |
|
| 174 |
$time_slots_arr = wpbc_get_timeslots__for_rangetime( $content ); |
| 175 |
|
| 176 |
} else { |
| 177 |
|
| 178 |
// == START TIME | END TIME == -------------------------------------------------------------------------------- |
| 179 |
if ( |
| 180 |
( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime] |
| 181 |
&& ( false !== strpos( $content, ' endtime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox endtime and skip this [endtime] |
| 182 |
){ |
| 183 |
// Now we need to create the smallest time intervals ( 60 sec. ) for each start / end times. |
| 184 |
|
| 185 |
// == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ] |
| 186 |
$start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content , 60 ); |
| 187 |
foreach ( $start_time_slots_arr as $ts ) { |
| 188 |
$time_slots_arr[] = $ts; |
| 189 |
} |
| 190 |
|
| 191 |
// == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ] |
| 192 |
$end_time_slots_arr = wpbc_get_timeslots__for_endtime( $content , 60 ); |
| 193 |
foreach ( $end_time_slots_arr as $ts ) { |
| 194 |
$time_slots_arr[] = $ts; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// == START TIME | DURATION TIME == --------------------------------------------------------------------------- |
| 199 |
if ( |
| 200 |
( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime] |
| 201 |
&& ( false !== strpos( $content, ' durationtime ' ) ) |
| 202 |
){ |
| 203 |
// 900 -> e.g.: '00:15' |
| 204 |
$min_time_duration = wpbc_get__min_time_duration__in_seconds( $content ); |
| 205 |
|
| 206 |
if ( false !== $min_time_duration ) { |
| 207 |
|
| 208 |
// == START TIME == [ '10:00 - 10:15', '12:00 - 12:15' ... ] |
| 209 |
$start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content, $min_time_duration ); |
| 210 |
foreach ( $start_time_slots_arr as $ts ) { |
| 211 |
$time_slots_arr[] = $ts; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return $time_slots_arr; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Get Time-Slots == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] from provided booking form configuration content |
| 224 |
* |
| 225 |
* @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"] ...' |
| 226 |
* |
| 227 |
* @return array |
| 228 |
* |
| 229 |
* $time_slots_arr = [ |
| 230 |
* [0] => 00:00 - 24:00 |
| 231 |
* [1] => 10:00 - 12:00 |
| 232 |
* [2] => 12:00 - 14:00 |
| 233 |
* [3] => 14:00 - 16:00 |
| 234 |
* [4] => 16:00 - 18:00 |
| 235 |
* [5] => 18:00 - 20:00 |
| 236 |
* ] |
| 237 |
* OR |
| 238 |
* [] |
| 239 |
*/ |
| 240 |
function wpbc_get_timeslots__for_rangetime( $bookingform_configuration_content ) { |
| 241 |
|
| 242 |
$time_slots_arr = array(); |
| 243 |
$shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'rangetime', $bookingform_configuration_content ); |
| 244 |
|
| 245 |
if ( ! empty( $shortcode_values ) ) { |
| 246 |
foreach ( $shortcode_values as $timeslot_value ) { |
| 247 |
|
| 248 |
if ( ! empty( $timeslot_value['value'] ) ) { |
| 249 |
$time_slots_arr[] = $timeslot_value['value']; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return $time_slots_arr; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* Get Time-Slots from == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ] searching in provided booking form configuration content |
| 260 |
* |
| 261 |
* @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"] ...' |
| 262 |
* @param int $time_shift_duration_in_seconds 60 |
| 263 |
* |
| 264 |
* @return array |
| 265 |
* |
| 266 |
* $time_slots_arr = [ |
| 267 |
* [1] => 10:00 - 10:01 |
| 268 |
* [2] => 12:00 - 12:01 |
| 269 |
* [3] => 14:00 - 14:01 |
| 270 |
* [4] => 16:00 - 16:01 |
| 271 |
* [5] => 18:00 - 18:01 |
| 272 |
* ] |
| 273 |
* OR |
| 274 |
* [] |
| 275 |
*/ |
| 276 |
function wpbc_get_timeslots__for_starttime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) { |
| 277 |
|
| 278 |
$time_slots_arr = array(); |
| 279 |
|
| 280 |
$shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'starttime', $bookingform_configuration_content ); |
| 281 |
if ( ! empty( $shortcode_values ) ) { |
| 282 |
foreach ( $shortcode_values as $timeslot_value ) { |
| 283 |
|
| 284 |
if ( ! empty( $timeslot_value['value'] ) ) { |
| 285 |
// '12:00:00' -> 43190 |
| 286 |
$time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' ); |
| 287 |
|
| 288 |
$time_in_seconds += $time_shift_duration_in_seconds; |
| 289 |
|
| 290 |
// 43250 -> '12:01:00' |
| 291 |
$time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds ); |
| 292 |
$time_his = substr( $time_his, 0, 5 ); |
| 293 |
|
| 294 |
// '12:00 - 12:01' |
| 295 |
$time_slots_arr[] = $timeslot_value['value'] . ' - ' . $time_his; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
return $time_slots_arr; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Get Time-Slots from == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ] searching in provided booking form configuration content |
| 307 |
* |
| 308 |
* @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"] ...' |
| 309 |
* @param int $time_shift_duration_in_seconds 60 |
| 310 |
* |
| 311 |
* @return array |
| 312 |
* |
| 313 |
* $time_slots_arr = [ |
| 314 |
* [1] => 09:59 - 10:00 |
| 315 |
* [2] => 11:59 - 12:00 |
| 316 |
* [3] => 13:59 - 14:00 |
| 317 |
* [4] => 15:59 - 16:00 |
| 318 |
* [5] => 17:59 - 18:00 |
| 319 |
* ] |
| 320 |
* OR |
| 321 |
* [] |
| 322 |
*/ |
| 323 |
function wpbc_get_timeslots__for_endtime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) { |
| 324 |
|
| 325 |
$time_slots_arr = array(); |
| 326 |
|
| 327 |
$shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'endtime', $bookingform_configuration_content ); |
| 328 |
if ( ! empty( $shortcode_values ) ) { |
| 329 |
foreach ( $shortcode_values as $timeslot_value ) { |
| 330 |
|
| 331 |
if ( ! empty( $timeslot_value['value'] ) ) { |
| 332 |
// '12:00:00' -> 43190 |
| 333 |
$time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' ); |
| 334 |
|
| 335 |
$time_in_seconds -= $time_shift_duration_in_seconds; |
| 336 |
|
| 337 |
// 43130 -> '11:59:00' |
| 338 |
$time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds ); |
| 339 |
$time_his = substr( $time_his, 0, 5 ); |
| 340 |
|
| 341 |
// '11:59 - 12:00' |
| 342 |
$time_slots_arr[] = $time_his . ' - ' . $timeslot_value['value']; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return $time_slots_arr; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Get minimum duration of time ( from [selectbox durationtime ... ] ) or FALSE, if no such shortcode or no values. |
| 353 |
* |
| 354 |
* @param $bookingform_configuration_content |
| 355 |
* |
| 356 |
* @return false|int |
| 357 |
*/ |
| 358 |
function wpbc_get__min_time_duration__in_seconds( $bookingform_configuration_content ){ |
| 359 |
|
| 360 |
$min_time_duration = false; |
| 361 |
$time_duration_in_seconds_arr = array(); |
| 362 |
|
| 363 |
$shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'durationtime', $bookingform_configuration_content ); |
| 364 |
if ( ! empty( $shortcode_values ) ) { |
| 365 |
foreach ( $shortcode_values as $timeslot_value ) { |
| 366 |
|
| 367 |
if ( ! empty( $timeslot_value['value'] ) ) { |
| 368 |
// '00:15' -> 900 |
| 369 |
$time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' ); |
| 370 |
|
| 371 |
// [ 900 , ... ] |
| 372 |
$time_duration_in_seconds_arr[] = $time_in_seconds; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! empty( $time_duration_in_seconds_arr ) ) { |
| 378 |
$min_time_duration = min( $time_duration_in_seconds_arr ); |
| 379 |
} |
| 380 |
|
| 381 |
return $min_time_duration; |
| 382 |
} |