<?php 

//Register Meta box
function ultimate_hotel_booking_page_meta_boxes() {
	add_meta_box( 'ultimate-hotel-booking-page-meta', 'Page Settings', 'ultimate_hotel_booking_page_meta_boxes_fields', 'page', 'normal' );
}
add_action( 'add_meta_boxes', 'ultimate_hotel_booking_page_meta_boxes' );

//Meta callback function
function ultimate_hotel_booking_page_meta_boxes_fields( $post ) {
    ?>
        <div class="ultimate-hotel-booking-page-metabox-fields-wrapper">

            <div class="ultimate-hotel-booking-page-metabox-item">
                <?php
                    if( !empty( get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-disable-sidebar', true ) ) && get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-disable-sidebar', true ) == 1){
                        $ultimate_hotel_booking_pmb_sidebar = 'checked';
                    }else{
                        $ultimate_hotel_booking_pmb_sidebar = '';
                    }
                ?>
                <div class="ultimate-hotel-booking-page-metabox-title">
                    <label for="ultimate-hotel-booking-pmb-disable-sidebar"><?php echo esc_html__('Disable Sidebar', 'ultimate-hotel-booking')?></label>
                </div>
                <div class="ultimate-hotel-booking-page-metabox-field">
                    <input type="checkbox" name="ultimate-hotel-booking-pmb-disable-sidebar" id="ultimate-hotel-booking-pmb-disable-sidebar" <?php echo !empty( $ultimate_hotel_booking_pmb_sidebar ) ? esc_attr( $ultimate_hotel_booking_pmb_sidebar ) : ''; ?>>
                </div>
            </div>

            <div class="ultimate-hotel-booking-page-metabox-item">
                <?php
                    if( !empty( get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-banner', true ) ) && get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-banner', true ) == 1){
                        $ultimate_hotel_booking_pmb_banner = 'checked';
                    }else{
                        $ultimate_hotel_booking_pmb_banner = '';
                    }
                ?>
                <div class="ultimate-hotel-booking-page-metabox-title">
                    <label for="ultimate-hotel-booking-pmb-banner"><?php echo esc_html__('Disable Banner', 'ultimate-hotel-booking')?></label>
                </div>
                <div class="ultimate-hotel-booking-page-metabox-field">
                    <input type="checkbox" name="ultimate-hotel-booking-pmb-banner" id="ultimate-hotel-booking-pmb-banner" <?php echo !empty( $ultimate_hotel_booking_pmb_banner ) ? esc_attr( $ultimate_hotel_booking_pmb_banner ) : ''; ?>>
                </div>
            </div>
            
            <div class="ultimate-hotel-booking-page-metabox-item">
                <?php
                    if( !empty( get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-transfar-header', true ) ) && get_post_meta( $post->ID, 'ultimate-hotel-booking-pmb-transfar-header', true ) == 1){
                        $ultimate_hotel_booking_pmb_transfar_header = 'checked';
                    }else{
                        $ultimate_hotel_booking_pmb_transfar_header = '';
                    }
                ?>
                <div class="ultimate-hotel-booking-page-metabox-title">
                    <label for="ultimate-hotel-booking-pmb-transfar-header"><?php echo esc_html__('Disable Transparent Header', 'ultimate-hotel-booking')?></label>
                </div>
                <div class="ultimate-hotel-booking-page-metabox-field">
                    <input type="checkbox" name="ultimate-hotel-booking-pmb-transfar-header" id="ultimate-hotel-booking-pmb-transfar-header" <?php echo !empty( $ultimate_hotel_booking_pmb_transfar_header ) ? esc_attr( $ultimate_hotel_booking_pmb_transfar_header ) : ''; ?>>
                    <?php wp_nonce_field( 'ultimate_hotel_booking_metabox_nonce_action', 'ultimate_hotel_booking_metabox_nonce' ); ?>
                </div>
            </div>
        </div>
    <?php
}

//save meta value with save post hook


function ultimate_hotel_booking_page_meta_boxes_data_save( $post_id ) {
    // Check nonce security
    if ( ! empty( $_POST['ultimate_hotel_booking_metabox_nonce'] ) && ! wp_verify_nonce( $_POST['ultimate_hotel_booking_metabox_nonce'], 'ultimate_hotel_booking_metabox_nonce_action' ) ) {
        return;
    }

	if ( isset( $_POST['ultimate-hotel-booking-pmb-disable-sidebar'] ) ) {
		update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-disable-sidebar', 1 );
	}else{
		update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-disable-sidebar', 0 );
    }

	if ( isset( $_POST['ultimate-hotel-booking-pmb-banner'] ) ) {
		update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-banner', 1 );
	}else{
		update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-banner', 0 );
    }

	if ( isset( $_POST['ultimate-hotel-booking-pmb-transfar-header'] ) ) {
		update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-transfar-header', 1 );
	}else{
        update_post_meta( $post_id, 'ultimate-hotel-booking-pmb-transfar-header', 0 );
    }

}
add_action( 'save_post', 'ultimate_hotel_booking_page_meta_boxes_data_save' ); 