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

        // If the demo import is completed, display the "View Site" button
        if ($classic_portfolio_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.', 'classic-portfolio' ) . '</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>';
        }
     // POST and update the customizer and other related data of Classic Portfolio
    if ( isset( $_POST['submit'] ) ) {
        echo '<div class="plugin-notice">';
            // 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
                $classic_portfolio_plugin_slug = 'classic-blog-grid';
                $classic_portfolio_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_portfolio_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_portfolio_installed_plugins = get_plugins();
                    if ( ! isset( $classic_portfolio_installed_plugins[ $classic_portfolio_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
                        $classic_portfolio_upgrader = new Plugin_Upgrader();
                        $classic_portfolio_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( $classic_portfolio_plugin_file );
                }
            }
        echo '</div>';
        // ------- Create Main Menu --------
        $classic_portfolio_menuname = 'Primary Menu';
        $classic_portfolio_bpmenulocation = 'primary';
        $classic_portfolio_menu_exists = wp_get_nav_menu_object( $classic_portfolio_menuname );
    
        if (!$classic_portfolio_menu_exists) {
            // Create a new menu
            $classic_portfolio_menu_id = wp_create_nav_menu($classic_portfolio_menuname);

            // Define pages to be created
            $classic_portfolio_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'about' => array(
                    'title' => 'About',
                    '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>'
                ),
                'services' => array(
                    'title' => 'Services',
                    '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>'
                ),
                'doctors' => array(
                    'title' => 'Doctors',
                    '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>'
                ),
                'pages' => array(
                    'title' => '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>'
                ),
                'blog' => array(
                    'title' => 'Blog',
                    'content' => ''
                ),
                'contact-us' => array(
                    'title' => 'Contact Us',
                    '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>'
                ),
            );

            $classic_portfolio_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($classic_portfolio_pages as $classic_portfolio_slug => $classic_portfolio_data) {
                $classic_portfolio_existing_page = get_page_by_path($classic_portfolio_slug);

                if ($classic_portfolio_existing_page) {
                    // If the page already exists, use its ID
                    $classic_portfolio_page_id = $classic_portfolio_existing_page->ID;
                } else {
                    // Create a new page
                    $classic_portfolio_page_data = array(
                        'post_type'    => 'page',
                        'post_title'   => $classic_portfolio_data['title'],
                        'post_content' => isset($classic_portfolio_data['content']) ? $classic_portfolio_data['content'] : '',
                        'post_status'  => 'publish',
                        'post_author'  => get_current_user_id(), // Set author dynamically
                        'post_name'    => $classic_portfolio_slug,
                    );

                    $classic_portfolio_page_id = wp_insert_post($classic_portfolio_page_data);

                    // Assign custom page template if specified
                    if (!empty($classic_portfolio_data['template'])) {
                        update_post_meta($classic_portfolio_page_id, '_wp_page_template', $classic_portfolio_data['template']);
                    }
                }

                // Store the page IDs
                $classic_portfolio_page_ids[$classic_portfolio_slug] = $classic_portfolio_page_id;
            }

            // Set homepage and blog page
            update_option('page_for_posts', $classic_portfolio_page_ids['blog']);
            update_option('page_on_front', $classic_portfolio_page_ids['home']);
            update_option('show_on_front', 'page');

            // Define menu items
            $classic_portfolio_menu_items = array(
                'home',
                'about',
                'services',
                'doctors',
                'pages',
                'blog',
                'contact-us'
            );

            // Add menu items dynamically
            foreach ($classic_portfolio_menu_items as $classic_portfolio_slug) {
                wp_update_nav_menu_item($classic_portfolio_menu_id, 0, array(
                    'menu-item-title' => esc_html($classic_portfolio_pages[$classic_portfolio_slug]['title']),
                    'menu-item-url' => get_permalink($classic_portfolio_page_ids[$classic_portfolio_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $classic_portfolio_page_ids[$classic_portfolio_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $classic_portfolio_locations = get_theme_mod('nav_menu_locations', array());
            $classic_portfolio_locations[$classic_portfolio_bpmenulocation] = $classic_portfolio_menu_id;
            set_theme_mod('nav_menu_locations', $classic_portfolio_locations);
        }

        //Logo
        set_theme_mod( 'classic_portfolio_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));

        //Header Section
        set_theme_mod( 'classic_portfolio_header_btn_text', 'Book Now');
        set_theme_mod( 'classic_portfolio_header_btn_url', '#');

        //Slider Section
        set_theme_mod( 'classic_portfolio_slider', true);
        set_theme_mod( 'classic_portfolio_banner_small_title', 'WELCOME TO MY PORTFOLIO');
        set_theme_mod( 'classic_portfolio_button_text', 'Contact Us');
        set_theme_mod( 'classic_portfolio_total_no_satisfied_customer', '4830');
        set_theme_mod( 'classic_portfolio_total_no_award_win', '90+');
        set_theme_mod( 'classic_portfolio_total_no_successfull_projects', '3065');
        set_theme_mod( 'classic_portfolio_total_year_experience', '15+');
        set_theme_mod( 'classic_portfolio_customer_review', '4.5');
        set_theme_mod( 'classic_portfolio_total_customer_review', '89k+');

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $classic_portfolio_page_title, $classic_portfolio_page_content = '' ) {
            $classic_portfolio_args = array(
                'post_type'      => 'page',
                'title'          => $classic_portfolio_page_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $classic_portfolio_query = new WP_Query( $classic_portfolio_args );

            if ( ! empty( $classic_portfolio_query->posts ) ) {
                return $classic_portfolio_query->posts[0];
            } else {
                // Create the page if it doesn't exist
                $classic_portfolio_page_id = wp_insert_post( array(
                    'post_type'    => 'page',
                    'post_title'   => $classic_portfolio_page_title,
                    'post_content' => $classic_portfolio_page_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1
                ));
                return $classic_portfolio_page_id;
            }
        }

        // Create Page
        $classic_portfolio_page_title = 'Hi I AM Kelvin Shah A Best Designer';
        $classic_portfolio_page_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.';
        $classic_portfolio_page_id = get_or_create_page_by_title( $classic_portfolio_page_title, $classic_portfolio_page_content );

        if ( $classic_portfolio_page_id ) {
            set_theme_mod( 'classic_portfolio_banner_pageboxes', $classic_portfolio_page_id );
        } else {
            error_log('Failed to create or fetch the "Welcome to Corporate Business Theme" page.');
        }

        $classic_portfolio_image_url = get_template_directory_uri().'/images/slider.png';
        $classic_portfolio_image_id = media_sideload_image($classic_portfolio_image_url, $classic_portfolio_page_id, null, 'id');
        if (!is_wp_error($classic_portfolio_image_id)) {
            // Set the downloaded image as the post's featured image
            set_post_thumbnail($classic_portfolio_page_id, $classic_portfolio_image_id);
        }   
        
        //category
        $classic_portfolio_featured_category_id = wp_create_category('Portfolio');
        set_theme_mod('classic_portfolio_about_catData', 'Portfolio');

        $classic_portfolio_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, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';

        for ($classic_portfolio_i = 0; $classic_portfolio_i < 4; $classic_portfolio_i++) {

            $classic_portfolio_my_post = array(
                'post_title'    => 'Lorem ipsum dolor sit amet',
                'post_content'  => $classic_portfolio_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_portfolio_featured_category_id),
            );

            $classic_portfolio_post_id = wp_insert_post($classic_portfolio_my_post);

            if (!is_wp_error($classic_portfolio_post_id)) {
                $classic_portfolio_image_url = get_template_directory_uri() . '/images/reviewer' . ($classic_portfolio_i + 1) . '.png';
                $classic_portfolio_image_id = media_sideload_image($classic_portfolio_image_url, $classic_portfolio_post_id, null, 'id');
                if (!is_wp_error($classic_portfolio_image_id)) {
                    set_post_thumbnail($classic_portfolio_post_id, $classic_portfolio_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $classic_portfolio_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($classic_portfolio_post_id, true));
            }
        }

        // My Experience Section             
        set_theme_mod( 'classic_portfolio_disabled_pgboxes', true);
        set_theme_mod( 'classic_portfolio_headingtext1', 'My Experience');
        set_theme_mod( 'classic_portfolio_headingtext_para', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry');
        set_theme_mod( 'classic_portfolio_education_btn_text', 'My Education');
        set_theme_mod( 'classic_portfolio_education_btn_url', '#');
        set_theme_mod( 'classic_portfolio_experience_btn_text', 'My Experience');
        set_theme_mod( 'classic_portfolio_experience_btn_url', '#');
        set_theme_mod( 'classic_portfolio_num_courses', 8);

        $classic_portfolio_num_courses = 8;
        $classic_portfolio_course_years = array('2023-2024', '2022-2023', '2022-2021', '2021-2020', '2017-2016', '2018-2017', '2019-2018', '2020-2019');
        $classic_portfolio_course_names = array('Graphic Design Course', 'UI Design Course', 'Motion Course', 'UX Design Course', 'Art Design Course', 'Web Design Course', '3D Modeling Course', 'Auto CAD Course Parsons');
        $classic_portfolio_university_names = array('University of Denmark', 'Harverd University', 'Blockdots, London', 'House of Life, Leeds', 'Theme Junction, Bursa', 'University of Denmark', 'University of California', 'The New School');

        for ($classic_portfolio_i = 1; $classic_portfolio_i <= $classic_portfolio_num_courses; $classic_portfolio_i++) {
            // Set each course's data
            set_theme_mod('classic_portfolio_course_year' . $classic_portfolio_i, $classic_portfolio_course_years[$classic_portfolio_i - 1]);
            set_theme_mod('classic_portfolio_course_name' . $classic_portfolio_i, $classic_portfolio_course_names[$classic_portfolio_i - 1]);
            set_theme_mod('classic_portfolio_university_name' . $classic_portfolio_i, $classic_portfolio_university_names[$classic_portfolio_i - 1]);
        }
        
        // Show success message and the "View Site" button
        update_option('classic_portfolio_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.', 'classic-portfolio' ) . '</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('classic_portfolio_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'classic-portfolio' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'classic-portfolio' ); ?></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.', 'classic-portfolio' ); ?></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','classic-portfolio'); ?>" 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>