<?php
/**
 * Create a metabox to added some custom filed in posts.
 *
 * @package MatrixAddons
 * @subpackage trainifyframework
 * @since 1.0.0
 */
if(!class_exists('TrainifyFramework_Theme_Settings_Metabox')):

    class TrainifyFramework_Theme_Settings_Metabox{

        public function __construct(){
            add_action( 'add_meta_boxes', array($this, 'add_meta_boxes' ) );
            add_action( 'save_post', array($this, 'save_post') );
        }

        public function save_post($post_id){
            
            global $post;

            $_all_post_vals = wp_unslash( $_POST );

            // Verify the nonce before proceeding.
            $trainify_framework_post_nonce   = isset( $_all_post_vals['trainify_framework_post_meta_nonce'] ) ? esc_html($_all_post_vals['trainify_framework_post_meta_nonce']) : '';
            $trainify_framework_post_nonce_action = basename( __FILE__ );

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

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

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

            //* Check if not an autosave...
            if ( wp_is_post_autosave( $post_id ) ) {
                return;
            }

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

            /**
             * Post sidebar
             */
            
            $trainify_framework_single_sidebar_details = isset( $_all_post_vals['trainify_framework_single_post_sidebar'] ) ? $_all_post_vals['trainify_framework_single_post_sidebar'] : array();
            $trainify_framework_single_post_sidebar = array_map( 'esc_attr', $trainify_framework_single_sidebar_details );
            update_post_meta ( $post_id, 'trainify_framework_single_post_sidebar', $trainify_framework_single_post_sidebar );

            /**
             * post meta identity
             */
            $trainify_framework_post_meta_identity = get_post_meta( $post_id, 'trainify_framework_theme_settings_metabox_tab', true );
            $sanitize_post_identity = sanitize_text_field( $_all_post_vals[ 'trainify_framework_theme_settings_metabox_tab' ] );

            if ( $sanitize_post_identity && '' == $sanitize_post_identity ){
                add_post_meta( $post_id, 'trainify_framework_theme_settings_metabox_tab', $sanitize_post_identity );
            }elseif ( $sanitize_post_identity && $sanitize_post_identity != $trainify_framework_post_meta_identity ) {
                update_post_meta($post_id, 'trainify_framework_theme_settings_metabox_tab', $sanitize_post_identity );
            } elseif( '' == $sanitize_post_identity && $trainify_framework_post_meta_identity ) {
                delete_post_meta( $post_id, 'trainify_framework_theme_settings_metabox_tab', $trainify_framework_post_meta_identity );
            }
            
        }

        public function sidebar_layouts(){

            $sidebar_layouts = array(
                'default-sidebar' => array(
                    'id'        => 'post-default-sidebar',
                    'value'     => 'default_sidebar',
                    'label'     => esc_html__( 'Default Sidebar', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/default-sidebar.png'
                ),
                'left-sidebar' => array(
                    'id'        => 'post-right-sidebar',
                    'value'     => 'left_sidebar',
                    'label'     => esc_html__( 'Left sidebar', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/left-sidebar.png'
                ),
                'right-sidebar' => array(
                    'id'        => 'post-left-sidebar',
                    'value'     => 'right_sidebar',
                    'label'     => esc_html__( 'Right sidebar', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/right-sidebar.png'
                ),
                'no-sidebar' => array(
                    'id'        => 'post-no-sidebar',
                    'value'     => 'no_sidebar',
                    'label'     => esc_html__( 'No sidebar Full width', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/no-sidebar.png'
                ),
                'no-sidebar-center' => array(
                    'id'        => 'post-no-sidebar-center',
                    'value'     => 'no_sidebar_center',
                    'label'     => esc_html__( 'No sidebar Content Centered', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/no-sidebar-center.png'
                ),
                'both-sidebar' => array(
                    'id'        => 'post-both-sidebar',
                    'value'     => 'both_sidebar',
                    'label'     => esc_html__( 'Both Sidebar', 'trainify' ),
                    'thumbnail' => get_template_directory_uri() . '/inc/trainifyframework/assets/img/sidebars/both-sidebar.png'
                ),
            );

            return $sidebar_layouts;

        }

        public function add_meta_boxes(){

            add_meta_box(
                'trainify_framework_sidebar_layout_meta',
                esc_html__( 'Theme Options', 'trainify' ),
                array($this, 'metabox_callback'),
                array('post', 'page'),
                'normal',
                'high'
            );

        }

        public function metabox_fields(){

            $metabox_fields = array(
                'trainify_framework_single_post_sidebar'       => array(
                    'sidebar_layout' => array(
                        'tff_metabox_field_name'     => 'sidebar_layout',
                        'tff_metabox_field_title'    => esc_html__( 'Sidebar Layout', 'trainify' ),
                        'tff_metabox_field_default'  => 'general',
                        'tff_metabox_field_type'     => 'imageoptions',
                        'tff_metabox_field_options'  => array(
                            'default_sidebar' => array(
                                'label' => esc_html__( 'Default Sidebar', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/default-sidebar.png'
                            ),
                            'left_sidebar' => array(
                                'label' => esc_html__( 'Left Sidebar', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/left-sidebar.png'
                            ),
                            'right_sidebar' => array(
                                'label' => esc_html__( 'Right Sidebar', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/right-sidebar.png'
                            ),
                            'no_sidebar' => array(
                                'label' => esc_html__( 'No Sidebar', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/no-sidebar.png'
                            ),
                            'no_sidebar_center' => array(
                                'label' => esc_html__( 'No Sidebar Center', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/no-sidebar-center.png'
                            ),
                            'both_sidebar' => array(
                                'label' => esc_html__( 'Both Sidebar', 'trainify' ),
                                'url'   => '%s/inc/trainifyframework/assets/img/sidebars/both-sidebar.png'
                            )
                        )
                    )
                )
            );

            return $metabox_fields;

        }

        public function metabox_tabs(){

            $metabox_tabs = array(
                'trainify_framework_single_post_sidebar'       => array(
                    'tff_metabox_field_title'    => esc_html__( 'Sidebars', 'trainify' ),
                    'tff_metabox_field_dashicons'=>  'dashicons-exerpt-view',
                    'tff_metabox_field_heading'=>  esc_html__( 'Sidebar Settings', 'trainify' ),
                    'tff_metabox_field_description'    =>  esc_html__( 'If you want to override customizer settings please choose sidebar otherwise leave it default sidebar.', 'trainify' ),
                ),
            );
            return $metabox_tabs;
            
        }

        public function metabox_callback(){

            global $post;
            $sidebar_layouts = $this->sidebar_layouts();

            $trainify_framework_theme_settings_metabox_tab = get_post_meta( $post->ID, 'trainify_framework_theme_settings_metabox_tab', true );
            $trainify_framework_theme_settings_metabox_tab = isset( $trainify_framework_theme_settings_metabox_tab ) ? $trainify_framework_theme_settings_metabox_tab : 'sidebars';
            wp_nonce_field( basename( __FILE__ ), 'trainify_framework_post_meta_nonce' );

            $trainify_framework_metabox_tabs = $this->metabox_tabs();
            $trainify_framework_metabox_fields = $this->metabox_fields();

            ?> 
            <style type = "text/css">

            .trainify-framework-metabox-theme-settings{
                border:none;
                padding-bottom: 40px;
            }
            .trainify-framework-metabox-theme-settings:after{
                content:"";
                clear:both;
                display:block;
            }
            .tff-widget-tab-list{
                width: 25%;
                float: left;
                padding-top: 0px;
                box-sizing: border-box;
            }
            .tff-widget-tab-list .nav-tab{
                float: none;
                display: block;
            }
            .nav-tab:focus, .nav-tab:hover, .nav-tab-active{
                color: #fff;
                border-color: #0085ba;
                background-color: #0085ba;
            }
            .tff-tab-content-wraper{
                width: 75%;
                float: right;
                box-sizing: border-box;
            }
            .trainify-framework-imageoption{
                display:none;
            }
            .trainify-framework-fieldset-imageoption{
                margin:0 5px 5px 0;
                display: inline-block;
            }
            .trainify-framework-fieldset-imageoption  .trainify-framework-imageoption{
                display: none;
            }
            .trainify-framework-imageoption~label{
                display:block;
                line-height: 1em;
                border:2px solid #dedede;
            }
            .trainify-framework-imageoption:checked~label{
                border-color:#0085ba;
            }
            </style> 
            <div class="trainify-framework-metabox-theme-settings tff-widget-field-tab-wraper">
                <div class="nav-tab-wrapper tff-widget-tab-list">
                    <?php 
                    $trainify_framework_theme_settings_metabox_tab = ($trainify_framework_theme_settings_metabox_tab) ? $trainify_framework_theme_settings_metabox_tab : 'trainify_framework_single_post_sidebar';
                    foreach($trainify_framework_metabox_tabs as $trainify_framework_tab_slug => $tabs_details){ 
                        $tff_metabox_field_title = (isset($tabs_details['tff_metabox_field_title'])) ? esc_html($tabs_details['tff_metabox_field_title']) : '';
                        $tff_metabox_field_dashicons = (isset($tabs_details['tff_metabox_field_dashicons'])) ? esc_html($tabs_details['tff_metabox_field_dashicons']) : '';
                        ?>
                        <label class="trainify-framework-meta-tab nav-tab <?php echo ( $trainify_framework_theme_settings_metabox_tab==$trainify_framework_tab_slug ) ? 'nav-tab-active' : ''; ?>" for="trainify_framework_theme_settings_metabox_tab_<?php echo esc_attr($trainify_framework_tab_slug); ?>" data-id="#trainify_framework_theme_option_content_<?php echo esc_attr($trainify_framework_tab_slug); ?>"> <span class="dashicons <?php echo esc_attr($tff_metabox_field_dashicons); ?>"></span><?php echo esc_html($tff_metabox_field_title); ?><input id="trainify_framework_theme_settings_metabox_tab_<?php echo esc_attr($trainify_framework_tab_slug); ?>" type="radio" name="trainify_framework_theme_settings_metabox_tab" value="<?php echo esc_attr($trainify_framework_tab_slug); ?>" <?php checked( $trainify_framework_theme_settings_metabox_tab, $trainify_framework_tab_slug ); ?> class="tff-hidden"></label>
                    <?php } ?>
                </div><!-- .tff-widget-tab-list -->
                <div class="tff-tab-content-wraper">
                    <!-- Info tab content -->
                    <?php foreach($trainify_framework_metabox_tabs as $trainify_framework_tab_slug => $tabs_details){ 
                        $tff_metabox_field_heading = (isset($tabs_details['tff_metabox_field_heading'])) ? esc_html($tabs_details['tff_metabox_field_heading']) : '';
                        $tff_metabox_field_description = (isset($tabs_details['tff_metabox_field_description'])) ? esc_html($tabs_details['tff_metabox_field_description']) : '';
                        $trainify_framework_tab_fields = (isset($trainify_framework_metabox_fields[$trainify_framework_tab_slug] ) ) ? $trainify_framework_metabox_fields[$trainify_framework_tab_slug] : array();
                        ?>
                        <div data-value="<?php echo esc_attr($trainify_framework_theme_settings_metabox_tab.':'.$trainify_framework_tab_slug); ?>" class="trainify-framework-tab-contents <?php echo ( $trainify_framework_theme_settings_metabox_tab==$trainify_framework_tab_slug ) ? 'trainify-framework-tab-active' : ''; ?>" id="trainify_framework_theme_option_content_<?php echo esc_attr($trainify_framework_tab_slug); ?>">
                            <div class="trainify-framework-tab-content-header">
                                <h3><?php echo esc_html($tff_metabox_field_heading); ?></h3> 
                                <p><?php echo esc_html($tff_metabox_field_description); ?></p>
                            </div><!-- .content-header --> 
                            <div class="trainify-framework-tab-content-fields">
                                <?php if($trainify_framework_tab_fields){
                                    foreach($trainify_framework_tab_fields as $trainify_framework_field_slug=>$trainify_framework_field_details){
                                        $tff_metabox_field_name = (isset($trainify_framework_field_details['tff_metabox_field_name'])) ? esc_attr($trainify_framework_field_details['tff_metabox_field_name']) : '';
                                        $tff_metabox_field_title = (isset($trainify_framework_field_details['tff_metabox_field_title'])) ? esc_attr($trainify_framework_field_details['tff_metabox_field_title']) : '';
                                        $tff_metabox_field_default = (isset($trainify_framework_field_details['tff_metabox_field_default'])) ? esc_attr($trainify_framework_field_details['tff_metabox_field_default']) : '';
                                        $tff_metabox_field_type = (isset($trainify_framework_field_details['tff_metabox_field_type'])) ? esc_attr($trainify_framework_field_details['tff_metabox_field_type']) : '';
                                        $tff_metabox_field_options = (isset($trainify_framework_field_details['tff_metabox_field_options'])) ? $trainify_framework_field_details['tff_metabox_field_options'] : array();
                                        $trainify_framework_metabox_field_name = $trainify_framework_tab_slug.'['.$trainify_framework_field_slug.']';
                                        $trainify_framework_metabox_field_id = 'id_'.$trainify_framework_tab_slug.'_'.$trainify_framework_field_slug;
                                        ?><div class="trainify-framework-metabox-field-single"><?php
                                        switch ($tff_metabox_field_type){
                                            case 'imageoptions':
                                                ?>
                                                <div class="trainify-framework-imageoption-field-wrapper">
                                                    <?php
                                                    foreach($tff_metabox_field_options as $imageoption_key => $imageoption_details){ 
                                                        $trainify_framework_image_option_url = (isset($imageoption_details['url'])) ? $imageoption_details['url'] : '%s';
                                                        $trainify_framework_image_option_label = (isset($imageoption_details['label'])) ? esc_html($imageoption_details['label']) : '';
                                                        $trainify_framework_imageoption_id = $trainify_framework_metabox_field_id.'_'.$imageoption_key;
                                                        $trainify_framework_post_sidebar = 'default_sidebar';
                                                        ?>
                                                        <div class="trainify-framework-fieldset-imageoption">
                                                            <input class="trainify-framework-imageoption" type="radio" id="<?php echo esc_attr( $trainify_framework_imageoption_id ); ?>" value="<?php echo esc_attr( $imageoption_key ); ?>" name="<?php echo esc_attr($trainify_framework_metabox_field_name); ?>" <?php checked( $imageoption_key, $trainify_framework_post_sidebar ); ?> />
                                                            <label for="<?php echo esc_attr( $trainify_framework_imageoption_id ); ?>">
                                                                <span class="screen-reader-text"><?php echo esc_html( $trainify_framework_image_option_label ); ?> </span>
                                                                <img src="<?php echo esc_url(sprintf( $trainify_framework_image_option_url, get_template_directory_uri() ) ); ?>" title="<?php echo esc_attr( $trainify_framework_image_option_label ); ?>" alt="<?php echo esc_attr( $trainify_framework_image_option_label ); ?>" />
                                                            </label>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                                <?php
                                                break;
                                            default:
                                                ?><p><?php esc_html_e('Sorry metabox field not found.', 'trainify'); ?></p><?php
                                                break;
                                        }
                                        ?></div><?php
                                    }
                                }else{
                                    ?><p><?php esc_html_e('Please put some fields to show on metabox', 'trainify'); ?></p><?php
                                }
                                ?>
                            </div><!-- .meta-options-wrap  --> 
                        </div><!-- #trainify-framework-metabox-info -->
                    <?php } ?>
                </div><!-- .tff-tab-content-wraper -->
            </div><!-- .trainify-framework-meta-container --> 
            <?php
        }
    }
endif;
new TrainifyFramework_Theme_Settings_Metabox();