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

        // If the demo import is completed, display the "View Site" button
        if ($affiliate_marketing_business_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'affiliate-marketing-business') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'affiliate-marketing-business') . '</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
                $affiliate_marketing_business_plugin_slug = 'woocommerce';
                $affiliate_marketing_business_plugin_file = 'woocommerce/woocommerce.php';

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

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

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

            //Check if Sales Countdown Timer is installed and activated
            if (!is_plugin_active('sales-countdown-timer/sales-countdown-timer.php')) {
                // Install the plugin
                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';

                $affiliate_marketing_business_plugin_slug = 'sales-countdown-timer';
                $affiliate_marketing_business_api = plugins_api('plugin_information', array('slug' => $affiliate_marketing_business_plugin_slug));

                if (is_wp_error($affiliate_marketing_business_api)) {
                    return;
                }

                $affiliate_marketing_business_upgrader = new Plugin_Upgrader(new Automatic_Upgrader_Skin());
                $affiliate_marketing_business_upgrader->install($affiliate_marketing_business_api->download_link);

                // Activate the plugin
                activate_plugin('sales-countdown-timer/sales-countdown-timer.php');
            }

            // ------- Create Nav Menu --------
            $affiliate_marketing_business_menuname = 'Main Menus';
            $affiliate_marketing_business_bpmenulocation = 'primary';
            $affiliate_marketing_business_menu_exists = wp_get_nav_menu_object($affiliate_marketing_business_menuname);

            if (!$affiliate_marketing_business_menu_exists) {
                $affiliate_marketing_business_menu_id = wp_create_nav_menu($affiliate_marketing_business_menuname);

                // Create Home Page
                $affiliate_marketing_business_home_title = 'Home';
                $affiliate_marketing_business_home = array(
                    'post_type' => 'page',
                    'post_title' => $affiliate_marketing_business_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $affiliate_marketing_business_home_id = wp_insert_post($affiliate_marketing_business_home);
                // Assign Home Page Template
                add_post_meta($affiliate_marketing_business_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', $affiliate_marketing_business_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'affiliate-marketing-business'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $affiliate_marketing_business_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $affiliate_marketing_business_about_title = 'About Us';
                $affiliate_marketing_business_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.';
                $affiliate_marketing_business_about = array(
                    'post_type' => 'page',
                    'post_title' => $affiliate_marketing_business_about_title,
                    'post_content' => $affiliate_marketing_business_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $affiliate_marketing_business_about_id = wp_insert_post($affiliate_marketing_business_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'affiliate-marketing-business'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $affiliate_marketing_business_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Shop Page with Dummy Content
                $affiliate_marketing_business_shop_title = 'Shop';
                $affiliate_marketing_business_shop_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 1500s.';

                $affiliate_marketing_business_shop = array(
                    'post_type'    => 'page',
                    'post_title'   => $affiliate_marketing_business_shop_title,
                    'post_content' => $affiliate_marketing_business_shop_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'shop'
                );

                $affiliate_marketing_business_shop_id = wp_insert_post($affiliate_marketing_business_shop);

                // Add Shop Page to Menu
                wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title'     => __('Shop', 'affiliate-marketing-business'),
                    'menu-item-classes'  => 'shop',
                    'menu-item-url'      => home_url('/shop/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $affiliate_marketing_business_shop_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Products Page with Dummy Content
                $affiliate_marketing_business_products_title = 'Products';
                $affiliate_marketing_business_products_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form.';

                $affiliate_marketing_business_products = array(
                    'post_type'    => 'page',
                    'post_title'   => $affiliate_marketing_business_products_title,
                    'post_content' => $affiliate_marketing_business_products_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'products'
                );

                $affiliate_marketing_business_products_id = wp_insert_post($affiliate_marketing_business_products);

                // Add Products Page to Menu
                wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title'     => __('Products', 'affiliate-marketing-business'),
                    'menu-item-classes'  => 'products',
                    'menu-item-url'      => home_url('/products/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $affiliate_marketing_business_products_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Blog Page 
                $affiliate_marketing_business_blog_page_title = 'Blog';
                $affiliate_marketing_business_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($affiliate_marketing_business_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$affiliate_marketing_business_blog_page_query->have_posts()) {
                    $affiliate_marketing_business_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $affiliate_marketing_business_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $affiliate_marketing_business_blog_page_id = wp_insert_post($affiliate_marketing_business_blog_page);
                    update_option('page_for_posts', $affiliate_marketing_business_blog_page_id);
                    wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'affiliate-marketing-business'),
                    'menu-item-url'        => get_permalink($affiliate_marketing_business_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $affiliate_marketing_business_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $affiliate_marketing_business_contact_title = 'Contact Us';
                $affiliate_marketing_business_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.';
                $affiliate_marketing_business_contact = array(
                    'post_type' => 'page',
                    'post_title' => $affiliate_marketing_business_contact_title,
                    'post_content' => $affiliate_marketing_business_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $affiliate_marketing_business_contact_id = wp_insert_post($affiliate_marketing_business_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($affiliate_marketing_business_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'affiliate-marketing-business'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $affiliate_marketing_business_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($affiliate_marketing_business_bpmenulocation)) {
                    $affiliate_marketing_business_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($affiliate_marketing_business_locations)) {
                        $affiliate_marketing_business_locations = array();
                    }
                    $affiliate_marketing_business_locations[$affiliate_marketing_business_bpmenulocation] = $affiliate_marketing_business_menu_id;
                    set_theme_mod('nav_menu_locations', $affiliate_marketing_business_locations);
                }
            }

            //Header
            set_theme_mod('affiliate_marketing_business_phone_text', '24/7 support');
            set_theme_mod('affiliate_marketing_business_phone_number', '+123 456 7890');

            //Slider Section
            set_theme_mod( 'affiliate_marketing_business_banner_background_image_sec', esc_url( get_template_directory_uri().'/images/slider.png'));
            set_theme_mod('affiliate_marketing_business_slide_number', '3');

            // Define default slider data
            $affiliate_marketing_business_slides = [
                [
                    'small_text' => 'Best Deals!',
                    'title' => 'WIRELESS HEADPHONE',
                    'text' => 'Experience crystal-clear sound quality.',
                    'button_icon' => 'fas fa-shopping-basket',
                    'button_text' => 'SHOP NOW',
                    'button_link' => '#',
                    'image' => get_template_directory_uri() . '/images/slider1.png',
                ],
                [
                    'small_text' => 'Limited Offer!',
                    'title' => 'ULTRA COMFORT',
                    'text' => 'Track your fitness and stay connected.',
                    'button_icon' => 'fas fa-shopping-basket',
                    'button_text' => 'SHOP NOW',
                    'button_link' => '#',
                    'image' => get_template_directory_uri() . '/images/slider2.png',
                ],
                [
                    'small_text' => 'Flash Sale!',
                    'title' => 'SEAMLESS CONNECTIVITY',
                    'text' => 'Unleash powerful sound anywhere.',
                    'button_icon' => 'fas fa-shopping-basket',
                    'button_text' => 'SHOP NOW',
                    'button_link' => '#',
                    'image' => get_template_directory_uri() . '/images/slider3.png',
                ],
            ];

            // Loop through slides and set theme mods
            foreach ($affiliate_marketing_business_slides as $affiliate_marketing_business_index => $affiliate_marketing_business_slide) {
                $affiliate_marketing_business_i = $affiliate_marketing_business_index + 1; // Start from 1 for settings
                set_theme_mod("affiliate_marketing_business_slider_small_text$affiliate_marketing_business_i", $affiliate_marketing_business_slide['small_text']);
                set_theme_mod("affiliate_marketing_business_slider_title$affiliate_marketing_business_i", $affiliate_marketing_business_slide['title']);
                set_theme_mod("affiliate_marketing_business_slider_text$affiliate_marketing_business_i", $affiliate_marketing_business_slide['text']);
                set_theme_mod("affiliate_marketing_business_slider_btn_icon$affiliate_marketing_business_i", $affiliate_marketing_business_slide['button_icon']);
                set_theme_mod("affiliate_marketing_business_slider_button_text$affiliate_marketing_business_i", $affiliate_marketing_business_slide['button_text']);
                set_theme_mod("affiliate_marketing_business_slider_button_link$affiliate_marketing_business_i", $affiliate_marketing_business_slide['button_link']);
                set_theme_mod("affiliate_marketing_business_side_img$affiliate_marketing_business_i", $affiliate_marketing_business_slide['image']);
            }

            //Banner Section
            set_theme_mod( 'affiliate_marketing_business_banner_image', esc_url( get_template_directory_uri().'/images/banner.png'));
            set_theme_mod('affiliate_marketing_business_banner_small_text', 'Best Deals!');
            set_theme_mod('affiliate_marketing_business_banner_text', 'Latest Phone');
            set_theme_mod('affiliate_marketing_business_banner_price', '$25');
            set_theme_mod('affiliate_marketing_business_banner_btn_icon', 'fas fa-shopping-basket');
            set_theme_mod('affiliate_marketing_business_banner_button_text', 'SHOP NOW');
            set_theme_mod('affiliate_marketing_business_banner_button_url', '#');

            // Product Section
            set_theme_mod('affiliate_marketing_business_section_title', 'Deal Of The Day!');
            set_theme_mod('affiliate_marketing_business_product_page', 'productcategory1');

            // Define product category names, product titles, and tags
            $affiliate_marketing_business_category_names = array('productcategory1', 'productcategory2', 'productcategory3');
            $affiliate_marketing_business_title_array = array(
                array("Sony Alpha A6000+ 16-50mm - Black", "Powerbeats3 Wireless Earphones", "Apple Watch Series 03"),
                array("Sony Alpha A6000+ 16-50mm - Black", "Powerbeats3 Wireless Earphones", "Apple Watch Series 03"),
                array("Sony Alpha A6000+ 16-50mm - Black", "Powerbeats3 Wireless Earphones", "Apple Watch Series 03")
            );

            foreach ($affiliate_marketing_business_category_names as $affiliate_marketing_business_index => $affiliate_marketing_business_category_name) {
                // Create or retrieve the product category term ID
                $affiliate_marketing_business_term = term_exists($affiliate_marketing_business_category_name, 'product_cat');
                
                // If the term doesn't exist, create it
                if ($affiliate_marketing_business_term === 0 || $affiliate_marketing_business_term === null) {
                    $affiliate_marketing_business_term = wp_insert_term($affiliate_marketing_business_category_name, 'product_cat');
                }
                
                // Check for errors in category creation
                if (is_wp_error($affiliate_marketing_business_term)) {
                    error_log('Error creating category: ' . $affiliate_marketing_business_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                // Retrieve the category ID for assignment
                $affiliate_marketing_business_term_id = $affiliate_marketing_business_term['term_id'];

                // Loop to create 4 products for each category
                for ($affiliate_marketing_business_i = 0; $affiliate_marketing_business_i < 3; $affiliate_marketing_business_i++) {
                    // Create product content
                    $affiliate_marketing_business_title = $affiliate_marketing_business_title_array[$affiliate_marketing_business_index][$affiliate_marketing_business_i];
                    $affiliate_marketing_business_content = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout';

                    // Create product post object
                    $affiliate_marketing_business_my_post = array(
                        'post_title'    => wp_strip_all_tags($affiliate_marketing_business_title),
                        'post_content'  => $affiliate_marketing_business_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'product', // Post type set to 'product'
                    );

                    // Insert the product into the database
                    $affiliate_marketing_business_post_id = wp_insert_post($affiliate_marketing_business_my_post);

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

                    // Assign the category to the product
                    wp_set_object_terms($affiliate_marketing_business_post_id, array($affiliate_marketing_business_term_id), 'product_cat');
                    
                    // Set product as simple product and assign price
                    update_post_meta($affiliate_marketing_business_post_id, '_regular_price', 500.00); // Assign regular price
                    update_post_meta($affiliate_marketing_business_post_id, '_sale_price', 450.00); // Assign sale price
                    update_post_meta($affiliate_marketing_business_post_id, '_sale_price_dates_from', strtotime('2024-12-12')); // Sale start date
                    update_post_meta($affiliate_marketing_business_post_id, '_sale_price_dates_to', strtotime('2026-12-12')); // Sale end date
                    update_post_meta($affiliate_marketing_business_post_id, '_price', 450.00); // Set current price (sale price)

                    // Handle the featured image using media_sideload_image
                    $affiliate_marketing_business_image_url = get_template_directory_uri() . '/images/affilliate-product' . ($affiliate_marketing_business_i + 1) . '.png';
                    $affiliate_marketing_business_image_id = media_sideload_image($affiliate_marketing_business_image_url, $affiliate_marketing_business_post_id, null, 'id');

                    // Check if there was an error downloading the image
                    if (is_wp_error($affiliate_marketing_business_image_id)) {
                        error_log('Error downloading image: ' . $affiliate_marketing_business_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }

                    // Assign featured image to product
                    set_post_thumbnail($affiliate_marketing_business_post_id, $affiliate_marketing_business_image_id);
                }
            }

            // Set the demo import completion flag
    		update_option('affiliate_marketing_business_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.', 'affiliate-marketing-business') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'affiliate-marketing-business') . '</a></span>';
            //end
   
            //Copyright Text
            set_theme_mod( 'affiliate_marketing_business_footer_copy', 'By Buywptemplate' );

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=affiliate-marketing-business-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('affiliate_marketing_business_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','affiliate-marketing-business'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','affiliate-marketing-business'); ?>" 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>
