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

        // If the demo import is completed, display the "Visit site" button
        if ($modern_blogger_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'modern-blogger') . '</p>';
            echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'modern-blogger') . '</a></span>';
            echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'modern-blogger') . '</a></span>';
        }
        

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

        // ------- Create Main Menu --------
        $modern_blogger_menuname = 'Primary Menu';
        $modern_blogger_bpmenulocation = 'primary';
        $modern_blogger_menu_exists = wp_get_nav_menu_object( $modern_blogger_menuname );
    
        if ( !$modern_blogger_menu_exists ) {
            $modern_blogger_menu_id = wp_create_nav_menu( $modern_blogger_menuname );

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

            // Create a new Page 
            $modern_blogger_pages_title = 'Pages';
            $modern_blogger_pages_content = '<p>Explore all the pages we have on our website. Find information about our services, company, and more.</p>

                 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<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.';
            $modern_blogger_pages = array(
                'post_type'    => 'page',
                'post_title'   => $modern_blogger_pages_title,
                'post_content' => $modern_blogger_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $modern_blogger_pages_id = wp_insert_post($modern_blogger_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($modern_blogger_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'modern-blogger'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $modern_blogger_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $modern_blogger_about_title = 'About Us';
            $modern_blogger_about_content = 'Explore all the pages 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 recently 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.';
            $modern_blogger_about = array(
                'post_type'    => 'page',
                'post_title'   => $modern_blogger_about_title,
                'post_content' => $modern_blogger_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $modern_blogger_about_id = wp_insert_post($modern_blogger_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($modern_blogger_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'modern-blogger'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $modern_blogger_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));
            // Create Blog Page only if it doesn't exist already
            $modern_blogger_existing_blog_page = get_page_by_path('blog');
            if (!$modern_blogger_existing_blog_page) {
                $modern_blogger_blog_id = wp_insert_post(array(
                'post_type'    => 'page',
                'post_title'   => __('Blog', 'modern-blogger'),
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blog'
             ));
            } else {
                $modern_blogger_blog_id = $modern_blogger_existing_blog_page->ID;
            }
            // Assign as Posts Page
            update_option('page_for_posts', $modern_blogger_blog_id);
            update_option('show_on_front', 'page');
            //Add to Menu (only if not already added)
            $modern_blogger_menu_items = wp_get_nav_menu_items($modern_blogger_menu_id);
            $modern_blogger_blog_in_menu = false;
            if ($modern_blogger_menu_items) {
                foreach ($modern_blogger_menu_items as $modern_blogger_item) {
                    if ((int) $modern_blogger_item->object_id === (int) $modern_blogger_blog_id) {
                        $modern_blogger_blog_in_menu = true;
                        break;
                    }
                }
            }
            if (!$modern_blogger_blog_in_menu) {
            wp_update_nav_menu_item($modern_blogger_menu_id, 0, array(
                'menu-item-title'       => __('Blog', 'modern-blogger'),
                'menu-item-url'         => home_url('/blog/'),
                'menu-item-status'      => 'publish',
                'menu-item-object-id'   => $modern_blogger_blog_id,
                'menu-item-object'      => 'page',
                'menu-item-type'        => 'post_type',
                'menu-item-classes'     => 'modern-blogger-blog-link'
            ));
            }
        // Assign the menu to the primary location if not already set
        if ( ! has_nav_menu( $modern_blogger_bpmenulocation ) ) {
            $modern_blogger_locations = get_theme_mod( 'nav_menu_locations' );
            if ( empty( $modern_blogger_locations ) ) {
                $modern_blogger_locations = array();
            }
            $modern_blogger_locations[ $modern_blogger_bpmenulocation ] = $modern_blogger_menu_id;
            set_theme_mod( 'nav_menu_locations', $modern_blogger_locations );
            }
        }
        // Set the demo import completion flag
        update_option('modern_blogger_demo_import_completed', true);
        // Display success message and "Visit Site" button
         echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'modern-blogger') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'modern-blogger') . '</a></span>';
         echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'modern-blogger') . '</a></span>';
        //end 

        // Slider Section
        set_theme_mod( 'modern_blogger_show_slider', true );
        set_theme_mod( 'modern_blogger_slider_category', 'postcategory1' );
        $modern_blogger_category_names = array('postcategory1', 'postcategory2', 'postcategory3');
        $modern_blogger_title_array = array(
            array('How Virtual Reality is Revolutionizing Entertainment and Education', 'Augmented Reality is Shaping Retail and Real Estate Experiences', 'Artificial Intelligence is Transforming Healthcare and Financial Services'),
            array('How Virtual Reality is Revolutionizing Entertainment and Education', 'Augmented Reality is Shaping Retail and Real Estate Experiences', 'Artificial Intelligence is Transforming Healthcare and Financial Services'),
            array('How Virtual Reality is Revolutionizing Entertainment and Education', 'Augmented Reality is Shaping Retail and Real Estate Experiences', 'Artificial Intelligence is Transforming Healthcare and Financial Services'),
        );
        foreach ($modern_blogger_category_names as $modern_blogger_index => $modern_blogger_category_name) {
            // Create or retrieve the post category term ID
            $modern_blogger_term = term_exists($modern_blogger_category_name, 'category');
            if ($modern_blogger_term === 0 || $modern_blogger_term === null) {
                // If the term does not exist, create it
                $modern_blogger_term = wp_insert_term($modern_blogger_category_name, 'category');
            }
            if (is_wp_error($modern_blogger_term)) {
                error_log('Error creating category: ' . $modern_blogger_term->get_error_message());
                continue; // Skip to the next iteration if category creation fails
            }

            for ($modern_blogger_i = 0; $modern_blogger_i < 3; $modern_blogger_i++) {
                // Create post content
                $modern_blogger_title = $modern_blogger_title_array[$modern_blogger_index][$modern_blogger_i];
                $modern_blogger_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. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

                // Create post post object
                $modern_blogger_my_post = array(
                    'post_title'    => wp_strip_all_tags($modern_blogger_title),
                    'post_content'  => $modern_blogger_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Post type set to 'post'
                );

                // Insert the post into the database
                $modern_blogger_post_id = wp_insert_post($modern_blogger_my_post);

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

                // Assign the category to the post
                wp_set_post_categories($modern_blogger_post_id, array((int)$modern_blogger_term['term_id']));

                // Handle the featured image using media_sideload_image
           
                $modern_blogger_image_url = get_template_directory_uri().'/images/slider'.$modern_blogger_i.'.png';
                $modern_blogger_image_id = media_sideload_image($modern_blogger_image_url, $modern_blogger_post_id, null, 'id');

                if (is_wp_error($modern_blogger_image_id)) {
                    error_log('Error downloading image: ' . $modern_blogger_image_id->get_error_message());
                    continue; // Skip to the next post if image download fails
                }
                // Assign featured image to post
                set_post_thumbnail($modern_blogger_post_id, $modern_blogger_image_id);
            }
        }

        // Services Section
        
        set_theme_mod( 'modern_blogger_show_service_section', true );
        set_theme_mod( 'modern_blogger_services_section_setting', 'Amazon Launches New Grocery Delivery Service in Select Cities' );
        $modern_blogger_service_titles = array(
            "Amazon Launches New Grocery Delivery Service in Select Cities",
            "Microsoft Launches AI-Powered Tools for Small Businesses"
        );
        $modern_blogger_service_image_url = get_template_directory_uri() . '/images/service1.png';

        for ($modern_blogger_i = 1; $modern_blogger_i <= 2; $modern_blogger_i++) {
            // Create post
            $modern_blogger_title = $modern_blogger_service_titles[$modern_blogger_i - 1];
            $modern_blogger_content = "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quisquam velit recusandae unde doloremque, veniam ullam.";
            $modern_blogger_post_data = array(
                'post_title'   => wp_strip_all_tags($modern_blogger_title),
                'post_content' => wp_strip_all_tags($modern_blogger_content),
                'post_status'  => 'publish',
                'post_type'    => 'post',
            );
            $modern_blogger_post_id = wp_insert_post($modern_blogger_post_data);

            if (!is_wp_error($modern_blogger_post_id)) {
                // Add featured image
                $modern_blogger_image_id = media_sideload_image($modern_blogger_service_image_url, $modern_blogger_post_id, null, 'id');
                if (!is_wp_error($modern_blogger_image_id)) {
                    set_post_thumbnail($modern_blogger_post_id, $modern_blogger_image_id);
                }
                // Save post ID in theme mod
                set_theme_mod('modern_blogger_services_section_setting' . $modern_blogger_i, $modern_blogger_post_id);
            }
        }

        // Middle Category Section
        set_theme_mod( 'modern_blogger_category1', 'middlecategory1' );
        $modern_blogger_categories = array('middlecategory1', 'middlecategory2');
        $modern_blogger_middle_titles = array(
            array("Social media influencers are changing the Ads Landscape", "The Benefits and Risks of Using AI in Healthcare"),
            array("New Trends in Online Learning Platforms", "The Future of Remote Work")
        );
        foreach ($modern_blogger_categories as $modern_blogger_index => $modern_blogger_category_name) {
            // Create or retrieve category
            $modern_blogger_term = term_exists($modern_blogger_category_name, 'category');
            if ($modern_blogger_term === 0 || $modern_blogger_term === null) {
                $modern_blogger_term = wp_insert_term($modern_blogger_category_name, 'category');
            }
            if (is_wp_error($modern_blogger_term)) {
                error_log('Error creating category: ' . $modern_blogger_term->get_error_message());
                continue;
            }
            $category_id = $modern_blogger_term['term_id'];

            // Create posts for the category
            foreach ($modern_blogger_middle_titles[$modern_blogger_index] as $modern_blogger_title) {
                $modern_blogger_post_data = array(
                    'post_title'   => wp_strip_all_tags($modern_blogger_title),
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );
                $modern_blogger_post_id = wp_insert_post($modern_blogger_post_data);

                if (!is_wp_error($modern_blogger_post_id)) {
                    // Assign category
                    wp_set_post_categories($modern_blogger_post_id, array($category_id));
                    // Add featured image
                    $modern_blogger_image_url = get_template_directory_uri() . '/images/middle-category' . ($modern_blogger_index + 1) . '.png';
                    $modern_blogger_image_id = media_sideload_image($modern_blogger_image_url, $modern_blogger_post_id, null, 'id');
                    if (!is_wp_error($modern_blogger_image_id)) {
                        set_post_thumbnail($modern_blogger_post_id, $modern_blogger_image_id);
                    }
                }
            }
        }

        //Offers Section
        set_theme_mod( 'modern_blogger_add_post', 'Super Holiday Sale' );
        $modern_blogger_holiday_titles = array("Super Holiday Sale", "Holiday Special Offers");
        $modern_blogger_holiday_image_url = get_template_directory_uri() . '/images/service3.png';
        for ($modern_blogger_i = 1; $modern_blogger_i <= 2; $modern_blogger_i++) {
            // Create post
            $modern_blogger_title = $modern_blogger_holiday_titles[$modern_blogger_i - 1];
            $modern_blogger_post_data = array(
                'post_title'   => wp_strip_all_tags($modern_blogger_title),
                'post_status'  => 'publish',
                'post_type'    => 'post',
            );
            $modern_blogger_post_id = wp_insert_post($modern_blogger_post_data);

            if (!is_wp_error($modern_blogger_post_id)) {
                // Add featured image
                $modern_blogger_image_id = media_sideload_image($modern_blogger_holiday_image_url, $modern_blogger_post_id, null, 'id');
                if (!is_wp_error($modern_blogger_image_id)) {
                    set_post_thumbnail($modern_blogger_post_id, $modern_blogger_image_id);
                }
                // Save post ID in theme mod
                set_theme_mod('modern_blogger_category1' . $modern_blogger_i, $modern_blogger_post_id);
            }
        }

        //Copyright Text
        set_theme_mod( 'modern_blogger_footer_text', 'By Themesglance' );

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=modern_blogger_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('modern_blogger_demo_import_completed')) : ?>
            <div class="demo-btn">
                <p><?php echo  esc_html_e( 'You are about to use the most easy to use and flexible WordPress theme.', 'modern-blogger' ); ?></p>
                <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
                    <?php esc_html_e('Run Importer', 'modern-blogger'); ?>
                        <span id="spinner" style="display: none;">
                            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/spinner.gif" alt="Loading..." style="width:34px; height:34px; margin-left:10px;vertical-align: middle;" />
                        </span>
                </button>
            </div>
        <?php endif; ?>
        
    </form>
	<script type="text/javascript">
		function validate(valid) {
			 if(confirm("Do you really want to import the Demo Theme Content?")){
			    document.getElementById('spinner').style.display = 'inline-block';
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>
