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

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

		//POST and update the customizer and other related data
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $ultimate_blogger_menuname = 'Main Menus';
            $ultimate_blogger_bpmenulocation = 'primary';
            $ultimate_blogger_menu_exists = wp_get_nav_menu_object($ultimate_blogger_menuname);

            if (!$ultimate_blogger_menu_exists) {
                $ultimate_blogger_menu_id = wp_create_nav_menu($ultimate_blogger_menuname);

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

                // Create About Us Page with Dummy Content
                $ultimate_blogger_about_title = 'About Us';
                $ultimate_blogger_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.';
                $ultimate_blogger_about = array(
                    'post_type' => 'page',
                    'post_title' => $ultimate_blogger_about_title,
                    'post_content' => $ultimate_blogger_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $ultimate_blogger_about_id = wp_insert_post($ultimate_blogger_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($ultimate_blogger_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'ultimate-blogger'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $ultimate_blogger_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $ultimate_blogger_blog_page_title = 'Blog';
                $ultimate_blogger_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($ultimate_blogger_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$ultimate_blogger_blog_page_query->have_posts()) {
                    $ultimate_blogger_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $ultimate_blogger_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $ultimate_blogger_blog_page_id = wp_insert_post($ultimate_blogger_blog_page);
                    update_option('page_for_posts', $ultimate_blogger_blog_page_id);

                    wp_update_nav_menu_item($ultimate_blogger_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'ultimate-blogger'),
                    'menu-item-url'        => get_permalink($ultimate_blogger_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $ultimate_blogger_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $ultimate_blogger_contact_title = 'Contact Us';
                $ultimate_blogger_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.';
                $ultimate_blogger_contact = array(
                    'post_type' => 'page',
                    'post_title' => $ultimate_blogger_contact_title,
                    'post_content' => $ultimate_blogger_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $ultimate_blogger_contact_id = wp_insert_post($ultimate_blogger_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($ultimate_blogger_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'ultimate-blogger'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $ultimate_blogger_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($ultimate_blogger_bpmenulocation)) {
                    $ultimate_blogger_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($ultimate_blogger_locations)) {
                        $ultimate_blogger_locations = array();
                    }
                    $ultimate_blogger_locations[$ultimate_blogger_bpmenulocation] = $ultimate_blogger_menu_id;
                    set_theme_mod('nav_menu_locations', $ultimate_blogger_locations);
                }
            }

            // Set the demo import completion flag
    		update_option('ultimate_blogger_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.', 'ultimate-blogger') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'ultimate-blogger') . '</a></span>';
            //end

            //Topbar
            set_theme_mod('multipurpose_blog_contact', '123456789');
            set_theme_mod('multipurpose_blog_email', 'xyz123@example.com');

            //Social Media
            set_theme_mod('multipurpose_blog_cont_facebook', 'https://www.facebook.com');
            set_theme_mod('multipurpose_blog_cont_twitter', 'https://www.twtter.com');
            set_theme_mod('multipurpose_blog_pinterest', 'https://www.pinterest.com');
            set_theme_mod('multipurpose_blog_tumblr', 'https://www.tumblr.com');
            set_theme_mod('multipurpose_blog_instagram', 'https://instagram.com');
            set_theme_mod('multipurpose_blog_linkedin', 'https://www.linkedin.com');

            // Slider
            set_theme_mod('multipurpose_blog_slider_hide', true);
            set_theme_mod('multipurpose_blog_slider_button_text', 'CONTINUE READING');
            set_theme_mod('multipurpose_blog_slider_button_link', '#');

            $multipurpose_blog_slider_titles = array(
                'Latest Trends & Insights',
                'Inspiring Stories & Ideas',
                'Expert Tips & Guides',
                'Stay Updated & Informed'
            );
            for ($multipurpose_blog_i = 0; $multipurpose_blog_i < 4; $multipurpose_blog_i++) {

                $multipurpose_blog_slider_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.';
                $multipurpose_blog_slider_title = $multipurpose_blog_slider_titles[$multipurpose_blog_i];

                // Create post object
                $multipurpose_blog_my_post = array(
                    'post_title'    => wp_strip_all_tags($multipurpose_blog_slider_title),
                    'post_content'  => $multipurpose_blog_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );

                // Insert the post into the database
                $multipurpose_blog_post_id = wp_insert_post($multipurpose_blog_my_post);

                if ($multipurpose_blog_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('multipurpose_blog_slidersettings_page' . ($multipurpose_blog_i + 1), $multipurpose_blog_post_id);

                    $multipurpose_blog_image_url = get_theme_file_uri() . '/images/slider' . ($multipurpose_blog_i + 1) . '.png';

                    // Handle image upload and set as post thumbnail
                    $multipurpose_blog_image_id = media_sideload_image($multipurpose_blog_image_url, $multipurpose_blog_post_id, null, 'id');

                    if (!is_wp_error($multipurpose_blog_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($multipurpose_blog_post_id, $multipurpose_blog_image_id);
                    }
                }
            }

            //Events Section
            set_theme_mod('multipurpose_blog_photography_section', true );
            set_theme_mod('multipurpose_blog_photo_setting', 'postcategory1' );

            // Define post category names and post titles
            $multipurpose_blog_category_names = array('postcategory1', 'postcategory2', 'postcategory3');
            $multipurpose_blog_title_array = array(
                array("CATRGORY 1", "CATRGORY 2", "CATRGORY 3"),
                array("CATRGORY 1", "CATRGORY 2", "CATRGORY 3"),
                array("CATRGORY 1", "CATRGORY 2", "CATRGORY 3")
            );
            
            $multipurpose_blog_content_array = array(
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry","Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry")
            );
            
            foreach ($multipurpose_blog_category_names as $multipurpose_blog_index => $multipurpose_blog_category_name) {
                // Create or retrieve the post category term ID
                $multipurpose_blog_term = term_exists($multipurpose_blog_category_name, 'category');
                if ($multipurpose_blog_term === 0 || $multipurpose_blog_term === null) {
                    // If the term does not exist, create it
                    $multipurpose_blog_term = wp_insert_term($multipurpose_blog_category_name, 'category');
                }
                if (is_wp_error($multipurpose_blog_term)) {
                    error_log('Error creating category: ' . $multipurpose_blog_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }
            
                for ($multipurpose_blog_i = 0; $multipurpose_blog_i < 3; $multipurpose_blog_i++) {
                    // Create post content
                    $multipurpose_blog_title = $multipurpose_blog_title_array[$multipurpose_blog_index][$multipurpose_blog_i];
                    $multipurpose_blog_content = $multipurpose_blog_content_array[$multipurpose_blog_index][$multipurpose_blog_i];
            
                    // Create post post object
                    $multipurpose_blog_my_post = array(
                        'post_title'    => wp_strip_all_tags($multipurpose_blog_title),
                        'post_content'  => $multipurpose_blog_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                        'post_excerpt'  => substr($multipurpose_blog_content, 0, 100) . '...', // Add an excerpt
                    );
            
                    // Insert the post into the database
                    $multipurpose_blog_post_id = wp_insert_post($multipurpose_blog_my_post);
            
                    if (is_wp_error($multipurpose_blog_post_id)) {
                        error_log('Error creating post: ' . $multipurpose_blog_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }
            
                    // Assign the category to the post
                    wp_set_post_categories($multipurpose_blog_post_id, array((int)$multipurpose_blog_term['term_id']));
            
                    // Handle the featured image using media_sideload_image
                    $multipurpose_blog_image_url = get_theme_file_uri() . '/images/event' . ($multipurpose_blog_i + 1) . '.png';
                    $multipurpose_blog_image_id = media_sideload_image($multipurpose_blog_image_url, $multipurpose_blog_post_id, null, 'id');
            
                    if (is_wp_error($multipurpose_blog_image_id)) {
                        error_log('Error downloading image: ' . $multipurpose_blog_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
            
                    // Assign featured image to post
                    set_post_thumbnail($multipurpose_blog_post_id, $multipurpose_blog_image_id);
            
                }
            }

        }
    ?>

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