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

        // If the demo import is completed, display the "View Site" button
        if ($mobile_accessories_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.', 'mobile-accessories-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>';
        }
     // POST and update the customizer and other related data of Mobile Accessories Shop
    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
                $mobile_accessories_shop_plugin_slug = 'woocommerce';
                $mobile_accessories_shop_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $mobile_accessories_shop_installed_plugins = get_plugins();
                if (!isset($mobile_accessories_shop_installed_plugins[$mobile_accessories_shop_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
                    $mobile_accessories_shop_upgrader = new Plugin_Upgrader();
                    $mobile_accessories_shop_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($mobile_accessories_shop_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
                $mobile_accessories_shop_plugin_slug = 'classic-blog-grid';
                $mobile_accessories_shop_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $mobile_accessories_shop_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $mobile_accessories_shop_installed_plugins = get_plugins();
                    if ( ! isset( $mobile_accessories_shop_installed_plugins[ $mobile_accessories_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
                        $mobile_accessories_shop_upgrader = new Plugin_Upgrader();
                        $mobile_accessories_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( $mobile_accessories_shop_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $mobile_accessories_shop_menuname = 'Primary Menu'; 
        $mobile_accessories_shop_bpmenulocation = 'primary';
        $mobile_accessories_shop_menu_exists = wp_get_nav_menu_object($mobile_accessories_shop_menuname);

        if (!$mobile_accessories_shop_menu_exists) {
            // Create a new menu
            $mobile_accessories_shop_menu_id = wp_create_nav_menu($mobile_accessories_shop_menuname);

            // Define pages to be created
            $mobile_accessories_shop_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'categories' => array(
                    'title' => 'Categories',
                    '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>'
                ),
                'shop' => array(
                    'title' => 'Shop',
                    'content' => '[products]' 
                ),
                'blogs' => array(
                    'title' => 'Blogs',
                    '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>'
                ),
                'best-sellers' => array(
                    'title' => 'Best Sellers',
                    '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>'
                ),
                'new-arrivals' => array(
                    'title' => 'New Arrivals',
                    '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>'
                ),
                'contact' => array(
                    'title' => 'Contact',
                    'content' => ''
                ),
            );

            $mobile_accessories_shop_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($mobile_accessories_shop_pages as $mobile_accessories_shop_slug => $mobile_accessories_shop_data) {
                $mobile_accessories_shop_existing_page = get_page_by_path($mobile_accessories_shop_slug);

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

                    $mobile_accessories_shop_page_id = wp_insert_post($mobile_accessories_shop_page_data);

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

                // Store the page IDs
                $mobile_accessories_shop_page_ids[$mobile_accessories_shop_slug] = $mobile_accessories_shop_page_id;
            }

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

            // Define menu items
            $mobile_accessories_shop_menu_items = array(
                'home',
                'categories',
                'shop',
                'blogs',
                'best-sellers',
                'new-arrivals',
                'contact',
            );

            // Add menu items dynamically
            foreach ($mobile_accessories_shop_menu_items as $mobile_accessories_shop_slug) {
                wp_update_nav_menu_item($mobile_accessories_shop_menu_id, 0, array(
                    'menu-item-title' => esc_html($mobile_accessories_shop_pages[$mobile_accessories_shop_slug]['title']),
                    'menu-item-url' => get_permalink($mobile_accessories_shop_page_ids[$mobile_accessories_shop_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $mobile_accessories_shop_page_ids[$mobile_accessories_shop_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $mobile_accessories_shop_locations = get_theme_mod('nav_menu_locations', array());
            $mobile_accessories_shop_locations[$mobile_accessories_shop_bpmenulocation] = $mobile_accessories_shop_menu_id;
            set_theme_mod('nav_menu_locations', $mobile_accessories_shop_locations);
        }

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

        //Header Section
        set_theme_mod('mobile_accessories_shop_enable_topbar', true);        
        set_theme_mod('mobile_accessories_shop_topbar_text', 'Free Shipping on Orders Over $50!');
        set_theme_mod('mobile_accessories_shop_email_address', 'mobileshop@example.com');    
        set_theme_mod('mobile_accessories_shop_phone_number', '+1234567890'); 

        //Banner Section
        set_theme_mod('mobile_accessories_shop_banner', true);        
        set_theme_mod('mobile_accessories_shop_banner_title', 'Upgrade Your Mobile Experience');
        set_theme_mod('mobile_accessories_shop_banner_description', '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.1960s with the release of Letraset sheets containing Lorem Ipsum passages,');    
        set_theme_mod('mobile_accessories_shop_banner_btn_text', 'Shop Now'); 
        set_theme_mod('mobile_accessories_shop_facebook_url', 'https://www.facebook.com'); 
        set_theme_mod('mobile_accessories_shop_instagram_url', 'https://www.instagram.com'); 
        set_theme_mod('mobile_accessories_shop_twitter_url', 'https://www.twitter.com');
        set_theme_mod('mobile_accessories_shop_youtube_url', 'https://www.youtube.com'); 
        set_theme_mod('mobile_accessories_shop_pinterest_url', 'https://www.pinterest.com'); 
        set_theme_mod( 'mobile_accessories_shop_banner_side_img', esc_url( get_template_directory_uri().'/images/banner.png'));

        $mobile_accessories_shop_products = array(
            array(
                "title" => "Best Deals & Discounts",
                "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.",
                "price" => "2500.00",
                "regular_price" => "3000.00",
            ),
            array(
                "title" => "Hot Offer on Power Laptop",
                "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.",
                "price" => "22000.00",
                "regular_price" => "27000.00",
            )
        );
        
        $mobile_accessories_shop_counter = 1;
        foreach ($mobile_accessories_shop_products as $mobile_accessories_shop_product_data) {
            // Skip if title is empty
            if (empty($mobile_accessories_shop_product_data["title"])) continue;
        
            // Create the product post
            $mobile_accessories_shop_post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($mobile_accessories_shop_product_data["title"]),
                'post_content' => $mobile_accessories_shop_product_data["content"],
                'post_status'  => 'publish',
                'post_type'    => 'product',
            ));
        
            // Skip on error
            if (is_wp_error($mobile_accessories_shop_post_id)) continue;
        
            // Set product meta
            update_post_meta($mobile_accessories_shop_post_id, '_price', $mobile_accessories_shop_product_data["price"]);
            update_post_meta($mobile_accessories_shop_post_id, '_regular_price', $mobile_accessories_shop_product_data["regular_price"]);
            update_post_meta($mobile_accessories_shop_post_id, '_product_type', 'simple');
        
            // Handle the featured image: use product-img1.png, product-img2.png, etc.
            $mobile_accessories_shop_image_url = get_template_directory_uri() . '/images/product-img' . $mobile_accessories_shop_counter . '.png';
            $mobile_accessories_shop_image_id = media_sideload_image($mobile_accessories_shop_image_url, $mobile_accessories_shop_post_id, null, 'id');
        
            if (!is_wp_error($mobile_accessories_shop_image_id)) {
                set_post_thumbnail($mobile_accessories_shop_post_id, $mobile_accessories_shop_image_id);
            }
        
            // Set the theme mod only for the first product
            if ($mobile_accessories_shop_counter === 1) {
                set_theme_mod('mobile_accessories_shop_slider_product', $mobile_accessories_shop_post_id);
            }
        
            $mobile_accessories_shop_counter++;
        }
        
        // Best Seller Section
        set_theme_mod('mobile_accessories_shop_disabled_best_seller_section', true);        
        set_theme_mod('mobile_accessories_shop_best_seller_title', 'Best Sellers');
        set_theme_mod('mobile_accessories_shop_best_seller_text', 'Top-Rated Picks You Can’t Miss'); 

        // Set the theme mod for the product category
        set_theme_mod('mobile_accessories_shop_products_cat', 'productcategory1');

        // Define the single product category name, product titles, and tags
        $mobile_accessories_shop_category_name = 'productcategory1';
        $mobile_accessories_shop_titles = array(
            "ChronoBand™ Smartwatch",
            "PowerStick™ Mini Portable Charger",
            "SonicWave™ True Wireless Speaker",
            "EchoLine™ Wired Stereo Earphones",
            "VoltPro™ Fast Charge Kit",
        );

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

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

        $mobile_accessories_shop_term_id = is_array($mobile_accessories_shop_term) ? $mobile_accessories_shop_term['term_id'] : $mobile_accessories_shop_term;

        // Loop to create 5 products for the category
        foreach ($mobile_accessories_shop_titles as $mobile_accessories_shop_index => $mobile_accessories_shop_title) {
            // Create product content
            $mobile_accessories_shop_content = 'Ultra-compact cylindrical power bank with fast charging—perfect for emergency top-ups on the go.';

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

            if (is_wp_error($mobile_accessories_shop_post_id)) {
                error_log('Error creating product: ' . $mobile_accessories_shop_post_id->get_error_message());
                continue; // Skip to the next product if creation fails
            }

            // Assign the category to the product
            wp_set_object_terms($mobile_accessories_shop_post_id, $mobile_accessories_shop_term_id, 'product_cat');

            // Set product price and sale price
            update_post_meta($mobile_accessories_shop_post_id, '_regular_price', '39.99'); // Regular price
            update_post_meta($mobile_accessories_shop_post_id, '_sale_price', '29.99'); // Sale price 
            update_post_meta($mobile_accessories_shop_post_id, '_price', '29.99'); // Active price

            // Handle the featured image using media_sideload_image
            $mobile_accessories_shop_image_url = get_template_directory_uri() . '/images/Product' . ($mobile_accessories_shop_index + 1) . '.png';
            $mobile_accessories_shop_image_id = media_sideload_image($mobile_accessories_shop_image_url, $mobile_accessories_shop_post_id, null, 'id');

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

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



 