| 1 |
<?php |
| 2 |
/** |
| 3 |
* Working Time settings and availability helpers. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Get default Working Time settings. |
| 14 |
* |
| 15 |
* @return array |
| 16 |
*/ |
| 17 |
function wpbc_working_time__get_default_settings() { |
| 18 |
|
| 19 |
$weekdays = array(); |
| 20 |
|
| 21 |
for ( $day = 0; $day <= 6; $day++ ) { |
| 22 |
$weekdays[ $day ] = array(); |
| 23 |
if ( $day >= 1 && $day <= 5 ) { |
| 24 |
$weekdays[ $day ][] = array( |
| 25 |
'start_second' => 9 * HOUR_IN_SECONDS, |
| 26 |
'end_second' => 18 * HOUR_IN_SECONDS, |
| 27 |
); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
return array( |
| 32 |
'enabled' => 'Off', |
| 33 |
'default' => array( |
| 34 |
'weekdays' => $weekdays, |
| 35 |
), |
| 36 |
'resources' => array(), |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Normalize Working Time settings. |
| 42 |
* |
| 43 |
* @param mixed $settings Settings. |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
function wpbc_working_time__normalize_settings( $settings ) { |
| 48 |
|
| 49 |
$defaults = wpbc_working_time__get_default_settings(); |
| 50 |
|
| 51 |
if ( ! is_array( $settings ) ) { |
| 52 |
return $defaults; |
| 53 |
} |
| 54 |
|
| 55 |
$settings = wp_parse_args( $settings, $defaults ); |
| 56 |
|
| 57 |
$settings['enabled'] = ( 'On' === (string) $settings['enabled'] ) ? 'On' : 'Off'; |
| 58 |
$settings['default'] = array( |
| 59 |
'weekdays' => wpbc_working_time__normalize_weekday_intervals( |
| 60 |
isset( $settings['default']['weekdays'] ) ? $settings['default']['weekdays'] : array() |
| 61 |
), |
| 62 |
); |
| 63 |
|
| 64 |
if ( ! is_array( $settings['resources'] ) ) { |
| 65 |
$settings['resources'] = array(); |
| 66 |
} |
| 67 |
|
| 68 |
foreach ( $settings['resources'] as $resource_id => $resource_settings ) { |
| 69 |
$resource_id = absint( $resource_id ); |
| 70 |
if ( empty( $resource_id ) || ! is_array( $resource_settings ) ) { |
| 71 |
unset( $settings['resources'][ $resource_id ] ); |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
$mode = isset( $resource_settings['mode'] ) ? (string) $resource_settings['mode'] : 'inherit'; |
| 76 |
if ( ! in_array( $mode, array( 'inherit', 'custom', 'disabled' ), true ) ) { |
| 77 |
$mode = 'inherit'; |
| 78 |
} |
| 79 |
|
| 80 |
$settings['resources'][ $resource_id ] = array( |
| 81 |
'mode' => $mode, |
| 82 |
'weekdays' => wpbc_working_time__normalize_weekday_intervals( |
| 83 |
isset( $resource_settings['weekdays'] ) ? $resource_settings['weekdays'] : array() |
| 84 |
), |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
return $settings; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Normalize weekly intervals. |
| 93 |
* |
| 94 |
* @param mixed $weekdays Weekday intervals. |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
function wpbc_working_time__normalize_weekday_intervals( $weekdays ) { |
| 99 |
|
| 100 |
$normalized = array(); |
| 101 |
|
| 102 |
for ( $day = 0; $day <= 6; $day++ ) { |
| 103 |
$normalized[ $day ] = array(); |
| 104 |
|
| 105 |
if ( empty( $weekdays[ $day ] ) || ! is_array( $weekdays[ $day ] ) ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
foreach ( $weekdays[ $day ] as $interval ) { |
| 110 |
if ( ! is_array( $interval ) ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$start_second = isset( $interval['start_second'] ) ? absint( $interval['start_second'] ) : 0; |
| 115 |
$end_second = isset( $interval['end_second'] ) ? absint( $interval['end_second'] ) : 0; |
| 116 |
$start_second = max( 0, min( DAY_IN_SECONDS, $start_second ) ); |
| 117 |
$end_second = max( 0, min( DAY_IN_SECONDS, $end_second ) ); |
| 118 |
|
| 119 |
if ( $start_second < $end_second ) { |
| 120 |
$normalized[ $day ][] = array( |
| 121 |
'start_second' => $start_second, |
| 122 |
'end_second' => $end_second, |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
usort( |
| 128 |
$normalized[ $day ], |
| 129 |
function ( $a, $b ) { |
| 130 |
return $a['start_second'] - $b['start_second']; |
| 131 |
} |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
return $normalized; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get stored Working Time settings. |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
function wpbc_working_time__get_settings() { |
| 144 |
|
| 145 |
$settings = get_bk_option( 'booking_working_time_rules', array() ); |
| 146 |
|
| 147 |
return wpbc_working_time__normalize_settings( maybe_unserialize( $settings ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Save Working Time settings. |
| 152 |
* |
| 153 |
* @param array $settings Settings. |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
function wpbc_working_time__update_settings( $settings ) { |
| 158 |
|
| 159 |
$settings = wpbc_working_time__normalize_settings( $settings ); |
| 160 |
|
| 161 |
update_bk_option( 'booking_working_time_rules', $settings ); |
| 162 |
update_bk_option( 'booking_working_time_enabled', $settings['enabled'] ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Add default Working Time options on activation/update. |
| 167 |
* |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
function wpbc_activation__working_time() { |
| 171 |
|
| 172 |
$settings = wpbc_working_time__get_default_settings(); |
| 173 |
|
| 174 |
add_bk_option( 'booking_working_time_enabled', 'Off' ); |
| 175 |
add_bk_option( 'booking_working_time_rules', $settings ); |
| 176 |
} |
| 177 |
add_bk_action( 'wpbc_free_version_activation', 'wpbc_activation__working_time' ); |
| 178 |
add_bk_action( 'wpbc_other_versions_activation', 'wpbc_activation__working_time' ); |
| 179 |
|
| 180 |
/** |
| 181 |
* Keep Working Time options present after plugin updates for existing installs. |
| 182 |
* |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
function wpbc_working_time__maybe_install_options() { |
| 186 |
|
| 187 |
if ( ! is_admin() ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if ( |
| 192 |
( false === get_bk_option( 'booking_working_time_enabled', false ) ) |
| 193 |
|| ( false === get_bk_option( 'booking_working_time_rules', false ) ) |
| 194 |
) { |
| 195 |
wpbc_activation__working_time(); |
| 196 |
} |
| 197 |
} |
| 198 |
add_action( 'admin_init', 'wpbc_working_time__maybe_install_options' ); |
| 199 |
|
| 200 |
/** |
| 201 |
* Remove Working Time options when Booking Calendar data is deleted on deactivation. |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
function wpbc_deactivation__working_time() { |
| 206 |
|
| 207 |
delete_bk_option( 'booking_working_time_enabled' ); |
| 208 |
delete_bk_option( 'booking_working_time_rules' ); |
| 209 |
} |
| 210 |
add_bk_action( 'wpbc_free_version_deactivation', 'wpbc_deactivation__working_time' ); |
| 211 |
add_bk_action( 'wpbc_other_versions_deactivation', 'wpbc_deactivation__working_time' ); |
| 212 |
|
| 213 |
/** |
| 214 |
* Convert time string into seconds. |
| 215 |
* |
| 216 |
* @param string $time_value Time in H:i format. |
| 217 |
* |
| 218 |
* @return int |
| 219 |
*/ |
| 220 |
function wpbc_working_time__time_to_seconds( $time_value ) { |
| 221 |
|
| 222 |
$time_value = (string) $time_value; |
| 223 |
if ( ! preg_match( '/^([01]?\d|2[0-4]):([0-5]\d)$/', $time_value, $matches ) ) { |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
$hours = min( 24, absint( $matches[1] ) ); |
| 228 |
$mins = ( 24 === $hours ) ? 0 : absint( $matches[2] ); |
| 229 |
|
| 230 |
return min( DAY_IN_SECONDS, ( $hours * HOUR_IN_SECONDS ) + ( $mins * MINUTE_IN_SECONDS ) ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Convert seconds into H:i time. |
| 235 |
* |
| 236 |
* @param int $seconds Seconds. |
| 237 |
* |
| 238 |
* @return string |
| 239 |
*/ |
| 240 |
function wpbc_working_time__seconds_to_time( $seconds ) { |
| 241 |
|
| 242 |
$seconds = max( 0, min( DAY_IN_SECONDS, absint( $seconds ) ) ); |
| 243 |
$hours = (int) floor( $seconds / HOUR_IN_SECONDS ); |
| 244 |
$mins = (int) floor( ( $seconds % HOUR_IN_SECONDS ) / MINUTE_IN_SECONDS ); |
| 245 |
|
| 246 |
return sprintf( '%02d:%02d', $hours, $mins ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get effective Working Time rule for a resource. |
| 251 |
* |
| 252 |
* @param int $resource_id Resource ID. |
| 253 |
* |
| 254 |
* @return array|false |
| 255 |
*/ |
| 256 |
function wpbc_working_time__get_effective_rule( $resource_id ) { |
| 257 |
|
| 258 |
$settings = wpbc_working_time__get_settings(); |
| 259 |
$resource_id = absint( $resource_id ); |
| 260 |
|
| 261 |
if ( 'On' !== $settings['enabled'] ) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
if ( ! empty( $settings['resources'][ $resource_id ] ) ) { |
| 266 |
$resource_settings = $settings['resources'][ $resource_id ]; |
| 267 |
|
| 268 |
if ( 'disabled' === $resource_settings['mode'] ) { |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
if ( 'custom' === $resource_settings['mode'] ) { |
| 273 |
return array( |
| 274 |
'source' => 'resource', |
| 275 |
'weekdays' => $resource_settings['weekdays'], |
| 276 |
); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return array( |
| 281 |
'source' => 'default', |
| 282 |
'weekdays' => $settings['default']['weekdays'], |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Get working intervals for date/resource. |
| 288 |
* |
| 289 |
* @param int $resource_id Resource ID. |
| 290 |
* @param string $sql_date SQL date. |
| 291 |
* |
| 292 |
* @return array |
| 293 |
*/ |
| 294 |
function wpbc_working_time__get_working_intervals_for_date( $resource_id, $sql_date ) { |
| 295 |
|
| 296 |
$rule = wpbc_working_time__get_effective_rule( $resource_id ); |
| 297 |
if ( false === $rule || empty( $sql_date ) ) { |
| 298 |
return array(); |
| 299 |
} |
| 300 |
|
| 301 |
$weekday = (int) gmdate( 'w', strtotime( $sql_date . ' 00:00:00' ) ); |
| 302 |
|
| 303 |
return isset( $rule['weekdays'][ $weekday ] ) ? $rule['weekdays'][ $weekday ] : array(); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get outside-working-time intervals for date/resource. |
| 308 |
* |
| 309 |
* @param int $resource_id Resource ID. |
| 310 |
* @param string $sql_date SQL date. |
| 311 |
* |
| 312 |
* @return array |
| 313 |
*/ |
| 314 |
function wpbc_working_time__get_non_working_intervals_for_date( $resource_id, $sql_date ) { |
| 315 |
|
| 316 |
$working_intervals = wpbc_working_time__get_working_intervals_for_date( $resource_id, $sql_date ); |
| 317 |
|
| 318 |
if ( false === wpbc_working_time__get_effective_rule( $resource_id ) ) { |
| 319 |
return array(); |
| 320 |
} |
| 321 |
|
| 322 |
if ( empty( $working_intervals ) ) { |
| 323 |
return array( |
| 324 |
array( |
| 325 |
'date' => $sql_date, |
| 326 |
'resource_id' => absint( $resource_id ), |
| 327 |
'start_second' => 0, |
| 328 |
'end_second' => DAY_IN_SECONDS, |
| 329 |
'source_type' => 'working_time', |
| 330 |
'readable_time' => __( 'Full day', 'booking' ), |
| 331 |
), |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
$busy = array(); |
| 336 |
$cursor = 0; |
| 337 |
|
| 338 |
foreach ( $working_intervals as $interval ) { |
| 339 |
$start_second = absint( $interval['start_second'] ); |
| 340 |
$end_second = absint( $interval['end_second'] ); |
| 341 |
|
| 342 |
if ( $cursor < $start_second ) { |
| 343 |
$busy[] = array( |
| 344 |
'date' => $sql_date, |
| 345 |
'resource_id' => absint( $resource_id ), |
| 346 |
'start_second' => $cursor, |
| 347 |
'end_second' => $start_second, |
| 348 |
'source_type' => 'working_time', |
| 349 |
'readable_time' => wpbc_transform_timerange__seconds_arr__in__formated_hours( array( $cursor, $start_second ) ), |
| 350 |
); |
| 351 |
} |
| 352 |
|
| 353 |
$cursor = max( $cursor, $end_second ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( $cursor < DAY_IN_SECONDS ) { |
| 357 |
$busy[] = array( |
| 358 |
'date' => $sql_date, |
| 359 |
'resource_id' => absint( $resource_id ), |
| 360 |
'start_second' => $cursor, |
| 361 |
'end_second' => DAY_IN_SECONDS, |
| 362 |
'source_type' => 'working_time', |
| 363 |
'readable_time' => wpbc_transform_timerange__seconds_arr__in__formated_hours( array( $cursor, DAY_IN_SECONDS ) ), |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
return $busy; |
| 368 |
} |
| 369 |
|