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

        // If the demo import is completed, display the "View Site" button
        if ($storefront_ecommerce_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'storefront-ecommerce') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'storefront-ecommerce') . '</a></span>';
        }
		
        if (isset($_POST['submit'])) {

            // Check if woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $storefront_ecommerce_plugin_slug = 'woocommerce';
                $storefront_ecommerce_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $storefront_ecommerce_installed_plugins = get_plugins();
                if (!isset($storefront_ecommerce_installed_plugins[$storefront_ecommerce_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
                    $storefront_ecommerce_upgrader = new Plugin_Upgrader();
                    $storefront_ecommerce_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($storefront_ecommerce_plugin_file);
            }

            // Check if  GTranslate is installed and activated
            if (!is_plugin_active('gtranslate/gtranslate.php')) {
                // Install the plugin if it doesn't exist
                $storefront_ecommerce_plugin_slug = 'gtranslate';
                $storefront_ecommerce_plugin_file = 'gtranslate/gtranslate.php';

                // Check if plugin is installed
                $storefront_ecommerce_installed_plugins = get_plugins();
                if (!isset($storefront_ecommerce_installed_plugins[$storefront_ecommerce_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
                    $storefront_ecommerce_upgrader = new Plugin_Upgrader();
                    $storefront_ecommerce_upgrader->install('https://downloads.wordpress.org/plugin/gtranslate.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($storefront_ecommerce_plugin_file);
            }

            // Check if ace-post-type-builder is installed and activated
            if (!is_plugin_active('ace-post-type-builder/ace-post-type-builder.php')) {
                // Install the plugin if it doesn't exist
                $storefront_ecommerce_plugin_slug = 'ace-post-type-builder';
                $storefront_ecommerce_plugin_file = 'ace-post-type-builder/ace-post-type-builder.php';

                // Check if plugin is installed
                $storefront_ecommerce_installed_plugins = get_plugins();
                if (!isset($storefront_ecommerce_installed_plugins[$storefront_ecommerce_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
                    $storefront_ecommerce_upgrader = new Plugin_Upgrader();
                    $storefront_ecommerce_upgrader->install('https://downloads.wordpress.org/plugin/ace-post-type-builder.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($storefront_ecommerce_plugin_file);
            }

            // ------- Create Nav Menu --------
            $storefront_ecommerce_menuname = 'Main Menus';
            $storefront_ecommerce_bpmenulocation = 'primary';
            $storefront_ecommerce_menu_exists = wp_get_nav_menu_object($storefront_ecommerce_menuname);

            if (!$storefront_ecommerce_menu_exists) {
                $storefront_ecommerce_menu_id = wp_create_nav_menu($storefront_ecommerce_menuname);

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

                // Create About Us Page with Dummy Content
                $storefront_ecommerce_about_title = 'About Us';
                $storefront_ecommerce_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>
                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.<br>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br>
                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $storefront_ecommerce_about = array(
                    'post_type' => 'page',
                    'post_title' => $storefront_ecommerce_about_title,
                    'post_content' => $storefront_ecommerce_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $storefront_ecommerce_about_id = wp_insert_post($storefront_ecommerce_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($storefront_ecommerce_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'storefront-ecommerce'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $storefront_ecommerce_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                /* Create Shop Page */
                $storefront_ecommerce_shop_title = 'Shop';
                $storefront_ecommerce_shop_content = 'Browse our online shop and discover quality products at great prices.
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';

                $storefront_ecommerce_shop = array(
                    'post_type'    => 'page',
                    'post_title'   => $storefront_ecommerce_shop_title,
                    'post_content' => $storefront_ecommerce_shop_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'shop'
                );
                $storefront_ecommerce_shop_id = wp_insert_post($storefront_ecommerce_shop);

                // Add Shop Page to Menu
                wp_update_nav_menu_item($storefront_ecommerce_menu_id, 0, array(
                    'menu-item-title'   => __('Shop', 'storefront-ecommerce'),
                    'menu-item-classes' => 'shop',
                    'menu-item-url'     => home_url('/shop/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $storefront_ecommerce_shop_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                /* Create Products Page */
                $storefront_ecommerce_products_title = 'Products';
                $storefront_ecommerce_products_content = 'Explore our wide range of products designed to meet your needs.
                Lorem ipsum is simply dummy text of the printing and typesetting industry.';

                $storefront_ecommerce_products = array(
                    'post_type'    => 'page',
                    'post_title'   => $storefront_ecommerce_products_title,
                    'post_content' => $storefront_ecommerce_products_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'products'
                );
                $storefront_ecommerce_products_id = wp_insert_post($storefront_ecommerce_products);

                // Add Products Page to Menu
                wp_update_nav_menu_item($storefront_ecommerce_menu_id, 0, array(
                    'menu-item-title'   => __('Products', 'storefront-ecommerce'),
                    'menu-item-classes' => 'products',
                    'menu-item-url'     => home_url('/products/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $storefront_ecommerce_products_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));
               
                // Create Blog Page 
                $storefront_ecommerce_blog_page_title = 'Blog';
                $storefront_ecommerce_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($storefront_ecommerce_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$storefront_ecommerce_blog_page_query->have_posts()) {
                    $storefront_ecommerce_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $storefront_ecommerce_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $storefront_ecommerce_blog_page_id = wp_insert_post($storefront_ecommerce_blog_page);
                    update_option('page_for_posts', $storefront_ecommerce_blog_page_id);

                    wp_update_nav_menu_item($storefront_ecommerce_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'storefront-ecommerce'),
                    'menu-item-url'        => get_permalink($storefront_ecommerce_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $storefront_ecommerce_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $storefront_ecommerce_contact_title = 'Contact Us';
                $storefront_ecommerce_contact_content = 'Explore all the contact we have on our website. Find information about our services, company, and more.
                 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 contactly with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br>
                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $storefront_ecommerce_contact = array(
                    'post_type' => 'page',
                    'post_title' => $storefront_ecommerce_contact_title,
                    'post_content' => $storefront_ecommerce_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $storefront_ecommerce_contact_id = wp_insert_post($storefront_ecommerce_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($storefront_ecommerce_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'storefront-ecommerce'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $storefront_ecommerce_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Set the menu location if it's not already set
                if (!has_nav_menu($storefront_ecommerce_bpmenulocation)) {
                    $storefront_ecommerce_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($storefront_ecommerce_locations)) {
                        $storefront_ecommerce_locations = array();
                    }
                    $storefront_ecommerce_locations[$storefront_ecommerce_bpmenulocation] = $storefront_ecommerce_menu_id;
                    set_theme_mod('nav_menu_locations', $storefront_ecommerce_locations);
                }
            }

            //Header
            set_theme_mod('storefront_ecommerce_topbar_text', 'Exclusive Offer on Electronics! Hop on to Electronics to grab this deal!');
            set_theme_mod('storefront_ecommerce_phone_number', '+1 123 456 7890');
            set_theme_mod('storefront_ecommerce_location_text', 'Delivery Location');
            set_theme_mod('storefront_ecommerce_location_url', '#');

            //Social Media
            set_theme_mod('storefront_ecommerce_facebook_url', '#');
            set_theme_mod('storefront_ecommerce_twitter_url', '#');
            set_theme_mod('storefront_ecommerce_instagram_url', '#');
            set_theme_mod('storefront_ecommerce_pinterest_url', '#');

            //Slider
            set_theme_mod('storefront_ecommerce_slider_hide', true);
            set_theme_mod('storefront_ecommerce_slider_small_title', 'Fashion Collection');
            set_theme_mod('storefront_ecommerce_slider_button_text', 'Read More');
            set_theme_mod('storefront_ecommerce_slider_button_link', '#');
            for ($storefront_ecommerce_i = 1; $storefront_ecommerce_i <= 4; $storefront_ecommerce_i++) {
                $storefront_ecommerce_slider_title = array(
                    'Distress Shades',
                    'Elegant Apparel',
                    'Modern Furnishings',
                    'Tech Innovations'
                );
                $storefront_ecommerce_slider_content = array(
                    'Our new collection is totally dank at 30% Off',
                    'Experience elegance with our exclusive deals—up to 40% Off!',
                    'Revamp your style with our latest arrivals—shop now and save big!',
                    'Discover cutting-edge gadgets at unbeatable prices—limited time only!'
                );
            
                // Ensure the titles and contents are matched correctly for each iteration
                $storefront_ecommerce_current_title = $storefront_ecommerce_slider_title[$storefront_ecommerce_i - 1];
                $storefront_ecommerce_current_content = $storefront_ecommerce_slider_content[$storefront_ecommerce_i - 1];
            
                // Create post object
                $storefront_ecommerce_my_post = array(
                    'post_title'    => wp_strip_all_tags($storefront_ecommerce_current_title),
                    'post_content'  => wp_strip_all_tags($storefront_ecommerce_current_content),
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );
            
                // Insert the post into the database
                $storefront_ecommerce_post_id = wp_insert_post($storefront_ecommerce_my_post);
            
                if ($storefront_ecommerce_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('storefront_ecommerce_slider_setting' . $storefront_ecommerce_i, $storefront_ecommerce_post_id);
            
                    // Define the image URL for the slider
                    $storefront_ecommerce_image_url = get_template_directory_uri() . '/images/slider' . $storefront_ecommerce_i . '.png';
            
                    // Download the image and set it as the featured image
                    $storefront_ecommerce_image_id = media_sideload_image($storefront_ecommerce_image_url, $storefront_ecommerce_post_id, null, 'id');
            
                    if (!is_wp_error($storefront_ecommerce_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($storefront_ecommerce_post_id, $storefront_ecommerce_image_id);
                    }
                }
            }            

            // Product Section
            set_theme_mod('storefront_ecommerce_product_enable', true);
            $storefront_ecommerce_title_array = array(
                array("Product name here", "Product name here", "Product name here", "Product name here")
            );

            foreach ($storefront_ecommerce_title_array as $storefront_ecommerce_titles) {
                // Loop to create only 3 products
                for ($storefront_ecommerce_i = 0; $storefront_ecommerce_i < 4; $storefront_ecommerce_i++) {
                    // Create product content
                    $storefront_ecommerce_title = $storefront_ecommerce_titles[$storefront_ecommerce_i];
                    $storefront_ecommerce_content = '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.';

                    // Create product post object
                    $storefront_ecommerce_my_post = array(
                        'post_title'    => wp_strip_all_tags($storefront_ecommerce_title),
                        'post_content'  => $storefront_ecommerce_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'product',
                    );

                    // Insert the product into the database
                    $storefront_ecommerce_post_id = wp_insert_post($storefront_ecommerce_my_post);

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

                    // Add product meta (price, etc.)
                    update_post_meta($storefront_ecommerce_post_id, '_regular_price', '25.00'); // Regular price
                    update_post_meta($storefront_ecommerce_post_id, '_sale_price', '23.87'); // Sale price
                    update_post_meta($storefront_ecommerce_post_id, '_price', '23.87'); // Active price

                    // Handle the featured image using media_sideload_image
                    $storefront_ecommerce_image_url = get_template_directory_uri() . '/images/product' . ($storefront_ecommerce_i + 1) . '.png';
                    $storefront_ecommerce_image_id = media_sideload_image($storefront_ecommerce_image_url, $storefront_ecommerce_post_id, null, 'id');

                    if (is_wp_error($storefront_ecommerce_image_id)) {
                        error_log('Error downloading image: ' . $storefront_ecommerce_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }

                    // Assign featured image to product
                    set_post_thumbnail($storefront_ecommerce_post_id, $storefront_ecommerce_image_id);

                    // Assign the product to the 'Uncategorized' category
                    wp_set_object_terms($storefront_ecommerce_post_id, 'uncategorized', 'product_cat');
                }
            }

            // Create the 'Products' page if it doesn't exist
            $storefront_ecommerce_page_query = new WP_Query(array(
                'post_type'      => 'page',
                'title'          => 'Products',
                'post_status'    => 'publish',
                'posts_per_page' => 1
            ));

            if (!$storefront_ecommerce_page_query->have_posts()) {
                $storefront_ecommerce_page_title = 'Products';
                $storefront_ecommerce_productpage = '[products limit="4" columns="4"]';

                // Append the WooCommerce products shortcode to the content
                $storefront_ecommerce_content = '';
                $storefront_ecommerce_content .= do_shortcode($storefront_ecommerce_productpage);

                // Create the new page
                $storefront_ecommerce_page = array(
                    'post_type'    => 'page',
                    'post_title'   => $storefront_ecommerce_page_title,
                    'post_content' => $storefront_ecommerce_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'products'
                );

                // Insert the page and get its ID
                $storefront_ecommerce_page_id = wp_insert_post($storefront_ecommerce_page);

                // Store the page ID in theme mod
                if (!is_wp_error($storefront_ecommerce_page_id)) {
                    set_theme_mod('storefront_ecommerce_product_page', $storefront_ecommerce_page_id);
                }
            }

            // Set the demo import completion flag
    		update_option('storefront_ecommerce_demo_import_completed', true);
    		// Display success message and "View Site" button
    		echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'storefront-ecommerce') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'storefront-ecommerce') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=storefront-ecommerce-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('storefront_ecommerce_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','storefront-ecommerce'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','storefront-ecommerce'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show loader
                document.getElementById('demo-import-loader').style.display = 'block';
                
                // Submit form
                document.forms[0].submit();
            } else {
                return false;
            }
		}
	</script>
</div>
