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

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

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

            // ------- Create Nav Menu --------
            $movie_critic_review_menuname = 'Main Menus';
            $movie_critic_review_bpmenulocation = 'primary';
            $movie_critic_review_menu_exists = wp_get_nav_menu_object($movie_critic_review_menuname);

            if (!$movie_critic_review_menu_exists) {
                $movie_critic_review_menu_id = wp_create_nav_menu($movie_critic_review_menuname);

                // Create Home Page
                $movie_critic_review_home_title = 'Home';
                $movie_critic_review_home = array(
                    'post_type' => 'page',
                    'post_title' => $movie_critic_review_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $movie_critic_review_home_id = wp_insert_post($movie_critic_review_home);
                // Assign Home Page Template
                add_post_meta($movie_critic_review_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', $movie_critic_review_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'movie-critic-review'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $movie_critic_review_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $movie_critic_review_pages_title = 'Pages';
                $movie_critic_review_pages_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.';
                $movie_critic_review_pages = array(
                    'post_type' => 'page',
                    'post_title' => $movie_critic_review_pages_title,
                    'post_content' => $movie_critic_review_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $movie_critic_review_pages_id = wp_insert_post($movie_critic_review_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'movie-critic-review'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $movie_critic_review_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $movie_critic_review_about_title = 'About Us';
                $movie_critic_review_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.';
                $movie_critic_review_about = array(
                    'post_type' => 'page',
                    'post_title' => $movie_critic_review_about_title,
                    'post_content' => $movie_critic_review_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $movie_critic_review_about_id = wp_insert_post($movie_critic_review_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'movie-critic-review'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $movie_critic_review_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Browse Page with Dummy Content
                $movie_critic_review_about_title = 'Browse';
                $movie_critic_review_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.';
                $movie_critic_review_about = array(
                    'post_type' => 'page',
                    'post_title' => $movie_critic_review_about_title,
                    'post_content' => $movie_critic_review_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'browse'
                );
                $movie_critic_review_about_id = wp_insert_post($movie_critic_review_about);

                // Add Browse Page to Menu
                wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                    'menu-item-title' => __('Browse', 'movie-critic-review'),
                    'menu-item-classes' => 'browse',
                    'menu-item-url' => home_url('/browse/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $movie_critic_review_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page (Empty page used as Posts Page)
                $blog_page_title = 'Blog';

                $blog_page_query = new WP_Query(array(
                    'post_type' => 'page',
                    'title' => $blog_page_title,
                    'post_status' => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$blog_page_query->have_posts()) {
                    $blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $blog_page_id = wp_insert_post($blog_page);

                    update_option('page_for_posts', $blog_page_id);

                    wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                        'menu-item-title'   => __('Blog', 'movie-critic-review'),
                        'menu-item-url'     => home_url('/blog/'),
                        'menu-item-status'  => 'publish',
                        'menu-item-object-id' => $blog_page_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                    ));
                }
                
               // Create Contact Us Page with Dummy Content
                $movie_critic_review_about_title = 'Contact Us';
                $movie_critic_review_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.';
                $movie_critic_review_about = array(
                    'post_type' => 'page',
                    'post_title' => $movie_critic_review_about_title,
                    'post_content' => $movie_critic_review_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'Contact Us'
                );
                $movie_critic_review_about_id = wp_insert_post($movie_critic_review_about);

                // Add Contact Us Page to Menu
                wp_update_nav_menu_item($movie_critic_review_menu_id, 0, array(
                    'menu-item-title' => __('Contact Us', 'movie-critic-review'),
                    'menu-item-classes' => 'contact us',
                    'menu-item-url' => home_url('/contact us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $movie_critic_review_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($movie_critic_review_bpmenulocation)) {
                    $locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($locations)) {
                        $locations = array();
                    }
                    $locations[$movie_critic_review_bpmenulocation] = $movie_critic_review_menu_id;
                    set_theme_mod('nav_menu_locations', $locations);
                }

        }
            // Set the demo import completion flag
    		update_option('movie_critic_review_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.', 'movie-critic-review') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="run-import view-site" target="_blank">' . esc_html__('View Site', 'movie-critic-review') . '</a></span>';
            //end


            // Top Bar //

            set_theme_mod( 'movie_critic_review_topbar_text', 'John Wick 4 movie review is finally here! Read it now.' );
            set_theme_mod( 'movie_critic_review_topbar_text_click', 'Click Here' );
            set_theme_mod( 'movie_critic_review_topbar_link', '#' );
            set_theme_mod( 'movie_critic_review_account_btn_link', '#' );
            set_theme_mod( 'movie_critic_review_myaccount_icon', 'fas fa-user' );
        
            // slider section start //
            set_theme_mod( 'movie_critic_review_slider_small_title', 'Popeye The Sailor' );
            set_theme_mod( 'movie_critic_review_slider_button_text', 'Read Reviews' );
            set_theme_mod( 'movie_critic_review_slider_button_url', '#' );
            

            for($movie_critic_review_i=1;$movie_critic_review_i<=3;$movie_critic_review_i++){
                $movie_critic_review_slider_title = 'Thrilling Adventure That Will Keep You On Edge';
                $movie_critic_review_slider_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita.';
                   // Create post object
                $my_post = array(
                'post_title'    => wp_strip_all_tags( $movie_critic_review_slider_title ),
                'post_content'  => $movie_critic_review_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );

                // Insert the post into the database
                $movie_critic_review_post_id = wp_insert_post( $my_post );

                if ($movie_critic_review_post_id) {
                  // Set the theme mod for the slider page
                  set_theme_mod('movie_critic_review_slider_page' . $movie_critic_review_i, $movie_critic_review_post_id);

                   $movie_critic_review_image_url = get_template_directory_uri().'/assets/images/banner'.$movie_critic_review_i.'.png';

                 $movie_critic_review_image_id = media_sideload_image($movie_critic_review_image_url, $movie_critic_review_post_id, null, 'id');

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


            // Featured Section
            set_theme_mod( 'movie_critic_review_featured_heading', 'Featured Today' );
            set_theme_mod( 'movie_critic_review_featured_small_title', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' );    
            set_theme_mod( 'movie_critic_review_feature_today_video_icon_text', 'Watch Trailer' );
            set_theme_mod( 'movie_critic_review_meta_field_separator_text', '.' );
            set_theme_mod('movie_critic_review_video_category', 'vediocategory1' );
            
            // Define post category names and post titles
            $movie_critic_review_category_names = array('vediocategory1', 'vediocategory2');
            $movie_critic_review_title_array = array(
                array("Movie Title Here", "Movie Title Here", "Movie Title Here", "Movie Title Here"),
                array("Movie Title Here", "Movie Title Here", "Movie Title Here", "Movie Title Here")
            );

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

                for ($movie_critic_review_i = 0; $movie_critic_review_i < 4; $movie_critic_review_i++) {
                    // Create post content
                    $movie_critic_review_title = $movie_critic_review_title_array[$movie_critic_review_index][$movie_critic_review_i];

                    // Create post post object
                    $movie_critic_review_my_post = array(
                        'post_title'    => wp_strip_all_tags($movie_critic_review_title),                       
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

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

                    // Insert the post into the database
                    $movie_critic_review_post_id = wp_insert_post($movie_critic_review_my_post);

                    update_post_meta($movie_critic_review_post_id, 'meta_blog_user_video', "https://www.youtube.com/embed/9xwazD5SyVg");
                    update_post_meta($movie_critic_review_post_id, 'meta_blog_user_reviews_rate', "8.9");
                    update_post_meta($movie_critic_review_post_id, 'meta_feature_today_date_title', "Directer Name");
                    update_post_meta($movie_critic_review_post_id, 'meta_feature_today_date', "Movie Release Year");

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

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

                    // Handle the featured image using media_sideload_image
                    $movie_critic_review_image_url = get_template_directory_uri() . '/assets/images/vedio-image' . ($movie_critic_review_i + 1) . '.png';
                    $movie_critic_review_image_id = media_sideload_image($movie_critic_review_image_url, $movie_critic_review_post_id, null, 'id');

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

	
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=movie_critic_review_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('movie_critic_review_demo_import_completed')) : ?>
        <form method="post">
        <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 Movie Critic Review','movie-critic-review'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','movie-critic-review'); ?>" 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?")){
			    document.forms[0].submit();
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>
