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

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

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

    // Check if ibtana visual editor is installed and activated
    if (!is_plugin_active('ibtana-visual-editor/plugin.php')) {
      // Install the plugin if it doesn't exist
      $forest_jungle_safari_plugin_slug = 'ibtana-visual-editor';
      $forest_jungle_safari_plugin_file = 'ibtana-visual-editor/plugin.php';

      // Check if plugin is installed
      $forest_jungle_safari_installed_plugins = get_plugins();
      if (!isset($forest_jungle_safari_installed_plugins[$forest_jungle_safari_plugin_file])) {
          include_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
          include_once(ABSPATH . 'wp-admin/includes/file.php');
          include_once(ABSPATH . 'wp-admin/includes/misc.php');
          include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');

          // Install the plugin
          $forest_jungle_safari_upgrader = new Plugin_Upgrader();
          $forest_jungle_safari_upgrader->install('https://downloads.wordpress.org/plugin/ibtana-visual-editor.latest-stable.zip');
      }
      // Activate the plugin
      activate_plugin($forest_jungle_safari_plugin_file);
    }    
      // Create / Get Menu FIRST
    $forest_jungle_safari_menu_name = 'Primary Menu';

    $forest_jungle_safari_menu = wp_get_nav_menu_object($forest_jungle_safari_menu_name);

    if (!$forest_jungle_safari_menu) {
        $forest_jungle_safari_menu_id = wp_create_nav_menu($forest_jungle_safari_menu_name);
    } else {
        $forest_jungle_safari_menu_id = $forest_jungle_safari_menu->term_id;
    }

    // Create a front page and assign the template
    $forest_jungle_safari_home_page = null;
    // Using WP_Query instead of get_page_by_title()
    $forest_jungle_safari_home_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Home',
           'post_status' => 'publish',
           'posts_per_page' => 1
    ));
        if (!$forest_jungle_safari_home_query->have_posts()) {
           $forest_jungle_safari_home_title = 'Home';           
           // Create the page
           $forest_jungle_safari_home = array(
               'post_type' => 'page',
               'post_title' => $forest_jungle_safari_home_title,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'home'
           );
           $forest_jungle_safari_home_id = wp_insert_post($forest_jungle_safari_home);
        } else {
           $forest_jungle_safari_home_page = $forest_jungle_safari_home_query->posts[0];
           $forest_jungle_safari_home_id = $forest_jungle_safari_home_page->ID;
        }

        // Set the home page template
        add_post_meta($forest_jungle_safari_home_id, '_wp_page_template', 'page-template/custom-home-page.php');

        // Set the static front page
        update_option('page_on_front', $forest_jungle_safari_home_id);
        update_option('show_on_front', 'page');

        // Create another page if needed
        $forest_jungle_safari_page_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Page',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));

        if (!$forest_jungle_safari_page_query->have_posts()) {
           $forest_jungle_safari_page_title = 'Page';
           $forest_jungle_safari_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.';
           
            $forest_jungle_safari_page = array(
               'post_type' => 'page',
               'post_title' => $forest_jungle_safari_page_title,
               'post_content' => $forest_jungle_safari_content,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'page'
            );
            $forest_jungle_safari_page_id = wp_insert_post($forest_jungle_safari_page);
        }   
        
        // Create another page if needed
        $forest_jungle_safari_page_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Packages',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));

        if (!$forest_jungle_safari_page_query->have_posts()) {
           $forest_jungle_safari_page_title = 'Packages';
           $forest_jungle_safari_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.';
           
            $forest_jungle_safari_page = array(
               'post_type' => 'page',
               'post_title' => $forest_jungle_safari_page_title,
               'post_content' => $forest_jungle_safari_content,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'page'
            );
            $forest_jungle_safari_page_id = wp_insert_post($forest_jungle_safari_page);
        }
        
        // Create another page if needed
        $forest_jungle_safari_page_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Events',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));

        if (!$forest_jungle_safari_page_query->have_posts()) {
           $forest_jungle_safari_page_title = 'Events';
           $forest_jungle_safari_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.';
           
            $forest_jungle_safari_page = array(
               'post_type' => 'page',
               'post_title' => $forest_jungle_safari_page_title,
               'post_content' => $forest_jungle_safari_content,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'page'
            );
            $forest_jungle_safari_page_id = wp_insert_post($forest_jungle_safari_page);
        }
        
                // Create Blog Page 
                $forest_jungle_safari_blog_page_title = 'Blog';
                $forest_jungle_safari_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($forest_jungle_safari_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$forest_jungle_safari_blog_page_query->have_posts()) {
                    $forest_jungle_safari_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $forest_jungle_safari_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $forest_jungle_safari_blog_page_id = wp_insert_post($forest_jungle_safari_blog_page);
                    update_option('page_for_posts', $forest_jungle_safari_blog_page_id);
                    wp_update_nav_menu_item($forest_jungle_safari_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'forest-jungle-safari'),
                    'menu-item-url'        => get_permalink($forest_jungle_safari_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $forest_jungle_safari_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    'menu-item-type'       => 'post_type',
                    ));
                }

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

        //end     

        // Top Bar start //
        set_theme_mod( 'forest_jungle_safari_topbar_location_text_icon', 'fa-solid fa-bullhorn' ); 
        set_theme_mod( 'forest_jungle_safari_topbar_add_text_advertising', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.' );
        set_theme_mod( 'forest_jungle_safari_topbar_about_us', 'About Us' ); 
        set_theme_mod( 'forest_jungle_safari_about_us_link', '#' );
        set_theme_mod( 'forest_jungle_safari_topbar_contact_us', 'Contact Us' ); 
        set_theme_mod( 'forest_jungle_safari_contact_us_link', '#' );
        set_theme_mod( 'forest_jungle_safari_topbar_privacy_policy', 'Privacy Policy' );
        set_theme_mod( 'forest_jungle_safari_privacy_policy_link', '#' );
        set_theme_mod( 'forest_jungle_safari_appointment_button_text', 'Book Now' );
        set_theme_mod( 'forest_jungle_safari_appointment_btn_link', '#' );

        // slider section start //         
        set_theme_mod( 'forest_jungle_safari_slide_number', '3' );
        for($forest_jungle_safari_i=1; $forest_jungle_safari_i<=3; $forest_jungle_safari_i++) {
            set_theme_mod( 'forest_jungle_safari_slider_image'.$forest_jungle_safari_i, get_template_directory_uri().'/assets/images/banner'.$forest_jungle_safari_i.'.png' );
            set_theme_mod( 'forest_jungle_safari_banner_background_image_sec'.$forest_jungle_safari_i, get_template_directory_uri().'/assets/images/animal'.$forest_jungle_safari_i.'.png' );
            set_theme_mod( 'forest_jungle_safari_tagline_title'.$forest_jungle_safari_i, 'Explore The VW Forest Of Animal' );
            set_theme_mod( 'forest_jungle_safari_designation_text'.$forest_jungle_safari_i, '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 took a galley' );                
        }  

        // Popular Section start //
        set_theme_mod( 'forest_jungle_safari_popular_heading', 'Popular Packages' ); 
        set_theme_mod( 'forest_jungle_safari_popular_text', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua' );
        set_theme_mod( 'forest_jungle_safari_claases_number', '3' );

        // select Post Box
        $forest_jungle_safari_title_array = array("Breakfast With Lion", "Student Zoo Package", "Night At Zoo");
        for ($forest_jungle_safari_i = 1; $forest_jungle_safari_i <= 3; $forest_jungle_safari_i++) {
          set_theme_mod( 'forest_jungle_safari_user'.$forest_jungle_safari_i, '$19.00' );
          set_theme_mod( 'forest_jungle_safari_service_button_label'.$forest_jungle_safari_i, 'Read More' );
            // Create post
            $forest_jungle_safari_title = $forest_jungle_safari_title_array[$forest_jungle_safari_i - 1];
            $forest_jungle_safari_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting.';            
            $forest_jungle_safari_my_post = array(
                 'post_title'   => wp_strip_all_tags($forest_jungle_safari_title),
                 'post_content' => $forest_jungle_safari_content,
                 'post_status'  => 'publish',
                 'post_type'    => 'post',
            );
            $forest_jungle_safari_post_id = wp_insert_post($forest_jungle_safari_my_post);
            set_theme_mod('forest_jungle_safari_services_category' . $forest_jungle_safari_i, $forest_jungle_safari_post_id);
            // Get image URL
            $forest_jungle_safari_image_url = get_template_directory_uri() . '/assets/images/post' . $forest_jungle_safari_i . '.png';
            $forest_jungle_safari_image_data = wp_remote_get($forest_jungle_safari_image_url);
            if (!is_wp_error($forest_jungle_safari_image_data)) {
                 $forest_jungle_safari_image_body = wp_remote_retrieve_body($forest_jungle_safari_image_data);
                 $forest_jungle_safari_upload_dir = wp_upload_dir();
                 $forest_jungle_safari_image_name = 'post' . $forest_jungle_safari_i . '.png';
                 $uploaded_file = wp_upload_bits($forest_jungle_safari_image_name, null, $forest_jungle_safari_image_body);
                 if (!$uploaded_file['error']) {
                     // Set attachment data
                     $attachment = array(
                         'post_mime_type' => $uploaded_file['type'],
                         'post_title'     => sanitize_file_name($forest_jungle_safari_image_name),
                         'post_content'   => '',
                         'post_status'    => 'inherit',
                     );
                     $forest_jungle_safari_attach_id = wp_insert_attachment($attachment, $uploaded_file['file'], $forest_jungle_safari_post_id);
                     require_once(ABSPATH . 'wp-admin/includes/image.php');
                     $forest_jungle_safari_attach_data = wp_generate_attachment_metadata($forest_jungle_safari_attach_id, $uploaded_file['file']);
                     wp_update_attachment_metadata($forest_jungle_safari_attach_id, $forest_jungle_safari_attach_data);
                     set_post_thumbnail($forest_jungle_safari_post_id, $forest_jungle_safari_attach_id);
                 }
            }
          }      

          // Special Services Section //
          set_theme_mod( 'forest_jungle_safari_special_heading', 'Special Services' );
          set_theme_mod( 'forest_jungle_safari_special_text', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua' );

          set_theme_mod( 'forest_jungle_safari_services_number', '4' );

            $forest_jungle_safari_service_text_array = array("Photos With Animal", "Restaurants & Fast Foods", "Free Wi-Fi With Hi Speed", "Many Competitionss");

            $forest_jungle_safari_service_icon_array = array("fa-solid fa-camera", "fa-solid fa-utensils", "fa-solid fa-wifi", "fa-solid fa-trophy");

            for($forest_jungle_safari_i=1; $forest_jungle_safari_i<=4; $forest_jungle_safari_i++) {
              
              // title setting
                $theme_mod_key = 'forest_jungle_safari_client_tagline_title' . $forest_jungle_safari_i;
                $theme_mod_value = $forest_jungle_safari_service_text_array[$forest_jungle_safari_i - 1];
                set_theme_mod($theme_mod_key, $theme_mod_value);  
                // title setting
                $theme_mod_key = 'forest_jungle_safari_services_icon' . $forest_jungle_safari_i;
                $theme_mod_value = $forest_jungle_safari_service_icon_array[$forest_jungle_safari_i - 1];
                set_theme_mod($theme_mod_key, $theme_mod_value); 
                // paragraph setting
                set_theme_mod( 'forest_jungle_safari_client_text'.$forest_jungle_safari_i, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.' );                    
            }  
                echo "<script>window.location.href='" . admin_url('themes.php?page=forest_jungle_safari_guide') . "';</script>";
                //Copyright Text
                set_theme_mod( 'forest_jungle_safari_footer_text', 'By VWThemes' );             
    }
  ?>
  
	<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 Forest Jungle Safari', 'forest-jungle-safari'); ?></p>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=forest_jungle_safari_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('forest_jungle_safari_demo_import_completed')) : ?>
            <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'forest-jungle-safari'); ?>" class="button button-primary button-large">
        <?php endif; ?>
        <div id="spinner" style="display:none;">         
            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/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?")) {
                document.getElementById('spinner').style.display = 'block';
                return true;
            } 
            else {
                return false;
            }
        }
    </script>
</div>

