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

        // If the demo import is completed, display the "View Site" button
        if ($classic_gaming_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-gaming' ) . '</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_gaming_plugin_slug = 'woocommerce';
                    $classic_gaming_plugin_file = 'woocommerce/woocommerce.php';

                    // Check if plugin is installed
                    $classic_gaming_installed_plugins = get_plugins();
                    if (!isset($classic_gaming_installed_plugins[$classic_gaming_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_gaming_upgrader = new Plugin_Upgrader();
                        $classic_gaming_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                    }
                    // Activate the plugin
                    activate_plugin($classic_gaming_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_gaming_plugin_slug = 'classic-blog-grid';
                    $classic_gaming_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
                
                    // Check if Classic Blog Grid is installed and activated
                    if ( ! is_plugin_active( $classic_gaming_plugin_file ) ) {
                
                        // Check if Classic Blog Grid is installed
                        $classic_gaming_installed_plugins = get_plugins();
                        if ( ! isset( $classic_gaming_installed_plugins[ $classic_gaming_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_gaming_upgrader = new Plugin_Upgrader();
                            $classic_gaming_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_gaming_plugin_file );
                    }
                }
        echo '</div>';
        // ------- Create Main Menu --------
        $classic_gaming_menuname = 'Primary Menu'; 
        $classic_gaming_bpmenulocation = 'primary';
        $classic_gaming_menu_exists = wp_get_nav_menu_object($classic_gaming_menuname);

        if (!$classic_gaming_menu_exists) {
            // Create a new menu
            $classic_gaming_menu_id = wp_create_nav_menu($classic_gaming_menuname);

            // Define pages to be created
            $classic_gaming_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'shop' => array(
                    'title' => 'Shop',
                    'content' => '[products]' // Shortcode for products
                ),
                'blog' => array(
                    'title' => 'Blog',
                    'content' => ''
                ),
                '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>'
                ),
            );

            $classic_gaming_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($classic_gaming_pages as $classic_gaming_slug => $classic_gaming_data) {
                $classic_gaming_existing_page = get_page_by_path($classic_gaming_slug);

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

                    $classic_gaming_page_id = wp_insert_post($classic_gaming_page_data);

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

                // Store the page IDs
                $classic_gaming_page_ids[$classic_gaming_slug] = $classic_gaming_page_id;
            }

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

            // Define menu items
            $classic_gaming_menu_items = array(
                'home',
                'blog',
                'shop',
                'about-us',
                'pages'
            );

            // Add menu items dynamically
            foreach ($classic_gaming_menu_items as $classic_gaming_slug) {
                wp_update_nav_menu_item($classic_gaming_menu_id, 0, array(
                    'menu-item-title' => esc_html($classic_gaming_pages[$classic_gaming_slug]['title']),
                    'menu-item-url' => get_permalink($classic_gaming_page_ids[$classic_gaming_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $classic_gaming_page_ids[$classic_gaming_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $classic_gaming_locations = get_theme_mod('nav_menu_locations', array());
            $classic_gaming_locations[$classic_gaming_bpmenulocation] = $classic_gaming_menu_id;
            set_theme_mod('nav_menu_locations', $classic_gaming_locations);
        }

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

        //Header Section
        set_theme_mod( 'classic_gaming_header_btn_text', 'CONTACT US');
        set_theme_mod( 'classic_gaming_header_btn_url', '#');

        //Banner Section
        set_theme_mod( 'classic_gaming_banner_small_title', 'GET 20% OFF ON SELECTED ITEMS  |  SHOP NOW');
        set_theme_mod( 'classic_gaming_button_text', 'READ MORE');
        set_theme_mod( 'classic_gaming_button_link_banner', '#');
        set_theme_mod( 'classic_gaming_banner_email_address', 'contact@example.com');
        set_theme_mod( 'classic_gaming_fb_link', '#');
        set_theme_mod( 'classic_gaming_youtube_link', '#');
        set_theme_mod( 'classic_gaming_twitter_link', '#');
        set_theme_mod( 'classic_gaming_steam_link', '#');
        set_theme_mod( 'classic_gaming_behance_link', '#');
        set_theme_mod( 'classic_gaming_banner_bg_img', esc_url( get_template_directory_uri().'/images/Banner.png'));
        set_theme_mod('classic_gaming_posts_number', '2');
          
        $classic_gaming_slider_title = 'Level Up Your Gaming Experience with Endless Adventures!';
        $classic_gaming_slider_content = '<p>Discover the ultimate gaming destination! Dive into thrilling adventures, connect with gamers worldwide, and level up your skills for endless excitement and victories."</p>';
        
        // Create post object
        $classic_gaming_my_post = array(
            'post_title'   => wp_strip_all_tags($classic_gaming_slider_title),
            'post_content' => $classic_gaming_slider_content,
            'post_status'  => 'publish',
            'post_type'    => 'page',
        );
        
        // Insert the post into the database
        $classic_gaming_post_id = wp_insert_post($classic_gaming_my_post);
        
        if ($classic_gaming_post_id) {
            // Set the theme mod for the slider page
            set_theme_mod('classic_gaming_banner_pageboxes', $classic_gaming_post_id);
        
            // Include necessary WordPress files
            require_once(ABSPATH . 'wp-admin/includes/media.php');
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/image.php');
        
            // Define the image URL
            $classic_gaming_image_url = get_template_directory_uri() . '/images/banner.png';
        
            // Download and attach the image to the post
            $classic_gaming_image_id = media_sideload_image($classic_gaming_image_url, $classic_gaming_post_id, null, 'id');
        
            if (!is_wp_error($classic_gaming_image_id)) {
                // Set the downloaded image as the post's featured image
                set_post_thumbnail($classic_gaming_post_id, $classic_gaming_image_id);
            }
        }
        
        // Create demo posts with different titles and get their IDs
        $classic_gaming_demo_posts = array(
            array('title' => 'Assassin’s Creed', 'image' => 'images/post1.png'),
            array('title' => 'Ghost Of Tsushima', 'image' => 'images/post2.png')
        );
        
        $classic_gaming_post_ids = array();
        
        foreach ($classic_gaming_demo_posts as $classic_gaming_index => $classic_gaming_post) {
            $classic_gaming_current_date = current_time('Y-m-d H:i:s');
        
            $classic_gaming_post_id = wp_insert_post(array(
                'post_title'   => $classic_gaming_post['title'],
                'post_content' => 'Assassin\'s Creed Valhalla is a 2020 action role-playing game developed by Ubisoft Montreal and published by Ubisoft. ' . $classic_gaming_post['title'] . '. Initial Release Date: ' . $classic_gaming_current_date . '.',
                'post_status'  => 'publish',
                'post_type'    => 'post',
                'post_date'    => $classic_gaming_current_date,
                'post_date_gmt'=> get_gmt_from_date($classic_gaming_current_date)
            ));
        
            if ($classic_gaming_post_id) {
                // Upload and set the post thumbnail
                $classic_gaming_image_path = get_template_directory() . '/' . $classic_gaming_post['image'];
        
                if (file_exists($classic_gaming_image_path)) {
                    $classic_gaming_upload_file = wp_upload_bits(basename($classic_gaming_image_path), null, file_get_contents($classic_gaming_image_path));
        
                    if (!$classic_gaming_upload_file['error']) {
                        $classic_gaming_wp_filetype = wp_check_filetype($classic_gaming_upload_file['file'], null);
                        $classic_gaming_attachment = array(
                            'post_mime_type' => $classic_gaming_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($classic_gaming_upload_file['file']),
                            'post_content'   => '',
                            'post_status'    => 'inherit'
                        );
        
                        $classic_gaming_attach_id = wp_insert_attachment($classic_gaming_attachment, $classic_gaming_upload_file['file'], $classic_gaming_post_id);
                        require_once(ABSPATH . 'wp-admin/includes/image.php');
                        $classic_gaming_attach_data = wp_generate_attachment_metadata($classic_gaming_attach_id, $classic_gaming_upload_file['file']);
                        wp_update_attachment_metadata($classic_gaming_attach_id, $classic_gaming_attach_data);
        
                        // Set post thumbnail properly
                        set_post_thumbnail($classic_gaming_post_id, $classic_gaming_attach_id);
                    }
                }
        
                $classic_gaming_post_ids[] = $classic_gaming_post_id;
            }
        }
        
        // Assign the demo posts to the customizer settings
        foreach ($classic_gaming_post_ids as $classic_gaming_index => $classic_gaming_post_id) {
            $classic_gaming_i = $classic_gaming_index + 1;
            set_theme_mod('classic_gaming_select_post' . $classic_gaming_i, $classic_gaming_post_id);
            set_theme_mod('classic_gaming_add_steam_count' . $classic_gaming_i, '7/10');
            set_theme_mod('classic_gaming_steam_icon' . $classic_gaming_i, 'fa-brands fa-steam');
            set_theme_mod('classic_gaming_add_score' . $classic_gaming_i, '4.5/5');
            set_theme_mod('classic_gaming_score_icon' . $classic_gaming_i, 'fa-brands fa-playstation');
        }

        //Product Section
        set_theme_mod( 'classic_gaming_product_title', 'Our Most Thrilling And Iconic Game Line-Up!');
        set_theme_mod( 'classic_gaming_product_text', 'Discover the Latest Games and Elevate Your Play With Unique and Thrilling Titles!');
        // Set the theme mod for the product category
        set_theme_mod('classic_gaming_hot_products_cat', 'productcategory1');

        // Define the single product category name, product titles, and tags
        $classic_gaming_category_name = 'productcategory1';
        $classic_gaming_titles = array(
            "Product Name Here 1",
            "Product Name Here 2",
            "Product Name Here 3",
            "Product Name Here 4"
        );

        $classic_gaming_regular_price = 1300; 
        $classic_gaming_sale_price = 1200; 

        // Create or retrieve the product category term ID
        $classic_gaming_term = term_exists($classic_gaming_category_name, 'product_cat');
        if (!$classic_gaming_term) {
            $classic_gaming_term = wp_insert_term($classic_gaming_category_name, 'product_cat');
        }

        if (is_wp_error($classic_gaming_term)) {
            error_log('Error creating category: ' . $classic_gaming_term->get_error_message());
            return; // Exit if category creation fails
        }

        $classic_gaming_term_id = is_array($classic_gaming_term) ? $classic_gaming_term['term_id'] : $classic_gaming_term;

        // Loop to create 4 products for the category
        foreach ($classic_gaming_titles as $classic_gaming_index => $classic_gaming_title) {

            // Create product content
            $classic_gaming_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

            // Create product post object
            $classic_gaming_post_id = wp_insert_post(array(
                'post_title'    => wp_strip_all_tags($classic_gaming_title),
                'post_content'  => $classic_gaming_content,
                'post_status'   => 'publish',
                'post_type'     => 'product', // Post type set to 'product'
            ));

            if (is_wp_error($classic_gaming_post_id)) {
                error_log('Error creating product: ' . $classic_gaming_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_gaming_post_id, $classic_gaming_term_id, 'product_cat');

            // Set product prices
            update_post_meta($classic_gaming_post_id, '_regular_price', $classic_gaming_regular_price);
            update_post_meta($classic_gaming_post_id, '_sale_price', $classic_gaming_sale_price);
            update_post_meta($classic_gaming_post_id, '_price', $classic_gaming_sale_price); // Current price is the sale price

            // Handle the featured image using media_sideload_image
            $classic_gaming_image_url = get_template_directory_uri() . '/images/Product' . ($classic_gaming_index + 1) . '.png';
            $classic_gaming_image_id = media_sideload_image($classic_gaming_image_url, $classic_gaming_post_id, null, 'id');

            if (!is_wp_error($classic_gaming_image_id)) {
                // Assign featured image to product
                set_post_thumbnail($classic_gaming_post_id, $classic_gaming_image_id);
            } else {
                error_log('Error downloading image for product: ' . $classic_gaming_image_id->get_error_message());
            }
        }    

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