Skip to content

Instantly share code, notes, and snippets.

@kutoi94
Created January 16, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kutoi94/0823d986cc0d2ee34136440e03039239 to your computer and use it in GitHub Desktop.
Save kutoi94/0823d986cc0d2ee34136440e03039239 to your computer and use it in GitHub Desktop.
Part 3 Tạo và quản lí khách sạn với metabox plugin
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 ); // Lấy giá trị của field group_booking đưa vào biến $booking
if (empty($bookings) && !is_array($bookings)) return;
$option = get_option('rwmb_bookings');
foreach ($bookings as $key => $booking) { // Chạy vòng lặp biến $booking vì đây là 1 group field
$room = $booking['room']; //$room là biến chứa id của phòng
$begin = new DateTime( $booking['check_in'] ); // $begin là ngày bắt đầu cũng như ngày check in phòng
$end = new DateTime( $booking['check_out'] ); // $end là ngày kết thúc cũng như ngày check out
$end = $end->modify( '+1 day' ); // Cộng biến $end lên 1 ngày vì hàm DatePeriod không bao gồm ngày end
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end); // Hàm DatePeriod trả về mảng chứa tất cả ngày giữa 2 ngày đưa vào
$dates_booking = array();
foreach($daterange as $date){
array_push($dates_booking, $date->format("Y-m-d")); // Đưa giá trị vào biến $dates_booking
}
$option[$room][$post_id][$key] = $dates_booking; // Gán lại giá trị cho biến options
}
update_option('rwmb_bookings', $option); // Cập nhật option 'rwmb_bookings'
}
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