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

        // If the demo import is completed, display the "View Site" button
        if ($classic_fashion_blog_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.', 'classic-fashion-blog' ) . '</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 woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $classic_fashion_blog_plugin_slug = 'woocommerce';
                $classic_fashion_blog_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $classic_fashion_blog_installed_plugins = get_plugins();
                if (!isset($classic_fashion_blog_installed_plugins[$classic_fashion_blog_plugin_file])) {
                    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');

                    // Install the plugin
                    $classic_fashion_blog_upgrader = new Plugin_Upgrader();
                    $classic_fashion_blog_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($classic_fashion_blog_plugin_file);
            }

            // 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
                $classic_fashion_blog_plugin_slug = 'classic-blog-grid';
                $classic_fashion_blog_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_fashion_blog_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_fashion_blog_installed_plugins = get_plugins();
                    if ( ! isset( $classic_fashion_blog_installed_plugins[ $classic_fashion_blog_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
                        $classic_fashion_blog_upgrader = new Plugin_Upgrader();
                        $classic_fashion_blog_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( $classic_fashion_blog_plugin_file );
                }
            }

        echo '</div>';

        // ------- Create Main Menu --------
        $classic_fashion_blog_menuname = 'Primary Menu';
        $classic_fashion_blog_bpmenulocation = 'primary';
        $classic_fashion_blog_menu_exists = wp_get_nav_menu_object( $classic_fashion_blog_menuname );
    
        if ( !$classic_fashion_blog_menu_exists ) {
            $classic_fashion_blog_menu_id = wp_create_nav_menu( $classic_fashion_blog_menuname );

            // Create Home Page
            $classic_fashion_blog_home_title = 'Home';
            $classic_fashion_blog_home = array(
                'post_type'    => 'page',
                'post_title'   => $classic_fashion_blog_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $classic_fashion_blog_home_id = wp_insert_post($classic_fashion_blog_home);
            // Assign Home Page Template
            add_post_meta($classic_fashion_blog_home_id, '_wp_page_template', 'templates/template-home-page.php');
            // Update options to set Home Page as the front page
            update_option('page_on_front', $classic_fashion_blog_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($classic_fashion_blog_menu_id, 0, array(
                'menu-item-title' => __('Home', 'classic-fashion-blog'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_fashion_blog_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $classic_fashion_blog_about_title = 'About Us';
            $classic_fashion_blog_about_content = '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.';
            $classic_fashion_blog_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_fashion_blog_about_title,
                'post_content' => $classic_fashion_blog_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $classic_fashion_blog_about_id = wp_insert_post($classic_fashion_blog_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($classic_fashion_blog_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'classic-fashion-blog'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_fashion_blog_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Blogs Page with Dummy Content
            $classic_fashion_blog_about_title = 'Blogs';
            $classic_fashion_blog_about_content = '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...';

            $classic_fashion_blog_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_fashion_blog_about_title,
                'post_content' => $classic_fashion_blog_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blogs' 
            );

            $classic_fashion_blog_about_id = wp_insert_post($classic_fashion_blog_about);

            // Add Blogs Page to Menu
            wp_update_nav_menu_item( $classic_fashion_blog_menu_id, 0, array(
                'menu-item-title'     => __( 'Blogs', 'classic-fashion-blog' ),
                'menu-item-classes'  => 'blogs',
                'menu-item-object-id'=> $classic_fashion_blog_about_id,
                'menu-item-object'   => 'page',
                'menu-item-type'     => 'post_type',
                'menu-item-status'   => 'publish'
            ));

            update_option( 'page_for_posts', $classic_fashion_blog_about_id );

            // Create a new Page 
            $classic_fashion_blog_pages_title = 'Categories';
            $classic_fashion_blog_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>';
            $classic_fashion_blog_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_fashion_blog_pages_title,
                'post_content' => $classic_fashion_blog_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'categories'
            );
            $classic_fashion_blog_pages_id = wp_insert_post($classic_fashion_blog_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_fashion_blog_menu_id, 0, array(
                'menu-item-title' => __('Categories', 'classic-fashion-blog'),
                'menu-item-classes' => 'categories',
                'menu-item-url' => home_url('/categories/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_fashion_blog_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $classic_fashion_blog_pages_title = 'Membership';
            $classic_fashion_blog_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>';
            $classic_fashion_blog_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_fashion_blog_pages_title,
                'post_content' => $classic_fashion_blog_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'membership'
            );
            $classic_fashion_blog_pages_id = wp_insert_post($classic_fashion_blog_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_fashion_blog_menu_id, 0, array(
                'menu-item-title' => __('Membership', 'classic-fashion-blog'),
                'menu-item-classes' => 'membership',
                'menu-item-url' => home_url('/membership/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_fashion_blog_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign the menu to the primary location if not already set
            if ( ! has_nav_menu( $classic_fashion_blog_bpmenulocation ) ) {
                $classic_fashion_blog_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $classic_fashion_blog_locations ) ) {
                    $classic_fashion_blog_locations = array();
                }
                $classic_fashion_blog_locations[ $classic_fashion_blog_bpmenulocation ] = $classic_fashion_blog_menu_id;
                set_theme_mod( 'nav_menu_locations', $classic_fashion_blog_locations );
            }
        }

        //Logo
        set_theme_mod( 'classic_fashion_blog_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));
       
        //Sidebar Section
        set_theme_mod( 'classic_fashion_blog_sidebar_text','Fashion Blog');
        set_theme_mod( 'classic_fashion_blog_facebook_link','#');
        set_theme_mod( 'classic_fashion_blog_twitter_link','#');
        set_theme_mod( 'classic_fashion_blog_instagram_link','#');
        set_theme_mod( 'classic_fashion_blog_youtube_link','#');

        //Banner Section
        set_theme_mod( 'classic_fashion_blog_banner_small_title','Vogue Vibes');
        set_theme_mod( 'classic_fashion_blog_button_text','Explore more');
        set_theme_mod( 'classic_fashion_blog_button_link_banner','#');
        set_theme_mod( 'classic_fashion_blog_banner_side_content','Inspiring Fashion Stories, Fresh Ideas, and Bold Style Thoughts');
        set_theme_mod( 'classic_fashion_blog_banner_alphabet','V');

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $classic_fashion_blog_page_title, $classic_fashion_blog_page_content = '' ) {
            $classic_fashion_blog_args = array(
                'post_type'   => 'page',
                'title'       => $classic_fashion_blog_page_title,
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'fields'      => 'ids'
            );
            $classic_fashion_blog_query = new WP_Query( $classic_fashion_blog_args );

            if ( ! empty( $classic_fashion_blog_query->posts ) ) {
                return $classic_fashion_blog_query->posts[0];
            } else {
                // Create the page if it doesn't exist
                $classic_fashion_blog_page_id = wp_insert_post( array(
                    'post_type'   => 'page',
                    'post_title'  => $classic_fashion_blog_page_title,
                    'post_content'=> $classic_fashion_blog_page_content,
                    'post_status' => 'publish',
                    'post_author' => 1
                ));
                return $classic_fashion_blog_page_id;
            }
        }

        // Upload an image and set it as a featured image for a page
        function upload_service_image( $classic_fashion_blog_image_url, $classic_fashion_blog_post_id ) {
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
            require_once( ABSPATH . 'wp-admin/includes/media.php' );
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Upload image from URL
            $classic_fashion_blog_image_id = media_sideload_image( $classic_fashion_blog_image_url, $classic_fashion_blog_post_id, null, 'id' );

            if ( !is_wp_error( $classic_fashion_blog_image_id ) ) {
                // Set the uploaded image as the featured image for the page
                set_post_thumbnail( $classic_fashion_blog_post_id, $classic_fashion_blog_image_id );
                // Optionally, set the image as a theme mod
                set_theme_mod( 'classic_fashion_blog_about_section_image', $classic_fashion_blog_image_id );
                error_log('Image uploaded and set as theme mod and post thumbnail successfully.');
            } else {
                error_log('Failed to upload image: ' . $classic_fashion_blog_image_url . ' - Error: ' . $classic_fashion_blog_image_id->get_error_message() );
            }
        }

        // Get the absolute URL for theme images
        function get_theme_image_url( $classic_fashion_blog_relative_path ) {
            return get_template_directory_uri() . $classic_fashion_blog_relative_path;
        }

        // Create Page
        $classic_fashion_blog_page_title = 'Discover Today’s Trends and Timeless Style Inspirations';
        $classic_fashion_blog_page_content = 'VogueVibes brings you the latest fashion trends, style inspirations, and creative stories that shape the world of fashion. From runway reports to sustainable style tips, explore a world where your fashion ideas come to life.';
        $classic_fashion_blog_page_id = get_or_create_page_by_title( $classic_fashion_blog_page_title, $classic_fashion_blog_page_content );

        // Set theme mod and upload image
        if ( $classic_fashion_blog_page_id ) {
            set_theme_mod( 'classic_fashion_blog_banner_pageboxes', $classic_fashion_blog_page_id );
            // Upload an image for this page (make sure the path is correct)
            $classic_fashion_blog_image_url = get_theme_image_url( '/images/banner-img.png' );
            upload_service_image( $classic_fashion_blog_image_url, $classic_fashion_blog_page_id );
        } else {
            error_log('Failed to create or fetch the "Discover Today’s Trends and Timeless Style Inspirations" page.');
        }

        // Create the 'Fashion' category and retrieve its ID
        $classic_fashion_blog_banner_pageboxes = wp_create_category('Fashion');
    
        // Set the slider category in theme mods
        set_theme_mod('classic_fashion_blog_trending_cat', 'Fashion');

        $classic_fashion_blog_titles = array(
            'The Revival of Y2K Fashion',
            'An Eco-Friendly Wardrobe'
        );
        $classic_fashion_blog_contents = array(
            'Dive into the resurgence of Y2K fashion, exploring why early 2000s styles',
            'Learn how to curate a sustainable wardrobe with eco-friendly brands'
        );
        // Create three posts and assign them to the 'Fashion' category
        // Define the image URLs
        for ($classic_fashion_blog_i = 1; $classic_fashion_blog_i <= 2; $classic_fashion_blog_i++) {
            $classic_fashion_blog_title = $classic_fashion_blog_titles[$classic_fashion_blog_i - 1];           
            $classic_fashion_blog_content = $classic_fashion_blog_contents[$classic_fashion_blog_i - 1];    
            
            // Create the post object
            $classic_fashion_blog_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_fashion_blog_title),
                'post_content'  => $classic_fashion_blog_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_fashion_blog_banner_pageboxes),  // Use the category ID
            );

            // Insert the post into the database
            $classic_fashion_blog_post_id = wp_insert_post( $classic_fashion_blog_my_post );
 
            if ($classic_fashion_blog_post_id) {
              // Set the theme mod for the slider page
              set_theme_mod('classic_fashion_blog_trending_cat' . $classic_fashion_blog_i, $classic_fashion_blog_post_id);

               $classic_fashion_blog_image_url = get_template_directory_uri().'/images/fashion-post'.$classic_fashion_blog_i.'.png';

             $classic_fashion_blog_image_id = media_sideload_image($classic_fashion_blog_image_url, $classic_fashion_blog_post_id, null, 'id');

                 if (!is_wp_error($classic_fashion_blog_image_id)) {
                     // Set the downloaded image as the post's featured image
                     set_post_thumbnail($classic_fashion_blog_post_id, $classic_fashion_blog_image_id);
                 }
            }
        }
              
        //Products Section
        set_theme_mod( 'classic_fashion_blog_product_sec_text','Fashion Lookbook');
        set_theme_mod( 'classic_fashion_blog_product_sec_title','Curated Styles for Inspiration');
        set_theme_mod( 'classic_fashion_blog_hot_products_cat', 'productcategory1');

        // Define the single product category name, product titles, and tags
        $classic_fashion_blog_category_name = 'productcategory1';
        $classic_fashion_blog_titles = array(
            "Style Stories Collection",
            "Style Stories Collection",
            "Style Stories Collection"
        );
        
        // Create or retrieve the product category term ID
        $classic_fashion_blog_term = term_exists($classic_fashion_blog_category_name, 'product_cat');
        if (!$classic_fashion_blog_term) {
            $classic_fashion_blog_term = wp_insert_term($classic_fashion_blog_category_name, 'product_cat');
        }
        
        if (is_wp_error($classic_fashion_blog_term)) {
            error_log('Error creating category: ' . $classic_fashion_blog_term->get_error_message());
            return; // Exit if category creation fails
        }
        
        $classic_fashion_blog_term_id = is_array($classic_fashion_blog_term) ? $classic_fashion_blog_term['term_id'] : $classic_fashion_blog_term;
        
        // Loop to create 4 products for the category
        foreach ($classic_fashion_blog_titles as $classic_fashion_blog_index => $classic_fashion_blog_title) {
        
            // Create product content
            $classic_fashion_blog_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.';
        
            // Create product post object
            $classic_fashion_blog_post_id = wp_insert_post(array(
                'post_title'    => wp_strip_all_tags($classic_fashion_blog_title),
                'post_content'  => $classic_fashion_blog_content,
                'post_status'   => 'publish',
                'post_type'     => 'product', // Post type set to 'product'
            ));
        
            if (is_wp_error($classic_fashion_blog_post_id)) {
                error_log('Error creating product: ' . $classic_fashion_blog_post_id->get_error_message());
                continue; // Skip to the next product if creation fails
            }
        
            // Assign the category to the product
            wp_set_object_terms($classic_fashion_blog_post_id, $classic_fashion_blog_term_id, 'product_cat');
        
            // Set product prices
            update_post_meta($classic_fashion_blog_post_id, '_regular_price', 29.99);
            update_post_meta($classic_fashion_blog_post_id, '_price', 29.99); // Current price is the sale price
        
            // Handle the featured image using media_sideload_image
            $classic_fashion_blog_image_url = get_template_directory_uri() . '/images/product' . ($classic_fashion_blog_index + 1) . '.png';
            $classic_fashion_blog_image_id = media_sideload_image($classic_fashion_blog_image_url, $classic_fashion_blog_post_id, null, 'id');
        
            if (!is_wp_error($classic_fashion_blog_image_id)) {
                // Assign featured image to product
                set_post_thumbnail($classic_fashion_blog_post_id, $classic_fashion_blog_image_id);
            } else {
                error_log('Error downloading image for product: ' . $classic_fashion_blog_image_id->get_error_message());
            }
        } 

    //Footer Copyright Text
    set_theme_mod( 'classic_fashion_blog_copyright_line', 'Classic Fashion Blog WordPress Theme' );

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