Skip to content

Instantly share code, notes, and snippets.

@rilwis

rilwis/function.php

Forked from kutoi94/function.php
Last active Jan 31, 2020
Embed
What would you like to do?
How to Build a Hotel Booking Website Using Meta Box (P3) - functions.php
if ( ! get_option( 'rwmb_bookings' ) ) {
add_option( 'rwmb_bookings', array() );
}
add_action( 'rwmb_booking-fields_after_save_post', 'update_bookings_date' );
function update_bookings_date( $post_id ) {
$bookings = get_post_meta( $post_id, 'group_booking', true );
if ( empty( $bookings ) || ! is_array( $bookings ) ) {
return;
}
$option = get_option( 'rwmb_bookings' );
foreach ( $bookings as $key => $booking ) {
$room = $booking['room'];
$begin = new DateTime( $booking['check_in'] );
$end = new DateTime( $booking['check_out'] );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates_booking = array();
foreach($daterange as $date){
array_push($dates_booking, $date->format("Y-m-d"));
}
$option[$room][$post_id][$key] = $dates_booking;
}
update_option('rwmb_bookings', $option);
}
function dates_disable($room_id){
$bookings = get_option('rwmb_bookings')[$room_id];
if (empty($bookings) && !is_array($bookings)) return;
$dates = array();
$disable = array();
foreach ($bookings as $booking) {
foreach ($booking as $value) {
foreach ($value as $k ) {
array_push($dates, $k);
}
}
}
$dates = array_count_values($dates);
$quantity = rwmb_meta( 'quantity', $room_id);
foreach ($dates as $key => $date) {
if ($date >= $quantity) {
array_push($disable, $key);
}
}
return $disable;
}
function enqueue_script() {
if (is_singular('room')) {
wp_enqueue_script('custom-script', get_template_directory_uri().'/js/custom.js', array( 'jquery' ));
wp_localize_script( 'custom-script', 'ajaxurl', admin_url('admin-ajax.php'));
wp_localize_script( 'custom-script', 'disable_dates', json_encode(dates_disable(get_the_ID())));
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_script');
/**
* Remove expired date in variable option
*/
function array_filter_recursive ($data) {
$original = $data;
$data = array_filter($data);
$data = array_map(function ($e) {
return is_array($e) ? array_filter_recursive($e) : $e;
}, $data);
return $original === $data ? $data : array_filter_recursive($data);
}
function optimal_bookings_option() {
$bookings = get_option('rwmb_bookings');
$today = date("Y-m-d");
foreach ($bookings as $key_1 => $bk_1) {
foreach ($bk_1 as $key_2 => $bk_2) {
foreach ($bk_2 as $key_3 => $bk_3 ) {
foreach ($bk_3 as $key_4 => $bk_4) {
if ($bk_4 < $today) {
unset($bookings[$key_1][$key_2][$key_3][$key_4]);
}
}
}
}
}
$bookings = array_filter_recursive($bookings);
update_option('rwmb_bookings', $bookings);
}
add_action( 'init', 'optimal_bookings_option' );
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) {
if ( 'booking-fields' !== $config['id'] ) {
return $validate;
}
$disable_dates = dates_disable(get_the_ID());
$checkin = date("Y-m-d", strtotime($_POST['group_booking_check_in']));
$checkout = date("Y-m-d", strtotime($_POST['group_booking_check_out']));
if ( false !== array_search($checkin, $disable_dates)) {
$validate = false;
} else {
update_post_meta($config['post_id'], 'group_booking_room', get_the_ID());
}
return $validate;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.