<?php
defined('ABSPATH') || exit; // Exit if accessed directly
if (! class_exists('Gridpress_Grid_settings')) {
    class Gridpress_Grid_settings {

        public function __construct() {
            add_action('wp_ajax_gridpress_ajax_get_posts_results', [$this, 'gridpress_get_posts_list']);
            add_action('wp_ajax_gridpress_ajax_get_terms_results', [$this, 'gridpress_get_terms_list']);
            add_action('wp_ajax_gridpress_ajax_get_post_taxonomies_html', [$this, 'gridpress_get_post_taxonomies_html']);
            add_action('wp_ajax_gridpress_ajax_add_manulcard_itm_html', [$this, 'gridpress_add_manulcard_itm_html']);
            add_action('wp_ajax_gridpress_ajax_import_grid_settings', [$this, 'gridpress_import_grid_settings_handler']);
        }

        public static function gridpress_get_post_items($gridpress_post_ids, $gridpress_post_type) {

            $gridpress_posts = get_posts(array(
                'post__in' => $gridpress_post_ids,
                'post_type' => $gridpress_post_type, // or your custom post type
                'posts_per_page' => -1, // get all matching posts
                'orderby' => 'post__in'
            ));

            $gridpress_post_list = [];
            if ( ! is_wp_error( $gridpress_posts ) && ! empty( $gridpress_posts ) ) {
                foreach ( $gridpress_posts as $gridpress_post_obj ) {

                    $gridpress_label = sprintf(
                        /* translators: 1: post title, 2: post ID */
                        esc_html__( '%1$s (id: %2$d)', 'gridpress-grid-layout-design' ),
                        esc_html( get_the_title( $gridpress_post_obj->ID ) ),
                        absint( $gridpress_post_obj->ID )
                    );

                    $gridpress_post_list[ absint( $gridpress_post_obj->ID ) ] = $gridpress_label;
                }
            }
            return $gridpress_post_list;
        }

        public function gridpress_import_grid_settings_handler() {

            check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce');

            if (!current_user_can('manage_options')) {
                wp_send_json_error([
                    'msg' => esc_html__( 'Permission denied.', 'gridpress-grid-layout-design' ),
                ]);
                wp_die();
            }

            if (
                !isset($_FILES['json_file']['name'], $_FILES['json_file']['tmp_name'], $_FILES['json_file']['error']) ||
                $_FILES['json_file']['error'] !== UPLOAD_ERR_OK
            ) {
                wp_send_json_error([
                    'msg' => esc_html__('File upload failed.', 'gridpress-grid-layout-design')
                ]);
            }

            // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- File upload array cannot be sanitized directly. Individual values are validated and sanitized before use.
            $gridpress_file = $_FILES['json_file'];
            $gridpress_filename = sanitize_file_name(wp_unslash($gridpress_file['name']));
            $gridpress_filetype = wp_check_filetype($gridpress_filename);

            if ($gridpress_filetype['ext'] !== 'json') {
                wp_send_json_error(
                    [
                        'msg' => esc_html__( 'Please upload a valid JSON file.', 'gridpress-grid-layout-design' ),
                    ]
                );
            }

            if ( ! function_exists( 'wp_handle_upload' ) ) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
            }

            // Handle the uploaded file via WP API to ensure it's safely moved to uploads.
            $gridpress_overrides = array(
                'test_form' => false,
                'mimes'     => array( 'json' => 'application/json' ),
            );

            $gridpress_movefile = wp_handle_upload( $gridpress_file, $gridpress_overrides );

            if ( isset( $gridpress_movefile['error'] ) ) {
                wp_send_json_error([
                    'msg' => esc_html__( 'File upload failed: ', 'gridpress-grid-layout-design' ) . esc_html( $gridpress_movefile['error'] ),
                ]);
            }

            if ( empty( $gridpress_movefile['file'] ) || ! file_exists( $gridpress_movefile['file'] ) ) {
                wp_send_json_error([
                    'msg' => esc_html__( 'Could not locate uploaded file.', 'gridpress-grid-layout-design' ),
                ]);
            }

            $gridpress_content = file_get_contents( $gridpress_movefile['file'] );

            // Remove the temporary uploaded file after reading.
            if ( function_exists( 'wp_delete_file' ) ) {
                wp_delete_file( $gridpress_movefile['file'] );
            }

            if ( false === $gridpress_content ) {
                wp_send_json_error([
                    'msg' => esc_html__('Could not read uploaded file.', 'gridpress-grid-layout-design')
                ]);
                wp_die();
            }

            $gridpress_json_data = json_decode($gridpress_content, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                wp_send_json_error([
                    'msg' => esc_html__('Invalid JSON file: ', 'gridpress-grid-layout-design') . esc_html(json_last_error_msg())
                ]);
                wp_die();
            }

            $gridpress_grid_id = isset($_POST['gridpress_grid_id']) ? absint(wp_unslash($_POST['gridpress_grid_id'])) : 0;

            if (!$gridpress_grid_id) {
                wp_send_json_error([
                    'msg' => esc_html__('Invalid grid ID.', 'gridpress-grid-layout-design')
                ]);
                wp_die();
            }

            // Sanitize imported data before saving
            $gridpress_sanitized_data = $this->gridpress_sanitize_settings_recursive($gridpress_json_data);

            update_post_meta($gridpress_grid_id, 'gridpress_grid_settings', $gridpress_sanitized_data);

            wp_send_json_success([
                'msg' => esc_html__('Settings imported successfully.', 'gridpress-grid-layout-design')
            ]);
        }

        private function gridpress_sanitize_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_settings_recursive($gridpress_item);
                }
                return $gridpress_sanitized;
            }

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

            return sanitize_text_field((string) $gridpress_value);
        }

        public function gridpress_get_posts_list() {

            check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce');

            $gridpress_pages = [];

            if (!gridpress_user_normal_permission()) {
                wp_send_json_error(
                    [
                        'msg' => esc_html__( 'Permission denied.', 'gridpress-grid-layout-design' ),
                    ]
                );
                wp_die();
            }

            if (!current_user_can('manage_options')) {
                wp_send_json_error(['msg' => esc_html__('Permission denied', 'gridpress-grid-layout-design')]);
                wp_die();
            }

            $gridpress_post_type = isset($_POST['pstype']) ? sanitize_key(wp_unslash($_POST['pstype'])) : '';
            $gridpress_query_args = array(
                'post_type' => $gridpress_post_type,
                'post_status' => 'publish',
                'posts_per_page' => 50,
                'fields' => 'ids',
                'orderby' => 'title',
                'order' => 'ASC',
                'no_found_rows' => true,
                'update_post_meta_cache' => false,
                'update_post_term_cache' => false,
            );
            if (isset($_POST['q'])) {
                $gridpress_query_args['s'] = sanitize_text_field(wp_unslash($_POST['q']));
            }
            $gridpress_query_results = get_posts($gridpress_query_args);
            if (!empty($gridpress_query_results) && is_array($gridpress_query_results)) {
                foreach ($gridpress_query_results as $gridpress_id) {
                    /* translators: %d: item id. */
                    $gridpress_pages[] = array('id' => absint($gridpress_id), 'text' => get_the_title($gridpress_id) . ' (' . sprintf(esc_html__('id: %d', 'gridpress-grid-layout-design'), $gridpress_id) . ')');
                }
            }

            wp_send_json(array('results' => $gridpress_pages));
        }

        public function gridpress_get_terms_list() {

            check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce');

            if (!current_user_can('manage_options')) {
                wp_send_json_error(['msg' => esc_html__('Permission denied', 'gridpress-grid-layout-design')]);
                wp_die();
            }

            $gridpress_pages = [];

            if (!gridpress_user_normal_permission()) {
                wp_send_json(array('results' => $gridpress_pages));
            }

            $gridpress_tax_name = isset($_POST['tax_name']) ? sanitize_key(wp_unslash($_POST['tax_name'])) : '';
            $gridpress_trms_args = array(
                'taxonomy' => $gridpress_tax_name,
                'hide_empty' => false,
            );
            if (isset($_POST['q'])) {
                $gridpress_trms_args['search'] = sanitize_text_field(wp_unslash($_POST['q']));
            }
            $gridpress_terms = get_terms($gridpress_trms_args);

            if (!empty($gridpress_terms)) {
                foreach ($gridpress_terms as $gridpress_term_row) {
                    $gridpress_id = $gridpress_term_row->term_id;
                    /* translators: %d: term id. */
                    $gridpress_pages[] = array(
                        'id'   => absint($gridpress_id),

                        /* translators: 1: term name, 2: term ID */
                        'text' => sprintf(
                            '%1$s (%2$s)',

                            // 1. Term name
                            esc_html($gridpress_term_row->name),

                            sprintf(
                                /* translators: %d is the term ID */
                                esc_html__('id: %d', 'gridpress-grid-layout-design'),
                                absint($gridpress_id)
                            )
                        ),
                    );
                }
            }

            wp_send_json(array('results' => $gridpress_pages));
        }

        public static function gridpress_get_post_taxonomies($gridpress_post_type) {

            $gridpress_tax_list = [];
            $gridpress_post_taxnomies = get_object_taxonomies($gridpress_post_type, 'objects');
            if (!empty($gridpress_post_taxnomies) && is_array($gridpress_post_taxnomies)) {
                foreach ($gridpress_post_taxnomies as $gridpress_post_taxnomy) {
                    if ($gridpress_post_type == 'product') {
                        if (strpos($gridpress_post_taxnomy->name, 'shipping') !== false || strpos($gridpress_post_taxnomy->name, 'visibility') !== false || strpos($gridpress_post_taxnomy->name, 'pa_') !== false) {
                            continue;
                        }
                    }
                    if ($gridpress_post_type == 'post') {
                        if ($gridpress_post_taxnomy->name == 'post_format') {
                            continue;
                        }
                    }
                    //if ($gridpress_post_taxnomy->public) {
                        $gridpress_tax_list[$gridpress_post_taxnomy->name] = $gridpress_post_taxnomy->label;
                    //}
                }
            }

            return $gridpress_tax_list;
        }

        public static function gridpress_post_taxonomies_fields($post_type, $gridpress_grid_all_settings) {

            $gridpress_tax_list = self::gridpress_get_post_taxonomies($post_type);
            if (!empty($gridpress_tax_list) && is_array($gridpress_tax_list)) {
                foreach ($gridpress_tax_list as $gridpress_tax_name => $gridpress_tax_label) {
                    $gridpress_tax_terms_list = [];
                    $gridpress_slected_termitms = isset($gridpress_grid_all_settings['posts']['all'][$gridpress_tax_name . '_terms']) ? $gridpress_grid_all_settings['posts']['all'][$gridpress_tax_name . '_terms'] : '';

                    if (!empty($gridpress_slected_termitms) && is_array($gridpress_slected_termitms)) {
                        foreach ($gridpress_slected_termitms as $gridpress_slected_termitm) {
                            $gridpress_trm_obj = get_term_by('id', $gridpress_slected_termitm, $gridpress_tax_name);
                            if (isset($gridpress_trm_obj->name)) {
                                $gridpress_tax_terms_list[$gridpress_slected_termitm] = $gridpress_trm_obj->name;
                            }
                        }

                    }
                    ?>
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label">
                            <strong>
                                <?php


                                echo sprintf(
                                    /* translators: %s: taxonomy label */
                                    esc_html__('Select %s', 'gridpress-grid-layout-design'),
                                    esc_html($gridpress_tax_label)
                                );
                                ?>
                            </strong>
                            <em><?php esc_html_e('Posts will show attached to these terms only',  'gridpress-grid-layout-design') ?></em>
                        </div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_select([
                                'name' => 'gridpress_grid_settings[posts][all][' . $gridpress_tax_name . '_terms][]',
                                'value' => isset($gridpress_grid_all_settings['posts']['all'][$gridpress_tax_name . '_terms']) ? $gridpress_grid_all_settings['posts']['all'][$gridpress_tax_name . '_terms'] : '',
                                'multiple' => true,
                                'classes' => ['gridpress-taxterms-selectr'],
                                'default' => '',
                                'extra_attributes' => ['data-tax' => $gridpress_tax_name],
                                'options' => $gridpress_tax_terms_list,
                            ]);
                            ?>
                        </div>
                    </div>
                    <?php
                }
            }
        }

        public function gridpress_get_post_taxonomies_html() {
            check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce');

            if (!current_user_can('manage_options')) {
                wp_send_json_error(['msg' => esc_html__('Permission denied', 'gridpress-grid-layout-design')]);
                wp_die();
            }

            $gridpress_post_type = isset( $_POST['pstype'] ) ? sanitize_key( wp_unslash( $_POST['pstype'] ) ) : '';
            $gridpress_grid_id = isset( $_POST['gid'] ) ? absint( wp_unslash( $_POST['gid'] ) ) : 0;
            $gridpress_grid_all_settings = get_post_meta($gridpress_grid_id, 'gridpress_grid_settings', true);

            ob_start();
            self::gridpress_post_taxonomies_fields($gridpress_post_type, $gridpress_grid_all_settings);
            $gridpress_html = ob_get_clean();

            //
            ob_start();
            $gridpress_tax_list = self::gridpress_get_post_taxonomies($gridpress_post_type);
            if (!empty($gridpress_tax_list) && is_array($gridpress_tax_list)) {
                $gridpress_slected_post_tax = isset($gridpress_grid_all_settings['display']['top_filter_tax']) ? $gridpress_grid_all_settings['display']['top_filter_tax'] : '';
                Gridpress_Form_Fields::gridpress_option_buttons([
                    'name' => 'gridpress_grid_settings[display][top_filter_tax]',
                    'value' => $gridpress_slected_post_tax,
                    'default' => '',
                    'options' => $gridpress_tax_list,
                ]);
            }
            $gridpress_filtr_tax_html = ob_get_clean();

            wp_send_json(array('html' => $gridpress_html, 'filtrs_tax_html' => $gridpress_filtr_tax_html));
        }

        public static function gridpress_post_filter_taxonomies_fields($gridpress_post_type, $gridpress_grid_all_settings) {

            $gridpress_tax_list = self::gridpress_get_post_taxonomies($gridpress_post_type);

            if (!empty($gridpress_tax_list) && is_array($gridpress_tax_list)) {
                $gridpress_slected_post_tax = isset($gridpress_grid_all_settings['display']['top_filter_tax']) ? $gridpress_grid_all_settings['display']['top_filter_tax'] : '';
                ?>
                <div class="gridpress-formseting-field gridpress-topfiltrs-slectdtax">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Select Taxonomy', 'gridpress-grid-layout-design') ?></strong>
                        <em><?php esc_html_e('Selected taxonomy terms    will appear in items and in top filters.', 'gridpress-grid-layout-design') ?></em>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_option_buttons([
                            'name' => 'gridpress_grid_settings[display][top_filter_tax]',
                            'value' => $gridpress_slected_post_tax,
                            'default' => '',
                            'options' => $gridpress_tax_list,
                        ]);
                        ?>
                    </div>
                </div>
                <?php
            }
        }

        public static function gridpress_manul_itms_list($gridpress_grid_all_settings) {
            $gridpress_card_items = isset($gridpress_grid_all_settings['cards']) ? $gridpress_grid_all_settings['cards'] : '';
            ?>
            <div class="gridpress-carditms-holdr">
                <?php
                if (!empty($gridpress_card_items) && is_array($gridpress_card_items)) {
                    foreach ($gridpress_card_items as $gridpress_card_id => $gridpress_card_item) {
                        self::gridpress_manul_itm($gridpress_card_id, $gridpress_card_item);
                    }
                } else {
                    self::gridpress_manul_itm();
                }
                ?>
            </div>
            <div class="gridpress-cardadd-btnholdr">
                <a href="javascript:;" class="button"><?php esc_html_e('Add item', 'gridpress-grid-layout-design') ?></a>
            </div>
            <?php
        }

        public static function gridpress_manul_itm($gridpress_card_id = 0, $gridpress_itm_args = []) {
            global $gridpress_grid_all_settings;

            $gridpress_grid_style = '';
            if (isset($gridpress_grid_all_settings['grid_style'])) {
                $gridpress_grid_style = isset($gridpress_grid_all_settings['grid_style']) ? $gridpress_grid_all_settings['grid_style'] : '';
            } else {
                if (isset($_REQUEST['style'])) {
                    if (!check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce')) {
                        return; // phpcs:ignore HTML render function not ajax function
                    }
                    $gridpress_grid_style = sanitize_key(wp_unslash($_REQUEST['style']));
                }
            }
            $gridpress_media_type = 'image';
            $gridpress_media_type = isset($gridpress_itm_args['m_type']) ? $gridpress_itm_args['m_type'] : $gridpress_media_type;

            $gridpress_show_mediatype_field = false;
            $gridpress_video_styles = ['style21','style22','style23'];

            if (in_array($gridpress_grid_style, $gridpress_video_styles, true)) {
                $gridpress_media_type = 'video';
                $gridpress_show_mediatype_field = false;
            }

            $gridpress_itm_id = $gridpress_card_id > 0 ? $gridpress_card_id : wp_rand(10000000, 99999999);
            $gridpress_itm_title = isset($gridpress_itm_args['title']) && !empty($gridpress_itm_args['title']) ? $gridpress_itm_args['title'] : esc_html__('Item title', 'gridpress-grid-layout-design');
            ?>
            <div class="gridpress-card-itm" data-id="<?php echo esc_attr($gridpress_itm_id) ?>">
                <div class="gridcard-itm-hder">
                    <div class="carditm-sorter"><i class="gridpress-icon-arrows-alt"></i></div>
                    <div class="carditm-titlecon"><strong><?php echo esc_html($gridpress_itm_title); ?></strong></div>
                    <div class="carditm-actscon">
                        <a href="javascript:;" class="carditm-clone-btn" title="<?php echo esc_attr__('Clone', 'gridpress-grid-layout-design') ?>"><i class="gridpress-icon-plus"></i></a>
                        <a href="javascript:;" class="carditm-updte-btn" title="<?php echo esc_attr__('Update', 'gridpress-grid-layout-design') ?>"><i class="gridpress-icon-pencil"></i></a>
                        <a href="javascript:;" class="carditm-rmv-btn" title="<?php echo esc_attr__('Remove', 'gridpress-grid-layout-design') ?>"><i class="gridpress-icon-trash-o"></i></a>
                    </div>
                </div>
                <div class="gridcard-itm-contntcon" style="display: none;">
                    <a href="javascript:;" class="gridcard-box-closebtn">❌</a>
                    <div class="gridcard-itm-contntiner">
                        <div class="gridpress-formseting-field">
                            <div class="formseting-field-label"><strong><?php esc_html_e('Title', 'gridpress-grid-layout-design') ?></strong></div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_input([
                                    'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][title]',
                                    'value' => $gridpress_itm_title,
                                    'classes' => ['gridpress-carditm-titlefield'],
                                    'default' => '',
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-formseting-field gridpress-card-mediatype-slctrfield<?php echo esc_attr($gridpress_show_mediatype_field === false ? ' gridpress-hidden-item' : '') ?>">
                            <div class="formseting-field-label"><strong><?php esc_html_e('Media type', 'gridpress-grid-layout-design') ?></strong></div>
                            <div class="formseting-field-entry">
                                <?php
                                Gridpress_Form_Fields::gridpress_option_buttons([
                                    'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][m_type]',
                                    'value' => $gridpress_media_type,
                                    'default' => 'image',
                                    'classes' => ['gridpress-card-mediaslect-btn'],
                                    'options' => [
                                        'image' => esc_html__('Image', 'gridpress-grid-layout-design'),
                                        'video' => esc_html__('Video', 'gridpress-grid-layout-design'),
                                    ],
                                ]);
                                ?>
                            </div>
                        </div>
                        <div class="gridpress-carditm-imgcon<?php echo esc_attr($gridpress_media_type == 'video' ? ' gridpress-hidden-item' : '') ?>">
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label"><strong><?php esc_html_e('Image', 'gridpress-grid-layout-design') ?></strong></div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_image([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][image]',
                                        'id_name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][image_attach_id]',
                                        'value' => isset($gridpress_itm_args['image']) ? $gridpress_itm_args['image'] : '',
                                        'id_value' => isset($gridpress_itm_args['image_attach_id']) ? $gridpress_itm_args['image_attach_id'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('URL', 'gridpress-grid-layout-design') ?></strong>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][item_url]',
                                        'value' => isset($gridpress_itm_args['item_url']) ? $gridpress_itm_args['item_url'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_dblimgrow_fields = false;
                            if ($gridpress_grid_style == 'style15') {
                                $gridpress_show_dblimgrow_fields = true;
                            }
                            ?>
                            <div class="gridpress-carditm-dbleimgfields<?php echo esc_attr($gridpress_show_dblimgrow_fields ? '' : ' gridpress-hidden-item') ?>">
                                <div class="gridpress-formseting-field">
                                    <div class="formseting-field-label"><strong><?php esc_html_e('Second Image', 'gridpress-grid-layout-design') ?></strong></div>
                                    <div class="formseting-field-entry">
                                        <?php
                                        Gridpress_Form_Fields::gridpress_input([
                                            'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][sec_image]',
                                            'id_name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][sec_image_attach_id]',
                                            'value' => isset($gridpress_itm_args['sec_image']) ? $gridpress_itm_args['sec_image'] : '',
                                            'id_value' => isset($gridpress_itm_args['sec_image_attach_id']) ? $gridpress_itm_args['sec_image_attach_id'] : '',
                                            'default' => '',
                                        ]);
                                        ?>
                                    </div>
                                </div>
                                <div class="gridpress-formseting-field">
                                    <div class="formseting-field-label">
                                        <strong><?php esc_html_e('Second Image URL', 'gridpress-grid-layout-design') ?></strong>
                                        <em><?php esc_html_e('URL for second image.', 'gridpress-grid-layout-design') ?></em>
                                    </div>
                                    <div class="formseting-field-entry">
                                        <?php
                                        Gridpress_Form_Fields::gridpress_input([
                                            'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][sec_image_url]',
                                            'value' => isset($gridpress_itm_args['sec_image_url']) ? $gridpress_itm_args['sec_image_url'] : '',
                                            'default' => '',
                                        ]);
                                        ?>
                                    </div>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_bgfield = false;
                            if ($gridpress_grid_style == 'style19') {
                                $gridpress_show_bgfield = true;
                            }
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-bgfield<?php echo esc_attr($gridpress_show_bgfield ? '' : ' gridpress-hidden-item') ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Background Color', 'gridpress-grid-layout-design') ?></strong>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_color_pickr([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][bg_color]',
                                        'value' => isset($gridpress_itm_args['bg_color']) ? $gridpress_itm_args['bg_color'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_datefield = false;

                            $gridpress_styles_with_date = ['style1','style2','style3','style4','style5','style6','style7','style27'];
                            if (in_array($gridpress_grid_style, $gridpress_styles_with_date, true)) {
                                $gridpress_show_datefield = true;
                            }

                            $gridpress_date_class = $gridpress_show_datefield ? '' : ' gridpress-hidden-item';
                            ?>

                            <div class="gridpress-formseting-field gridpress-carditm-datefield<?php echo esc_attr( $gridpress_date_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e( 'Date', 'gridpress-grid-layout-design' ); ?></strong>
                                </div>

                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name'  => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][date]',
                                        'classes' => ['gridpress-date-input'],
                                        'extra_attributes' => [
                                            'readonly'  => 'readonly',
                                            'data-maxy' => (int) wp_date('Y') + 10,
                                        ],
                                        'value'   => isset($gridpress_itm_args['date']) ? $gridpress_itm_args['date'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_authorfield = false;

                            $gridpress_styles_with_author = ['style2','style4','style5','style28'];
                            if (in_array($gridpress_grid_style, $gridpress_styles_with_author, true)) {
                                $gridpress_show_authorfield = true;
                            }

                            $gridpress_author_class = $gridpress_show_authorfield ? '' : ' gridpress-hidden-item';
                            ?>

                            <div class="gridpress-carditm-authorfield<?php echo esc_attr( $gridpress_author_class ); ?>">
                                <div class="gridpress-formseting-field">
                                    <div class="formseting-field-label">
                                        <strong><?php esc_html_e('Author', 'gridpress-grid-layout-design') ?></strong>
                                        <em><?php esc_html_e('Type author name here', 'gridpress-grid-layout-design') ?></em>
                                    </div>
                                    <div class="formseting-field-entry">
                                        <?php
                                        Gridpress_Form_Fields::gridpress_input([
                                            'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][author_name]',
                                            'value' => isset($gridpress_itm_args['author_name']) ? $gridpress_itm_args['author_name'] : '',
                                            'default' => '',
                                        ]);
                                        ?>
                                    </div>
                                </div>
                                <div class="gridpress-formseting-field">
                                    <div class="formseting-field-label"><strong><?php esc_html_e('Author Picture', 'gridpress-grid-layout-design') ?></strong></div>
                                    <div class="formseting-field-entry">
                                        <?php
                                        Gridpress_Form_Fields::gridpress_image([
                                            'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][author_image]',
                                            'id_name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][author_image_attach_id]',
                                            'value' => isset($gridpress_itm_args['author_image']) ? $gridpress_itm_args['author_image'] : '',
                                            'id_value' => isset($gridpress_itm_args['author_image_attach_id']) ? $gridpress_itm_args['author_image_attach_id'] : '',
                                            'default' => '',
                                        ]);
                                        ?>
                                    </div>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_viewsfield = false;
                            if ($gridpress_grid_style == 'style28') {
                                $gridpress_show_viewsfield = true;
                            }

                            $gridpress_views_class = $gridpress_show_viewsfield ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-viewsfield<?php echo esc_attr( $gridpress_views_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Views', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add number of total views. Leave it blank to hide it.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][item_views]',
                                        'value' => isset($gridpress_itm_args['item_views']) ? $gridpress_itm_args['item_views'] : '',
                                        'extra_attributes' => ['placeholder' => esc_html__('5.3M', 'gridpress-grid-layout-design')],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_likesfield = false;

                           $gridpress_styles_with_likes = ['style27','style28'];
                            if (in_array($gridpress_grid_style, $gridpress_styles_with_likes, true)) {
                                $gridpress_show_likesfield = true;
                            }

                            $gridpress_likes_class = $gridpress_show_likesfield ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-likesfield<?php echo esc_attr( $gridpress_likes_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Likes', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add number of total likes. Leave it blank to hide it.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][item_likes]',
                                        'value' => isset($gridpress_itm_args['item_likes']) ? $gridpress_itm_args['item_likes'] : '',
                                        'extra_attributes' => ['placeholder' => esc_html__('1M Likes', 'gridpress-grid-layout-design')],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_behancefield = false;
                            if ($gridpress_grid_style == 'style28') {
                                $gridpress_show_behancefield = true;
                            }

                            $gridpress_behance_class = $gridpress_show_behancefield ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-behancefield<?php echo esc_attr($gridpress_behance_class); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Behance URL', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add behance profile URL here. Leave it blank to hide it.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][behance_url]',
                                        'value' => isset($gridpress_itm_args['behance_url']) ? $gridpress_itm_args['behance_url'] : '',
                                        'extra_attributes' => ['placeholder' => 'https://www.behance.net/Alaraselin'],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_comments_field = false;
                            $gridpress_styles_with_comments = ['style2','style3','style6'];
                            if (in_array($gridpress_grid_style, $gridpress_styles_with_comments, true)) {
                                $gridpress_show_comments_field = true;
                            }

                            $gridpress_comments_class = $gridpress_show_comments_field ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-commentsfield<?php echo esc_attr( $gridpress_comments_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Comments', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Number of comments', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][comments]',
                                        'value' => isset($gridpress_itm_args['comments']) ? $gridpress_itm_args['comments'] : '',
                                        'input_type' => 'number',
                                        'extra_attributes' => ['min' => 1, 'max' => 1000],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_catsfield = false;
                            $gridpress_styles_with_cats = ['style1','style2','style3','style4','style5','style6','style10'];

                            if (in_array($gridpress_grid_style, $gridpress_styles_with_cats, true)) {
                                $gridpress_show_catsfield = true;
                            }

                            $gridpress_cats_class = $gridpress_show_catsfield ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-catsfield<?php echo esc_attr( $gridpress_cats_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Category/Tags', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('You can add multiple comma separated terms names.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][category]',
                                        'value' => isset($gridpress_itm_args['category']) ? $gridpress_itm_args['category'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <?php
                            $gridpress_show_excerpt_field = false;
                            if ($gridpress_grid_style == 'style4') {
                                $gridpress_show_excerpt_field = true;
                            }

                            $gridpress_excerpt_class = $gridpress_show_excerpt_field ? '' : ' gridpress-hidden-item';
                            ?>
                            <div class="gridpress-formseting-field gridpress-carditm-excerptfield<?php echo esc_attr( $gridpress_excerpt_class ); ?>">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Excerpt', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Write small description for the item.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_textarea([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][excerpt]',
                                        'value' => isset($gridpress_itm_args['excerpt']) ? $gridpress_itm_args['excerpt'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                        </div>
                        <?php
                        $gridpress_video_class = $gridpress_media_type == 'video' ? '' : ' gridpress-hidden-item';
                        ?>
                        <div class="gridpress-carditm-videocon<?php echo esc_attr($gridpress_video_class); ?>">
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Video', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Upload video OR add external video URL i.e. https://www.youtube.com/watch?v=a3ICNMQW7Ok', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_file([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][video]',
                                        'value' => isset($gridpress_itm_args['video']) ? $gridpress_itm_args['video'] : '',
                                        'btn_txt' => esc_html__('Upload Video', 'gridpress-grid-layout-design'),
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Thumb Image', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add thumbnail image for video', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_image([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][thumb_image]',
                                        'id_name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][thumb_image_attach_id]',
                                        'value' => isset($gridpress_itm_args['thumb_image']) ? $gridpress_itm_args['thumb_image'] : '',
                                        'id_value' => isset($gridpress_itm_args['thumb_image_attach_id']) ? $gridpress_itm_args['thumb_image_attach_id'] : '',
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Views', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add number of total views to video. Leave it blank to hide it.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][video_views]',
                                        'value' => isset($gridpress_itm_args['video_views']) ? $gridpress_itm_args['video_views'] : '',
                                        'extra_attributes' => ['placeholder' => esc_html__('5.1M Views', 'gridpress-grid-layout-design')],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                            <div class="gridpress-formseting-field">
                                <div class="formseting-field-label">
                                    <strong><?php esc_html_e('Likes', 'gridpress-grid-layout-design') ?></strong>
                                    <em><?php esc_html_e('Add number of total likes to video. Leave it blank to hide it.', 'gridpress-grid-layout-design') ?></em>
                                </div>
                                <div class="formseting-field-entry">
                                    <?php
                                    Gridpress_Form_Fields::gridpress_input([
                                        'name' => 'gridpress_grid_settings[cards][' . absint($gridpress_itm_id) . '][video_likes]',
                                        'value' => isset($gridpress_itm_args['video_likes']) ? $gridpress_itm_args['video_likes'] : '',
                                        'extra_attributes' => ['placeholder' => esc_html__('1M Likes', 'gridpress-grid-layout-design')],
                                        'default' => '',
                                    ]);
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }

        public function gridpress_add_manulcard_itm_html() {
            check_ajax_referer('gridpress_admin_nonce', 'gridpress_nonce');
            if ( ! current_user_can( 'manage_options' ) ) {
                wp_send_json_error(
                    [
                        'msg' => esc_html__( 'Permission denied.', 'gridpress-grid-layout-design' ),
                    ]
                );
                wp_die();
            }
            ob_start();
            self::gridpress_manul_itm();
            $gridpress_html = ob_get_clean();

            wp_send_json(array('html' => $gridpress_html));
        }

        public static function gridpress_item_typo_section($gridpress_name, $gridpress_grid_all_settings, $gridpressis_button = false) {
            ?>
            <div class="gridpress-itmtypo-main">
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Font Size', 'gridpress-grid-layout-design') ?></strong>
                        <em><?php esc_html_e('(in px)', 'gridpress-grid-layout-design') ?></em>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_input([
                            'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][font_size]',
                            'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['font_size']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['font_size'] : '',
                            'extra_attributes' => ['placeholder' => esc_html__('16', 'gridpress-grid-layout-design'), 'min' => 1, 'max' => 1000],
                            'input_type' => 'number',
                            'default' => '',
                        ]);
                        ?>
                    </div>
                </div>
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Line Height', 'gridpress-grid-layout-design') ?></strong>
                        <em><?php esc_html_e('(in px)', 'gridpress-grid-layout-design') ?></em>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_input([
                            'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][line_height]',
                            'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['line_height']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['line_height'] : '',
                            'extra_attributes' => ['placeholder' => esc_html__('20', 'gridpress-grid-layout-design'), 'min' => 1, 'max' => 1000],
                            'input_type' => 'number',
                            'default' => '',
                        ]);
                        ?>
                    </div>
                </div>
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Font Style', 'gridpress-grid-layout-design') ?></strong>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_select([
                            'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][font_style]',
                            'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['font_style']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['font_style'] : '',
                            'classes' => ['gridpress-select2-dropdown'],
                            'default' => 'normal',
                            'options' => [
                                'normal' => esc_html__('Default', 'gridpress-grid-layout-design'),
                                'italic' => esc_html__('Italic', 'gridpress-grid-layout-design'),
                            ],
                        ]);
                        ?>
                    </div>
                </div>
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Font Weight', 'gridpress-grid-layout-design') ?></strong>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_select([
                            'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][font_weight]',
                            'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['font_weight']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['font_weight'] : '',
                            'classes' => ['gridpress-select2-dropdown'],
                            'default' => 'normal',
                            'options' => [
                                'normal' => esc_html__('Normal', 'gridpress-grid-layout-design'),
                                'bold' => esc_html__('Bold', 'gridpress-grid-layout-design'),
                            ],
                        ]);
                        ?>
                    </div>
                </div>
                <div class="gridpress-formseting-field">
                    <div class="formseting-field-label">
                        <strong><?php esc_html_e('Color', 'gridpress-grid-layout-design') ?></strong>
                    </div>
                    <div class="formseting-field-entry">
                        <?php
                        Gridpress_Form_Fields::gridpress_color_pickr([
                            'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][color]',
                            'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['color']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['color'] : '',
                            'default' => '',
                        ]);
                        ?>
                    </div>
                </div>
                <?php
                if (!empty($gridpressis_button)) {
                    ?>
                    <div class="gridpress-formseting-field">
                        <div class="formseting-field-label">
                            <strong><?php esc_html_e('Background Color', 'gridpress-grid-layout-design') ?></strong>
                        </div>
                        <div class="formseting-field-entry">
                            <?php
                            Gridpress_Form_Fields::gridpress_color_pickr([
                                'name' => 'gridpress_grid_settings[typo][' . $gridpress_name . '][bg_color]',
                                'value' => isset($gridpress_grid_all_settings['typo'][$gridpress_name]['bg_color']) ? $gridpress_grid_all_settings['typo'][$gridpress_name]['bg_color'] : '',
                                'default' => '',
                            ]);
                            ?>
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>
            <?php
        }
    }
    new Gridpress_Grid_settings();
}