<?php
/**
* Sidebar Metabox.
*
* @package Vintuna
*/
 
add_action( 'add_meta_boxes', 'vintuna_metabox' );

if( ! function_exists( 'vintuna_metabox' ) ):


    function  vintuna_metabox() {
        
        add_meta_box(
            'theme-custom-metabox',
            esc_html__( 'Layout Settings', 'vintuna' ),
            'vintuna_post_metafield_callback',
            'post', 
            'normal', 
            'high'
        );
        add_meta_box(
            'theme-custom-metabox',
            esc_html__( 'Layout Settings', 'vintuna' ),
            'vintuna_post_metafield_callback',
            'page',
            'normal', 
            'high'
        ); 
    }

endif;

function vintuna_get_page_layout_options() {
    return array(
        'layout-1' => esc_html__( 'Simple Layout', 'vintuna' ),
        'layout-2' => esc_html__( 'Banner Layout', 'vintuna' ),
    );
}

function vintuna_get_sidebar_fields() {
    return array(
        'global-sidebar' => array(
            'id'    => 'post-global-sidebar',
            'value' => 'global-sidebar',
            'label' => esc_html__( 'Global sidebar', 'vintuna' ),
        ),
        'right-sidebar' => array(
            'id'    => 'post-left-sidebar',
            'value' => 'right-sidebar',
            'label' => esc_html__( 'Right sidebar', 'vintuna' ),
        ),
        'left-sidebar' => array(
            'id'    => 'post-right-sidebar',
            'value' => 'left-sidebar',
            'label' => esc_html__( 'Left sidebar', 'vintuna' ),
        ),
        'no-sidebar' => array(
            'id'    => 'post-no-sidebar',
            'value' => 'no-sidebar',
            'label' => esc_html__( 'No sidebar', 'vintuna' ),
        ),
    );
}

function vintuna_get_post_layout_options() {
    return array(
        'global-layout' => esc_html__( 'Global Layout', 'vintuna' ),
        'layout-1'      => esc_html__( 'Simple Layout', 'vintuna' ),
        'layout-2'      => esc_html__( 'Banner Layout', 'vintuna' ),
    );
}

function vintuna_get_header_overlay_options() {
    return array(
        'global-layout'   => esc_html__( 'Global Layout', 'vintuna' ),
        'enable-overlay'  => esc_html__( 'Enable Header Overlay', 'vintuna' ),
    );
}

/**
 * Callback function for post option.
*/
if( ! function_exists( 'vintuna_post_metafield_callback' ) ):
    
    function vintuna_post_metafield_callback() {
        global $post;
        $vintuna_post_sidebar_fields   = vintuna_get_sidebar_fields();
        $vintuna_post_layout_options   = vintuna_get_post_layout_options();
        $vintuna_page_layout_options   = vintuna_get_page_layout_options();
        $vintuna_header_overlay_options = vintuna_get_header_overlay_options();
        $post_type = get_post_type($post->ID);
        wp_nonce_field( basename( __FILE__ ), 'vintuna_post_meta_nonce' ); ?>
        
        <div class="metabox-main-block">

            <div class="metabox-navbar">
                <ul>

                    <li>
                        <a id="metabox-navbar-general" class="metabox-navbar-active" href="javascript:void(0)">

                            <?php esc_html_e('General Settings', 'vintuna'); ?>

                        </a>
                    </li>

                    <li>
                        <a id="metabox-navbar-appearance" href="javascript:void(0)">

                            <?php esc_html_e('Appearance Settings', 'vintuna'); ?>

                        </a>
                    </li>

                </ul>
            </div>

            <div class="wpi-tab-content">

                <div id="metabox-navbar-general-content" class="metabox-content-wrap metabox-content-wrap-active">

                    <div class="metabox-opt-panel">

                        <h3 class="meta-opt-title"><?php esc_html_e('Sidebar Layout','vintuna'); ?></h3>

                        <div class="metabox-opt-wrap metabox-opt-wrap-alt">

                            <?php
                            $vintuna_post_sidebar = esc_html( get_post_meta( $post->ID, 'vintuna_post_sidebar_option', true ) ); 
                            if( $vintuna_post_sidebar == '' ){ $vintuna_post_sidebar = 'global-sidebar'; }

                            foreach ( $vintuna_post_sidebar_fields as $vintuna_post_sidebar_field) { ?>

                                <label class="description">

                                    <input type="radio" name="vintuna_post_sidebar_option" value="<?php echo esc_attr( $vintuna_post_sidebar_field['value'] ); ?>" <?php if( $vintuna_post_sidebar_field['value'] == $vintuna_post_sidebar ){ echo "checked='checked'";} if( empty( $vintuna_post_sidebar ) && $vintuna_post_sidebar_field['value']=='right-sidebar' ){ echo "checked='checked'"; } ?>/>&nbsp;<?php echo esc_html( $vintuna_post_sidebar_field['label'] ); ?>

                                </label>

                            <?php } ?>

                        </div>

                    </div>

                </div>


                <div id="metabox-navbar-appearance-content" class="metabox-content-wrap">

                    <?php if( $post_type == 'page' ): ?>

                        <div class="metabox-opt-panel">

                            <h3 class="meta-opt-title"><?php esc_html_e('Appearance Layout','vintuna'); ?></h3>

                            <div class="metabox-opt-wrap metabox-opt-wrap-alt">

                                <?php
                                $vintuna_page_layout = esc_html( get_post_meta( $post->ID, 'vintuna_page_layout', true ) ); 
                                if( $vintuna_page_layout == '' ){ $vintuna_page_layout = 'layout-1'; }

                                foreach ( $vintuna_page_layout_options as $key => $vintuna_page_layout_option) { ?>

                                    <label class="description">
                                        <input type="radio" name="vintuna_page_layout" value="<?php echo esc_attr( $key ); ?>" <?php if( $key == $vintuna_page_layout ){ echo "checked='checked'";} ?>/>&nbsp;<?php echo esc_html( $vintuna_page_layout_option ); ?>
                                    </label>

                                <?php } ?>

                            </div>

                        </div>

                        <div class="metabox-opt-panel">

                            <h3 class="meta-opt-title"><?php esc_html_e('Header Overlay','vintuna'); ?></h3>

                            <div class="metabox-opt-wrap theme-checkbox-wrap">

                                <?php
                                $vintuna_ed_header_overlay = esc_attr( get_post_meta( $post->ID, 'vintuna_ed_header_overlay', true ) ); ?>

                                <input type="checkbox" id="vintuna-header-overlay" name="vintuna_ed_header_overlay" value="1" <?php if( $vintuna_ed_header_overlay ){ echo "checked='checked'";} ?>/>

                                <label for="vintuna-header-overlay"><?php esc_html_e( 'Enable Header Overlay','vintuna' ); ?></label>

                            </div>

                        </div>

                    <?php endif; ?>

                    <?php if( $post_type == 'post' ): ?>

                        <div class="metabox-opt-panel">

                            <h3 class="meta-opt-title"><?php esc_html_e('Appearance Layout','vintuna'); ?></h3>

                            <div class="metabox-opt-wrap metabox-opt-wrap-alt">

                                <?php
                                $vintuna_post_layout = esc_html( get_post_meta( $post->ID, 'vintuna_post_layout', true ) ); 
                                if( $vintuna_post_layout == '' ){ $vintuna_post_layout = 'global-layout'; }

                                foreach ( $vintuna_post_layout_options as $key => $vintuna_post_layout_option) { ?>

                                    <label class="description">
                                        <input type="radio" name="vintuna_post_layout" value="<?php echo esc_attr( $key ); ?>" <?php if( $key == $vintuna_post_layout ){ echo "checked='checked'";} ?>/>&nbsp;<?php echo esc_html( $vintuna_post_layout_option ); ?>
                                    </label>

                                <?php } ?>

                            </div>

                        </div>

                        <div class="metabox-opt-panel">

                            <h3 class="meta-opt-title"><?php esc_html_e('Header Overlay','vintuna'); ?></h3>

                            <div class="metabox-opt-wrap metabox-opt-wrap-alt">

                                <?php
                                $vintuna_header_overlay = esc_html( get_post_meta( $post->ID, 'vintuna_header_overlay', true ) ); 
                                if( $vintuna_header_overlay == '' ){ $vintuna_header_overlay = 'global-layout'; }

                                foreach ( $vintuna_header_overlay_options as $key => $vintuna_header_overlay_option) { ?>

                                    <label class="description">
                                        <input type="radio" name="vintuna_header_overlay" value="<?php echo esc_attr( $key ); ?>" <?php if( $key == $vintuna_header_overlay ){ echo "checked='checked'";} ?>/>&nbsp;<?php echo esc_html( $vintuna_header_overlay_option ); ?>
                                    </label>

                                <?php } ?>

                            </div>

                        </div>

                    <?php endif; ?>

                    <div class="metabox-opt-panel">

                        <h3 class="meta-opt-title"><?php esc_html_e('Feature Image Setting','vintuna'); ?></h3>

                        <div class="metabox-opt-wrap theme-checkbox-wrap">

                            <?php
                            $vintuna_ed_feature_image = esc_html( get_post_meta( $post->ID, 'vintuna_ed_feature_image', true ) ); 
                            if (!isset( $_POST['vintuna_ed_feature_image'] )) {
                                $vintuna_ed_feature_image = get_theme_mod('ed_post_thumbnail');
                            }
                            ?>

                            <input type="checkbox" id="vintuna-ed-feature-image" name="vintuna_ed_feature_image" value="<?php echo $vintuna_default_feature_image; ?>" <?php if( $vintuna_ed_feature_image ){ echo "checked='checked'";} ?>/>

                            <label for="vintuna-ed-feature-image"><?php esc_html_e( 'Disable Feature Image','vintuna' ); ?></label>


                        </div>

                    </div>

                     <div class="metabox-opt-panel">

                        <h3 class="meta-opt-title"><?php esc_html_e('Navigation Setting','vintuna'); ?></h3>

                        <?php $wpi_disable_ajax_load_next_post = esc_attr( get_post_meta($post->ID, 'wpi_disable_ajax_load_next_post', true) ); ?>
                        <div class="metabox-opt-wrap metabox-opt-wrap-alt">

                            <label><b><?php esc_html_e( 'Navigation Type','vintuna' ); ?></b></label>

                            <select name="wpi_disable_ajax_load_next_post">

                                <option <?php if( $wpi_disable_ajax_load_next_post == '' || $wpi_disable_ajax_load_next_post == 'global-layout' ){ echo 'selected'; } ?> value="global-layout"><?php esc_html_e('Global Layout','vintuna'); ?></option>
                                <option <?php if( $wpi_disable_ajax_load_next_post == 'no-navigation' ){ echo 'selected'; } ?> value="no-navigation"><?php esc_html_e('Disable Navigation','vintuna'); ?></option>
                                <option <?php if( $wpi_disable_ajax_load_next_post == 'norma-navigation' ){ echo 'selected'; } ?> value="norma-navigation"><?php esc_html_e('Next Previous Navigation','vintuna'); ?></option>
                                <option <?php if( $wpi_disable_ajax_load_next_post == 'ajax-next-post-load' ){ echo 'selected'; } ?> value="ajax-next-post-load"><?php esc_html_e('Ajax Load Next 3 Posts Contents','vintuna'); ?></option>

                            </select>

                        </div>
                    </div>

                </div>

                
            </div>

        </div>  
            
    <?php }
endif;

// Save metabox value.
add_action( 'save_post', 'vintuna_save_post_meta' );

if( ! function_exists( 'vintuna_save_post_meta' ) ):

    function vintuna_save_post_meta( $post_id ) {

        global $post;
        $vintuna_post_sidebar_fields   = vintuna_get_sidebar_fields();
        $vintuna_post_layout_options   = vintuna_get_post_layout_options();
        $vintuna_page_layout_options   = vintuna_get_page_layout_options();
        $vintuna_header_overlay_options = vintuna_get_header_overlay_options();

        if ( !isset( $_POST[ 'vintuna_post_meta_nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['vintuna_post_meta_nonce'] ) ), basename( __FILE__ ) ) ){

            return;

        }

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){

            return;

        }
            
        if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {  

            if ( !current_user_can( 'edit_page', $post_id ) ){  

                return $post_id;

            }

        }elseif( !current_user_can( 'edit_post', $post_id ) ) {

            return $post_id;

        }


        foreach ( $vintuna_post_sidebar_fields as $vintuna_post_sidebar_field ) {  
            

                $old = esc_html( get_post_meta( $post_id, 'vintuna_post_sidebar_option', true ) ); 
                $new = isset( $_POST['vintuna_post_sidebar_option'] ) ? vintuna_sanitize_sidebar_option_meta( wp_unslash( $_POST['vintuna_post_sidebar_option'] ) ) : '';

                if ( $new && $new != $old ){

                    update_post_meta ( $post_id, 'vintuna_post_sidebar_option', $new );

                }elseif( '' == $new && $old ) {

                    delete_post_meta( $post_id,'vintuna_post_sidebar_option', $old );

                }

            
        }

        $wpi_disable_ajax_load_next_post_old = esc_html( get_post_meta( $post_id, 'wpi_disable_ajax_load_next_post', true ) ); 
        $wpi_disable_ajax_load_next_post_new = isset( $_POST['wpi_disable_ajax_load_next_post'] ) ? vintuna_sanitize_meta_pagination( wp_unslash( $_POST['wpi_disable_ajax_load_next_post'] ) ) : '';

        if( $wpi_disable_ajax_load_next_post_new && $wpi_disable_ajax_load_next_post_new != $wpi_disable_ajax_load_next_post_old ){

            update_post_meta ( $post_id, 'wpi_disable_ajax_load_next_post', $wpi_disable_ajax_load_next_post_new );

        }elseif( '' == $wpi_disable_ajax_load_next_post_new && $wpi_disable_ajax_load_next_post_old ) {

            delete_post_meta( $post_id,'wpi_disable_ajax_load_next_post', $wpi_disable_ajax_load_next_post_old );

        }


        foreach ( $vintuna_post_layout_options as $vintuna_post_layout_option ) {  
            
            $vintuna_post_layout_old = esc_html( get_post_meta( $post_id, 'vintuna_post_layout', true ) ); 
            $vintuna_post_layout_new = isset( $_POST['vintuna_post_layout'] ) ? vintuna_sanitize_post_layout_option_meta( wp_unslash( $_POST['vintuna_post_layout'] ) ) : '';

            if ( $vintuna_post_layout_new && $vintuna_post_layout_new != $vintuna_post_layout_old ){

                update_post_meta ( $post_id, 'vintuna_post_layout', $vintuna_post_layout_new );

            }elseif( '' == $vintuna_post_layout_new && $vintuna_post_layout_old ) {

                delete_post_meta( $post_id,'vintuna_post_layout', $vintuna_post_layout_old );

            }
            
        }



        foreach ( $vintuna_header_overlay_options as $vintuna_header_overlay_option ) {  
            
            $vintuna_header_overlay_old = esc_html( get_post_meta( $post_id, 'vintuna_header_overlay', true ) ); 
            $vintuna_header_overlay_new = isset( $_POST['vintuna_header_overlay'] ) ? vintuna_sanitize_header_overlay_option_meta( wp_unslash( $_POST['vintuna_header_overlay'] ) ) : '';

            if ( $vintuna_header_overlay_new && $vintuna_header_overlay_new != $vintuna_header_overlay_old ){

                update_post_meta ( $post_id, 'vintuna_header_overlay', $vintuna_header_overlay_new );

            }elseif( '' == $vintuna_header_overlay_new && $vintuna_header_overlay_old ) {

                delete_post_meta( $post_id,'vintuna_header_overlay', $vintuna_header_overlay_old );

            }
            
        }



        $vintuna_ed_feature_image_old = esc_html( get_post_meta( $post_id, 'vintuna_ed_feature_image', true ) ); 
        $vintuna_ed_feature_image_new = isset( $_POST['vintuna_ed_feature_image'] ) ? absint( wp_unslash( $_POST['vintuna_ed_feature_image'] ) ) : '';

        if ( $vintuna_ed_feature_image_new && $vintuna_ed_feature_image_new != $vintuna_ed_feature_image_old ){

            update_post_meta ( $post_id, 'vintuna_ed_feature_image', $vintuna_ed_feature_image_new );

        }elseif( '' == $vintuna_ed_feature_image_new && $vintuna_ed_feature_image_old ) {

            delete_post_meta( $post_id,'vintuna_ed_feature_image', $vintuna_ed_feature_image_old );

        }


        foreach ( $vintuna_page_layout_options as $vintuna_post_layout_option ) {  
        
            $vintuna_page_layout_old = sanitize_text_field( get_post_meta( $post_id, 'vintuna_page_layout', true ) ); 
            $vintuna_page_layout_new = isset( $_POST['vintuna_page_layout'] ) ? vintuna_sanitize_post_layout_option_meta( wp_unslash( $_POST['vintuna_page_layout'] ) ) : '';

            if ( $vintuna_page_layout_new && $vintuna_page_layout_new != $vintuna_page_layout_old ){

                update_post_meta ( $post_id, 'vintuna_page_layout', $vintuna_page_layout_new );

            }elseif( '' == $vintuna_page_layout_new && $vintuna_page_layout_old ) {

                delete_post_meta( $post_id,'vintuna_page_layout', $vintuna_page_layout_old );

            }
            
        }

        $vintuna_ed_header_overlay_old = absint( get_post_meta( $post_id, 'vintuna_ed_header_overlay', true ) ); 
        $vintuna_ed_header_overlay_new = isset( $_POST['vintuna_ed_header_overlay'] ) ? absint( wp_unslash( $_POST['vintuna_ed_header_overlay'] ) ) : '';

        if ( $vintuna_ed_header_overlay_new && $vintuna_ed_header_overlay_new != $vintuna_ed_header_overlay_old ){

            update_post_meta ( $post_id, 'vintuna_ed_header_overlay', $vintuna_ed_header_overlay_new );

        }elseif( '' == $vintuna_ed_header_overlay_new && $vintuna_ed_header_overlay_old ) {

            delete_post_meta( $post_id,'vintuna_ed_header_overlay', $vintuna_ed_header_overlay_old );

        }

    }

endif;   