2
* 'skip_booking_id' => '', | 125 if edit booking
* 'dates_only_sql_arr' => [ "2023-10-18", "2023-10-25", "2023-11-25" ]
* 'time_as_seconds_arr' => [ 36000, 39600 ]
* 'how_many_items_to_book' => 1
* 'request_uri' => 'http://beta/resource-id2/' // Optional default -> DOING_AJAX ? $_SERVER['HTTP_REFERER'] : $_SERVER['REQUEST_URI']
* 'as_single_resource' => false, // Optional default false
* 'is_use_booking_recurrent_time' => false // Optional default ( 'On' === get_bk_option( 'booking_recurrent_time' ) )
* ]
* @return array = [ 'result' => 'ok', 'main__resource_id' => 2, 'resources_in_dates' => [ '2023-09-23':[2,10,11], '2023-09-24':[2,10,11] ], 'time_to_book' => [ "00:00:00", "24:00:00" ] ]
* Note. 'main__resource_id' - it's first booking resource in the list, basically needed for saving in wp_booking DB table.
* OR
* = [ 'result' => 'error', 'message' => 'Booking can not be saved ...' ]
*/
function wpbc__where_to_save_booking( $local_params ){
$server_request_uri = ( ( isset( $_SERVER['REQUEST_URI'] ) ) ? sanitize_text_field( $_SERVER['REQUEST_URI'] ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ /* FixIn: sanitize_unslash */
$server_http_referer_uri = ( ( isset( $_SERVER['HTTP_REFERER'] ) ) ? sanitize_text_field( $_SERVER['HTTP_REFERER'] ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ /* FixIn: sanitize_unslash */
$defaults = array(
'request_uri' => ( ( ( defined( 'DOING_AJAX' ) ) && ( DOING_AJAX ) ) ? $server_http_referer_uri : $server_request_uri ), // front-end: $server_request_uri | ajax: $server_http_referer_uri // It different in Ajax requests. It's used for change-over days to detect for exception at specific pages
'allow_past' => false,
'as_single_resource' => false,
'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) ),
'time_override_source' => '',
'aggregate_resource_id_arr' => array(),
'custom_form' => '' //FixIn: 10.0.0.10 // Required for checking all available time-slots and compare with booked time slots
);
$local_params = wp_parse_args( $local_params, $defaults );
$time_as_seconds_arr = $local_params['time_as_seconds_arr'];
$time_as_seconds_arr[0] = ( 0 != $time_as_seconds_arr[0] ) ? $time_as_seconds_arr[0] + 1 : $time_as_seconds_arr[0]; // +1 s. -- set check in time with ended 1 second
$time_as_seconds_arr[1] = ( ( 24 * 60 * 60 ) != $time_as_seconds_arr[1] ) ? $time_as_seconds_arr[1] + 2 : $time_as_seconds_arr[1]; // +2 s. -- set check out time with ended 2 seconds
if ( ( 0 != $time_as_seconds_arr[0] ) && ( ( 24 * 60 * 60 ) == $time_as_seconds_arr[1] ) ) {
//FixIn: 10.0.0.49 - in case if we have start time != 00:00 and end time as 24:00 then set end time as 23:59:52
$time_as_seconds_arr[1] += - 8;
}
$local_params['time_as_his_arr'] = array(
wpbc_transform__seconds__in__24_hours_his( $time_as_seconds_arr[0] ),
wpbc_transform__seconds__in__24_hours_his( $time_as_seconds_arr[1] )
);
// FixIn: 2981-01-13 13 Jan 2981
$availability_per_days_arr__params = array();
if ( ( ! empty( $local_params['dates_only_sql_arr'] ) ) && ( is_array( $local_params['dates_only_sql_arr'] ) ) && ( '2981-01-13' === $local_params['dates_only_sql_arr'][0] ) ) {
$availability_per_days_arr__params['is_days_always_available'] = true;
}
// If we are using the unavailable before/after booking dates, then we need to extend "$local_params['dates_only_sql_arr']" to such dates.
if ( function_exists( 'wpbc__extend_checking_dates_range__if_used__extra_seconds__before_after' ) ) {
$maybe_extended__dates_to_check_arr = wpbc__extend_checking_dates_range__if_used__extra_seconds__before_after( $local_params['dates_only_sql_arr'] );
} else {
$maybe_extended__dates_to_check_arr = $local_params['dates_only_sql_arr'];
}
// [ "dates": [ "2023-11-05": [ "day_availability":4, ... "2": [ "is_day_unavailable":false, ...]], '2023-11-06':[...] ] ,"resources_id_arr__in_dates":[2,12,10,11]}
$availability_per_days = wpbc_get_availability_per_days_arr( wp_parse_args( array(
'resource_id' => $local_params['resource_id'],
'skip_booking_id' => $local_params['skip_booking_id'],
'dates_to_check' => $maybe_extended__dates_to_check_arr,
'request_uri' => $local_params['request_uri'],
'allow_past' => ! empty( $local_params['allow_past'] ),
'as_single_resource' => $local_params['as_single_resource'], // default FALSE :: get dates as for 'single resource' or 'parent' resource including bookings in all 'child booking resources' .
'additional_bk_types' => $local_params['aggregate_resource_id_arr'],
'aggregate_type' => empty( $local_params['aggregate_type'] ) ? 'bookings_only' : $local_params['aggregate_type'], //TODO: this parameter does not transfer during saving, so here will be always default value 'bookings_only' // FixIn: 10.0.0.7.
'custom_form' => $local_params['custom_form'], // FixIn: 10.0.0.10.
) , $availability_per_days_arr__params ) );
// Get value of how many booking resources to book
$how_many_items_to_book = $local_params['how_many_items_to_book'];
// FixIn: 2981-01-13 13 Jan 2981.
if ( ( ! empty( $local_params['dates_only_sql_arr'] ) ) && ( wpbc_is_these_dates__for__no_dates( $local_params['dates_only_sql_arr'] ) ) ) {
$how_many_items_to_book = 0;
}
// -------------------------------------------------------------------------------------------------------------
// [ 2023-10-18: [ 0:[ resource_id: 2, is_available: true, booked__seconds: [], booked__readable: [], time_to_book__seconds: [ 36030, 39570 ], time_to_book__readable: "10:00:00", "11:00:00" ] ], 1: [...], 2: [...], 3: [...] ], 2023-10-25: [...], 2023-11-25: [...] ]
$available_slots = wpbc__get_available_slots__for_selected_dates_times__bl( array(
'dates_sql__arr' => $local_params['dates_only_sql_arr'],
'time_as_seconds_arr' => $local_params['time_as_seconds_arr'],
'is_use_booking_recurrent_time' => $local_params['is_use_booking_recurrent_time'],
'time_override_source' => $local_params['time_override_source'],
'availability_per_days' => $availability_per_days
) );
$main__resource_id = 0;
// == In SAME 'child' booking resources ============================================================
if ( 'On' === get_bk_option( 'booking_is_dissbale_booking_for_different_sub_resources' ) ) {
$availability_in_same_child_resources = $availability_per_days['resources_id_arr__in_dates'];
foreach ( $availability_per_days['resources_id_arr__in_dates'] as $child_resource_id ) {
foreach ( $local_params['dates_only_sql_arr'] as $day_sql_key ) {
foreach ( $available_slots[ $day_sql_key ] as $available_slot ) {
if (
( $child_resource_id == $available_slot['resource_id'] )
&& ( ! $available_slot['is_available'] )
){
$availability_in_same_child_resources = array_diff( $availability_in_same_child_resources, array( $child_resource_id ) ); // Remove $child_resource_id from array
break;
}
}
}
}
// Reset indexes in this array for ability to get first [0] element in this arr.
$availability_in_same_child_resources = array_values( $availability_in_same_child_resources );
$main__resource_id = ( ! empty( $availability_in_same_child_resources ) ) ? $availability_in_same_child_resources[0] : 0;
// [ 2, 12, 11 ] <- ID of child booking resources, where we can save our booking in the same child booking resource!
if ( count( $availability_in_same_child_resources ) >= $how_many_items_to_book ) {
$availability_in_same_child_resources_by_dates = array();
foreach ( $local_params['dates_only_sql_arr'] as $day_sql_key ) {
$availability_in_same_child_resources_by_dates[ $day_sql_key ] = $availability_in_same_child_resources;
}
// Good Save it
return array(
'result' => 'ok',
'resources_in_dates' => $availability_in_same_child_resources_by_dates, // [ 2023-09-23 = [ 2, 10, 11 ], 2023-09-24 = [ 2, 10, 11 ]
'time_to_book' => $local_params['time_as_his_arr'], // [ "00:00:00", "24:00:00" ]
'main__resource_id' => $main__resource_id
);
} else {
// Bad not save
return array(
'result' => 'error',
'message' => __( 'These dates and times in this calendar are already booked or unavailable.', 'booking' )
. '
'
. __( 'It is not possible to store this sequence of the dates into the one same resource.' , 'booking' )
. '
'
. __( 'Please choose alternative date(s), times, or adjust the number of slots booked.' , 'booking' )
. ' '
. __( 'Show more details', 'booking' )
. ''
. '