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

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

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

            // ------- Create Nav Menu --------
            $video_blog_menuname = 'Main Menus';
            $video_blog_bpmenulocation = 'primary';
            $video_blog_menu_exists = wp_get_nav_menu_object($video_blog_menuname);

            if (!$video_blog_menu_exists) {
                $video_blog_menu_id = wp_create_nav_menu($video_blog_menuname);

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

                // Create Pages Page with Dummy Content
                $video_blog_pages_title = 'Pages';
                $video_blog_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.';
                $video_blog_pages = array(
                    'post_type' => 'page',
                    'post_title' => $video_blog_pages_title,
                    'post_content' => $video_blog_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $video_blog_pages_id = wp_insert_post($video_blog_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($video_blog_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'video-blog'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_blog_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $video_blog_about_title = 'About Us';
                $video_blog_about_content = 'GIRL FASHION LIFESTYLE...<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.';
                $video_blog_about = array(
                    'post_type' => 'page',
                    'post_title' => $video_blog_about_title,
                    'post_content' => $video_blog_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $video_blog_about_id = wp_insert_post($video_blog_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($video_blog_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'video-blog'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_blog_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

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

            //Topbar
            set_theme_mod('video_blog_uplaod_video_text', 'Submit Video');
            set_theme_mod('video_blog_uplaod_video_url', '#');

            //Top Video Section - video1
            $video_blog_post_title = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';
            $video_blog_post_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 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.';

            $video_blog_video_url = 'https://www.youtube.com/embed/9xwazD5SyVg';

            $video_blog_post_content .= "\n\n[embed]{$video_blog_video_url}[/embed]";

            $video_blog_my_post = array(
                'post_title'    => wp_strip_all_tags($video_blog_post_title),
                'post_content'  => $video_blog_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
            );

            // Insert the post into the database
            $video_blog_post_id = wp_insert_post($video_blog_my_post);

            // Check if the post was successfully created
            if (!is_wp_error($video_blog_post_id)) {
                // Set the theme mod for the post ID
                set_theme_mod('video_blog_top_video_one', $video_blog_post_id);
            } else {
                // Handle the error if the post creation failed
                error_log('Post creation failed: ' . $video_blog_post_id->get_error_message());
            }

            // Top Video Section - - video2
            $video_blog_post_title = 'Lorem Ipsum is simply dummy text industry';
            $video_blog_post_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 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.';

            $video_blog_video_url = 'https://www.youtube.com/embed/9xwazD5SyVg';

            $video_blog_post_content .= "\n\n[embed]{$video_blog_video_url}[/embed]";

            $video_blog_my_post = array(
                'post_title'    => wp_strip_all_tags($video_blog_post_title),
                'post_content'  => $video_blog_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
            );

            // Insert the post into the database
            $video_blog_post_id = wp_insert_post($video_blog_my_post);

            // Check if the post was successfully created
            if (!is_wp_error($video_blog_post_id)) {
                // Set the theme mod for the post ID
                set_theme_mod('video_blog_top_video_two', $video_blog_post_id);
            } else {
                // Handle the error if the post creation failed
                error_log('Post creation failed: ' . $video_blog_post_id->get_error_message());
            }

            // Top Video Section - - video3
            $video_blog_post_title = 'Lorem Ipsum is simply dummy text industry';
            $video_blog_post_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 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.';

            $video_blog_video_url = 'https://www.youtube.com/embed/9xwazD5SyVg';

            $video_blog_post_content .= "\n\n[embed]{$video_blog_video_url}[/embed]";

            $video_blog_my_post = array(
                'post_title'    => wp_strip_all_tags($video_blog_post_title),
                'post_content'  => $video_blog_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
            );

            // Insert the post into the database
            $video_blog_post_id = wp_insert_post($video_blog_my_post);

            // Check if the post was successfully created
            if (!is_wp_error($video_blog_post_id)) {
                // Set the theme mod for the post ID
                set_theme_mod('video_blog_top_video_three', $video_blog_post_id);
            } else {
                // Handle the error if the post creation failed
                error_log('Post creation failed: ' . $video_blog_post_id->get_error_message());
            }

          
            //Playlist Section//
            set_theme_mod('video_blog_playlist', 'playlist1');

            // Define post category names and post titles
            $video_blog_category_names = array('playlist1', 'playlist2');
            $video_blog_title_array = array(
                array("Lorem ipsum is simply dummy", "Lorem ipsum is simply dummy", "Lorem ipsum is simply dummy"),
                array("Lorem ipsum is simply dummy", "Lorem ipsum is simply dummy", "Lorem ipsum is simply dummy")
            );

            foreach ($video_blog_category_names as $video_blog_index => $video_blog_category_name) {
                // Create or retrieve the post category term ID
                $video_blog_term = term_exists($video_blog_category_name, 'category');
                if ($video_blog_term === 0 || $video_blog_term === null) {
                    // If the term does not exist, create it
                    $video_blog_term = wp_insert_term($video_blog_category_name, 'category');
                }
                if (is_wp_error($video_blog_term)) {
                    error_log('Error creating category: ' . $video_blog_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                for ($video_blog_j = 0; $video_blog_j < 3; $video_blog_j++) {
                    // Create post content
                    $video_blog_title = $video_blog_title_array[$video_blog_index][$video_blog_j];
                    // Use unique or common YouTube URLs
                    $video_blog_video_url = 'https://www.youtube.com/embed/9xwazD5SyVg';

                    // ✅ Embed video using iframe directly
                    $video_blog_content = '<p>It is the Taisho Period in Japan. Tanjiro, a kindhearted boy who sells charcoal for a living, finds his family slaughtered by a demon. To make matters worse, his younger sister Nezuko, the sole survivor, has been transformed into a demon herself. Though devastated by this grim reality, Tanjiro resolves to become a “demon slayer” so that he can turn his sister back into a human, and kill the demon that massacred his family.</p>';
                    $video_blog_content .= '<div style="margin-top:15px;"><embed width="560" height="315" src="' . esc_url($video_blog_video_url) . '" frameborder="0" allowfullscreen></embed></div>';

                    // Create post post object
                    $video_blog_my_post = array(
                        'post_title'    => wp_strip_all_tags($video_blog_title),
                        'post_content'  => $video_blog_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $video_blog_post_id = wp_insert_post($video_blog_my_post);

                    if (is_wp_error($video_blog_post_id)) {
                        error_log('Error creating post: ' . $video_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($video_blog_post_id, array((int)$video_blog_term['term_id']));
                }
            }
           

            //Category Section //
            set_theme_mod( 'vw_blog_magazine_category', 'category1');
 
            // Define post category names and post titles
            $vw_blog_magazine_category_names = array('category1', 'category2');
            $vw_blog_magazine_title_array = array(
                array("FASHION", "FOOD & DRINK", "SPORTS","LIFESTYLE"),
                array("FASHION", "FOOD & DRINK", "SPORTS","LIFESTYLE")
            );

            foreach ($vw_blog_magazine_category_names as $vw_blog_magazine_index => $vw_blog_magazine_category_name) {
                // Create or retrieve the post category term ID
                $vw_blog_magazine_term = term_exists($vw_blog_magazine_category_name, 'category');
                if ($vw_blog_magazine_term === 0 || $vw_blog_magazine_term === null) {
                    // If the term does not exist, create it
                    $vw_blog_magazine_term = wp_insert_term($vw_blog_magazine_category_name, 'category');
                }
                if (is_wp_error($vw_blog_magazine_term)) {
                    error_log('Error creating category: ' . $vw_blog_magazine_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                for ($vw_blog_magazine_i = 0; $vw_blog_magazine_i < 4; $vw_blog_magazine_i++) {
                    // Create post content
                    $vw_blog_magazine_title = $vw_blog_magazine_title_array[$vw_blog_magazine_index][$vw_blog_magazine_i];
                    $vw_blog_magazine_content = 'Te Obtinuit ut adepto satis somno aliisque deliciae.';

                    // Create post post object
                    $vw_blog_magazine_my_post = array(
                        'post_title'    => wp_strip_all_tags($vw_blog_magazine_title),
                        'post_content'  => $vw_blog_magazine_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $vw_blog_magazine_post_id = wp_insert_post($vw_blog_magazine_my_post);

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

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

                    // Handle the featured image using media_sideload_image
                    $vw_blog_magazine_image_url = get_theme_file_uri() . '/images/postcat' . ($vw_blog_magazine_i + 1) . '.png';
                    $vw_blog_magazine_image_id = media_sideload_image($vw_blog_magazine_image_url, $vw_blog_magazine_post_id, null, 'id');

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

            //Category section 2 //
            set_theme_mod( 'vw_blog_magazine_section_two', 'Postcategory1');
 
            // Define post category names and post titles
            $vw_blog_magazine_category_names = array('Postcategory1', 'Postcategory2');
            $vw_blog_magazine_title_array = array(
                array("GIRL FASHION LIFESTYLE", "GIRL FASHION LIFESTYLE"),
                array("GIRL FASHION LIFESTYLE", "GIRL FASHION LIFESTYLE")
            );

            foreach ($vw_blog_magazine_category_names as $vw_blog_magazine_index => $vw_blog_magazine_category_name) {
                // Create or retrieve the post category term ID
                $vw_blog_magazine_term = term_exists($vw_blog_magazine_category_name, 'category');
                if ($vw_blog_magazine_term === 0 || $vw_blog_magazine_term === null) {
                    // If the term does not exist, create it
                    $vw_blog_magazine_term = wp_insert_term($vw_blog_magazine_category_name, 'category');
                }
                if (is_wp_error($vw_blog_magazine_term)) {
                    error_log('Error creating category: ' . $vw_blog_magazine_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                for ($vw_blog_magazine_i = 0; $vw_blog_magazine_i < 2; $vw_blog_magazine_i++) {
                    // Create post content
                    $vw_blog_magazine_title = $vw_blog_magazine_title_array[$vw_blog_magazine_index][$vw_blog_magazine_i];
                    $vw_blog_magazine_content = ' 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.';

                    // Create post post object
                    $vw_blog_magazine_my_post = array(
                        'post_title'    => wp_strip_all_tags($vw_blog_magazine_title),
                        'post_content'  => $vw_blog_magazine_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $vw_blog_magazine_post_id = wp_insert_post($vw_blog_magazine_my_post);

                    update_post_meta($vw_blog_magazine_post_id, 'vw_blog_magazine_expertise', "OUR EXPERTISE");

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

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

                    // Handle the featured image using media_sideload_image
                    $vw_blog_magazine_image_url = get_theme_file_uri() . '/images/post' . ($vw_blog_magazine_i + 1) . '.png';
                    $vw_blog_magazine_image_id = media_sideload_image($vw_blog_magazine_image_url, $vw_blog_magazine_post_id, null, 'id');

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


            
        }
    ?>


    <p><?php esc_html_e('Please back up your website if it’s already live with data. This importer will overwrite your existing settings with the new customizer values for Video Blog', 'video-blog'); ?></p>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=video_blog_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('video_blog_demo_import_completed')) : ?>
            <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'video-blog'); ?>" class="button button-primary button-large">
        <?php endif; ?>
        <div id="spinner" style="display:none;">         
            <img src="<?php echo esc_url(get_theme_file_uri()); ?>/images/spinner.png" alt="" />
        </div>
    </form>
    <script type="text/javascript">
        function validate(form) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show the spinner
                document.getElementById('spinner').style.display = 'block';
                // Allow the form to be submitted
                return true;
            } 
            else {
                return false;
            }
        }
    </script>
</div>

