<?php
/**
 * Matrimony Metabox
 *
 * @package Matrimony
 */
if ( ! function_exists( 'matrimony_add_metabox' ) ) :
    /**
     *
     *
     */
    function matrimony_add_metabox() {
        add_meta_box(
            'matrimony_theme_setting', // $id
            esc_html__( 'Theme Setting','matrimony' ),
            'matrimony_render_metabox', // $callback
            array( 'page', 'post' ), // $page
            'side', // $context
            'default' // $priority
        ); 
    }

endif;
add_action('add_meta_boxes', 'matrimony_add_metabox');

function matrimony_render_metabox( $post ) {
    $meta = get_post_meta( $post->ID );
    $matrimony_sidebar_layout = ( isset( $meta['matrimony_sidebar_layout'][0] ) && '' !== $meta['matrimony_sidebar_layout'][0] ) ? $meta['matrimony_sidebar_layout'][0] : 'customizer_setting';     
    $matrimony_enable_title = ( isset( $meta['matrimony_enable_title'][0] ) &&  'no' === $meta['matrimony_enable_title'][0] ) ? 'no' : 'yes';
    $matrimony_enable_comment = ( isset( $meta['matrimony_enable_comment'][0] ) &&  'no' === $meta['matrimony_enable_comment'][0] ) ? 'no' : 'yes';
    $matrimony_enable_feature_image = ( isset( $meta['matrimony_enable_feature_image'][0] ) &&  'no' === $meta['matrimony_enable_feature_image'][0] ) ? 'no' : 'yes';
    wp_nonce_field( 'matrimony_control_meta_box', 'matrimony_control_meta_box_nonce' ); // Always add nonce to your meta boxes!

    ?>

    <div class="post_meta_extras">
        <p><label><?php esc_html_e( 'Sidebar Option', 'matrimony' ); ?></label><br>
            <select id="matrimony_sidebar_layout" name="matrimony_sidebar_layout">
                <option value="<?php echo esc_attr( 'right-sidebar' ); ?>" <?php selected( $matrimony_sidebar_layout, 'right-sidebar', true ); ?>><?php esc_html_e( 'Right Sidebar', 'matrimony' ); ?></option>

                <option value="<?php echo esc_attr( 'left-sidebar' ); ?>" <?php selected( $matrimony_sidebar_layout, 'left-sidebar', true ); ?>><?php esc_html_e( 'Left Sidebar', 'matrimony' ); ?></option>
                <option value="<?php echo esc_attr( 'both-sidebar' ); ?>" <?php selected( $matrimony_sidebar_layout, 'both-sidebar', true ); ?>><?php esc_html_e( 'Both Sidebar', 'matrimony' ); ?></option>

                <option value="<?php echo esc_attr( 'no-sidebar' ); ?>" <?php selected( $matrimony_sidebar_layout, 'no-sidebar', true ); ?>><?php esc_html_e( 'No Sidebar', 'matrimony' ); ?></option>

            </select>
        </p>

        <p>
            <label><input type="checkbox" name="matrimony_enable_title" value="yes" <?php checked( $matrimony_enable_title, 'yes' ); ?> /><?php esc_html_e( 'Enable Title', 'matrimony' ); ?></label>
        </p>

        <p>
            <label><input type="checkbox" name="matrimony_enable_comment" value="yes" <?php checked( $matrimony_enable_comment, 'yes' ); ?> /><?php esc_html_e( 'Enable Comment', 'matrimony' ); ?></label>
        </p>

        <p>
            <label><input type="checkbox" name="matrimony_enable_feature_image" value="yes" <?php checked( $matrimony_enable_feature_image, 'yes' ); ?> /><?php esc_html_e( 'Enable Feature Image', 'matrimony' ); ?></label>
        </p>
    </div>                          

    <?php           

}
if ( ! function_exists( 'matrimony_save_metabox' ) ) :
    function matrimony_save_metabox( $post_id, $post ) {
        /*

         * We need to verify this came from the our screen and with proper authorization,

         * because save_post can be triggered at other times. Add as many nonces, as you

         * have metaboxes.

         */
        if ( ! isset( $_POST['matrimony_control_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['matrimony_control_meta_box_nonce'] ), 'matrimony_control_meta_box' ) ) { // Input var okay.
            return $post_id;
        }

        // Check the user's permissions.
        if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) { // Input var okay.
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        } 

        /*
         * If this is an autosave, our form has not been submitted,
         * so we don't want to do anything.
         */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        if ( isset( $_POST['matrimony_sidebar_layout'] ) ) { // Input var okay.
            update_post_meta( $post_id, 'matrimony_sidebar_layout', sanitize_key( wp_unslash( $_POST['matrimony_sidebar_layout'] ) ) ); // Input var okay.
        } 
        $matrimony_enable_title = ( isset( $_POST['matrimony_enable_title'] ) && 'yes' === $_POST['matrimony_enable_title'] ) ? 'yes' : 'no'; // Input var okay.
        update_post_meta( $post_id, 'matrimony_enable_title',  $matrimony_enable_title );
        $matrimony_enable_feature_image = ( isset( $_POST['matrimony_enable_feature_image'] ) && 'yes' === $_POST['matrimony_enable_feature_image'] ) ? 'yes' : 'no'; // Input var okay.
        update_post_meta( $post_id, 'matrimony_enable_feature_image',  $matrimony_enable_feature_image );
        $matrimony_enable_comment = ( isset( $_POST['matrimony_enable_comment'] ) && 'yes' === $_POST['matrimony_enable_comment'] ) ? 'yes' : 'no'; // Input var okay.
        update_post_meta( $post_id, 'matrimony_enable_comment', $matrimony_enable_comment  ); 
    }
endif;
add_action('save_post', 'matrimony_save_metabox', 10, 2 );