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

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

            // ------- Create Nav Menu --------
            $multipurpose_blog_menuname = 'Main Menus';
            $multipurpose_blog_bpmenulocation = 'primary';
            $multipurpose_blog_menu_exists = wp_get_nav_menu_object($multipurpose_blog_menuname);

            if (!$multipurpose_blog_menu_exists) {
                $multipurpose_blog_menu_id = wp_create_nav_menu($multipurpose_blog_menuname);

                // Create Home Page
                $multipurpose_blog_home_title = 'Home';
                $multipurpose_blog_home = array(
                    'post_type' => 'page',
                    'post_title' => $multipurpose_blog_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $multipurpose_blog_home_id = wp_insert_post($multipurpose_blog_home);
                // Assign Home Page Template
                add_post_meta($multipurpose_blog_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', $multipurpose_blog_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'multipurpose-blog'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $multipurpose_blog_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $multipurpose_blog_about_title = 'About Us';
                $multipurpose_blog_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.';
                $multipurpose_blog_about = array(
                    'post_type' => 'page',
                    'post_title' => $multipurpose_blog_about_title,
                    'post_content' => $multipurpose_blog_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $multipurpose_blog_about_id = wp_insert_post($multipurpose_blog_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'multipurpose-blog'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $multipurpose_blog_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Layout Page
                $multipurpose_blog_layout_title = 'Layout';
                $multipurpose_blog_layout_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>
                This page showcases different blog and website layout options available in this theme.';

                $multipurpose_blog_layout = array(
                    'post_type'    => 'page',
                    'post_title'   => $multipurpose_blog_layout_title,
                    'post_content' => $multipurpose_blog_layout_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'layout'
                );

                $multipurpose_blog_layout_id = wp_insert_post($multipurpose_blog_layout);

                // Add Layout Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title'     => __('Layout', 'multipurpose-blog'),
                    'menu-item-classes'  => 'layout',
                    'menu-item-url'      => home_url('/layout/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $multipurpose_blog_layout_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Features Page
                $multipurpose_blog_features_title = 'Features';
                $multipurpose_blog_features_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>
                This page highlights the powerful features and customization options available in this theme.';

                $multipurpose_blog_features = array(
                    'post_type'    => 'page',
                    'post_title'   => $multipurpose_blog_features_title,
                    'post_content' => $multipurpose_blog_features_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'features'
                );

                $multipurpose_blog_features_id = wp_insert_post($multipurpose_blog_features);

                // Add Features Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title'     => __('Features', 'multipurpose-blog'),
                    'menu-item-classes'  => 'features',
                    'menu-item-url'      => home_url('/features/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $multipurpose_blog_features_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));


                // Create Categories Page
                $multipurpose_blog_categories_title = 'Categories';
                $multipurpose_blog_categories_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...<br>
                Browse blog posts by different categories and topics.';

                $multipurpose_blog_categories = array(
                    'post_type'    => 'page',
                    'post_title'   => $multipurpose_blog_categories_title,
                    'post_content' => $multipurpose_blog_categories_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'categories'
                );

                $multipurpose_blog_categories_id = wp_insert_post($multipurpose_blog_categories);

                // Add Categories Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title'     => __('Categories', 'multipurpose-blog'),
                    'menu-item-classes'  => 'categories',
                    'menu-item-url'      => home_url('/categories/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $multipurpose_blog_categories_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

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

                    wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'multipurpose-blog'),
                    'menu-item-url'        => get_permalink($multipurpose_blog_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $multipurpose_blog_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $multipurpose_blog_contact_title = 'Contact Us';
                $multipurpose_blog_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.';
                $multipurpose_blog_contact = array(
                    'post_type' => 'page',
                    'post_title' => $multipurpose_blog_contact_title,
                    'post_content' => $multipurpose_blog_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $multipurpose_blog_contact_id = wp_insert_post($multipurpose_blog_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($multipurpose_blog_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'multipurpose-blog'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $multipurpose_blog_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($multipurpose_blog_bpmenulocation)) {
                    $multipurpose_blog_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($multipurpose_blog_locations)) {
                        $multipurpose_blog_locations = array();
                    }
                    $multipurpose_blog_locations[$multipurpose_blog_bpmenulocation] = $multipurpose_blog_menu_id;
                    set_theme_mod('nav_menu_locations', $multipurpose_blog_locations);
                }
            }

            //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(
                'Discover the Latest Trends and Insights',
                'Read Inspiring Stories and Creative Ideas',
                'Expert Tips and Guides for Better Living',
                'Stay Updated with News and Fresh Content'
            );
            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_template_directory_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_template_directory_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);
            
                }
            }

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

        }
    ?>

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