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

        // If the demo import is completed, display the "View Site" button
        if ($astronomy_store_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.', 'astronomy-store' ) . '</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
                $astronomy_store_plugin_slug = 'classic-blog-grid';
                $astronomy_store_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $astronomy_store_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $astronomy_store_installed_plugins = get_plugins();
                    if ( ! isset( $astronomy_store_installed_plugins[ $astronomy_store_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
                        $astronomy_store_upgrader = new Plugin_Upgrader();
                        $astronomy_store_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( $astronomy_store_plugin_file );
                }
            }
        echo '</div>';
        // ------- Create Main Menu --------
        $astronomy_store_menuname = 'Primary Menu';
        $astronomy_store_bpmenulocation = 'primary';
        $astronomy_store_menu_exists = wp_get_nav_menu_object( $astronomy_store_menuname );
    
        if (!$astronomy_store_menu_exists) {
            // Create a new menu
            $astronomy_store_menu_id = wp_create_nav_menu($astronomy_store_menuname);

            // Define pages to be created
            $astronomy_store_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' => ''
                ),
            );

            $astronomy_store_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($astronomy_store_pages as $astronomy_store_slug => $astronomy_store_data) {
                $astronomy_store_existing_page = get_page_by_path($astronomy_store_slug);

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

                    $astronomy_store_page_id = wp_insert_post($astronomy_store_page_data);

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

                // Store the page IDs
                $astronomy_store_page_ids[$astronomy_store_slug] = $astronomy_store_page_id;
            }

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

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

            // Add menu items dynamically
            foreach ($astronomy_store_menu_items as $astronomy_store_slug) {
                wp_update_nav_menu_item($astronomy_store_menu_id, 0, array(
                    'menu-item-title' => esc_html($astronomy_store_pages[$astronomy_store_slug]['title']),
                    'menu-item-url' => get_permalink($astronomy_store_page_ids[$astronomy_store_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $astronomy_store_page_ids[$astronomy_store_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $astronomy_store_locations = get_theme_mod('nav_menu_locations', array());
            $astronomy_store_locations[$astronomy_store_bpmenulocation] = $astronomy_store_menu_id;
            set_theme_mod('nav_menu_locations', $astronomy_store_locations);
        }

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

        //Social Media Section
        set_theme_mod( 'astronomy_store_fb_link', 'https://www.facebook.com');
        set_theme_mod( 'astronomy_store_twitt_link', 'https://www.twitter.com');
        set_theme_mod( 'astronomy_store_pinterst_link', 'https://www.pinterst.com');
        set_theme_mod( 'astronomy_store_insta_link', 'https://www.instagram.com');

        //Slider Section
        set_theme_mod( 'astronomy_store_hide_categorysec', true);
        set_theme_mod( 'astronomy_store_button_text', 'Read More');
        set_theme_mod( 'astronomy_store_button2_text', 'Contact Us');
        set_theme_mod( 'astronomy_store_button2_url', '#');
        
        $astronomy_store_slider_category_id = wp_create_category('Slider');
        // Set the category in theme mods for the slider section
        set_theme_mod('astronomy_store_slidersection', $astronomy_store_slider_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $astronomy_store_titles = array(
            'ASTRONOMY FINGER POINTING AT REALITY',
            'EXPLORING THE MYSTERIES OF THE UNIVERSE',
            'TELESCOPE BUYING GUIDE FOR BEGINNERS'
        );                
        // Create three demo posts and assign them to the 'Slider' category
        for ($astronomy_store_i = 1; $astronomy_store_i <= 3; $astronomy_store_i++) {
            $astronomy_store_title = $astronomy_store_titles[$astronomy_store_i - 1];
            $astronomy_store_content = 'There are many variations of passages.';
        
            // Prepare the post object
            $astronomy_store_my_post = array(
                'post_title'    => wp_strip_all_tags($astronomy_store_title),
                'post_content'  => $astronomy_store_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($astronomy_store_slider_category_id),
            );
        
            // Insert the post into the database
            $astronomy_store_post_id = wp_insert_post($astronomy_store_my_post);
        
            // If the post was successfully created, set the featured image
            if ($astronomy_store_post_id && !is_wp_error($astronomy_store_post_id)) {
                // Set the image URL based on the current slider index
                $astronomy_store_image_url = esc_url(get_stylesheet_directory_uri() . '/images/slider' . $astronomy_store_i . '.png');
                $astronomy_store_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $astronomy_store_response = wp_remote_get($astronomy_store_image_url);
                if (!is_wp_error($astronomy_store_response)) {
                    $astronomy_store_image_data = wp_remote_retrieve_body($astronomy_store_response);
        
                    if (!empty($astronomy_store_image_data)) {
                        // Handle the file upload process
                        $astronomy_store_image_name = 'slider' . $astronomy_store_i . '.png';
                        $astronomy_store_unique_file_name = wp_unique_filename($astronomy_store_upload_dir['path'], $astronomy_store_image_name);
                        $astronomy_store_file = $astronomy_store_upload_dir['path'] . '/' . $astronomy_store_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($astronomy_store_file, $astronomy_store_image_data);
        
                        // Check file type and prepare for attachment
                        $astronomy_store_wp_filetype = wp_check_filetype($astronomy_store_unique_file_name, null);
                        $astronomy_store_attachment = array(
                            'post_mime_type' => $astronomy_store_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($astronomy_store_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $astronomy_store_attach_id = wp_insert_attachment($astronomy_store_attachment, $astronomy_store_file, $astronomy_store_post_id);
                        $astronomy_store_attach_data = wp_generate_attachment_metadata($astronomy_store_attach_id, $astronomy_store_file);
                        wp_update_attachment_metadata($astronomy_store_attach_id, $astronomy_store_attach_data);
                        set_post_thumbnail($astronomy_store_post_id, $astronomy_store_attach_id);
                    }
                }
            }
        }

        //Services Section
        set_theme_mod( 'astronomy_store_disabled_pgboxes', true);
        set_theme_mod( 'astronomy_store_service_heading', 'Zodiac Symbols');
        set_theme_mod( 'astronomy_store_service_subheading', 'EACH ZODIAC SYMBOL SAY SOMETHING');

        $astronomy_store_service_category_id = wp_create_category('Services');
        // Set the category in theme mods for the service section
        set_theme_mod('astronomy_store_services_cat', $astronomy_store_service_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $astronomy_store_titles = array(
            'ARIES',
            'TAURUS',
            'GEMINI',
            'CANCER',
            'LEO',
            'VIRGO'
        );        
        // Create three demo posts and assign them to the 'service' category
        for ($astronomy_store_i = 1; $astronomy_store_i <= 6; $astronomy_store_i++) {
            $astronomy_store_title = $astronomy_store_titles[$astronomy_store_i - 1];
            $astronomy_store_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.';
        
            // Prepare the post object
            $astronomy_store_my_post = array(
                'post_title'    => wp_strip_all_tags($astronomy_store_title),
                'post_content'  => $astronomy_store_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($astronomy_store_service_category_id),
            );
        
            // Insert the post into the database
            $astronomy_store_post_id = wp_insert_post($astronomy_store_my_post);
        
            // If the post was successfully created, set the featured image
            if ($astronomy_store_post_id && !is_wp_error($astronomy_store_post_id)) {
                // Set the image URL based on the current service index
                $astronomy_store_image_url = esc_url(get_stylesheet_directory_uri() . '/images/service-' . $astronomy_store_i . '.png');
                $astronomy_store_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $astronomy_store_response = wp_remote_get($astronomy_store_image_url);
                if (!is_wp_error($astronomy_store_response)) {
                    $astronomy_store_image_data = wp_remote_retrieve_body($astronomy_store_response);
        
                    if (!empty($astronomy_store_image_data)) {
                        // Handle the file upload process
                        $astronomy_store_image_name = 'service' . $astronomy_store_i . '.png';
                        $astronomy_store_unique_file_name = wp_unique_filename($astronomy_store_upload_dir['path'], $astronomy_store_image_name);
                        $astronomy_store_file = $astronomy_store_upload_dir['path'] . '/' . $astronomy_store_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($astronomy_store_file, $astronomy_store_image_data);
        
                        // Check file type and prepare for attachment
                        $astronomy_store_wp_filetype = wp_check_filetype($astronomy_store_unique_file_name, null);
                        $astronomy_store_attachment = array(
                            'post_mime_type' => $astronomy_store_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($astronomy_store_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $astronomy_store_attach_id = wp_insert_attachment($astronomy_store_attachment, $astronomy_store_file, $astronomy_store_post_id);
                        $astronomy_store_attach_data = wp_generate_attachment_metadata($astronomy_store_attach_id, $astronomy_store_file);
                        wp_update_attachment_metadata($astronomy_store_attach_id, $astronomy_store_attach_data);
                        set_post_thumbnail($astronomy_store_post_id, $astronomy_store_attach_id);
                    }
                }
            }
        }

        // Show success message and the "View Site" button
        update_option('astronomy_store_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.', 'astronomy-store' ) . '</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('astronomy_store_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'astronomy-store' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'astronomy-store' ); ?></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.', 'astronomy-store' ); ?></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','astronomy-store'); ?>" 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>