PluginProbe
Booking Calendar / 10.1.3
Booking Calendar v10.1.3
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.1.3, at core/wpbc-dates.php

931 lines 32.5 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 = date( '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 $dates_result = $wpdb->get_results( "SELECT DISTINCT booking_date FROM {$wpdb->prefix}bookingdates WHERE booking_id IN ({$booking_id_str}) ORDER BY booking_date" );
323
324 $dates_str = array();
325
326 foreach ( $dates_result as $my_date ) {
327
328 $dates_str[] = $my_date->booking_date;
329 }
330 $dates_str = implode( ', ', $dates_str );
331
332
333 return $dates_str;
334 }
335
336
337 /**
338 * Get Time in 24 hours (military) format, from possible AM/PM format
339 *
340 * @param string $time_str - '01:20 PM'
341 * @return string - '13:20'
342 */
343 function wpbc_get_time_in_24_hours_format( $time_str ) {
344
345 $time_str = trim( $time_str );
346 $time_str_plus = 0;
347
348 if ( strpos( strtolower( $time_str) ,'am' ) !== false ) {
349 $time_str = str_replace('am', '', $time_str );
350 $time_str = str_replace('AM', '', $time_str );
351 }
352
353 if ( strpos( strtolower( $time_str) ,'pm' ) !== false ) {
354 $time_str = str_replace('pm', '', $time_str );
355 $time_str = str_replace('PM', '', $time_str );
356 $time_str_plus = 12;
357 }
358
359 $time_str = explode( ':', trim( $time_str ) );
360 //FixIn: 9.9.0.4
361 $time_str[0] = intval( $time_str[0] ) + $time_str_plus;
362 $time_str[1] = intval( $time_str[1] );
363
364 if ($time_str[0] < 10 ) $time_str[0] = '0' . $time_str[0];
365 if ($time_str[1] < 10 ) $time_str[1] = '0' . $time_str[1];
366
367 return $time_str;
368 }
369
370
371 /**
372 * Get number of days between 2 dates (dates in mySQL format)
373 *
374 * @param string $day1 - Day in MySQL format
375 * @param string $day2 - Day in MySQL format
376 *
377 * @return int - number of days
378 */
379 function wpbc_get_difference_in_days( $day1, $day2 ) {
380 return floor( ( strtotime( $day1 ) - strtotime( $day2 ) ) / 86400 ); //FixIn: 8.2.1.11
381 }
382
383
384 /**
385 * Get sorted sql dates array, like: [ '2023-10-18 00:00:00', '2023-10-19 00:00:00', '2023-10-20 00:00:00' ]
386 *
387 * @param string $booking_days '19.10.2023,18.10.2023,20.10.2023' - comma separated dates:
388 * @return array - [ '2023-10-18 00:00:00', '2023-10-19 00:00:00', '2023-10-20 00:00:00' ] - sorted dates array
389 */
390 function wpbc_get_sorted_days_array( $booking_days ) {
391
392 if ( strpos($booking_days,' - ') !== false ) {
393 $booking_days = explode(' - ', $booking_days );
394 $booking_days = wpbc_get_comma_seprated_dates_from_to_day($booking_days[0],$booking_days[1]);
395 }
396
397 $days_array = explode(',', $booking_days);
398 $only_days = array();
399
400 foreach ($days_array as $new_day) {
401 if ( ! empty( $new_day ) ) {
402 $new_day = trim( $new_day );
403 if ( strpos( $new_day, '.' ) !== false ) $new_day = explode('.',$new_day);
404 else $new_day = explode('-',$new_day);
405 $only_days[] = $new_day[2] .'-' . $new_day[1] .'-' . $new_day[0] . ' 00:00:00';
406 }
407 }
408
409 if ( ! empty( $only_days ) ) {
410 sort($only_days);
411 }
412
413 return $only_days;
414 }
415
416
417 /**
418 * Get Dates in Comma seperated format, based on start and end dates.
419 *
420 * @param string $date_str_from - start date: 06.04.2015
421 * @param string $date_str_to - end date: 08.04.2015
422 * @return string - comma seperated dates: 06.04.2015, 07.04.2015, 08.04.2015
423 */
424 function wpbc_get_comma_seprated_dates_from_to_day( $date_str_from, $date_str_to ) {
425
426 $date_str_from = explode('.', $date_str_from);
427 $date_str_to = explode('.', $date_str_to);
428 $iDateFrom = mktime( 1, 0, 0, ( intval( $date_str_from[1] ) ), ( intval( $date_str_from[0] ) ), ( intval( $date_str_from[2] ) ) );
429 $iDateTo = mktime( 1, 0, 0, ( intval( $date_str_to[1] ) ), ( intval( $date_str_to[0] ) ), ( intval( $date_str_to[2] ) ) );
430
431 $aryRange=array();
432
433 if ( $iDateTo >= $iDateFrom ) {
434 array_push( $aryRange, date( 'd.m.Y', $iDateFrom ) ); // first entry
435
436 while ($iDateFrom<$iDateTo) {
437 $iDateFrom+=86400; // add 24 hours
438 array_push( $aryRange, date( 'd.m.Y', $iDateFrom ) );
439 }
440 }
441
442 $aryRange = implode(', ', $aryRange);
443
444 return $aryRange;
445 }
446
447
448 /**
449 * Get dates array based on start and end dates.
450 *
451 * @param string $sStartDate - start date: 2015-04-06
452 * @param string $sEndDate - end date: 2015-04-08
453 * @return array - array( 2015-04-06, 2015-04-07, 2015-04-08 )
454 */
455 function wpbc_get_dates_array_from_start_end_days( $sStartDate, $sEndDate ){
456 // Firstly, format the provided dates.
457 // This function works best with YYYY-MM-DD
458 // but other date formats will work thanks
459 // to strtotime().
460 $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
461 $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));
462
463 // Start the variable off with the start date
464 $aDays[] = $sStartDate;
465
466 // Set a 'temp' variable, sCurrentDate, with
467 // the start date - before beginning the loop
468 $sCurrentDate = $sStartDate;
469
470 // While the current date is less than the end date
471 while($sCurrentDate < $sEndDate){
472 // Add a day to the current date
473 $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
474
475 // Add this new day to the aDays array
476 $aDays[] = $sCurrentDate;
477 }
478 // Once the loop has finished, return the
479 // array of days.
480 return $aDays;
481 }
482
483
484 /**
485 * Get dates array, from range days selection
486 *
487 * @param $params = array(
488 * 'dates_separator' => ' ~ ', // Dates separator
489 * 'dates' => '2023-04-04 ~ 2023-04-07' // Dates in 'Y-m-d' format: '2023-01-31'
490 * )
491 *
492 * @return array = array(
493 * [0] => 2023-04-04
494 * [1] => 2023-04-05
495 * [2] => 2023-04-06
496 * [3] => 2023-04-07
497 * )
498 *
499 * Example #1: wpbc_get_dates_arr__from_dates_range( array( 'dates_separator' => ' ~ ', 'dates' => '2023-04-04 ~ 2023-04-07' ) );
500 * Example #2: wpbc_get_dates_arr__from_dates_range( array( 'dates_separator' => ' - ', 'dates' => '2023-04-04 - 2023-04-07' ) );
501 */
502 function wpbc_get_dates_arr__from_dates_range( $params ){
503
504 $defaults = array(
505 'dates_separator' => ' ~ ', // ' ~ '
506 'dates' => '', // '2023-04-04 ~ 2023-04-07'
507 );
508 $params = wp_parse_args( $params, $defaults );
509
510 $dates_arr = array();
511
512 if ( ! empty( $params['dates'] ) ) {
513
514 list( $check_in_date_ymd, $check_out_date_ymd ) = explode( $params['dates_separator'], $params['dates'] );
515
516 if ( ( ! empty( $check_in_date_ymd ) ) && ( ! empty( $check_out_date_ymd ) ) ) {
517
518 $dates_arr = wpbc_get_dates_array_from_start_end_days( $check_in_date_ymd, $check_out_date_ymd );
519 }
520 }
521 return $dates_arr;
522 }
523
524 /**
525 * Get dates array, from comma separated dates
526 *
527 * @param $params = array(
528 * 'dates_separator' => ', ', // Dates separator
529 * 'dates' => '2023-04-04, 2023-04-07, 2023-04-05' // Dates in 'Y-m-d' format: '2023-01-31'
530 * )
531 *
532 * @return array = array(
533 * [0] => 2023-04-04
534 * [1] => 2023-04-05
535 * [2] => 2023-04-06
536 * [3] => 2023-04-07
537 * )
538 *
539 * Example #1: wpbc_get_dates_arr__from_dates_comma_separated( array( 'dates_separator' => ', ', 'dates' => '2023-04-04, 2023-04-07, 2023-04-05' ) );
540 */
541 function wpbc_get_dates_arr__from_dates_comma_separated( $params ){
542
543 $defaults = array(
544 'dates_separator' => ', ', // ' ~ '
545 'dates' => '', // ''2023-04-04, 2023-04-07, 2023-04-05'
546 );
547 $params = wp_parse_args( $params, $defaults );
548
549 $dates_arr = array();
550
551 if ( ! empty( $params['dates'] ) ) {
552
553 $dates_arr = explode( $params['dates_separator'], $params['dates'] );
554
555 sort( $dates_arr );
556 }
557 return $dates_arr;
558 }
559
560
561 /**
562 * Get tommorow day from input value
563 *
564 * @param string $nowday : 2015-02-29
565 * @return int : Unix timestamp for a date like this 2015-02-30
566 */
567 function wpbc_get_tommorow_day( $nowday ){
568
569 $nowday_d = date( 'm.d.Y', mysql2date( 'U', $nowday ) );
570 $previos_array = explode( '.', $nowday_d );
571 $tommorow_day = mktime( 0, 0, 0, intval($previos_array[0]), ( intval($previos_array[1]) + 1 ), intval($previos_array[2]) ) ;
572 return $tommorow_day;
573 }
574
575
576 /**
577 * Check if this date is today day
578 *
579 * @param string $some_day : '2015-05-29'
580 * @return boolean : true | false
581 */
582 function wpbc_is_today_date( $some_day ) {
583
584 $some_day_d = date( 'm.d.Y', mysql2date( 'U', $some_day ) );
585 $today_day = date( 'm.d.Y' );
586
587 if ( $today_day == $some_day_d ) {
588 return true;
589 } else {
590 return false;
591 }
592 }
593
594
595 //FixIn: 8.8.1.2
596 /**
597 * Check if this date in past
598 *
599 * @param string $some_day : '2015-05-29'
600 * @return boolean : true | false
601 */
602 function wpbc_is_date_in_past( $some_day ) {
603
604 $some_day_d = date( 'm.d.Y', mysql2date( 'U', $some_day ) );
605 $some_array = explode( '.', $some_day_d );
606 $some_day = mktime( 0, 0, 0, intval($some_array[0]), ( intval($some_array[1]) + 1 ), intval($some_array[2]) );
607
608 $today_day = time();
609
610 if ( $today_day > $some_day ) {
611 return true;
612 } else {
613 return false;
614 }
615 }
616
617
618
619 //TODO: refactor it, by replacig date_i18n to wp_loc_date... (check depndencies of this function in other usage functions...)
620 /**
621 * Change date / time format
622 *
623 * @param string $dt - MySQL Date - '2015-11-21 00:00:00'
624 * @param type $date_format - Optional. Date format
625 * @param type $time_format - Optional. Time format
626 * @return array( 'DATE in custom Format', 'TIME in custom Format' )
627 */
628 function wpbc_get_date_in_correct_format( $dt, $date_format = false, $time_format = false ) {
629
630 if ( $date_format === false ) $date_format = get_bk_option( 'booking_date_format');
631 if ( empty( $date_format ) ) $date_format = "m / d / Y, D";
632
633 if ( $time_format === false ) $time_format = get_bk_option( 'booking_time_format');
634 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
635
636 $my_time = date( 'H:i:s' , mysql2date( 'U', $dt ) );
637 if ( $my_time == '00:00:00' ) $time_format = '';
638
639 $bk_date = date_i18n( $date_format, mysql2date( 'U', $dt ) );
640 $bk_time = date_i18n( ' ' . $time_format , mysql2date( 'U', $dt ) );
641
642 if ( $bk_time == ' ' ) $bk_time = '';
643
644 return array($bk_date, $bk_time);
645 }
646
647
648 /**
649 * Get SHORT Dates showing data
650 *
651 * @param array $bk_dates_short - Array of dates
652 * @param bool $is_approved - is dates approved or not
653 * @param type $bk_dates_short_id
654 * @param type $booking_types
655 * @return string
656 */
657 function wpbc_get_short_dates_formated_to_show( $bk_dates_short, $is_approved = false, $bk_dates_short_id = array() , $booking_types = array() ){
658
659 $short_dates_content = '';
660 $dcnt = 0;
661 foreach ( $bk_dates_short as $dt ) {
662 if ( $dt == '-' ) {
663 $short_dates_content .= '<span class="date_tire"> - </span>';
664 } elseif ( $dt == ',' ) {
665 $short_dates_content .= '<span class="date_tire">, </span>';
666 } else {
667 $short_dates_content .= '<a href="javascript:void(0)" class="field-booking-date label flex-label ';
668 if ( $is_approved )
669 $short_dates_content .= ' approved';
670 $short_dates_content .= '">';
671
672 $bk_date = wpbc_get_date_in_correct_format( $dt );
673 $short_dates_content .= $bk_date[0];
674 $short_dates_content .= '<sup class="field-booking-time">' . $bk_date[1] . '</sup>';
675
676 if ( class_exists( 'wpdev_bk_biz_l' ) ) { // BL
677 if ( ( !empty( $bk_dates_short_id[$dcnt] ) ) && ( isset( $booking_types[$bk_dates_short_id[$dcnt]] ) ) ){
678 $bk_booking_type_name_date = $booking_types[$bk_dates_short_id[$dcnt]]->title; // Default
679
680 if ( strlen( $bk_booking_type_name_date ) > 19 )
681 $bk_booking_type_name_date = substr( $bk_booking_type_name_date, 0, 13 )
682 . '...'
683 . substr( $bk_booking_type_name_date, -3 );
684
685 $short_dates_content .= '<sup class="field-booking-time date_from_dif_type"> ' . $bk_booking_type_name_date . '</sup>';
686 }
687 }
688 $short_dates_content .= '</a>';
689 }
690 $dcnt++;
691 }
692 return $short_dates_content;
693 }
694
695 //FixIn: 9.6.3.5
696
697 /**
698 * Get booking dates from DB for specific calendar
699 *
700 * @param array $params array(
701 'approved' => '' // '' - all | '0' - pending | '1' - approved
702 , 'resource_id' => 1 // int or dcv
703 , 'skip_booking_id' => '' // int or dcv
704
705 )
706 *
707 * @return array array( [1-7-2023] => array(
708 [sec_0] => stdClass Object
709 (
710 [booking_date] => 2023-01-07 00:00:00
711 [approved] => 0
712 [booking_id] => 96
713 )
714
715 )
716 [1-8-2023] => Array(
717 [sec_0] => stdClass Object
718 (
719 [booking_date] => 2023-01-08 00:00:00
720 [approved] => 0
721 [booking_id] => 42
722 )
723 )
724 ...
725
726 */
727 function wpbc__sql__get_booked_dates( $params ){
728
729 $defaults = array(
730 'approved' => '' // '' - all | '0' - pending | '1' - approved
731 , 'resource_id' => 1 // int or dcv
732 , 'skip_booking_id' => '' // int or dcv
733 );
734 $params = wp_parse_args( $params, $defaults );
735
736 // S a n i t i z e
737 $params['approved'] = ( '' != $params['approved'] ) ? intval( $params['approved'] ) : '';
738 $params['skip_booking_id'] = ( '' != $params['skip_booking_id'] ) ? wpbc_sanitize_digit_or_csd( $params['skip_booking_id'] ) : '';
739 $params['resource_id'] = ( '' != $params['resource_id'] ) ? wpbc_sanitize_digit_or_csd( $params['resource_id'] ) : 1;
740
741
742 // S Q L
743 global $wpdb;
744 $sql = "SELECT DISTINCT dt.booking_date, dt.approved, bk.booking_id
745
746 FROM {$wpdb->prefix}bookingdates as dt
747
748 INNER JOIN {$wpdb->prefix}booking as bk
749
750 ON bk.booking_id = dt.booking_id
751
752 WHERE ( 1 = 1 )";
753
754 // W H E R E
755 $sql_where = '';
756 $sql_where .= ( '' != $params['approved'] ) ? " AND ( dt.approved = {$params['approved']} ) " : ''; // Approved (1) or Pending (0) or All // int
757 $sql_where .= " AND dt.booking_date >= CURDATE() "; // Only actual bookings
758 $sql_where .= " AND bk.trash != 1 "; // Not in Trash // int
759 $sql_where .= " AND bk.booking_type IN ( {$params['resource_id']} ) "; // For specific calendar (booking resource) // int
760 $sql_where .= ( '' != $params['skip_booking_id'] ) ? " AND dt.booking_id NOT IN ( {$params['skip_booking_id']} ) " : '' ; // Skip some bookings ? Usually, during booking edit.
761
762 // O R D E R
763 $sql_order = " ORDER BY dt.booking_date"; // Order by booking dates & times
764
765
766 /**
767 * Array( [0] => stdClass Object ( [booking_date] => 2022-12-27 00:00:00, [approved] => 0, [booking_id] => 187 )
768 [1] => stdClass Object ( [booking_date] => 2022-12-28 00:00:00, [approved] => 1, [booking_id] => 26 )
769 ...
770 */
771 $result_arr = $wpdb->get_results( $sql . $sql_where . $sql_order );
772
773 // P A R S E
774 $prior_check_out_date = false;
775 $dates_arr = array();
776 foreach ( $result_arr as $sql_date ) {
777
778 $blocked_days_range = array( $sql_date->booking_date );
779 $resource_id = explode( ',', $params['resource_id'] );
780 $resource_id = $resource_id[0];
781 if (
782 ( ! class_exists( 'wpdev_bk_biz_l' ) )
783 || ( ( class_exists( 'wpdev_bk_biz_l' ) ) && ( ! wpbc_is_this_parent_resource( $resource_id ) ) )
784 ){ //FixIn: 9.1.2.7
785 list( $blocked_days_range, $prior_check_out_date ) = apply_filters( 'wpbc_get_extended_block_dates_filter', array( $blocked_days_range, $prior_check_out_date ) );
786 }
787
788 foreach ( $blocked_days_range as $date_ymd_his ) {
789
790 $sql_date->booking_date = $date_ymd_his;
791
792
793 $date_as_int = strtotime( $sql_date->booking_date );
794
795 $date_key = date( 'Y-m-d', $date_as_int );
796 $date_seconds = $date_as_int - strtotime( $date_key );
797
798 // Transform '2022-09-01' to 9-1-2022
799 $date_key__for_calendar = date( '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
800
801 if ( empty( $dates_arr[ $date_key__for_calendar ] ) ) {
802 $dates_arr[ $date_key__for_calendar ] = array();
803 }
804
805 /**
806 * Important info about [ 'sec_' . $date_seconds ]
807 *
808 * We need to have 'sec_0' instead of simple 0
809 *
810 * for having JavaScript Objects (object property 'sec_o') instead of Array (index 0), after sending Ajax response and JSON decode!
811 */
812 $dates_arr[ $date_key__for_calendar ][ 'sec_' . $date_seconds] = $sql_date;
813 }
814
815
816 }
817
818 return $dates_arr;
819 }
820
821
822 /**
823 * Get season availability based on booking resource and seasons from Booking > Resources > Availability page
824 *
825 * @param array $params array(
826 'resource_id' => 1 // int or dcv
827 , 'from' => 'NOW' // any value that is possible to use in strtotime()
828 , 'count' => 365 // int
829 )
830 *
831 * @return array Array (
832 [2023-01-09] => 1
833 [2023-01-10] => 1
834 [2023-01-11] => 1
835 [2023-01-12] => 1
836 [2023-01-13] => 1
837 [2023-01-14] =>
838 [2023-01-15] =>
839 [2023-01-16] => 1
840 [2023-01-17] => 1
841 [2023-01-18] => 1
842 [2023-01-19] => 1
843 ...
844 */
845 function wpbc__sql__get_season_availability( $params ){
846
847 //FixIn: 9.5.4.4
848 $max_days_count = 365;
849 $max_monthes_in_calendar = get_bk_option( 'booking_max_monthes_in_calendar' );
850
851 if ( strpos( $max_monthes_in_calendar, 'm' ) !== false ) {
852 $max_days_count = intval( str_replace( 'm', '', $max_monthes_in_calendar ) ) * 31 + 5; //FixIn: 9.6.1.1
853 } else {
854 $max_days_count = intval( str_replace( 'y', '', $max_monthes_in_calendar ) ) * 365 + 15; //FixIn: 9.6.1.1
855 }
856
857 $defaults = array(
858 'resource_id' => 1 // int or dcv
859 , 'from' => 'NOW' // any value that is possible to use in strtotime()
860 , 'count' => $max_days_count // int
861 );
862 $params = wp_parse_args( $params, $defaults );
863
864
865 // S a n i t i z e
866 $params['resource_id'] = ( '' != $params['resource_id'] ) ? wpbc_sanitize_digit_or_csd( $params['resource_id'] ) : 1;
867
868 $is_all_days_available = true;
869
870 $season_filters_id_arr = array();
871
872 if ( ( class_exists( 'wpdev_bk_biz_m' ) ) && ( function_exists( 'wpbc_get_resource_meta' ) ) ) { // BM and higher //FixIn: 9.9.0.13
873
874 // S Q L
875 $availability_res = wpbc_get_resource_meta( $params['resource_id'], 'availability' );
876
877 if ( ! empty( $availability_res ) ) {
878
879 /**
880 * Array ( [general] => On, [filter] => Array ( [1] => On, ...
881 * [2] => Off
882 * ...
883 * [8] => Off
884 * [9] => On
885 * )
886 * )
887 */
888 $availability = maybe_unserialize( $availability_res[0]->value );
889
890 $is_all_days_available = ( 'On' === $availability['general'] ) ? true : false;
891 $season_filter = $availability['filter'];
892
893 // Get ID of only activated Seasons
894 if ( is_array( $season_filter ) ) {
895 foreach ( $season_filter as $key => $value ) {
896 if ( $value == 'On' ) {
897 $season_filters_id_arr[] = intval( $key ); // Sanitize booking_filter_id for future SQL
898 }
899 }
900 }
901
902 }
903
904 }
905
906 $days_availability = array();
907
908 for( $i = 0; $i < $params['count']; $i++) {
909
910 $date_y_m_d = date( 'Y-m-d', strtotime( '+' . $i . 'days', strtotime( $params['from'] ) ) );
911
912 $days_availability[ $date_y_m_d ] = $is_all_days_available;
913
914 $date_arr = explode( '-', $date_y_m_d );
915
916 foreach ( $season_filters_id_arr as $filter_id ) {
917
918 $day = intval( $date_arr[2] );
919 $month = intval( $date_arr[1] );
920 $year = intval( $date_arr[0] );
921
922 if ( wpbc_is_day_inside_of_filter( $day, $month, $year, $filter_id ) ){
923 $days_availability[ $date_y_m_d ] = ! $days_availability[ $date_y_m_d ];
924 break;
925 }
926 }
927
928 }
929
930 return $days_availability;
931 }