PluginProbe
Booking Calendar / 10.10.2
Booking Calendar v10.10.2
11.8.2 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 All 202 releases
booking / core / wpbc-dates.php

wpbc-dates.php in Booking Calendar 10.10.2, at core/wpbc-dates.php

986 lines 34.8 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 Dates Functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 29.09.2015
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
16
17
18 /**
19 * Get the dates in different formats
20 *
21 * @param type $str_dates__dd_mm_yyyy
22 * @param type $booking_type
23 * @param type $booking_form_data
24 * @return
25 array(
26 'string' => "30.02.2014, 31.02.2014, 01.03.2014"
27 , 'array' => array("2014-02-30", "2014-02-31", ....
28 , 'start_time' => array('00','00','00');
29 , 'end_time' => array('00','00','00');
30 );
31 */
32 function wpbc_get_dates_in_diff_formats( $str_dates__dd_mm_yyyy, $booking_type, $booking_form_data ){
33
34 $str_dates__dd_mm_yyyy = str_replace( '|', ',', $str_dates__dd_mm_yyyy); // Check this for some old versions of plugin
35
36 if ( strpos($str_dates__dd_mm_yyyy,' - ') !== false ) { // Recheck for any type of Range Days Formats
37 $arr_check_in_out_dates = explode(' - ', $str_dates__dd_mm_yyyy );
38 $str_dates__dd_mm_yyyy = wpbc_get_comma_seprated_dates_from_to_day( $arr_check_in_out_dates[0], $arr_check_in_out_dates[1] );
39 }
40
41
42 $days_array = explode( ',', $str_dates__dd_mm_yyyy ); // Create dates Array
43 $only_days = array();
44
45 foreach ($days_array as $new_day) {
46
47 if ( ! empty($new_day) ) {
48
49 $new_day = trim( $new_day );
50
51 $new_day = str_replace( '-', '.', $new_day);
52
53 $new_day = explode( '.', $new_day);
54
55 $only_days[] = sprintf( "%04d-%02d-%02d", intval( $new_day[2] ), intval( $new_day[1] ), intval( $new_day[0] ) );
56 }
57 }
58 sort($only_days); // Sort Dates
59
60
61 // Get Times from booking form if these fields exist
62 $start_end_time = wpbc_get_times_in_form( $booking_form_data, $booking_type );
63
64 if ( $start_end_time !== false ) {
65 $start_time = $start_end_time[0]; // array('00','00','01');
66 $end_time = $start_end_time[1]; // array('00','00','01');
67
68 if ( ( '00' == $start_time[0] ) && ( '00' == $start_time[1] ) ) { // FixIn: 8.7.8.8.
69 $start_time = array( '00', '00', '00' );
70 $end_time = array( '00', '00', '00' );
71 } else {
72 if ( count( $only_days ) == 1 ) { // add end date if selected 1 day only and times is exist
73 $only_days[] = $only_days[0];
74 }
75 }
76 } else {
77 $start_time = array( '00', '00', '00' );
78 $end_time = array( '00', '00', '00' );
79 }
80
81
82 return array(
83 'string' => $str_dates__dd_mm_yyyy // dd_mm_yyyy
84 , 'array' => $only_days
85 , 'start_time' => $start_time
86 , 'end_time' => $end_time
87 );
88 }
89
90
91 /**
92 * Check for minimum and maximum available times, and restrict value to these limits.
93 *
94 * @param string $time 24:00
95 * @param string $min_time 00:01
96 * @param string $max_time 23:59
97 *
98 * @return string 23:59
99 */
100 function wpbc_check_min_max_available_times( $time = '00:00', $min_time = '00:00', $max_time = '23:59' ) {
101
102 // Time in minutes
103 $time_m = explode( ':', trim( $time ) );
104 $time_m = intval( $time_m[0] ) * 60 + intval( $time_m[1] );
105
106 // Min time in minutes
107 $min_time_m = explode( ':', trim( $min_time ) );
108 $min_time_m = intval( $min_time_m[0] ) * 60 + intval( $min_time_m[1] );
109
110 // Max time in minutes
111 $max_time_m = explode( ':', trim( $max_time ) );
112 $max_time_m = intval( $max_time_m[0] ) * 60 + intval( $max_time_m[1] );
113
114
115 if ( $time_m < $min_time_m ) {
116 $time_m = $min_time_m;
117 }
118
119 if ( $time_m > $max_time_m ) {
120 $time_m = $max_time_m;
121 }
122
123 // Convert time in minutes back to string HH:MM
124 $time_m_h = floor( $time_m / 60 );
125 $time_m_m = $time_m - $time_m_h * 60;
126
127 // Check leading 0
128 if ( $time_m_h < 10 ) {
129 $time_m_h = '0' . $time_m_h;
130 }
131 if ( $time_m_m < 10 ) {
132 $time_m_m = '0' . $time_m_m;
133 }
134
135 return $time_m_h . ':' . $time_m_m;
136 }
137
138
139 /**
140 * Get Times from booking Form, if these times fields exist
141 *
142 * @param type $booking_form_data
143 * @param type $booking_type
144 * @return mixed
145 array ( array('00','00','01'), array('00','00','01') )
146 ||
147 false
148 */
149 function wpbc_get_times_in_form( $booking_form_data, $booking_type ){
150
151 $is_time_exist = false;
152
153 $start_time = $end_time = '00:00:00';
154
155 if ( strpos( $booking_form_data, 'rangetime' . $booking_type ) !== false ) {
156
157 // ~checkbox^mymultiple4^~checkbox^rangetime4^ ~checkbox^rangetime4^12:00 - 13:00~ checkbox^rangetime4^~checkbox^rangetime4^~text^name4^Jonny~
158
159 // Types of the conditions
160 $f_type = '[^\^]*';
161 $f_name = 'rangetime[\d]*[\[\]]{0,2}';
162 $f_value = '[\s]*([0-9:]*)[\s]*\-[\s]*([0-9:]*)[\s]*[^~]*';
163
164 $pattern_to_search='%[~]?'.$f_type.'\^'.$f_name.'\^'.$f_value.'[~]?%';
165
166 preg_match_all($pattern_to_search, $booking_form_data, $matches, PREG_SET_ORDER);
167
168 /**
169 * Example of $matches: [ [ [0] => '~checkbox^rangetime4^13:00 - 14:00~'
170 [1] => '13:00'
171 [2] => '14:00'
172 ] ]
173 */
174
175 if (count($matches)>0){
176
177 $start_time = wpbc_get_time_in_24_hours_format( trim( $matches[0][1] ) );
178 $start_time[2] = '01';
179
180 $end_time = wpbc_get_time_in_24_hours_format( trim( $matches[0][2] ) );
181 $end_time[2] = '02';
182
183 $is_time_exist = true;
184
185 } else {
186 $start_time = array('00','00','01');
187 $end_time = array('00','00','02');
188 }
189
190 } else {
191
192 if ( strpos( $booking_form_data, 'starttime' . $booking_type ) !== false ) { // Get START TIME From form request
193 $pos1 = strpos( $booking_form_data, 'starttime' . $booking_type ); // Find start time pos
194 $pos1 = strpos( $booking_form_data, '^', $pos1 ) + 1; // Find TIME pos
195 $pos2 = strpos( $booking_form_data, '~', $pos1 ); // Find TIME length
196 if ( $pos2 === false ) {
197 $pos2 = strlen( $booking_form_data );
198 }
199 $pos2 = $pos2 - $pos1;
200 $start_time = substr( $booking_form_data, $pos1, $pos2 );
201 if ( $start_time == '' ) {
202 $start_time = '00:00';
203 }
204
205 $start_time = wpbc_check_min_max_available_times( $start_time, '00:01', '23:59' ); // FixIn: 8.7.11.1.
206
207 $start_time = explode( ':', $start_time );
208
209 $start_time[2] = '01';
210 } else {
211 $start_time = explode( ':', $start_time );
212 }
213
214 if ( strpos( $booking_form_data, 'endtime' . $booking_type ) !== false ) { // Get END TIME From form request
215 $pos1 = strpos( $booking_form_data, 'endtime' . $booking_type ); // Find start time pos
216 $pos1 = strpos( $booking_form_data, '^', $pos1 ) + 1; // Find TIME pos
217 $pos2 = strpos( $booking_form_data, '~', $pos1 ); // Find TIME length
218 if ( $pos2 === false ) {
219 $pos2 = strlen( $booking_form_data );
220 }
221 $pos2 = $pos2 - $pos1;
222 $end_time = substr( $booking_form_data, $pos1, $pos2 );
223 if ( $end_time == '' ) {
224 $end_time = '00:00';
225 }
226
227 $end_time = wpbc_check_min_max_available_times( $end_time, '00:01', '23:59' ); // FixIn: 8.7.11.1.
228
229 $is_time_exist = true;
230
231 $end_time = explode( ':', $end_time );
232 $end_time[2] = '02';
233 } else {
234 $end_time = explode( ':', $end_time );
235 }
236
237 if ( strpos( $booking_form_data, 'durationtime' . $booking_type ) !== false ) { // Get END TIME From form request
238 $pos1 = strpos( $booking_form_data, 'durationtime' . $booking_type ); // Find start time pos
239 $pos1 = strpos( $booking_form_data, '^', $pos1 ) + 1; // Find TIME pos
240 $pos2 = strpos( $booking_form_data, '~', $pos1 ); // Find TIME length
241 if ( $pos2 === false ) {
242 $pos2 = strlen( $booking_form_data );
243 }
244 $pos2 = $pos2 - $pos1;
245 $end_time = substr( $booking_form_data, $pos1, $pos2 );
246
247 $is_time_exist = true;
248
249 $end_time = explode( ':', $end_time );
250
251 // Here we are get start time and add duration for end time
252 $new_end_time = mktime( intval( $start_time[0] ), intval( $start_time[1] ) );
253 $new_end_time = $new_end_time + $end_time[0] * 60 * 60 + $end_time[1] * 60;
254 $end_time = gmdate( 'H:i', $new_end_time );
255
256 if ( $end_time == '00:00' ) {
257 $end_time = '23:59';
258 }
259 $end_time = explode( ':', $end_time );
260 $end_time[2] = '02';
261 }
262
263 }
264
265 if ( $is_time_exist ) {
266 return array( $start_time, $end_time );
267 } else {
268 return false;
269 }
270 }
271
272
273 //FixIn: TimeFreeGenerator
274 /**
275 * Convert timeslot "10:00 - 12:00" to specfic timeformat, like "10:00 AM - 12:00 PM"
276 * @param string $timeslot - "10:00 - 12:00"
277 * @param string $time_format = "g:i A"
278 */
279 function wpbc_time_slot_in_format( $timeslot, $time_format = false ){
280
281 // FixIn: 8.9.3.1.
282 if ( ( empty( $timeslot ) ) ) {
283 return '';
284 }
285 $value_times = explode( '-', $timeslot );
286 $value_times[0] = trim( $value_times[0] );
287 $value_times[1] = trim( $value_times[1] );
288
289 $s_tm = wpbc_time_localized( $value_times[0] , $time_format);
290 $e_tm = wpbc_time_localized( $value_times[1] , $time_format);
291
292 $t_delimeter = ' - ';
293
294 return $s_tm . $t_delimeter . $e_tm ;
295 }
296
297
298 //FixIn: 8.4.2.11 Deprecated, use this: wpbc_time_localized
299 /**
300 * Convert timeslot "10:00" to specfic timeformat, like "10:00 AM"
301 * @param string $timeslot - "10:00"
302 * @param string $time_format = "g:i A"
303 */
304 function wpbc_time_in_format( $timeslot, $time_format = false ){
305
306 $s_tm = wpbc_time_localized( $timeslot, $time_format );
307 return $s_tm;
308 }
309
310
311 /**
312 * Get dates from DB of specific booking -> '2023-10-09 12:00:01, 2023-10-09 20:00:02'
313 *
314 * @global type $wpdb
315 * @param type $booking_id_str - booking ID
316 * @return string - comma separated dates in SQL format -> '2023-10-09 12:00:01, 2023-10-09 20:00:02'
317 */
318 function wpbc_db__get_sql_dates__in_booking__as_str( $booking_id_str ) {
319
320 global $wpdb;
321
322 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
323 $dates_result = $wpdb->get_results( "SELECT DISTINCT booking_date FROM {$wpdb->prefix}bookingdates WHERE booking_id IN ({$booking_id_str}) ORDER BY booking_date" );
324
325 $dates_str = array();
326
327 foreach ( $dates_result as $my_date ) {
328
329 $dates_str[] = $my_date->booking_date;
330 }
331 $dates_str = implode( ', ', $dates_str );
332
333
334 return $dates_str;
335 }
336
337
338 /**
339 * Get Only Dates array from Dates Dimes string with comma seperated values
340 *
341 * @param $dates_ymd_his_csv -> '2023-10-09 12:00:01, 2023-10-09 20:00:02'
342 *
343 * @return array|string[] -> '2023-10-09, 2023-10-09'
344 *
345 *
346 * Usually it called like in this Example:
347 * $dates_ymd_his_csv = wpbc_db__get_sql_dates__in_booking__as_str( $booking_id_str ); // '100' | '10,7' - booking ID
348 * $dates_only_arr = wpbc_get_only_dates__from_dates_ymd_his_csv__as_arr( $dates_ymd_his_csv ); // -> '2023-10-09, 2023-10-09'
349 */
350 function wpbc_get_only_dates__from_dates_ymd_his_csv__as_arr( $dates_ymd_his_csv ){
351
352
353 // FixIn: 10.1.5.6.
354 $dates_only_arr = wpbc_get_dates_arr__from_dates_comma_separated( array(
355 'dates_separator' => ', ', // ', '
356 'dates' => $dates_ymd_his_csv, // '2023-04-04 12:03:00, 2023-04-07 2024-06-30:00'
357 ) );
358 // Get Only Dates
359 $dates_only_arr = array_map( function ( $date_sql_ymd_his ) {
360 $date_sql_ymd_his = trim( $date_sql_ymd_his );
361 $date_sql_ymd_his = substr( $date_sql_ymd_his, 0, 10 );
362 return $date_sql_ymd_his;
363 }
364 , $dates_only_arr
365 );
366 $dates_only_arr = array_unique( $dates_only_arr );
367 return $dates_only_arr;
368 }
369
370 /**
371 * Get Time in 24 hours (military) format, from possible AM/PM format
372 *
373 * @param string $time_str - '01:20 PM'
374 * @return string - '13:20'
375 */
376 function wpbc_get_time_in_24_hours_format( $time_str ) {
377
378 $time_str = trim( $time_str );
379 $time_str_plus = 0;
380
381 if ( strpos( strtolower( $time_str) ,'am' ) !== false ) {
382 $time_str = str_replace('am', '', $time_str );
383 $time_str = str_replace('AM', '', $time_str );
384 }
385
386 if ( strpos( strtolower( $time_str) ,'pm' ) !== false ) {
387 $time_str = str_replace('pm', '', $time_str );
388 $time_str = str_replace('PM', '', $time_str );
389 $time_str_plus = 12;
390 }
391
392 $time_str = explode( ':', trim( $time_str ) );
393 // FixIn: 9.9.0.4.
394 $time_str[0] = intval( $time_str[0] ) + $time_str_plus;
395 $time_str[1] = intval( $time_str[1] );
396
397 if ($time_str[0] < 10 ) $time_str[0] = '0' . $time_str[0];
398 if ($time_str[1] < 10 ) $time_str[1] = '0' . $time_str[1];
399
400 return $time_str;
401 }
402
403
404 /**
405 * Get number of days between 2 dates (dates in mySQL format)
406 *
407 * @param string $day1 - Day in MySQL format
408 * @param string $day2 - Day in MySQL format
409 *
410 * @return int - number of days
411 */
412 function wpbc_get_difference_in_days( $day1, $day2 ) {
413 return floor( ( strtotime( $day1 ) - strtotime( $day2 ) ) / 86400 ); // FixIn: 8.2.1.11.
414 }
415
416
417 /**
418 * Get sorted sql dates array, like: [ '2023-10-18 00:00:00', '2023-10-19 00:00:00', '2023-10-20 00:00:00' ]
419 *
420 * @param string $booking_days '19.10.2023,18.10.2023,20.10.2023' - comma separated dates:
421 * @return array - [ '2023-10-18 00:00:00', '2023-10-19 00:00:00', '2023-10-20 00:00:00' ] - sorted dates array
422 */
423 function wpbc_get_sorted_days_array( $booking_days ) {
424
425 if ( strpos($booking_days,' - ') !== false ) {
426 $booking_days = explode(' - ', $booking_days );
427 $booking_days = wpbc_get_comma_seprated_dates_from_to_day($booking_days[0],$booking_days[1]);
428 }
429
430 $days_array = explode(',', $booking_days);
431 $only_days = array();
432
433 foreach ($days_array as $new_day) {
434 if ( ! empty( $new_day ) ) {
435 $new_day = trim( $new_day );
436 if ( strpos( $new_day, '.' ) !== false ) $new_day = explode('.',$new_day);
437 else $new_day = explode('-',$new_day);
438 $only_days[] = $new_day[2] .'-' . $new_day[1] .'-' . $new_day[0] . ' 00:00:00';
439 }
440 }
441
442 if ( ! empty( $only_days ) ) {
443 sort($only_days);
444 }
445
446 return $only_days;
447 }
448
449
450 /**
451 * Get Dates in Comma seperated format, based on start and end dates.
452 *
453 * @param string $date_str_from - start date: 06.04.2015
454 * @param string $date_str_to - end date: 08.04.2015
455 * @return string - comma seperated dates: 06.04.2015, 07.04.2015, 08.04.2015
456 */
457 function wpbc_get_comma_seprated_dates_from_to_day( $date_str_from, $date_str_to ) {
458
459 $date_str_from = explode('.', $date_str_from);
460 $date_str_to = explode('.', $date_str_to);
461 $iDateFrom = mktime( 1, 0, 0, ( intval( $date_str_from[1] ) ), ( intval( $date_str_from[0] ) ), ( intval( $date_str_from[2] ) ) );
462 $iDateTo = mktime( 1, 0, 0, ( intval( $date_str_to[1] ) ), ( intval( $date_str_to[0] ) ), ( intval( $date_str_to[2] ) ) );
463
464 $aryRange=array();
465
466 if ( $iDateTo >= $iDateFrom ) {
467 array_push( $aryRange, gmdate( 'd.m.Y', $iDateFrom ) ); // first entry
468
469 while ($iDateFrom<$iDateTo) {
470 $iDateFrom+=86400; // add 24 hours
471 array_push( $aryRange, gmdate( 'd.m.Y', $iDateFrom ) );
472 }
473 }
474
475 $aryRange = implode(', ', $aryRange);
476
477 return $aryRange;
478 }
479
480
481 /**
482 * Get dates array based on start and end dates.
483 *
484 * @param string $sStartDate - start date: 2015-04-06
485 * @param string $sEndDate - end date: 2015-04-08
486 * @return array - array( 2015-04-06, 2015-04-07, 2015-04-08 )
487 */
488 function wpbc_get_dates_array_from_start_end_days( $sStartDate, $sEndDate ){
489 // Firstly, format the provided dates.
490 // This function works best with YYYY-MM-DD
491 // but other date formats will work thanks
492 // to strtotime().
493 $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
494 $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));
495
496 // Start the variable off with the start date
497 $aDays[] = $sStartDate;
498
499 // Set a 'temp' variable, sCurrentDate, with
500 // the start date - before beginning the loop
501 $sCurrentDate = $sStartDate;
502
503 // While the current date is less than the end date
504 while($sCurrentDate < $sEndDate){
505 // Add a day to the current date
506 $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
507
508 // Add this new day to the aDays array
509 $aDays[] = $sCurrentDate;
510 }
511 // Once the loop has finished, return the
512 // array of days.
513 return $aDays;
514 }
515
516
517 /**
518 * Get dates array, from range days selection
519 *
520 * @param $params = array(
521 * 'dates_separator' => ' ~ ', // Dates separator
522 * 'dates' => '2023-04-04 ~ 2023-04-07' // Dates in 'Y-m-d' format: '2023-01-31'
523 * )
524 *
525 * @return array = array(
526 * [0] => 2023-04-04
527 * [1] => 2023-04-05
528 * [2] => 2023-04-06
529 * [3] => 2023-04-07
530 * )
531 *
532 * Example #1: wpbc_get_dates_arr__from_dates_range( array( 'dates_separator' => ' ~ ', 'dates' => '2023-04-04 ~ 2023-04-07' ) );
533 * Example #2: wpbc_get_dates_arr__from_dates_range( array( 'dates_separator' => ' - ', 'dates' => '2023-04-04 - 2023-04-07' ) );
534 */
535 function wpbc_get_dates_arr__from_dates_range( $params ){
536
537 $defaults = array(
538 'dates_separator' => ' ~ ', // ' ~ '
539 'dates' => '', // '2023-04-04 ~ 2023-04-07'
540 );
541 $params = wp_parse_args( $params, $defaults );
542
543 $dates_arr = array();
544
545 if ( ! empty( $params['dates'] ) ) {
546
547 list( $check_in_date_ymd, $check_out_date_ymd ) = explode( $params['dates_separator'], $params['dates'] );
548
549 if ( ( ! empty( $check_in_date_ymd ) ) && ( ! empty( $check_out_date_ymd ) ) ) {
550
551 $dates_arr = wpbc_get_dates_array_from_start_end_days( $check_in_date_ymd, $check_out_date_ymd );
552 }
553 }
554 return $dates_arr;
555 }
556
557 /**
558 * Get dates array, from comma separated dates
559 *
560 * @param $params = array(
561 * 'dates_separator' => ', ', // Dates separator
562 * 'dates' => '2023-04-04, 2023-04-07, 2023-04-05' // Dates in 'Y-m-d' format: '2023-01-31'
563 * )
564 *
565 * @return array = array(
566 * [0] => 2023-04-04
567 * [1] => 2023-04-05
568 * [2] => 2023-04-06
569 * [3] => 2023-04-07
570 * )
571 *
572 * Example #1: wpbc_get_dates_arr__from_dates_comma_separated( array( 'dates_separator' => ', ', 'dates' => '2023-04-04, 2023-04-07, 2023-04-05' ) );
573 */
574 function wpbc_get_dates_arr__from_dates_comma_separated( $params ){
575
576 $defaults = array(
577 'dates_separator' => ', ', // ' ~ '
578 'dates' => '', // ''2023-04-04, 2023-04-07, 2023-04-05'
579 );
580 $params = wp_parse_args( $params, $defaults );
581
582 $dates_arr = array();
583
584 if ( ! empty( $params['dates'] ) ) {
585
586 $dates_arr = explode( $params['dates_separator'], $params['dates'] );
587
588 sort( $dates_arr );
589 }
590 return $dates_arr;
591 }
592
593
594 /**
595 * Get tommorow day from input value
596 *
597 * @param string $nowday : 2015-02-29
598 * @return int : Unix timestamp for a date like this 2015-02-30
599 */
600 function wpbc_get_tommorow_day( $nowday ){
601
602 $nowday_d = gmdate( 'm.d.Y', mysql2date( 'U', $nowday ) );
603 $previos_array = explode( '.', $nowday_d );
604 $tommorow_day = mktime( 0, 0, 0, intval($previos_array[0]), ( intval($previos_array[1]) + 1 ), intval($previos_array[2]) ) ;
605 return $tommorow_day;
606 }
607
608
609 /**
610 * Check if this date is today day
611 *
612 * @param string $some_day : '2015-05-29'
613 * @return boolean : true | false
614 */
615 function wpbc_is_today_date( $some_day ) {
616
617 $some_day_d = gmdate( 'm.d.Y', mysql2date( 'U', $some_day ) );
618 $today_day = gmdate( 'm.d.Y' );
619
620 if ( $today_day == $some_day_d ) {
621 return true;
622 } else {
623 return false;
624 }
625 }
626
627
628 /**
629 * Check if this date is Tomorrow day
630 *
631 * @param string $some_day : '2024-08-18'
632 * @return boolean : true | false
633 */
634 function wpbc_is_tomorrow_date( $some_day ) {
635
636 $some_day_d = gmdate( 'Y-m-d 00:00:00', mysql2date( 'U', $some_day ) );
637 $today_day = gmdate( 'Y-m-d 00:00:00' );
638 $tomorrow_day = gmdate( 'Y-m-d 00:00:00', strtotime( '+1 day', strtotime( $today_day ) ) );
639
640 if ( $tomorrow_day == $some_day_d ) {
641 return true;
642 } else {
643 return false;
644 }
645 }
646
647
648 // FixIn: 8.8.1.2.
649 /**
650 * Check if this date in past
651 *
652 * @param string $some_day : '2015-05-29'
653 * @return boolean : true | false
654 */
655 function wpbc_is_date_in_past( $some_day ) {
656
657 $some_day_d = gmdate( 'm.d.Y', mysql2date( 'U', $some_day ) );
658 $some_array = explode( '.', $some_day_d );
659 $some_day = mktime( 0, 0, 0, intval($some_array[0]), ( intval($some_array[1]) + 1 ), intval($some_array[2]) );
660
661 $today_day = time();
662
663 if ( $today_day > $some_day ) {
664 return true;
665 } else {
666 return false;
667 }
668 }
669
670
671
672 //TODO: refactor it, by replacig date_i18n to wp_loc_date... (check depndencies of this function in other usage functions...)
673 /**
674 * Change date / time format
675 *
676 * @param string $dt - MySQL Date - '2015-11-21 00:00:00'
677 * @param type $date_format - Optional. Date format
678 * @param type $time_format - Optional. Time format
679 * @return array( 'DATE in custom Format', 'TIME in custom Format' )
680 */
681 function wpbc_get_date_in_correct_format( $dt, $date_format = false, $time_format = false ) {
682
683 if ( $date_format === false ) $date_format = get_bk_option( 'booking_date_format');
684 if ( empty( $date_format ) ) $date_format = "m / d / Y, D";
685
686 if ( $time_format === false ) $time_format = get_bk_option( 'booking_time_format');
687 if ( empty( $time_format ) ) $time_format = get_option( 'time_format' ); //'h:i a'; //FixIn: TimeFree 2 - in Booking Calendar Free version show by default times hints in AM/PM format
688
689 $my_time = gmdate( 'H:i:s' , mysql2date( 'U', $dt ) );
690 if ( $my_time == '00:00:00' ) $time_format = '';
691
692 $bk_date = date_i18n( $date_format, mysql2date( 'U', $dt ) );
693 $bk_time = date_i18n( ' ' . $time_format , mysql2date( 'U', $dt ) );
694
695 if ( $bk_time == ' ' ) $bk_time = '';
696
697 return array($bk_date, $bk_time);
698 }
699
700
701 /**
702 * Get SHORT Dates showing data
703 *
704 * @param array $bk_dates_short - Array of dates
705 * @param bool $is_approved - is dates approved or not
706 * @param type $bk_dates_short_id
707 * @param type $booking_types
708 * @return string
709 */
710 function wpbc_get_short_dates_formated_to_show( $bk_dates_short, $is_approved = false, $bk_dates_short_id = array() , $booking_types = array() ){
711
712 $short_dates_content = '';
713 $dcnt = 0;
714 foreach ( $bk_dates_short as $dt ) {
715 if ( $dt == '-' ) {
716 $short_dates_content .= '<span class="date_tire"> - </span>';
717 } elseif ( $dt == ',' ) {
718 $short_dates_content .= '<span class="date_tire">, </span>';
719 } else {
720 $short_dates_content .= '<a href="javascript:void(0)" class="field-booking-date label flex-label ';
721 if ( $is_approved )
722 $short_dates_content .= ' approved';
723 $short_dates_content .= '">';
724
725 $bk_date = wpbc_get_date_in_correct_format( $dt );
726 $short_dates_content .= $bk_date[0];
727 $short_dates_content .= '<sup class="field-booking-time">' . $bk_date[1] . '</sup>';
728
729 if ( class_exists( 'wpdev_bk_biz_l' ) ) { // BL
730 if ( ( !empty( $bk_dates_short_id[$dcnt] ) ) && ( isset( $booking_types[$bk_dates_short_id[$dcnt]] ) ) ){
731 $bk_booking_type_name_date = $booking_types[$bk_dates_short_id[$dcnt]]->title; // Default
732
733 if ( strlen( $bk_booking_type_name_date ) > 19 )
734 $bk_booking_type_name_date = substr( $bk_booking_type_name_date, 0, 13 )
735 . '...'
736 . substr( $bk_booking_type_name_date, -3 );
737
738 $short_dates_content .= '<sup class="field-booking-time date_from_dif_type"> ' . $bk_booking_type_name_date . '</sup>';
739 }
740 }
741 $short_dates_content .= '</a>';
742 }
743 $dcnt++;
744 }
745 return $short_dates_content;
746 }
747
748 // FixIn: 9.6.3.5.
749
750 /**
751 * Get booking dates from DB for specific calendar
752 *
753 * @param array $params array(
754 'approved' => '' // '' - all | '0' - pending | '1' - approved
755 , 'resource_id' => 1 // int or dcv
756 , 'skip_booking_id' => '' // int or dcv
757
758 )
759 *
760 * @return array array( [1-7-2023] => array(
761 [sec_0] => stdClass Object
762 (
763 [booking_date] => 2023-01-07 00:00:00
764 [approved] => 0
765 [booking_id] => 96
766 )
767
768 )
769 [1-8-2023] => Array(
770 [sec_0] => stdClass Object
771 (
772 [booking_date] => 2023-01-08 00:00:00
773 [approved] => 0
774 [booking_id] => 42
775 )
776 )
777 ...
778
779 */
780 function wpbc__sql__get_booked_dates( $params ){
781
782 $defaults = array(
783 'approved' => '' // '' - all | '0' - pending | '1' - approved
784 , 'resource_id' => 1 // int or dcv
785 , 'skip_booking_id' => '' // int or dcv
786 );
787 $params = wp_parse_args( $params, $defaults );
788
789 // S a n i t i z e
790 $params['approved'] = ( '' != $params['approved'] ) ? intval( $params['approved'] ) : '';
791 $params['skip_booking_id'] = ( '' != $params['skip_booking_id'] ) ? wpbc_sanitize_digit_or_csd( $params['skip_booking_id'] ) : '';
792 $params['resource_id'] = ( '' != $params['resource_id'] ) ? wpbc_sanitize_digit_or_csd( $params['resource_id'] ) : 1;
793
794
795 // S Q L
796 global $wpdb;
797 $sql = "SELECT DISTINCT dt.booking_date, dt.approved, bk.booking_id
798
799 FROM {$wpdb->prefix}bookingdates as dt
800
801 INNER JOIN {$wpdb->prefix}booking as bk
802
803 ON bk.booking_id = dt.booking_id
804
805 WHERE ( 1 = 1 )";
806
807 // W H E R E
808 $sql_where = '';
809 $sql_where .= ( '' != $params['approved'] ) ? " AND ( dt.approved = {$params['approved']} ) " : ''; // Approved (1) or Pending (0) or All // int
810 $sql_where .= " AND dt.booking_date >= CURDATE() "; // Only actual bookings
811 $sql_where .= " AND bk.trash != 1 "; // Not in Trash // int
812 $sql_where .= " AND bk.booking_type IN ( {$params['resource_id']} ) "; // For specific calendar (booking resource) // int
813 $sql_where .= ( '' != $params['skip_booking_id'] ) ? " AND dt.booking_id NOT IN ( {$params['skip_booking_id']} ) " : '' ; // Skip some bookings ? Usually, during booking edit.
814
815 // O R D E R
816 $sql_order = " ORDER BY dt.booking_date"; // Order by booking dates & times
817
818
819 /**
820 * Array( [0] => stdClass Object ( [booking_date] => 2022-12-27 00:00:00, [approved] => 0, [booking_id] => 187 )
821 [1] => stdClass Object ( [booking_date] => 2022-12-28 00:00:00, [approved] => 1, [booking_id] => 26 )
822 ...
823 */
824
825 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
826 $result_arr = $wpdb->get_results( $sql . $sql_where . $sql_order );
827
828 // P A R S E
829 $prior_check_out_date = false;
830 $dates_arr = array();
831 foreach ( $result_arr as $sql_date ) {
832
833 $blocked_days_range = array( $sql_date->booking_date );
834 $resource_id = explode( ',', $params['resource_id'] );
835 $resource_id = $resource_id[0];
836 if (
837 ( ! class_exists( 'wpdev_bk_biz_l' ) )
838 || ( ( class_exists( 'wpdev_bk_biz_l' ) ) && ( ! wpbc_is_this_parent_resource( $resource_id ) ) )
839 ){ // FixIn: 9.1.2.7.
840 list( $blocked_days_range, $prior_check_out_date ) = apply_filters( 'wpbc_get_extended_block_dates_filter', array( $blocked_days_range, $prior_check_out_date ) );
841 }
842
843 foreach ( $blocked_days_range as $date_ymd_his ) {
844
845 $sql_date->booking_date = $date_ymd_his;
846
847
848 $date_as_int = strtotime( $sql_date->booking_date );
849
850 $date_key = gmdate( 'Y-m-d', $date_as_int );
851 $date_seconds = $date_as_int - strtotime( $date_key );
852
853 // Transform '2022-09-01' to 9-1-2022
854 $date_key__for_calendar = gmdate( 'n-j-Y', $date_as_int ); // j - Day of the month without leading zeros 1 to 31 ; n - Number of month, without leading zeros 1 to 12
855
856 if ( empty( $dates_arr[ $date_key__for_calendar ] ) ) {
857 $dates_arr[ $date_key__for_calendar ] = array();
858 }
859
860 /**
861 * Important info about [ 'sec_' . $date_seconds ]
862 *
863 * We need to have 'sec_0' instead of simple 0
864 *
865 * for having JavaScript Objects (object property 'sec_o') instead of Array (index 0), after sending Ajax response and JSON decode!
866 */
867 $dates_arr[ $date_key__for_calendar ][ 'sec_' . $date_seconds] = $sql_date;
868 }
869
870
871 }
872
873 return $dates_arr;
874 }
875
876
877 /**
878 * Get season availability based on booking resource and seasons from Booking > Resources > Availability page
879 *
880 * @param array $params array(
881 'resource_id' => 1 // int or dcv
882 , 'from' => 'NOW' // any value that is possible to use in strtotime()
883 , 'count' => 365 // int
884 )
885 *
886 * @return array Array (
887 [2023-01-09] => 1
888 [2023-01-10] => 1
889 [2023-01-11] => 1
890 [2023-01-12] => 1
891 [2023-01-13] => 1
892 [2023-01-14] =>
893 [2023-01-15] =>
894 [2023-01-16] => 1
895 [2023-01-17] => 1
896 [2023-01-18] => 1
897 [2023-01-19] => 1
898 ...
899 */
900 function wpbc__sql__get_season_availability( $params ){
901
902 // FixIn: 9.5.4.4.
903 $max_days_count = 365;
904 $max_monthes_in_calendar = get_bk_option( 'booking_max_monthes_in_calendar' );
905
906 if ( strpos( $max_monthes_in_calendar, 'm' ) !== false ) {
907 $max_days_count = intval( str_replace( 'm', '', $max_monthes_in_calendar ) ) * 31 + 5; // FixIn: 9.6.1.1.
908 } else {
909 $max_days_count = intval( str_replace( 'y', '', $max_monthes_in_calendar ) ) * 365 + 15; // FixIn: 9.6.1.1.
910 }
911
912 $defaults = array(
913 'resource_id' => 1 // int or dcv
914 , 'from' => 'NOW' // any value that is possible to use in strtotime()
915 , 'count' => $max_days_count // int
916 );
917 $params = wp_parse_args( $params, $defaults );
918
919
920 // S a n i t i z e
921 $params['resource_id'] = ( '' != $params['resource_id'] ) ? wpbc_sanitize_digit_or_csd( $params['resource_id'] ) : 1;
922
923 $is_all_days_available = true;
924
925 $season_filters_id_arr = array();
926
927 if ( ( class_exists( 'wpdev_bk_biz_m' ) ) && ( function_exists( 'wpbc_get_resource_meta' ) ) ) { // BM and higher // FixIn: 9.9.0.13.
928
929 // S Q L
930 $availability_res = wpbc_get_resource_meta( $params['resource_id'], 'availability' );
931
932 if ( ! empty( $availability_res ) ) {
933
934 /**
935 * Array ( [general] => On, [filter] => Array ( [1] => On, ...
936 * [2] => Off
937 * ...
938 * [8] => Off
939 * [9] => On
940 * )
941 * )
942 */
943 $availability = maybe_unserialize( $availability_res[0]->value );
944
945 $is_all_days_available = ( 'On' === $availability['general'] ) ? true : false;
946 $season_filter = $availability['filter'];
947
948 // Get ID of only activated Seasons
949 if ( is_array( $season_filter ) ) {
950 foreach ( $season_filter as $key => $value ) {
951 if ( $value == 'On' ) {
952 $season_filters_id_arr[] = intval( $key ); // Sanitize booking_filter_id for future SQL
953 }
954 }
955 }
956
957 }
958
959 }
960
961 $days_availability = array();
962
963 for( $i = 0; $i < $params['count']; $i++) {
964
965 $date_y_m_d = gmdate( 'Y-m-d', strtotime( '+' . $i . 'days', strtotime( $params['from'] ) ) );
966
967 $days_availability[ $date_y_m_d ] = $is_all_days_available;
968
969 $date_arr = explode( '-', $date_y_m_d );
970
971 foreach ( $season_filters_id_arr as $filter_id ) {
972
973 $day = intval( $date_arr[2] );
974 $month = intval( $date_arr[1] );
975 $year = intval( $date_arr[0] );
976
977 if ( wpbc_is_day_inside_of_filter( $day, $month, $year, $filter_id ) ){
978 $days_availability[ $date_y_m_d ] = ! $days_availability[ $date_y_m_d ];
979 break;
980 }
981 }
982
983 }
984
985 return $days_availability;
986 }