<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if (! class_exists('Gridpress_Post_types')) {
    class Gridpress_Post_types {

        public function __construct() {
            add_action('init', array($this, 'gridpress_register_post_types'), 5);
            add_filter('manage_gridpress_grid_posts_columns', array($this, 'gridpress_extra_columns_add'));
            add_action('manage_gridpress_grid_posts_custom_column', array($this, 'gridpress_item_custom_columns'), 10, 2);
            add_action('add_meta_boxes', array($this, 'gridpress_item_meta_box'), 5);
            //
            add_action('gridpress_psettins_tab_general_content', array($this, 'gridpress_tab_general_content'));
            add_action('gridpress_psettins_tab_data_source_content', array($this, 'gridpress_tab_data_source_content'));
            add_action('gridpress_psettins_tab_layout_content', array($this, 'gridpress_tab_layout_content'));
            add_action('gridpress_psettins_tab_display_content', array($this, 'gridpress_tab_display_content'));
            add_action('gridpress_psettins_tab_typography_content', array($this, 'gridpress_tab_typography_content'));
            add_action('gridpress_psettins_tab_import_export_content', array($this, 'gridpress_import_export_tab_ui'));
            add_action('admin_init', array($this, 'gridpress_export_settings_handler'), 5);
            //
            add_action('save_post', array($this, 'gridpress_save_meta_fields'), 15);
        }

        public function gridpress_register_post_types() {
            if ( post_type_exists('gridpress_grid') ) {
                return;
            }
            $gridpress_labels = array(
                'name' => esc_html_x('Grids', 'Post type name', 'gridpress-grid-layout-design'),
                'singular_name' => esc_html_x('Grid', 'Post type singular name', 'gridpress-grid-layout-design'),
                'all_items' => esc_html__('All Grids', 'gridpress-grid-layout-design'),
                'menu_name' => esc_html_x('Grids', 'Admin menu name', 'gridpress-grid-layout-design'),
                'add_new' => esc_html__('Add New', 'gridpress-grid-layout-design'),
                'add_new_item' => esc_html__('Add new grid', 'gridpress-grid-layout-design'),
                'edit' => esc_html_x('Edit', 'Edit grid', 'gridpress-grid-layout-design'),
                'edit_item' => esc_html__('Edit grid', 'gridpress-grid-layout-design'),
                'new_item' => esc_html__('New grid', 'gridpress-grid-layout-design'),
                'view_item' => esc_html__('View grid', 'gridpress-grid-layout-design'),
                'view_items' => esc_html__('View grids', 'gridpress-grid-layout-design'),
                'search_items' => esc_html__('Search grids', 'gridpress-grid-layout-design'),
                'not_found' => esc_html__('No grids found', 'gridpress-grid-layout-design'),
                'not_found_in_trash' => esc_html__('No grids found in trash', 'gridpress-grid-layout-design'),
                'insert_into_item' => esc_html__('Insert into grid', 'gridpress-grid-layout-design'),
                'uploaded_to_this_item' => esc_html__('Uploaded to this grid', 'gridpress-grid-layout-design'),
                'filter_items_list' => esc_html__('Filter grids', 'gridpress-grid-layout-design'),
                'items_list_navigation' => esc_html__('Grids navigation', 'gridpress-grid-layout-design'),
                'items_list' => esc_html__('Grids list', 'gridpress-grid-layout-design'),
            );
            $gridpress_post_args = array(
                'labels' => $gridpress_labels,
                'description' => esc_html__('This is where you can see all grids.', 'gridpress-grid-layout-design'),
                'public' => false,
                'show_ui' => true,
                'capability_type' => 'post',
                'show_in_rest' => false,
                'publicly_queryable' => false,
                'exclude_from_search' => true,
                'hierarchical' => false,
                'query_var' => false,
                'show_in_menu' => true,
                'supports' => array('title'),
            );
            register_post_type('gridpress_grid', $gridpress_post_args);
        }

        public function gridpress_extra_columns_add($gridpress_columns) {
            unset($gridpress_columns['date']);
            $gridpress_columns['shortcode'] = esc_html__('Shortcode', 'gridpress-grid-layout-design');
            $gridpress_columns['date'] = esc_html__('Date', 'gridpress-grid-layout-design');
            return $gridpress_columns;
        }

        public function gridpress_item_custom_columns($gridpress_column, $gridpress_item_id) {
            switch ($gridpress_column) {
                case 'shortcode':
                    echo esc_html(
                        sprintf(
                            /* translators: %d: Grid ID. */
                            '[gridpress_grid id="%d"]',
                            $gridpress_item_id
                        )
                    );
                    break;
            }
        }

        public function gridpress_item_meta_box() {
            add_meta_box('gridpress_grid_meta', esc_html__('Settings', 'gridpress-grid-layout-design'), array($this, 'gridpress_item_post_meta_data'), 'gridpress_grid', 'normal');
        }

        public function gridpress_item_post_meta_data() {
            global $post, $gridpress_grid_all_settings;
            if (!isset($post) || empty($post->ID)) {
                return;
            }
            $gridpress_item_id = absint($post->ID);
            $gridpress_grid_all_settings = get_post_meta($gridpress_item_id, 'gridpress_grid_settings', true);
            $gridpress_settings_tabs = array(
                'general' => esc_html__('General Settings', 'gridpress-grid-layout-design'),
                'data_source' => esc_html__('Data Source', 'gridpress-grid-layout-design'),
                'layout' => esc_html__('Layout', 'gridpress-grid-layout-design'),
                'display' => esc_html__('Display Settings', 'gridpress-grid-layout-design'),
                'typography' => esc_html__('Typography', 'gridpress-grid-layout-design'),
                'import_export' => esc_html__('Import/Export', 'gridpress-grid-layout-design'),
            );
            ?>
            <div class="gridpress-post-metabox">
                <div class="gridpress-gridpsettins-main">
                    <div class="gridpsettins-tabs-cont">
                        <?php
                        $gridpress_tabs_counter = 1;
                        foreach ($gridpress_settings_tabs as $gridpress_tab_slug => $gridpress_settings_tab_label) {
                            $gridpress_active_class = ( $gridpress_tabs_counter === 1 ) ? ' active-gridpsettin-tab' : '';

                            $gridpress_tab_classes = 'gridpsettins-tab-lbl' . $gridpress_active_class;
                            ?>
                            <div
                                class="<?php echo esc_attr( $gridpress_tab_classes ); ?>"
                                data-slug="<?php echo esc_attr( $gridpress_tab_slug ); ?>"
                            >
                                <?php echo esc_html( $gridpress_settings_tab_label ); ?>
                            </div>
                            <?php
                            $gridpress_tabs_counter++;
                        }
                        ?>
                    </div>
                    <div class="gridpsettins-content-cont">
                        <?php
                        $gridpress_tabs_counter = 1;

                        foreach ( $gridpress_settings_tabs as $gridpress_tab_slug => $gridpress_settings_tab_label ) {
                            $gridpress_active_content_class = ( 1 === $gridpress_tabs_counter )
                                ? ' active-gridpsettin-cont'
                                : ' gridpress-hidden-item';

                            $gridpress_content_classes = 'gridpsettins-itmcontnt-con pseting_contnt_' .
                                sanitize_html_class( $gridpress_tab_slug ) .
                                $gridpress_active_content_class;
                            ?>
                            <div class="<?php echo esc_attr( $gridpress_content_classes ); ?>">
                                <?php
                                /**
                                 * Fires the settings tab content output.
                                 *
                                 * @param string $gridpress_tab_slug Current tab slug.
                                 */
                                do_action( "gridpress_psettins_tab_{$gridpress_tab_slug}_content" );
                                ?>
                            </div>
                            <?php
                            $gridpress_tabs_counter++;
                        }
                        ?>
                    </div>
                </div>
            </div>
            <?php
        }

        public function gridpress_tab_general_content() {
            global $gridpress_grid_all_settings;
            ?>
            <div class="settins-itmcontnt-inner gridpress-formfields-con">
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label"><strong><?php esc_html_e('Select Style', 'gridpress-grid-layout-design') ?></strong></div>
                    <div class="formseting-field-entry gridpress-styles-rdslectr">
                        <?php
                        $gridpress_styles_arr = [];
                        for ($gridpress_st = 1; $gridpress_st <= 30; $gridpress_st++) {
                            $gridpress_styles_arr['style' . $gridpress_st] = [
                                /* translators: %d: grid style number. */
                                'label' => sprintf(esc_html__('Style %d', 'gridpress-grid-layout-design'), $gridpress_st),
                                'image_url' => GRIDPRESS_URL . 'images/styles/' . $gridpress_st . '.jpg',
                            ];
                        }
                        Gridpress_Form_Fields::gridpress_option_buttons([
                            'name' => 'gridpress_grid_settings[grid_style]',
                            'value' => isset($gridpress_grid_all_settings['grid_style']) ? $gridpress_grid_all_settings['grid_style'] : '',
                            'default' => 'style1',
                            'options' => $gridpress_styles_arr,
                        ]);
                        ?>
                    </div>
                </div>
            </div>
            <?php
        }

        public function gridpress_tab_data_source_content() {
            global $gridpress_grid_all_settings, $gridpress_general_settings, $post, $gridpress_slected_post_type;
            $gridpress_grid_style = isset($gridpress_grid_all_settings['grid_style']) ? $gridpress_grid_all_settings['grid_style'] : '';

            $gridpress_post_types_toslect = isset($gridpress_general_settings['grid_post_types']) ? $gridpress_general_settings['grid_post_types'] : '';
            if (!empty($gridpress_post_types_toslect) && is_array($gridpress_post_types_toslect)) {
                $gridpress_select_ptypes_list = [];
                foreach ($gridpress_post_types_toslect as $pstype_name) {
                    $gridpress_post_type_obj = get_post_type_object($pstype_name);
                    $gridpress_select_ptypes_list[$pstype_name] = $gridpress_post_type_obj->label;
                }
            } else {
                $gridpress_select_ptypes_list = gridpress_cpt_list();
            }

            $gridpress_slected_post_type = 'post';
            if (!empty($gridpress_select_ptypes_list) && is_array($gridpress_select_ptypes_list)) {
                $gridpress_ptype_counter = 1;
                foreach ($gridpress_select_ptypes_list as $gridpress_select_ptype_name => $gridpress_select_ptype_lbl) {

                    if ($gridpress_ptype_counter == 1) {
                        $gridpress_slected_post_type = $gridpress_select_ptype_name;
                    }
                    $gridpress_ptype_counter++;
                }
            }
            $gridpress_source_data_type = isset($gridpress_grid_all_settings['data_source']) ? $gridpress_grid_all_settings['data_source'] : '';
            $gridpress_show_datasourc_field = true;

            if ($gridpress_grid_style == 'style15' || $gridpress_grid_style == 'style19' || $gridpress_grid_style == 'style21' || $gridpress_grid_style == 'style22' || $gridpress_grid_style == 'style23') {
                $gridpress_source_data_type = 'manual';
                $gridpress_show_datasourc_field = false;
            }

            $gridpress_show_datasourc_field_class = $gridpress_show_datasourc_field === false ? ' gridpress-hidden-item' : '';
            ?>
                <div class="settins-itmcontnt-inner gridpress-formfields-con">
                    <div class="gridpress-formseting-field<?php echo esc_attr($gridpress_show_datasourc_field_class); ?>">
                        <div class="formseting-field-label"><strong><?php esc_html_e('Data Type', 'gridpress-grid-layout-design') ?></strong></div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_option_buttons([
                                'name' => 'gridpress_grid_settings[data_source]',
                                'classes' => ['gridpress-grid-datasource-slecter'],
                                'value' => $gridpress_source_data_type,
                                'default' => 'post',
                                'options' => [
                                    'post' => esc_html__('Posts Data', 'gridpress-grid-layout-design'),
                                    'manual' => esc_html__('Manual Data', 'gridpress-grid-layout-design'),
                                ],
                            ]);
                            ?>
                        </div>
                    </div>
                    <?php
                    $gridpress_posts_data_type = isset($gridpress_grid_all_settings['posts']['data_type']) ? $gridpress_grid_all_settings['posts']['data_type'] : '';
                    $gridpress_posts_data_type_class = $gridpress_source_data_type == 'manual' ? ' gridpress-hidden-item' : '';
                    ?>
                    <div class="gridpress-dataposts-fields<?php echo esc_attr($gridpress_posts_data_type_class); ?>" data-id="<?php echo esc_attr($post->ID) ?>">
                        <?php
                        $posts_post_type = isset($gridpress_grid_all_settings['posts']['post_type']) ? $gridpress_grid_all_settings['posts']['post_type'] : '';
                        if (!empty($posts_post_type)) {
                            $gridpress_slected_post_type = $posts_post_type;
                        }
                        ?>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label"><strong><?php esc_html_e('Select Post Type', 'gridpress-grid-layout-design') ?></strong></div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_option_buttons([
                                    'name' => 'gridpress_grid_settings[posts][post_type]',
                                    'classes' => ['gridpress-grid-psttype-slecter'],
                                    'value' => $posts_post_type,
                                    'default' => $gridpress_slected_post_type,
                                    'options' => $gridpress_select_ptypes_list,
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label"><strong><?php esc_html_e('Post Items', 'gridpress-grid-layout-design') ?></strong></div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_option_buttons([
                                    'name' => 'gridpress_grid_settings[posts][data_type]',
                                    'classes' => ['gridpress-grid-pstsdatatype-slecter'],
                                    'value' => $gridpress_posts_data_type,
                                    'default' => 'all',
                                    'options' => [
                                        'all' => esc_html__('All Posts', 'gridpress-grid-layout-design'),
                                        'selected' => esc_html__('Selected Posts', 'gridpress-grid-layout-design'),
                                    ],
                                ]);
                                ?>
                            </div>
                        </div>
                        <?php
                        $gridpress_posts_data_type_class = $gridpress_posts_data_type == 'selected' ? ' gridpress-hidden-item' : '';
                        ?>
                        <div class="gridpress-dataposts-allfields<?php echo esc_attr($gridpress_posts_data_type_class); ?>">
                            <div class="gridpress-psttax-fields" data-id="<?php echo esc_attr($post->ID) ?>">
                                <?php Gridpress_Grid_settings::gridpress_post_taxonomies_fields($gridpress_slected_post_type, $gridpress_grid_all_settings); ?>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Order By', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_select([
                                        'name' => 'gridpress_grid_settings[posts][all][order_by]',
                                        'value' => isset($gridpress_grid_all_settings['posts']['all']['order_by']) ? $gridpress_grid_all_settings['posts']['all']['order_by'] : '',
                                        'classes' => ['gridpress-select2-dropdown'],
                                        'default' => 'ID',
                                        'options' => [
                                            'id' => esc_html__('ID', 'gridpress-grid-layout-design'),
                                            'title' => esc_html__('Title', 'gridpress-grid-layout-design'),
                                        ],
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Order', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_select([
                                        'name' => 'gridpress_grid_settings[posts][all][order]',
                                        'value' => isset($gridpress_grid_all_settings['posts']['all']['order']) ? $gridpress_grid_all_settings['posts']['all']['order'] : '',
                                        'classes' => ['gridpress-select2-dropdown'],
                                        'default' => 'DESC',
                                        'options' => [
                                            'DESC' => esc_html__('Descending', 'gridpress-grid-layout-design'),
                                            'ASC' => esc_html__('Ascending', 'gridpress-grid-layout-design'),
                                        ],
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Number of Posts', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[posts][all][num_posts]',
                                        'value' => isset($gridpress_grid_all_settings['posts']['all']['num_posts']) ? $gridpress_grid_all_settings['posts']['all']['num_posts'] : '20',
                                        'default' => '20',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Pagination Type', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_select([
                                        'name' => 'gridpress_grid_settings[posts][all][page_type]',
                                        'value' => isset($gridpress_grid_all_settings['posts']['all']['page_type']) ? $gridpress_grid_all_settings['posts']['all']['page_type'] : '',
                                        'classes' => ['gridpress-select2-dropdown'],
                                        'default' => 'page_nums',
                                        'options' => [
                                            'page_nums' => esc_html__('Page Numbers', 'gridpress-grid-layout-design'),
                                            'load_more' => esc_html__('Load More', 'gridpress-grid-layout-design'),
                                            'infinite' => esc_html__('Infinite Scroll', 'gridpress-grid-layout-design'),
                                        ],
                                    ]);
                                    ?>
                                </div>
                            </div>
                        </div>
                        <?php
                        $gridpress_posts_data_type_class = $gridpress_posts_data_type == 'selected' ? '' : ' gridpress-hidden-item';
                        ?>
                        <div class="gridpress-dataposts-slectedfields<?php echo esc_attr($gridpress_posts_data_type_class); ?>">
                            <?php
                            $gridpress_post_itms_list = [];
                            $gridpress_slected_postitms = isset($gridpress_grid_all_settings['posts']['selected']['items']) ? $gridpress_grid_all_settings['posts']['selected']['items'] : '';
                            if (!empty($gridpress_slected_postitms) && is_array($gridpress_slected_postitms)) {
                                $gridpress_post_itms_list = Gridpress_Grid_settings::gridpress_get_post_items($gridpress_slected_postitms, $gridpress_slected_post_type);
                            }
                            ?>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Select Post Items', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_select([
                                        'name' => 'gridpress_grid_settings[posts][selected][items][]',
                                        'value' => isset($gridpress_grid_all_settings['posts']['selected']['items']) ? $gridpress_grid_all_settings['posts']['selected']['items'] : '',
                                        'multiple' => true,
                                        'classes' => ['gridpress-postitms-selectr'],
                                        'default' => '',
                                        'extra_attributes' => ['data-ptype' => $gridpress_slected_post_type],
                                        'options' => $gridpress_post_itms_list,
                                    ]);
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php
                    $gridpress_datamanual_class = $gridpress_source_data_type == 'manual' ? '' : ' gridpress-hidden-item';
                    ?>
                    <div class="gridpress-datamanual-fields<?php echo esc_attr($gridpress_datamanual_class); ?>">
                        <div class=""><h3><?php esc_html_e('Add Items', 'gridpress-grid-layout-design') ?></h3></div>
                        <div class="gridpress-gridmnul-itmscon" data-id="<?php echo esc_attr($post->ID) ?>">
                            <?php Gridpress_Grid_settings::gridpress_manul_itms_list($gridpress_grid_all_settings) ?>
                        </div>
                    </div>
                </div>
            <?php
        }

        public function gridpress_tab_layout_content() {
            global $gridpress_grid_all_settings;
            ?>
                <div class="settins-itmcontnt-inner gridpress-formfields-con">
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label"><strong><?php esc_html_e('Grid Style', 'gridpress-grid-layout-design') ?></strong></div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_option_buttons([
                                'name' => 'gridpress_grid_settings[layout][grid_style]',
                                'value' => isset($gridpress_grid_all_settings['layout']['grid_style']) ? $gridpress_grid_all_settings['layout']['grid_style'] : '',
                                'default' => 'simple',
                                'options' => [
                                    'simple' => esc_html__('Simple', 'gridpress-grid-layout-design'),
                                    'masonry' => esc_html__('Masonry', 'gridpress-grid-layout-design'),
                                ],
                            ]);
                            ?>
                        </div>
                    </div>
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label"><strong><?php esc_html_e('Grid Columns', 'gridpress-grid-layout-design') ?></strong></div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_select([
                                'name' => 'gridpress_grid_settings[layout][grid_cols]',
                                'value' => isset($gridpress_grid_all_settings['layout']['grid_cols']) ? $gridpress_grid_all_settings['layout']['grid_cols'] : '3',
                                'classes' => ['gridpress-select2-dropdown'],
                                'default' => '3',
                                'options' => [
                                    '2' => esc_html__('2 Columns', 'gridpress-grid-layout-design'),
                                    '3' => esc_html__('3 Columns', 'gridpress-grid-layout-design'),
                                    '4' => esc_html__('4 Columns', 'gridpress-grid-layout-design'),
                                    '6' => esc_html__('6 Columns', 'gridpress-grid-layout-design'),
                                ],
                            ]);
                            ?>
                        </div>
                    </div>
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label"><strong><?php esc_html_e('Hover Effects', 'gridpress-grid-layout-design') ?></strong></div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_select([
                                'name' => 'gridpress_grid_settings[layout][hover_effect]',
                                'value' => isset($gridpress_grid_all_settings['layout']['hover_effect']) ? $gridpress_grid_all_settings['layout']['hover_effect'] : '',
                                'classes' => ['gridpress-select2-dropdown'],
                                'default' => '',
                                'options' => [
                                    '' => esc_html__('None', 'gridpress-grid-layout-design'),
                                    'zoom' => esc_html__('Zoom', 'gridpress-grid-layout-design'),
                                    'overlay' => esc_html__('Overlay', 'gridpress-grid-layout-design'),
                                    'cap_animate' => esc_html__('Caption Animation', 'gridpress-grid-layout-design'),
                                    'slide' => esc_html__('Slide', 'gridpress-grid-layout-design'),
                                ],
                            ]);
                            ?>
                        </div>
                    </div>
                </div>
            <?php
        }

        public function gridpress_tab_display_content() {
            global $gridpress_grid_all_settings, $gridpress_slected_post_type;
            $gridpress_grid_style = isset($gridpress_grid_all_settings['grid_style']) ? $gridpress_grid_all_settings['grid_style'] : '';

            $gridpress_disply_frmt_shwtype = 'psts';
            if ($gridpress_grid_style == 'style21' || $gridpress_grid_style == 'style22' || $gridpress_grid_style == 'style23') {
                $gridpress_disply_frmt_shwtype = 'video';
            }
            ?>
                <div class="settins-itmcontnt-inner gridpress-formfields-con">
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label">
                            <strong><?php esc_html_e('Title Length', 'gridpress-grid-layout-design') ?></strong>
                            <em><?php esc_html_e('Set 0 or remain empty for no limit', 'gridpress-grid-layout-design') ?></em>
                        </div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_input([
                                'name' => 'gridpress_grid_settings[display][title_length]',
                                'value' => isset($gridpress_grid_all_settings['display']['title_length']) ? $gridpress_grid_all_settings['display']['title_length'] : '',
                                'input_type' => 'number',
                                'extra_attributes' => ['placeholder' => '0'],
                                'default' => '',
                            ]);
                            ?>
                        </div>
                    </div>
                    <?php
                    $gridpress_disply_frmt_shwtype_class = $gridpress_disply_frmt_shwtype == 'video' ? ' gridpress-hidden-item' : '';
                    ?>
                    <div class="gridpress-displynpsts-fields<?php echo esc_attr($gridpress_disply_frmt_shwtype_class); ?>">
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display Excerpt', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide items short description', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_excerpt]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_excerpt']) ? $gridpress_grid_all_settings['display']['show_excerpt'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Excerpt Length', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Set 0 or remain empty for no limit', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_input([
                                    'name' => 'gridpress_grid_settings[display][excerpt_length]',
                                    'value' => isset($gridpress_grid_all_settings['display']['excerpt_length']) ? $gridpress_grid_all_settings['display']['excerpt_length'] : '',
                                    'input_type' => 'number',
                                    'extra_attributes' => ['placeholder' => '0'],
                                    'default' => '',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display button', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide read more button', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_button]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_button']) ? $gridpress_grid_all_settings['display']['show_button'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display date', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide date', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_date]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_date']) ? $gridpress_grid_all_settings['display']['show_date'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display author', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide author', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_author]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_author']) ? $gridpress_grid_all_settings['display']['show_author'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display comments', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide comments', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_comments]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_comments']) ? $gridpress_grid_all_settings['display']['show_comments'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display categories/tags', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide categories/tags in items.', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_terms]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_terms']) ? $gridpress_grid_all_settings['display']['show_terms'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                    </div>
                    <?php
                    $gridpress_displyvids_class = $gridpress_disply_frmt_shwtype != 'video' ? ' gridpress-hidden-item' : '';
                    ?>
                    <div class="gridpress-displyvids-fields<?php echo esc_attr($gridpress_displyvids_class) ?>">
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display Views', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide view', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_views]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_views']) ? $gridpress_grid_all_settings['display']['show_views'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Display Likes', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide likes', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][show_likes]',
                                    'value' => isset($gridpress_grid_all_settings['display']['show_likes']) ? $gridpress_grid_all_settings['display']['show_likes'] : 'on',
                                    'default' => 'on',
                                ]);
                                ?>
                            </div>
                        </div>
                    </div>
                    <?php
                    $gridpress_data_source_type = isset($gridpress_grid_all_settings['data_source']) ? $gridpress_grid_all_settings['data_source'] : '';
                    $gridpress_slcted_post_tax_list = Gridpress_Grid_settings::gridpress_get_post_taxonomies($gridpress_slected_post_type);
                    $gridpress_show_filtrs = false;
                    if ($gridpress_data_source_type != 'manual' && !empty($gridpress_slcted_post_tax_list)) {
                        $gridpress_show_filtrs = true;
                    }
                    $gridpress_posts_topfiltrs_fields_class = $gridpress_show_filtrs ? '' : ' gridpress-hidden-item';
                    ?>
                    <div class="gridpress-posts-topfiltrs-fields<?php echo esc_attr($gridpress_posts_topfiltrs_fields_class); ?>">
                        <?php Gridpress_Grid_settings::gridpress_post_filter_taxonomies_fields($gridpress_slected_post_type, $gridpress_grid_all_settings) ?>
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label">
                                <strong><?php esc_html_e('Top Filters', 'gridpress-grid-layout-design') ?></strong>
                                <em><?php esc_html_e('Show/hide top filters. Items will filter by terms.', 'gridpress-grid-layout-design') ?></em>
                            </div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_switch_button([
                                    'name' => 'gridpress_grid_settings[display][top_filters]',
                                    'classes' => ['gridpress-pstopfiltrs-switch'],
                                    'value' => isset($gridpress_grid_all_settings['display']['top_filters']) ? $gridpress_grid_all_settings['display']['top_filters'] : '',
                                    'default' => '',
                                ]);
                                ?>
                            </div>
                        </div>
                    </div>
                    <?php
                    $gridpress_sorting_class = ( 'manual' !== $gridpress_data_source_type ) ? '' : ' gridpress-hidden-item';
                    ?>
                    <div class="gridpress-formseting-field gridpress-posts-topsorting<?php echo esc_attr( $gridpress_sorting_class ); ?>" style="display:none;">
                        <div class="formseting-field-label">
                            <strong><?php esc_html_e('Items Sorting', 'gridpress-grid-layout-design') ?></strong>
                            <em><?php esc_html_e('Show/hide sorting dropdown for items.', 'gridpress-grid-layout-design') ?></em>
                        </div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_switch_button([
                                'name' => 'gridpress_grid_settings[display][top_sorting]',
                                'value' => isset($gridpress_grid_all_settings['display']['top_sorting']) ? $gridpress_grid_all_settings['display']['top_sorting'] : '',
                                'default' => '',
                            ]);
                            ?>
                        </div>
                    </div>
                </div>
            <?php
        }

        public function gridpress_tab_typography_content() {
            global $gridpress_grid_all_settings;
            $gridpress_grid_all_settings = is_array($gridpress_grid_all_settings) ? $gridpress_grid_all_settings : [];
            $gridpress_grid_style = isset($gridpress_grid_all_settings['grid_style']) ? $gridpress_grid_all_settings['grid_style'] : '';

            $gridpress_typo_frmt_shwtype = 'psts';
            if (!empty($gridpress_grid_style) && in_array($gridpress_grid_style, ['style21', 'style22', 'style23'], true)) {
                $gridpress_typo_frmt_shwtype = 'video';
            }
            $gridpress_typo_posts_class = ( 'video' === $gridpress_typo_frmt_shwtype ) ? ' gridpress-hidden-item' : '';
            $gridpress_typo_video_class = ( 'video' !== $gridpress_typo_frmt_shwtype ) ? ' gridpress-hidden-item' : '';
            ?>
            <div class="settins-itmcontnt-inner gridpress-formfields-con">
                <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Item Title', 'gridpress-grid-layout-design'); ?></strong></div>
                <?php Gridpress_Grid_settings::gridpress_item_typo_section('title', $gridpress_grid_all_settings); ?>
                <div class="gridpress-typonpsts-fields<?php echo esc_attr($gridpress_typo_posts_class); ?>">
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Item Excerpt', 'gridpress-grid-layout-design'); ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('excerpt', $gridpress_grid_all_settings); ?>
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Terms (Category/Tags)', 'gridpress-grid-layout-design'); ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('terms', $gridpress_grid_all_settings); ?>
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Author', 'gridpress-grid-layout-design'); ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('author', $gridpress_grid_all_settings); ?>
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Comments', 'gridpress-grid-layout-design'); ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('comments', $gridpress_grid_all_settings); ?>
                </div>
                <div class="gridpress-typovids-fields<?php echo esc_attr( $gridpress_typo_video_class ); ?>">
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('View Text', 'gridpress-grid-layout-design') ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('views', $gridpress_grid_all_settings); ?>
                    <div class="gridpress-spsec-hdincon"><strong><?php esc_html_e('Likes Text', 'gridpress-grid-layout-design'); ?></strong></div>
                    <?php Gridpress_Grid_settings::gridpress_item_typo_section('likes', $gridpress_grid_all_settings); ?>
                </div>
            </div>
            <?php
        }

        private function gridpress_get_typo_sec_properties($typo_sec, $gridpress_settings) {
            $gridpress_style_imps = '';
            $gridpress_typo_source = isset($gridpress_settings['typo'][$typo_sec]) && is_array($gridpress_settings['typo'][$typo_sec]) ? $gridpress_settings['typo'][$typo_sec] : [];
            $gridpress_typo_settings = array_map('sanitize_text_field', $gridpress_typo_source);
            if (!empty($gridpress_typo_settings['font_size'])) {
                $gridpress_style_imps .= 'font-size:' . $gridpress_typo_settings['font_size'] . 'px!important;';
            }
            if (!empty($gridpress_typo_settings['line_height'])) {
                $gridpress_style_imps .= 'line-height:' . $gridpress_typo_settings['line_height'] . 'px!important;';
            }
            if (!empty($gridpress_typo_settings['font_style']) && $gridpress_typo_settings['font_style'] == 'italic') {
                $gridpress_style_imps .= 'font-style:italic!important;';
            }
            if (!empty($gridpress_typo_settings['font_weight']) && $gridpress_typo_settings['font_weight'] == 'bold') {
                $gridpress_style_imps .= 'font-weight:bold!important;';
            }
            if (!empty($gridpress_typo_settings['color'])) {
                $gridpress_style_imps .= 'color:' . $gridpress_typo_settings['color'] . '!important;';
            }

            return $gridpress_style_imps;
        }

        public function gridpress_import_export_tab_ui() {
            ?>
            <div class="gridpress-gridimprt-settings">
                <div class="grid-impexprt-btnholder">
                    <div class="grid-impexprt-hdingcon">
                        <strong><?php esc_html_e('Export Settings', 'gridpress-grid-layout-design') ?></strong>
                        <em><?php esc_html_e('Download settings as JSON file.', 'gridpress-grid-layout-design') ?></em>
                    </div>
                    <div class="grid-impexprt-btncon">
                        <button class="button button-large grid-impexprt-export-btn"><?php esc_html_e('Download JSON', 'gridpress-grid-layout-design') ?></button>
                    </div>
                </div>
                <hr>
                <div class="grid-impexprt-btnholder">
                    <div class="grid-impexprt-hdingcon">
                        <strong><?php esc_html_e('Import Settings', 'gridpress-grid-layout-design') ?></strong>
                        <em><?php esc_html_e('Upload JSON file to import settings.', 'gridpress-grid-layout-design') ?></em>
                    </div>
                    <div class="grid-impexprt-btncon">
                        <input type="file" id="gridpress-import-json-file" accept=".json,application/json">
                        <button class="button button-large grid-impexprt-import-btn"><?php esc_html_e('Upload JSON', 'gridpress-grid-layout-design') ?></button>
                    </div>
                </div>
            </div>
            <?php
        }

        public function gridpress_export_settings_handler() {

            if (!current_user_can('manage_options')) {
                return;
            }

            if (
                isset($_POST['gridpress_export_json_action']) &&
                isset($_POST['gridpress_nonce']) &&
                wp_verify_nonce(sanitize_key(wp_unslash($_POST['gridpress_nonce'])), 'gridpress_admin_nonce')
            ) {
                $gridpress_grid_id = isset($_POST['gridpress_grid_id']) ? absint($_POST['gridpress_grid_id']) : 0;

                $gridpress_grid_settings = get_post_meta($gridpress_grid_id, 'gridpress_grid_settings', true);
                $gridpress_json_data = wp_json_encode($gridpress_grid_settings, JSON_PRETTY_PRINT);

                // Set headers for file download
                nocache_headers();
                header('Content-Type: application/json');
                header('Content-Disposition: attachment; filename="grid_settings_' . $gridpress_grid_id . '.json"');
                header('Content-Length: ' . strlen($gridpress_json_data));

                // Output JSON safely for file download
                echo $gridpress_json_data; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON download
                exit;
            }
        }

        public function gridpress_save_meta_fields($post_id) {

            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return;
            }
            $gridpress_post_type = get_post_type($post_id);
            if (get_post_type($post_id) !== 'gridpress_grid') {
                return;
            }

            if (!current_user_can('edit_post', $post_id)) {
                return;
            }
            $gridpress_save_nonce = isset($_POST['_wpnonce']) ? sanitize_key(wp_unslash($_POST['_wpnonce'])) : '';
            if (!$gridpress_save_nonce || !wp_verify_nonce($gridpress_save_nonce, 'update-post_' . $post_id)) {
                return;
            }
            if (isset($_POST['gridpress_grid_settings']) && is_array($_POST['gridpress_grid_settings'])) {
                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Already unslashed and validated
                $gridpress_raw_settings = isset($_POST['gridpress_grid_settings'])
                    ? map_deep(wp_unslash($_POST['gridpress_grid_settings']), 'sanitize_text_field')
                    : [];

                if (!is_array($gridpress_raw_settings)) {
                    return;
                }

                // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- validated and unslashed above
                $gridpress_sanitized_settings = $this->gridpress_sanitize_grid_settings_recursive($gridpress_raw_settings);
                update_post_meta($post_id, 'gridpress_grid_settings', $gridpress_sanitized_settings);

                $gridpress_grid_style = isset($gridpress_sanitized_settings['grid_style']) ? sanitize_text_field($gridpress_sanitized_settings['grid_style']) : '';

                $gridpress_grid_class = '.gridpress-dynm-grid-' . $post_id . ' ';
                $gridpress_style_str = '';
                if (isset($gridpress_sanitized_settings['typo']['title']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('title', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . 'h2 a,' . $gridpress_grid_class . '.jc-cards-title,' . $gridpress_grid_class . '.jc-cards-title a,' . $gridpress_grid_class . '.gridpress-dynm-carditm-title,' . $gridpress_grid_class . '.gridpress-dynm-carditm-title a{' . $gridpress_style_imps . '}';
                    }
                }

                if (isset($gridpress_sanitized_settings['typo']['views']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('views', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.gridpress-dynm-video-views{' . $gridpress_style_imps . '}';
                    }
                }
                if (isset($gridpress_sanitized_settings['typo']['likes']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('likes', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.gridpress-dynm-video-likes{' . $gridpress_style_imps . '}';
                    }
                }

                if (isset($gridpress_sanitized_settings['typo']['excerpt']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('excerpt', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.gridpress-dynm-carditm-excerpt{' . $gridpress_style_imps . '}';
                    }
                }
                if (isset($gridpress_sanitized_settings['typo']['terms']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('terms', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.jc-cards-category,' . $gridpress_grid_class . '.gridpress-dynm-carditm-terms{' . $gridpress_style_imps . '}';
                    }
                }
                if (isset($gridpress_sanitized_settings['typo']['author']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('author', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.jc-cards-author .jc-cards-subtext,' . $gridpress_grid_class . '.gridpress-dynm-carditm-author{' . $gridpress_style_imps . '}';
                    }
                }
                if (isset($gridpress_sanitized_settings['typo']['comments']['font_size'])) {
                    $gridpress_style_imps = $this->gridpress_get_typo_sec_properties('comments', $gridpress_sanitized_settings);
                    if (!empty($gridpress_style_imps)) {
                        $gridpress_style_str .= $gridpress_grid_class . '.gridpress-dynm-carditm-comments{' . $gridpress_style_imps . '}';
                    }
                }
                update_post_meta($post_id, 'gridpress_grid_styles_str', $gridpress_style_str);
            }
        }

        private function gridpress_sanitize_grid_settings_recursive($gridpress_value) {
            if (is_array($gridpress_value)) {
                $gridpress_sanitized = [];
                foreach ($gridpress_value as $gridpress_key => $gridpress_item) {
                    $gridpress_sanitized_key = is_string($gridpress_key) ? sanitize_key($gridpress_key) : $gridpress_key;
                    $gridpress_sanitized[$gridpress_sanitized_key] = $this->gridpress_sanitize_grid_settings_recursive($gridpress_item);
                }
                return $gridpress_sanitized;
            }

            if (is_numeric($gridpress_value)) {
                return $gridpress_value + 0;
            }

            return sanitize_text_field((string) $gridpress_value);
        }
    }

    new Gridpress_Post_types();
}