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

if( ! function_exists( 'newsnation_metabox' ) ):


    function  newsnation_metabox() {
        
        add_meta_box(
            'theme-custom-metabox',
            esc_html__( 'Layout Settings', 'newsnation' ),
            'newsnation_post_metafield_callback',
            'post', 
            'normal', 
            'high'
        );
        add_meta_box(
            'theme-custom-metabox',
            esc_html__( 'Layout Settings', 'newsnation' ),
            'newsnation_post_metafield_callback',
            'page',
            'normal', 
            'high'
        ); 
    }

endif;

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

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

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

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

/**
 * Callback function for post option.
*/
if( ! function_exists( 'newsnation_post_metafield_callback' ) ):
    
    function newsnation_post_metafield_callback() {
        global $post;
        $newsnation_post_sidebar_fields   = newsnation_get_sidebar_fields();
        $newsnation_post_layout_options   = newsnation_get_post_layout_options();
        $newsnation_page_layout_options   = newsnation_get_page_layout_options();
        $newsnation_header_overlay_options = newsnation_get_header_overlay_options();
        $post_type = get_post_type($post->ID);
        wp_nonce_field( basename( __FILE__ ), 'newsnation_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', 'newsnation'); ?>

                        </a>
                    </li>

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

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

                        </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','newsnation'); ?></h3>

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

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

                            foreach ( $newsnation_post_sidebar_fields as $newsnation_post_sidebar_field) { ?>

                                <label class="description">

                                    <input type="radio" name="newsnation_post_sidebar_option" value="<?php echo esc_attr( $newsnation_post_sidebar_field['value'] ); ?>" <?php if( $newsnation_post_sidebar_field['value'] == $newsnation_post_sidebar ){ echo "checked='checked'";} if( empty( $newsnation_post_sidebar ) && $newsnation_post_sidebar_field['value']=='right-sidebar' ){ echo "checked='checked'"; } ?>/>&nbsp;<?php echo esc_html( $newsnation_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','newsnation'); ?></h3>

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

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

                                foreach ( $newsnation_page_layout_options as $key => $newsnation_page_layout_option) { ?>

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

                                <?php } ?>

                            </div>

                        </div>

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

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

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

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

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

                                <label for="newsnation-header-overlay"><?php esc_html_e( 'Enable Header Overlay','newsnation' ); ?></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','newsnation'); ?></h3>

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

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

                                foreach ( $newsnation_post_layout_options as $key => $newsnation_post_layout_option) { ?>

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

                                <?php } ?>

                            </div>

                        </div>

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

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

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

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

                                foreach ( $newsnation_header_overlay_options as $key => $newsnation_header_overlay_option) { ?>

                                    <label class="description">
                                        <input type="radio" name="newsnation_header_overlay" value="<?php echo esc_attr( $key ); ?>" <?php if( $key == $newsnation_header_overlay ){ echo "checked='checked'";} ?>/>&nbsp;<?php echo esc_html( $newsnation_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','newsnation'); ?></h3>

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

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

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

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


                        </div>

                    </div>

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

                        <h3 class="meta-opt-title"><?php esc_html_e('Navigation Setting','newsnation'); ?></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','newsnation' ); ?></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','newsnation'); ?></option>
                                <option <?php if( $wpi_disable_ajax_load_next_post == 'no-navigation' ){ echo 'selected'; } ?> value="no-navigation"><?php esc_html_e('Disable Navigation','newsnation'); ?></option>
                                <option <?php if( $wpi_disable_ajax_load_next_post == 'norma-navigation' ){ echo 'selected'; } ?> value="norma-navigation"><?php esc_html_e('Next Previous Navigation','newsnation'); ?></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','newsnation'); ?></option>

                            </select>

                        </div>
                    </div>

                </div>

                
            </div>

        </div>  
            
    <?php }
endif;

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

if( ! function_exists( 'newsnation_save_post_meta' ) ):

    function newsnation_save_post_meta( $post_id ) {

        global $post;
        $newsnation_post_sidebar_fields   = newsnation_get_sidebar_fields();
        $newsnation_post_layout_options   = newsnation_get_post_layout_options();
        $newsnation_page_layout_options   = newsnation_get_page_layout_options();
        $newsnation_header_overlay_options = newsnation_get_header_overlay_options();

        if ( !isset( $_POST[ 'newsnation_post_meta_nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['newsnation_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 ( $newsnation_post_sidebar_fields as $newsnation_post_sidebar_field ) {  
            

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

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

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

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

                    delete_post_meta( $post_id,'newsnation_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'] ) ? newsnation_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 ( $newsnation_post_layout_options as $newsnation_post_layout_option ) {  
            
            $newsnation_post_layout_old = esc_html( get_post_meta( $post_id, 'newsnation_post_layout', true ) ); 
            $newsnation_post_layout_new = isset( $_POST['newsnation_post_layout'] ) ? newsnation_sanitize_post_layout_option_meta( wp_unslash( $_POST['newsnation_post_layout'] ) ) : '';

            if ( $newsnation_post_layout_new && $newsnation_post_layout_new != $newsnation_post_layout_old ){

                update_post_meta ( $post_id, 'newsnation_post_layout', $newsnation_post_layout_new );

            }elseif( '' == $newsnation_post_layout_new && $newsnation_post_layout_old ) {

                delete_post_meta( $post_id,'newsnation_post_layout', $newsnation_post_layout_old );

            }
            
        }



        foreach ( $newsnation_header_overlay_options as $newsnation_header_overlay_option ) {  
            
            $newsnation_header_overlay_old = esc_html( get_post_meta( $post_id, 'newsnation_header_overlay', true ) ); 
            $newsnation_header_overlay_new = isset( $_POST['newsnation_header_overlay'] ) ? newsnation_sanitize_header_overlay_option_meta( wp_unslash( $_POST['newsnation_header_overlay'] ) ) : '';

            if ( $newsnation_header_overlay_new && $newsnation_header_overlay_new != $newsnation_header_overlay_old ){

                update_post_meta ( $post_id, 'newsnation_header_overlay', $newsnation_header_overlay_new );

            }elseif( '' == $newsnation_header_overlay_new && $newsnation_header_overlay_old ) {

                delete_post_meta( $post_id,'newsnation_header_overlay', $newsnation_header_overlay_old );

            }
            
        }



        $newsnation_ed_feature_image_old = esc_html( get_post_meta( $post_id, 'newsnation_ed_feature_image', true ) ); 
        $newsnation_ed_feature_image_new = isset( $_POST['newsnation_ed_feature_image'] ) ? absint( wp_unslash( $_POST['newsnation_ed_feature_image'] ) ) : '';

        if ( $newsnation_ed_feature_image_new && $newsnation_ed_feature_image_new != $newsnation_ed_feature_image_old ){

            update_post_meta ( $post_id, 'newsnation_ed_feature_image', $newsnation_ed_feature_image_new );

        }elseif( '' == $newsnation_ed_feature_image_new && $newsnation_ed_feature_image_old ) {

            delete_post_meta( $post_id,'newsnation_ed_feature_image', $newsnation_ed_feature_image_old );

        }


        foreach ( $newsnation_page_layout_options as $newsnation_post_layout_option ) {  
        
            $newsnation_page_layout_old = sanitize_text_field( get_post_meta( $post_id, 'newsnation_page_layout', true ) ); 
            $newsnation_page_layout_new = isset( $_POST['newsnation_page_layout'] ) ? newsnation_sanitize_post_layout_option_meta( wp_unslash( $_POST['newsnation_page_layout'] ) ) : '';

            if ( $newsnation_page_layout_new && $newsnation_page_layout_new != $newsnation_page_layout_old ){

                update_post_meta ( $post_id, 'newsnation_page_layout', $newsnation_page_layout_new );

            }elseif( '' == $newsnation_page_layout_new && $newsnation_page_layout_old ) {

                delete_post_meta( $post_id,'newsnation_page_layout', $newsnation_page_layout_old );

            }
            
        }

        $newsnation_ed_header_overlay_old = absint( get_post_meta( $post_id, 'newsnation_ed_header_overlay', true ) ); 
        $newsnation_ed_header_overlay_new = isset( $_POST['newsnation_ed_header_overlay'] ) ? absint( wp_unslash( $_POST['newsnation_ed_header_overlay'] ) ) : '';

        if ( $newsnation_ed_header_overlay_new && $newsnation_ed_header_overlay_new != $newsnation_ed_header_overlay_old ){

            update_post_meta ( $post_id, 'newsnation_ed_header_overlay', $newsnation_ed_header_overlay_new );

        }elseif( '' == $newsnation_ed_header_overlay_new && $newsnation_ed_header_overlay_old ) {

            delete_post_meta( $post_id,'newsnation_ed_header_overlay', $newsnation_ed_header_overlay_old );

        }

    }

endif;   