<?php
/**
 * Adds post meta fields in the post, page and custom post types
 * 
 * @package One Newspaper
 * @since 1.0.0
 * 
 */
if ( ! function_exists( 'one_newspaper_post_metaboxes' ) ) :
    /**
     * Add post metabox
     *
     * @since 1.0.0
     */
    function one_newspaper_post_metaboxes() {
        add_meta_box(
            'one_newspaper_post_meta',
            esc_html__( 'Meta Settings', 'one-newspaper' ),
            'one_newspaper_post_meta_callback',
            array( 'page', 'post' ),
            'normal',
            'default'
        );
    }
    add_action( 'add_meta_boxes', 'one_newspaper_post_metaboxes', 10, 2 );
endif;

function one_newspaper_post_meta_callback( $post ) {
    // sidebar option
    $meta_sidebar_options = [
        'customizer-setting'    => array(
            'label' => esc_html__( 'Customizer Setting', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/customizer-setting.jpg'
        ),
        'no-sidebar' => array(
            'label' => esc_html__( 'No Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/no_sidebar.jpg'
        ),
        'left-sidebar' => array(
            'label' => esc_html__( 'Left Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/left_sidebar.jpg'
        ),
        'right-sidebar' => array(
            'label' => esc_html__( 'Right Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/right_sidebar.jpg'
        ),
        'both-sidebar' => array(
            'label' => esc_html__( 'Both Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/both_sidebar.jpg'
        ),
        'left-both-sidebar' => array(
            'label' => esc_html__( 'Both Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/left_both_sidebar.jpg'
        ),
        'right-both-sidebar' => array(
            'label' => esc_html__( 'Both Sidebar', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/right_both_sidebar.jpg'
        )
    ];

    // width layouts option
    $meta_width_layouts_options = [
        'customizer-setting'    => array(
            'label' => esc_html__( 'Customizer Setting', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/customizer-setting.jpg'
        ),
        'boxed--layout' => array(
            'label' => esc_html__( 'Boxed', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/boxed_content.jpg'
        ),
        'full-width--layout' => array(
            'label' => esc_html__( 'Full Width', 'one-newspaper' ),
            'url'   => get_template_directory_uri() . '/assets/images/customizer/full_content.jpg'
        )
    ];

    // Single layouts option
    $single_layout_options = array(
        'customizer-layout' => array(
            'label' => esc_html__( 'customizer Layout', 'one-newspaper' ),
            'url'   => get_template_directory_uri() .  '/assets/images/customizer/customizer-setting.jpg'
        ),
        'three' => array(
            'label' => esc_html__( 'Layout One', 'one-newspaper' ),
            'url'   => get_template_directory_uri() .  '/assets/images/customizer/single_three.jpg'
        ),
        'five' => array(
            'label' => esc_html__( 'Layout Two', 'one-newspaper' ),
            'url'   => get_template_directory_uri() .  '/assets/images/customizer/single_five.jpg'
        )
    );

    // default value set for post sidebar layout.
    if( !metadata_exists( 'post', $post->ID, 'post_sidebar_layout' ) ) {
        // add post sidebar layout value "custmomizer-setting".
        update_post_meta( $post->ID, 'post_sidebar_layout', 'customizer-setting' );
    }
    $post_sidebar_layout = get_post_meta( $post->ID, 'post_sidebar_layout', true );
    $post_sidebar_layout = ( $post_sidebar_layout ) ? $post_sidebar_layout : 'customizer-setting';

    // default value set for post width layout.
    if( !metadata_exists( 'post', $post->ID, 'post_width_layout' ) ) {
        // add post width layout value "custmomizer-setting".
        update_post_meta( $post->ID, 'post_width_layout', 'customizer-setting' );
    }
    $post_width_layout = get_post_meta( $post->ID, 'post_width_layout', true );
    $post_width_layout = ( $post_width_layout ) ? $post_width_layout : 'customizer-setting';

    // default value set for post width layout.
    if( !metadata_exists( 'post', $post->ID, 'single_layout' ) ) {
        // add post width layout value "custmomizer-setting".
        update_post_meta( $post->ID, 'single_layout', 'customizer-layout' );
    }
    $single_layout = get_post_meta( $post->ID, 'single_layout', true );
    $single_layout = ( $single_layout ) ? $single_layout : 'customizer-layout';
    
    // Create our nonce field.
    wp_nonce_field( basename( __FILE__ ) , 'one_newspaper_post_meta_nonce' );
    ?>
        <div id="one-newspaper-post-metabox">
            <div class="single-meta-field radio-image-field">
                <label for="post_sidebar_layout"><?php esc_html_e( 'Sidebar Layout Settings', 'one-newspaper' ); ?></label>
                <p class="meta-description"><?php esc_html_e( 'Choose sidebar layout for this post', 'one-newspaper' ); ?></p>
                <div class="radio-image-fields-wrap">
                    <?php
                        foreach( $meta_sidebar_options as $key => $value ) :
                        ?>
                            <div class="radio-field <?php if( $post_sidebar_layout === $key ) echo 'selected'; ?>" data-value="<?php echo esc_attr( $key ); ?>"><img src="<?php echo esc_url( $value['url'] ); ?>" alt="<?php echo esc_attr( $value['label'] ); ?>"></div>
                    <?php
                        endforeach;
                    ?>
                </div>
                <input type="hidden" name="post_sidebar_layout" value="<?php echo esc_attr($post_sidebar_layout); ?>">
            </div><!-- .single-meta-field -->
            <div class="single-meta-field radio-image-field">
                <label for="post_width_layout"><?php esc_html_e( 'Width Layout Settings', 'one-newspaper' ); ?></label>
                <p class="meta-description"><?php esc_html_e( 'Choose width layout for this post', 'one-newspaper' ); ?></p>
                <div class="radio-image-fields-wrap">
                    <?php
                        foreach( $meta_width_layouts_options as $key => $value ) :
                    ?>
                            <div class="radio-field <?php if( $post_width_layout === $key ) echo 'selected'; ?>" data-value="<?php echo esc_attr( $key ); ?>"><img src="<?php echo esc_url( $value['url'] ); ?>" alt="<?php echo esc_attr( $value['label'] ); ?>"></div>
                    <?php
                        endforeach;
                    ?>
                </div>
                <input type="hidden" name="post_width_layout" value="<?php echo esc_attr($post_width_layout); ?>">
            </div><!-- .single-meta-field -->
            <?php
                if( is_single() ) :
                    ?>
                        <div class="single-meta-field radio-image-field">
                            <label for="single_layout"><?php esc_html_e( 'Single Layout Settings', 'one-newspaper' ); ?></label>
                            <p class="meta-description"><?php esc_html_e( 'Choose single layout for this post', 'one-newspaper' ); ?></p>
                            <div class="radio-image-fields-wrap">
                                <?php
                                    foreach( $single_layout_options as $key => $value ) :
                                ?>
                                        <div class="radio-field <?php if( $single_layout === $key ) echo 'selected'; ?>" data-value="<?php echo esc_attr( $key ); ?>"><img src="<?php echo esc_url( $value['url'] ); ?>" alt="<?php echo esc_attr( $value['label'] ); ?>"></div>
                                <?php
                                    endforeach;
                                ?>
                            </div>
                            <input type="hidden" name="single_layout" value="<?php echo esc_attr($single_layout); ?>">
                        </div><!-- .single-meta-field -->
                    <?php
                endif;
            ?>
        </div>
    <?php
}

function one_newspaper_save_post_meta( $post_id ) {
    // Verify the nonce before proceeding.
    $one_newspaper_post_meta_nonce   = isset( $_POST['one_newspaper_post_meta_nonce'] ) ? $_POST['one_newspaper_post_meta_nonce'] : '';
    $one_newspaper_post_meta_nonce_action = basename( __FILE__ );

    //* Check if nonce is set...
    if ( ! isset( $one_newspaper_post_meta_nonce ) ) {
        return;
    }

    //* Check if nonce is valid...
    if ( ! wp_verify_nonce( $one_newspaper_post_meta_nonce, $one_newspaper_post_meta_nonce_action ) ) {
        return;
    }

    //* Check if user has permissions to save data...
    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

    // Check auto save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    //* Check if not a revision...
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    if( isset( $_POST['post_sidebar_layout'] ) ) {
        update_post_meta( $post_id, 'post_sidebar_layout', sanitize_text_field( $_POST['post_sidebar_layout'] ) );
    }

    if( isset( $_POST['post_width_layout'] ) ) {
        update_post_meta( $post_id, 'post_width_layout', sanitize_text_field( $_POST['post_width_layout'] ) );
    }

    if( isset( $_POST['single_layout'] ) ) {
        update_post_meta( $post_id, 'single_layout', sanitize_text_field( $_POST['single_layout'] ) );
    }
}
add_action( 'save_post', 'one_newspaper_save_post_meta' );

function one_newspaper_save_attchment_meta( $post_id ) {
    // Verify the nonce before proceeding.
    $one_newspaper_post_meta_nonce   = isset( $_POST['one_newspaper_post_meta_nonce'] ) ? $_POST['one_newspaper_post_meta_nonce'] : '';
    $one_newspaper_post_meta_nonce_action = basename( __FILE__ );

    //* Check if nonce is set...
    if ( ! isset( $one_newspaper_post_meta_nonce ) ) {
        return;
    }

    //* Check if nonce is valid...
    if ( ! wp_verify_nonce( $one_newspaper_post_meta_nonce, $one_newspaper_post_meta_nonce_action ) ) {
        return;
    }

    //* Check if user has permissions to save data...
    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

    // Check auto save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    //* Check if not a revision...
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }
}
add_action( 'edit_attachment', 'one_newspaper_save_attchment_meta' );