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

        // If the demo import is completed, display the "View Site" button
        if ($educational_university_demo_import_completed) {
            echo '<br>';
            echo '<div class="success">Demo Import Successful</div>';
            echo '<br>';
            echo '<hr>';
            echo '<br>';
            echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'educational-university' ) . '</span>';
            echo '<br>';
            echo '<br>';
            echo '<br>';
            echo '<div class="view-site-btn">';
            echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
            echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
            echo '</div>';
        }
    if ( isset( $_POST['submit'] ) ) {

        echo '<div class="plugin-notice">';

            // CBX Bookmark & Favorite plugin installation and activation
            if (!is_plugin_active('cbxwpbookmark/cbxwpbookmark.php')) {
                $educational_university_cbx_plugin_slug = 'cbxwpbookmark';
                $educational_university_cbx_plugin_file = 'cbxwpbookmark/cbxwpbookmark.php';
                $educational_university_installed_plugins = get_plugins();

                if (!isset($educational_university_installed_plugins[$educational_university_cbx_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');

                    $educational_university_upgrader = new Plugin_Upgrader();
                    $educational_university_upgrader->install('https://downloads.wordpress.org/plugin/cbxwpbookmark.latest-stable.zip');
                }

                activate_plugin($educational_university_cbx_plugin_file);
            }

            // Check if Classic Blog Grid plugin is installed
            if (!is_plugin_active('classic-blog-grid/classic-blog-grid.php')) {
                // Plugin slug and file path for Classic Blog Grid
                $educational_university_plugin_slug = 'classic-blog-grid';
                $educational_university_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $educational_university_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $educational_university_installed_plugins = get_plugins();
                    if ( ! isset( $educational_university_installed_plugins[ $educational_university_plugin_file ] ) ) {
            
                        // Include necessary files to install plugins
                        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' );
            
                        // Download and install Classic Blog Grid
                        $educational_university_upgrader = new Plugin_Upgrader();
                        $educational_university_upgrader->install( 'https://downloads.wordpress.org/plugin/classic-blog-grid.latest-stable.zip' );
                    }
            
                    // Activate the Classic Blog Grid plugin after installation (if needed)
                    activate_plugin( $educational_university_plugin_file );
                }
            }

        echo '</div>';
        
        // ------- Create Main Menu --------
        $educational_university_menuname = 'Primary Menu';
        $educational_university_bpmenulocation = 'primary';
        $educational_university_menu_exists = wp_get_nav_menu_object( $educational_university_menuname );
    
        if ( !$educational_university_menu_exists ) {
            $educational_university_menu_id = wp_create_nav_menu( $educational_university_menuname );

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

            // Create a new Page 
            $educational_university_pages_title = 'Pages';
            $educational_university_pages_content = '<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.</p>';
            $educational_university_pages = array(
                'post_type'    => 'page',
                'post_title'   => $educational_university_pages_title,
                'post_content' => $educational_university_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $educational_university_pages_id = wp_insert_post($educational_university_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($educational_university_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'educational-university'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $educational_university_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $educational_university_about_title = 'About Us';
            $educational_university_about_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. 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.';
            $educational_university_about = array(
                'post_type'    => 'page',
                'post_title'   => $educational_university_about_title,
                'post_content' => $educational_university_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $educational_university_about_id = wp_insert_post($educational_university_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($educational_university_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'educational-university'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $educational_university_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign the menu to the primary location if not already set
            if ( ! has_nav_menu( $educational_university_bpmenulocation ) ) {
                $educational_university_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $educational_university_locations ) ) {
                    $educational_university_locations = array();
                }
                $educational_university_locations[ $educational_university_bpmenulocation ] = $educational_university_menu_id;
                set_theme_mod( 'nav_menu_locations', $educational_university_locations );
            }
        }

        //Logo
        set_theme_mod( 'educational_university_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));
       
        //Topbar Section
        set_theme_mod( 'educational_university_topbar_text_title','Lorem Ipsum is simply dummy text of the printing and typesetting industry.');
        set_theme_mod( 'educational_university_header_btn_text','Contact Us');
        set_theme_mod( 'educational_university_header_btn_url','#');
        set_theme_mod( 'educational_university_email_address','university@example.com');
        set_theme_mod( 'educational_university_phone_number','+1 123 456 7890');
        set_theme_mod( 'educational_university_fb_link','https://www.facebook.com');
        set_theme_mod( 'educational_university_instagram_link','https://www.instagram.com');
        set_theme_mod( 'educational_university_twitter_link','https://www.twitter.com');
        set_theme_mod( 'educational_university_youtube_link','https://www.youtube.com');

        //Banner Section
        set_theme_mod( 'educational_university_banner_small_title','Welcome To University');
        set_theme_mod( 'educational_university_small_title_icon','fa-solid fa-graduation-cap');
        set_theme_mod( 'educational_university_button_text','Get Started');
        set_theme_mod( 'educational_university_banner_right_img', esc_url( get_template_directory_uri().'/images/banner-right.png'));

        // Create a single page 
        $educational_university_banner_title = 'Have your dream site in minutes For University';
        $educational_university_banner_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,';

        // Create the post object
        $educational_university_post = array(
            'post_title'    => wp_strip_all_tags($educational_university_banner_title),
            'post_content'  => $educational_university_banner_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
        );

        // Insert the post into the database
        $educational_university_post_id = wp_insert_post($educational_university_post);

        // If the post was successfully created
        if ($educational_university_post_id && !is_wp_error($educational_university_post_id)) {
            // Set the theme mod for the single banner page
            set_theme_mod('educational_university_banner_pageboxes', $educational_university_post_id);

            // Set the featured image for the post
            $educational_university_image_url = get_template_directory_uri() . '/images/banner.png';
            
            // Use media_sideload_image to upload and set the image
            $educational_university_image_id = media_sideload_image($educational_university_image_url, $educational_university_post_id, null, 'id');

            // Check if the image was successfully uploaded and set
            if (!is_wp_error($educational_university_image_id)) {
                // Set the image as the post's featured image
                set_post_thumbnail($educational_university_post_id, $educational_university_image_id);
            } else {
                // Log or handle the error
                error_log("Error setting the featured image: " . $educational_university_image_id->get_error_message());
            }

        } else {
            // Log or handle the error if the page creation failed
            error_log("Error creating the page: " . $educational_university_post_id->get_error_message());
        }

        // Degree & Courses Section
        set_theme_mod( 'educational_university_courses_title_icon','fa-solid fa-graduation-cap');
        set_theme_mod( 'educational_university_degree_text','Degree & Courses');
        set_theme_mod( 'educational_university_degree_title','Top Degree & Courses');
        
        // Set the default number of posts to show
        set_theme_mod('educational_university_claases_number', 4);
    
       // Sample posts data
        $educational_university_sample_posts = [
            [
                'title' => 'Bachelor Of Journalism And Mass Communication',
                'students' => '20',
                'time' => '4h52min',
                'price' => '$277',
                'level' => 'Expert',
                'image' => get_template_directory_uri() . '/images/course1.png',
            ],
            [
                'title' => 'Master Of Business Administration MBA',
                'students' => '30',
                'time' => '6 Months',
                'price' => '$399',
                'level' => 'Expert',
                'image' => get_template_directory_uri() . '/images/course2.png',
            ],
            [
                'title' => 'Post Graduate Diploma In Applied Statistics',
                'students' => '50',
                'time' => '1 Year',
                'price' => '$349',
                'level' => 'Expert',
                'image' => get_template_directory_uri() . '/images/course3.png',
            ],
            [
                'title' => 'Master Of Applied Data Science Courses',
                'students' => '40',
                'time' => '8 Months',
                'price' => '$377',
                'level' => 'Advanced',
                'image' => get_template_directory_uri() . '/images/course4.png',
            ],
        ];

        foreach ($educational_university_sample_posts as $educational_university_index => $educational_university_post_data) {
            $educational_university_post_id = wp_insert_post([
                'post_title'   => $educational_university_post_data['title'],
                'post_content' => 'This is a demo course description.',
                'post_status'  => 'publish',
                'post_type'    => 'post',
            ]);
        
            if ($educational_university_post_id) {
                set_theme_mod('educational_university_select_post' . ($educational_university_index + 1), $educational_university_post_id);
                set_theme_mod('educational_university_add_students_count' . ($educational_university_index + 1), $educational_university_post_data['students']);
                set_theme_mod('educational_university_add_time' . ($educational_university_index + 1), $educational_university_post_data['time']);
                set_theme_mod('educational_university_add_price' . ($educational_university_index + 1), $educational_university_post_data['price']);
                set_theme_mod('educational_university_add_level' . ($educational_university_index + 1), $educational_university_post_data['level']);
        
                // Upload and set the featured image safely
                $educational_university_image_url = $educational_university_post_data['image'];
                if (!empty($educational_university_image_url)) {
                    require_once ABSPATH . 'wp-admin/includes/media.php';
                    require_once ABSPATH . 'wp-admin/includes/file.php';
                    require_once ABSPATH . 'wp-admin/includes/image.php';
        
                    // Upload and attach the image
                    $educational_university_attach_id = media_sideload_image($educational_university_image_url, $educational_university_post_id, null, 'id');
        
                    if (!is_wp_error($educational_university_attach_id)) {
                        set_post_thumbnail($educational_university_post_id, $educational_university_attach_id);
                    }
                }
            }
        }        
      
        // Show success message and the "View Site" button
        update_option('educational_university_demo_import_completed', true);
        echo '<br>';
        echo '<div class="success">Demo Import Successful</div>';
        echo '<br>';
        echo '<hr>';
        echo '<br>';
        echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'educational-university' ) . '</span>';
        echo '<br>';
    }
     ?>
    <ul>
        <li>
        <?php 
        // Check if the form is submitted
        if ( !isset( $_POST['submit'] ) ) : ?>
            <!-- Show demo importer form only if it's not submitted -->
            <?php if (!get_option('educational_university_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'educational-university' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'educational-university' ); ?></b><br><br>
                <small><b><?php echo esc_html('Please take a backup if your website is already live with data. This importer will overwrite existing data.', 'educational-university' ); ?></b></small><br><br>
                <form id="demo-importer-form" action="" method="POST" onsubmit="return runDemoImport();">
                    <input type="submit" name="submit" value="<?php echo esc_attr('Run Importer','educational-university'); ?>" class="button button-primary button-large">
                </form>
                <script type="text/javascript">
                    function runDemoImport() {
                        if (confirm('Do you really want to do this?')) {
                            document.getElementById('demo-import-loader').style.display = 'block';
                            return true;
                        }
                        return false;
                    }
                </script>
             <?php endif; ?>
         <?php 
        endif; 

        // Show "View Site" button after form submission
        if ( isset( $_POST['submit'] ) ) {
        echo '<div class="view-site-btn">';
        echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
        echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
        echo '</div>';
        }
        ?>
        </li>
    </ul>
 </div>