<div class="theme-offer">
   <?php
        // Check if the demo import has been completed
        $jewellery_shop_demo_import_completed = get_option('jewellery_shop_demo_import_completed', false);

        // If the demo import is completed, display the "View Site" button
        if ($jewellery_shop_demo_import_completed) {
            echo '<br>';
            echo '<div class="success">Demo Import Successful</div>';
            echo '<br>';
            echo '<hr>';
            echo '<br>';
            echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'jewellery-shop' ) . '</span>';
            echo '<br>';
            echo '<br>';
            echo '<br>';
            echo '<div class="view-site-btn">';
            echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
            echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
            echo '</div>';
        }
    if ( isset( $_POST['submit'] ) ) {

        echo '<div class="plugin-notice">';
            // Check if Classic Blog Grid plugin is installed
            if (!is_plugin_active('classic-blog-grid/classic-blog-grid.php')) {
                // Plugin slug and file path for Classic Blog Grid
                $jewellery_shop_plugin_slug = 'classic-blog-grid';
                $jewellery_shop_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $jewellery_shop_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $jewellery_shop_installed_plugins = get_plugins();
                    if ( ! isset( $jewellery_shop_installed_plugins[ $jewellery_shop_plugin_file ] ) ) {
            
                        // Include necessary files to install plugins
                        include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
                        include_once( ABSPATH . 'wp-admin/includes/file.php' );
                        include_once( ABSPATH . 'wp-admin/includes/misc.php' );
                        include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
            
                        // Download and install Classic Blog Grid
                        $jewellery_shop_upgrader = new Plugin_Upgrader();
                        $jewellery_shop_upgrader->install( 'https://downloads.wordpress.org/plugin/classic-blog-grid.latest-stable.zip' );
                    }
            
                    // Activate the Classic Blog Grid plugin after installation (if needed)
                    activate_plugin( $jewellery_shop_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $jewellery_shop_menuname = 'Primary Menu';
        $jewellery_shop_bpmenulocation = 'primary';
        $jewellery_shop_menu_exists = wp_get_nav_menu_object( $jewellery_shop_menuname );
    
        if (!$jewellery_shop_menu_exists) {
            // Create a new menu
            $jewellery_shop_menu_id = wp_create_nav_menu($jewellery_shop_menuname);

            // Define pages to be created
            $jewellery_shop_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'about-us' => array(
                    'title' => 'About Us',
                    'content' => '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>'
                ),
                'pages' => array(
                    'title' => 'Pages',
                    'content' => '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>'
                ),
                'blogs' => array(
                    'title' => 'Blogs',
                    'content' => ''
                ),
            );

            $jewellery_shop_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($jewellery_shop_pages as $jewellery_shop_slug => $jewellery_shop_data) {
                $jewellery_shop_existing_page = get_page_by_path($jewellery_shop_slug);

                if ($jewellery_shop_existing_page) {
                    // If the page already exists, use its ID
                    $jewellery_shop_page_id = $jewellery_shop_existing_page->ID;
                } else {
                    // Create a new page
                    $jewellery_shop_page_data = array(
                        'post_type'    => 'page',
                        'post_title'   => $jewellery_shop_data['title'],
                        'post_content' => isset($jewellery_shop_data['content']) ? $jewellery_shop_data['content'] : '',
                        'post_status'  => 'publish',
                        'post_author'  => get_current_user_id(), // Set author dynamically
                        'post_name'    => $jewellery_shop_slug,
                    );

                    $jewellery_shop_page_id = wp_insert_post($jewellery_shop_page_data);

                    // Assign custom page template if specified
                    if (!empty($jewellery_shop_data['template'])) {
                        update_post_meta($jewellery_shop_page_id, '_wp_page_template', $jewellery_shop_data['template']);
                    }
                }

                // Store the page IDs
                $jewellery_shop_page_ids[$jewellery_shop_slug] = $jewellery_shop_page_id;
            }

            // Set homepage and blog page
            update_option('page_for_posts', $jewellery_shop_page_ids['blogs']);
            update_option('page_on_front', $jewellery_shop_page_ids['home']);
            update_option('show_on_front', 'page');

            // Define menu items
            $jewellery_shop_menu_items = array(
                'home',
                'about-us',
                'pages',
                'blogs',
            );

            // Add menu items dynamically
            foreach ($jewellery_shop_menu_items as $jewellery_shop_slug) {
                wp_update_nav_menu_item($jewellery_shop_menu_id, 0, array(
                    'menu-item-title' => esc_html($jewellery_shop_pages[$jewellery_shop_slug]['title']),
                    'menu-item-url' => get_permalink($jewellery_shop_page_ids[$jewellery_shop_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jewellery_shop_page_ids[$jewellery_shop_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $jewellery_shop_locations = get_theme_mod('nav_menu_locations', array());
            $jewellery_shop_locations[$jewellery_shop_bpmenulocation] = $jewellery_shop_menu_id;
            set_theme_mod('nav_menu_locations', $jewellery_shop_locations);
        }

        //Logo
        set_theme_mod( 'jewellery_shop_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));

        //Header Section
        set_theme_mod( 'jewellery_shop_phone_number', '+123 456 7890');
        set_theme_mod( 'jewellery_shop_email_address', 'abc@example.com');

        //Slider Section
        set_theme_mod( 'jewellery_shop_hide_categorysec', true);
        set_theme_mod( 'jewellery_shop_button_text', 'Read more');
        
        $jewellery_shop_slider_category_id = wp_create_category('Slider');

        // Set the category in theme mods for the slider section
        set_theme_mod('jewellery_shop_slidersection', $jewellery_shop_slider_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $jewellery_shop_titles = array(
            'Luxury fit for your lifestyle',
            'Timeless elegance crafted to perfection',
            'Sparkle every moment with signature designs'
        );
                
        // Create three demo posts and assign them to the 'Slider' category
        for ($jewellery_shop_i = 1; $jewellery_shop_i <= 3; $jewellery_shop_i++) {
            $jewellery_shop_title = $jewellery_shop_titles[$jewellery_shop_i - 1];
            $jewellery_shop_content = 'Elegant craftsmanship with standard material is what you need Designs so intricate seen before that you have never seen before.looking at its layout.';
        
            // Prepare the post object
            $jewellery_shop_my_post = array(
                'post_title'    => wp_strip_all_tags($jewellery_shop_title),
                'post_content'  => $jewellery_shop_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($jewellery_shop_slider_category_id),
            );
        
            // Insert the post into the database
            $jewellery_shop_post_id = wp_insert_post($jewellery_shop_my_post);
        
            // If the post was successfully created, set the featured image
            if ($jewellery_shop_post_id && !is_wp_error($jewellery_shop_post_id)) {
                // Set the image URL based on the current slider index
                $jewellery_shop_image_url = esc_url(get_stylesheet_directory_uri() . '/images/jewellary-slider' . $jewellery_shop_i . '.png');
                $jewellery_shop_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $jewellery_shop_response = wp_remote_get($jewellery_shop_image_url);
                if (!is_wp_error($jewellery_shop_response)) {
                    $jewellery_shop_image_data = wp_remote_retrieve_body($jewellery_shop_response);
        
                    if (!empty($jewellery_shop_image_data)) {
                        // Handle the file upload process
                        $jewellery_shop_image_name = 'slider' . $jewellery_shop_i . '.png';
                        $jewellery_shop_unique_file_name = wp_unique_filename($jewellery_shop_upload_dir['path'], $jewellery_shop_image_name);
                        $jewellery_shop_file = $jewellery_shop_upload_dir['path'] . '/' . $jewellery_shop_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($jewellery_shop_file, $jewellery_shop_image_data);
        
                        // Check file type and prepare for attachment
                        $jewellery_shop_wp_filetype = wp_check_filetype($jewellery_shop_unique_file_name, null);
                        $jewellery_shop_attachment = array(
                            'post_mime_type' => $jewellery_shop_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($jewellery_shop_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $jewellery_shop_attach_id = wp_insert_attachment($jewellery_shop_attachment, $jewellery_shop_file, $jewellery_shop_post_id);
                        $jewellery_shop_attach_data = wp_generate_attachment_metadata($jewellery_shop_attach_id, $jewellery_shop_file);
                        wp_update_attachment_metadata($jewellery_shop_attach_id, $jewellery_shop_attach_data);
                        set_post_thumbnail($jewellery_shop_post_id, $jewellery_shop_attach_id);
                    }
                }
            }
        }

        //Our Service Section
        set_theme_mod( 'jewellery_shop_disabled_pgboxes', true);
        set_theme_mod( 'jewellery_shop_service_heading', 'Our Service');

        $jewellery_shop_service_category_id = wp_create_category('Service');

        // Set the category in theme mods for the service section
        set_theme_mod('jewellery_shop_services_cat', $jewellery_shop_service_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $jewellery_shop_titles = array(
            '24 carat gold',
            'Min/Max Stock',
            'Cost savings',
            'safe stock inventory'
        );        
        // Create three demo posts and assign them to the 'service' category
        for ($jewellery_shop_i = 1; $jewellery_shop_i <= 4; $jewellery_shop_i++) {
            $jewellery_shop_title = $jewellery_shop_titles[$jewellery_shop_i - 1];
            $jewellery_shop_content = 'Designs so intricate seen before you have never seen before';
        
            // Prepare the post object
            $jewellery_shop_my_post = array(
                'post_title'    => wp_strip_all_tags($jewellery_shop_title),
                'post_content'  => $jewellery_shop_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($jewellery_shop_service_category_id),
            );
        
            // Insert the post into the database
            $jewellery_shop_post_id = wp_insert_post($jewellery_shop_my_post);
        
            // If the post was successfully created, set the featured image
            if ($jewellery_shop_post_id && !is_wp_error($jewellery_shop_post_id)) {
                // Set the image URL based on the current service index
                $jewellery_shop_image_url = esc_url(get_stylesheet_directory_uri() . '/images/jewellary-service' . $jewellery_shop_i . '.png');
                $jewellery_shop_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $jewellery_shop_response = wp_remote_get($jewellery_shop_image_url);
                if (!is_wp_error($jewellery_shop_response)) {
                    $jewellery_shop_image_data = wp_remote_retrieve_body($jewellery_shop_response);
        
                    if (!empty($jewellery_shop_image_data)) {
                        // Handle the file upload process
                        $jewellery_shop_image_name = 'service' . $jewellery_shop_i . '.png';
                        $jewellery_shop_unique_file_name = wp_unique_filename($jewellery_shop_upload_dir['path'], $jewellery_shop_image_name);
                        $jewellery_shop_file = $jewellery_shop_upload_dir['path'] . '/' . $jewellery_shop_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($jewellery_shop_file, $jewellery_shop_image_data);
        
                        // Check file type and prepare for attachment
                        $jewellery_shop_wp_filetype = wp_check_filetype($jewellery_shop_unique_file_name, null);
                        $jewellery_shop_attachment = array(
                            'post_mime_type' => $jewellery_shop_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($jewellery_shop_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $jewellery_shop_attach_id = wp_insert_attachment($jewellery_shop_attachment, $jewellery_shop_file, $jewellery_shop_post_id);
                        $jewellery_shop_attach_data = wp_generate_attachment_metadata($jewellery_shop_attach_id, $jewellery_shop_file);
                        wp_update_attachment_metadata($jewellery_shop_attach_id, $jewellery_shop_attach_data);
                        set_post_thumbnail($jewellery_shop_post_id, $jewellery_shop_attach_id);
                    }
                }
            }
        }
        

        // Show success message and the "View Site" button
        update_option('jewellery_shop_demo_import_completed', true);
        echo '<br>';
        echo '<div class="success">Demo Import Successful</div>';
        echo '<br>';
        echo '<hr>';
        echo '<br>';
        echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'jewellery-shop' ) . '</span>';
        echo '<br>';
    }
     ?>
    <ul>
        <li>
        <?php 
        // Check if the form is submitted
        if ( !isset( $_POST['submit'] ) ) : ?>
            <!-- Show demo importer form only if it's not submitted -->
            <?php if (!get_option('jewellery_shop_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'jewellery-shop' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'jewellery-shop' ); ?></b><br><br>
                <small><b><?php echo esc_html('Please take a backup if your website is already live with data. This importer will overwrite existing data.', 'jewellery-shop' ); ?></b></small><br><br>
                <form id="demo-importer-form" action="" method="POST" onsubmit="return runDemoImport();">
                    <input type="submit" name="submit" value="<?php echo esc_attr('Run Importer','jewellery-shop'); ?>" class="button button-primary button-large">
                </form>
                <script type="text/javascript">
                    function runDemoImport() {
                        if (confirm('Do you really want to do this?')) {
                            document.getElementById('demo-import-loader').style.display = 'block';
                            return true;
                        }
                        return false;
                    }
                </script>
             <?php endif; ?>
         <?php 
        endif; 

        // Show "View Site" button after form submission
        if ( isset( $_POST['submit'] ) ) {
        echo '<div class="view-site-btn">';
        echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
        echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
        echo '</div>';
        }
        ?>
        </li>
    </ul>
 </div>