| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4. |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* Conception: - Time-Slots A - B '10:00:01 - 12:00:02' '10:00:01 - 12:00:02' |
| 8 |
* |
| 9 |
* - Check in A - ... '14:00:01 - ... ' '14:00:01 - 24:00:00' |
| 10 |
* - Check out ... - B ' ... - 12:00:02' '00:00:00 - 12:00:02' |
| 11 |
* |
| 12 |
* - Full day ... - ... ' ... - ... ' '00:00:00 - 24:00:00' |
| 13 |
* |
| 14 |
* - Full IN ..1 - ... '00:00:01 - ... ' '00:00:01 - 24:00:00' |
| 15 |
* - Full OUT ... - ..2 ' ... - 24:00:02' '00:00:00 - 24:00:02' |
| 16 |
* |
| 17 |
* |
| 18 |
* 1) Always in one specific date, can be start & end times. |
| 19 |
* 2) Such start end times can define |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
class WPBC_BOOKING_DATE { |
| 24 |
|
| 25 |
public $readable_dates; |
| 26 |
|
| 27 |
public $is_debug; |
| 28 |
|
| 29 |
/** |
| 30 |
* Define booking date object by readable dates array >> [ '2023-08-01': [ 0: "10:00:01 - 12:00:02" ], '2023-08-05': [ 0:"10:00:01 - 12:00:02" ] ...] |
| 31 |
* @param $boking_dates_arr |
| 32 |
*/ |
| 33 |
function __construct( $boking_dates_arr ) { |
| 34 |
|
| 35 |
// [ '2023-08-01': [ 0: "10:00:01 - 12:00:02" ], '2023-08-05': [ 0:"10:00:01 - 12:00:02" ] ...] |
| 36 |
$this->readable_dates = $boking_dates_arr; |
| 37 |
|
| 38 |
$this->is_debug = false; // Gathering additional data into the variables |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Is this readable_time 'Check In' '10:00:01 - 12:00:02' | '14:00:01 - 24:00:00' | '00:00:01 - 24:00:00' -> true |
| 44 |
* |
| 45 |
* @param $readable_time_range '10:00:01 - 12:00:02' |
| 46 |
* |
| 47 |
* @return bool true | false |
| 48 |
*/ |
| 49 |
public function is_time_check_in( $readable_time_range ){ |
| 50 |
|
| 51 |
$time_24_hours_arr = explode( '-', $readable_time_range ); |
| 52 |
|
| 53 |
$time_24_hours_slot = (string) trim( $time_24_hours_arr[0] ); |
| 54 |
|
| 55 |
$is_check_in_out__or_full = substr( $time_24_hours_slot, -1 ); // '1', '2' (not '0') |
| 56 |
|
| 57 |
$result = ( '1' === $is_check_in_out__or_full ) ? true : false; |
| 58 |
|
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Is this readable_time 'Check Out' '10:00:01 - 12:00:02' | '00:00:00 - 12:00:02' | '00:00:00 - 24:00:02' -> true |
| 65 |
* |
| 66 |
* @param $readable_time_range '10:00:01 - 12:00:02' |
| 67 |
* |
| 68 |
* @return bool true | false |
| 69 |
*/ |
| 70 |
public function is_time_check_out( $readable_time_range ){ |
| 71 |
|
| 72 |
$time_24_hours_arr = explode( '-', $readable_time_range ); |
| 73 |
|
| 74 |
$time_24_hours_slot = (string) trim( $time_24_hours_arr[1] ); |
| 75 |
|
| 76 |
$is_check_in_out__or_full = substr( $time_24_hours_slot, -1 ); // '1', '2' (not '0') |
| 77 |
|
| 78 |
$result = ( '2' === $is_check_in_out__or_full ) ? true : false; |
| 79 |
|
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Is this readable_time 'Full Day' '00:00:00 - 24:00:00' => true |
| 86 |
* |
| 87 |
* @param $readable_time_range '10:00:01 - 12:00:02' |
| 88 |
* |
| 89 |
* @return bool true | false |
| 90 |
*/ |
| 91 |
public function is_time_full_day( $readable_time_range ){ |
| 92 |
|
| 93 |
$result = ( '00:00:00 - 24:00:00' === $readable_time_range ) ? true : false; |
| 94 |
|
| 95 |
return $result; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Loop all dates, and APPLY_SPECIFIC_ACTION to Times Arr of specific dates |
| 102 |
* |
| 103 |
* @param string $filter_action 'get_times__as_24_hour_arr' | 'get_times__as_seconds_arr' | 'get_times__as_timestamps_arr' |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function loop_all_dates( $filter_action ){ |
| 108 |
|
| 109 |
$result = array(); |
| 110 |
|
| 111 |
foreach ( $this->readable_dates as $only_date_sql => $readable_times_arr ) { |
| 112 |
|
| 113 |
switch ( $filter_action ) { |
| 114 |
|
| 115 |
case 'get_times__as_24_hour_arr': |
| 116 |
|
| 117 |
$result[$only_date_sql] = $this->get__filtered_times__as_arr( $only_date_sql ); |
| 118 |
break; |
| 119 |
|
| 120 |
case 'get_times__as_seconds_arr': |
| 121 |
|
| 122 |
$result[ $only_date_sql ] = $this->get__filtered_times__as_arr( $only_date_sql, array( 'filter' => 'as_seconds' ) ); |
| 123 |
break; |
| 124 |
|
| 125 |
case 'get_times__as_timestamps_arr': |
| 126 |
|
| 127 |
$result[ $only_date_sql ] = $this->get__filtered_times__as_arr( $only_date_sql, array( 'filter' => 'as_timestamps' ) ); |
| 128 |
break; |
| 129 |
|
| 130 |
default: |
| 131 |
// Default |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $result; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Get times in different formats: [ "10:00:01", "12:00:02" ] | [ 36001, 43202 ] |
| 141 |
* |
| 142 |
* @param $date_sql '2023-07-30' |
| 143 |
* @param $params [] | [ 'filter' => 'as_seconds' ] |
| 144 |
* |
| 145 |
* @return array [ "10:00:01", "12:00:02" ] | [ 36001, 43202 ] |
| 146 |
*/ |
| 147 |
protected function get__filtered_times__as_arr( $date_sql, $params = array() ){ |
| 148 |
|
| 149 |
if ( ! isset( $this->readable_dates[ $date_sql ] ) ) { |
| 150 |
return array(); |
| 151 |
} |
| 152 |
|
| 153 |
$defaults = array( |
| 154 |
'filter' => '' |
| 155 |
); |
| 156 |
$params = wp_parse_args( $params, $defaults ); |
| 157 |
|
| 158 |
|
| 159 |
$times_as_arr = array(); |
| 160 |
foreach ( $this->readable_dates[ $date_sql ] as $time_range ) { |
| 161 |
|
| 162 |
$time_24_hours_arr = explode( '-', $time_range ); |
| 163 |
|
| 164 |
foreach ( $time_24_hours_arr as $time_24_hour ) { // [ '10:00:01', '14:00:02' ] |
| 165 |
|
| 166 |
$time_24_hour = trim( $time_24_hour ); |
| 167 |
|
| 168 |
switch ( $params['filter'] ) { |
| 169 |
|
| 170 |
case 'as_seconds': |
| 171 |
|
| 172 |
$filtered_time = $this->transform__24_hours__in__seconds( $time_24_hour ); |
| 173 |
break; |
| 174 |
|
| 175 |
case 'as_timestamps': |
| 176 |
|
| 177 |
$filtered_time = $this->transform__24_hours__in__timestamp( $date_sql, $time_24_hour ); |
| 178 |
break; |
| 179 |
|
| 180 |
default: |
| 181 |
$filtered_time = $time_24_hour; |
| 182 |
} |
| 183 |
|
| 184 |
$times_as_arr[] = $filtered_time; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $times_as_arr; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
// ------------------------------------------------------------------------------------------------------------- |
| 193 |
|
| 194 |
/** |
| 195 |
* Get timestamp from SQL DATE '2023-07-30 10:00:01' -> 1690884001 |
| 196 |
* |
| 197 |
* @param $sql_date_ymd_his '2023-08-02 22:14:01' |
| 198 |
* |
| 199 |
* @return false|int |
| 200 |
*/ |
| 201 |
protected function transform__sql_date_ymd_his__in__timestamp( $sql_date_ymd_his ){ |
| 202 |
|
| 203 |
$date_ymd__in_seconds = strtotime( $sql_date_ymd_his ); |
| 204 |
|
| 205 |
return $date_ymd__in_seconds; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get timestamp '2023-08-01', '10:00:01' -> 1690884001 |
| 210 |
* |
| 211 |
* @param $only_date_sql '2023-08-01' |
| 212 |
* @param $time_24_hour '10:00:01' |
| 213 |
* |
| 214 |
* @return float|int 1690884001 |
| 215 |
*/ |
| 216 |
protected function transform__24_hours__in__timestamp( $only_date_sql, $time_24_hour ){ |
| 217 |
|
| 218 |
$date_ymd__in_seconds = $this->transform__only_date_ymd__in__timestamp( $only_date_sql ); |
| 219 |
|
| 220 |
$time_in_seconds = $this->transform__24_hours__in__seconds( $time_24_hour ); |
| 221 |
|
| 222 |
$timestamp = $date_ymd__in_seconds + $time_in_seconds; |
| 223 |
|
| 224 |
return $timestamp; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Get timestamp from SQL only DATE '2023-07-30' -> 1690884001 |
| 230 |
* |
| 231 |
* @param $only_date_sql |
| 232 |
* |
| 233 |
* @return false|int |
| 234 |
*/ |
| 235 |
protected function transform__only_date_ymd__in__timestamp( $only_date_sql ){ |
| 236 |
|
| 237 |
// $date_ymd__in_seconds = wpbc_convert__date_ymd__to_seconds( $date_sql ); |
| 238 |
|
| 239 |
$date_ymd__in_seconds = strtotime( $only_date_sql . ' 00:00:00' ); |
| 240 |
|
| 241 |
return $date_ymd__in_seconds; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Get SQL DATE from timestamp 1690884001 -> '2023-07-30 10:00:01' |
| 247 |
* |
| 248 |
* @param int $in_seconds |
| 249 |
* @param string $sql_date_format 'Y-m-d H:i:s' | 'Y-m-d 00:00:00' |
| 250 |
* |
| 251 |
* @return false|string |
| 252 |
*/ |
| 253 |
protected function transform__timestamp__to_sql_date( $in_seconds, $sql_date_format = 'Y-m-d H:i:s' ){ |
| 254 |
|
| 255 |
$sql_date = gmdate( $sql_date_format, $in_seconds ); |
| 256 |
|
| 257 |
return $sql_date; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
// ------------------------------------------------------------------------------------------------------------- |
| 262 |
|
| 263 |
/** |
| 264 |
* Get SQL TIME from SECONDS 43201 -> '10:00:02' |
| 265 |
* |
| 266 |
* @param int $in_seconds 43201 |
| 267 |
* |
| 268 |
* @return string '10:00:02' |
| 269 |
*/ |
| 270 |
protected function transform__seconds__in__24_hours_his( $in_seconds ){ |
| 271 |
|
| 272 |
return wpbc_transform__seconds__in__24_hours_his( $in_seconds ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Get seconds '10:00:01' -> 36001 |
| 278 |
* |
| 279 |
* @param $time_24_hour '10:00:01' |
| 280 |
* |
| 281 |
* @return float|int 36001 |
| 282 |
*/ |
| 283 |
protected function transform__24_hours__in__seconds( $time_24_hour ){ |
| 284 |
|
| 285 |
$time_24_hour_arr = explode( ':', $time_24_hour ); |
| 286 |
|
| 287 |
// 43202 |
| 288 |
$in_seconds = intval( $time_24_hour_arr[0] ) * 60 * 60 // Hours |
| 289 |
+ intval( $time_24_hour_arr[1] ) * 60 // Minutes |
| 290 |
+ intval( $time_24_hour_arr[2] ); // Seconds |
| 291 |
|
| 292 |
return $in_seconds; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Convert Readable dates to SQL dates array for using in Calendar: [ "2023-09-30":["10:00:01 - 24:00:00"],"2023-10-01":["00:00:00 - 24:00:00"], .. ] -> [ [1696068001] => 2023-09-30 10:00:01, [1696118400] => 2023-10-01 00:00:00, [1696204800] => 2023-10-02 00:00:00 ... ] |
| 298 |
* |
| 299 |
* @return array [ |
| 300 |
* [1696068001] => 2023-09-30 10:00:01 |
| 301 |
* [1696118400] => 2023-10-01 00:00:00 |
| 302 |
* [1696204800] => 2023-10-02 00:00:00 |
| 303 |
* [1696334402] => 2023-10-03 12:00:02 |
| 304 |
* ... |
| 305 |
* ] |
| 306 |
*/ |
| 307 |
public function convert_readable_dates__into__sql_dates__arr(){ |
| 308 |
|
| 309 |
$sql_dates_times_arr = array(); |
| 310 |
|
| 311 |
// Get unavailable extended time_ranges arr |
| 312 |
foreach ( $this->readable_dates as $only_date_sql => $readable_times_arr ) { |
| 313 |
|
| 314 |
foreach ( $this->readable_dates[ $only_date_sql ] as $readable_time_range ) { |
| 315 |
|
| 316 |
$time_24_hours_arr = explode( ' - ', $readable_time_range ); |
| 317 |
|
| 318 |
$only_date_timestamp = $this->transform__only_date_ymd__in__timestamp( $only_date_sql ); |
| 319 |
$check_in_seconds = $this->transform__24_hours__in__seconds( $time_24_hours_arr[0] ); |
| 320 |
$check_out_seconds = $this->transform__24_hours__in__seconds( $time_24_hours_arr[1] ); |
| 321 |
|
| 322 |
$is_add_check_in = true; |
| 323 |
$is_add_check_out = true; |
| 324 |
|
| 325 |
if ( |
| 326 |
( '00:00:00 - 24:00:00' == $readable_time_range ) // Full Day |
| 327 |
|| ( $check_out_seconds >= 86400 ) // Check out >= '24:00:00' then skip end time |
| 328 |
){ |
| 329 |
$is_add_check_out = false; |
| 330 |
} |
| 331 |
|
| 332 |
if ( |
| 333 |
( '00:00:00' == $time_24_hours_arr[0] ) && ( $check_out_seconds > 0 ) && ( $check_out_seconds < 86400 ) // '00:00:00 - 12:00:02' |
| 334 |
){ |
| 335 |
$is_add_check_in = false; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
// Check In |
| 340 |
if ( $is_add_check_in ) { |
| 341 |
$sql_dates_times_arr[ $only_date_timestamp + $check_in_seconds ] = $only_date_sql . ' ' . $time_24_hours_arr[0]; |
| 342 |
} |
| 343 |
|
| 344 |
// Check out |
| 345 |
if ( $is_add_check_out ) { |
| 346 |
$sql_dates_times_arr[ $only_date_timestamp + $check_out_seconds ] = $only_date_sql . ' ' . $time_24_hours_arr[1]; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return $sql_dates_times_arr; |
| 352 |
} |
| 353 |
} |
| 354 |
|