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

        // If the demo import is completed, display the "View Site" button
        if ($smallbiz_startup_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.', 'smallbiz-startup' ) . '</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 Smallbiz Startup
    if ( isset( $_POST['submit'] ) ) {

        // 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
            $smallbiz_startup_plugin_slug = 'classic-blog-grid';
            $smallbiz_startup_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $smallbiz_startup_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $smallbiz_startup_installed_plugins = get_plugins();
                if ( ! isset( $smallbiz_startup_installed_plugins[ $smallbiz_startup_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
                    $smallbiz_startup_upgrader = new Plugin_Upgrader();
                    $smallbiz_startup_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( $smallbiz_startup_plugin_file );
            }
        }

       // ------- Create Main Menu --------
       $smallbiz_startup_menuname = 'Primary Menu';
       $smallbiz_startup_bpmenulocation = 'primary';
       $smallbiz_startup_menu_exists = wp_get_nav_menu_object( $smallbiz_startup_menuname );

       if ( !$smallbiz_startup_menu_exists ) {
           $smallbiz_startup_menu_id = wp_create_nav_menu( $smallbiz_startup_menuname );

           // Create Home Page
           $smallbiz_startup_home_title = 'Home';
           $smallbiz_startup_home = array(
               'post_type'    => 'page',
               'post_title'   => $smallbiz_startup_home_title,
               'post_content' => '',
               'post_status'  => 'publish',
               'post_author'  => 1,
               'post_name'    => 'home'
           );
           $smallbiz_startup_home_id = wp_insert_post($smallbiz_startup_home);
           // Assign Home Page Template
           add_post_meta($smallbiz_startup_home_id, '_wp_page_template', '/template-home-page.php');
           // Update options to set Home Page as the front page
           update_option('page_on_front', $smallbiz_startup_home_id);
           update_option('show_on_front', 'page');
           // Add Home Page to Menu
           wp_update_nav_menu_item($smallbiz_startup_menu_id, 0, array(
               'menu-item-title' => __('Home', 'smallbiz-startup'),
               'menu-item-classes' => 'home',
               'menu-item-url' => home_url('/'),
               'menu-item-status' => 'publish',
               'menu-item-object-id' => $smallbiz_startup_home_id,
               'menu-item-object' => 'page',
               'menu-item-type' => 'post_type'
           ));

           // Create Pages
           $smallbiz_startup_pages_title = 'Pages';
           $smallbiz_startup_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>';
           $smallbiz_startup_pages = array(
               'post_type'    => 'page',
               'post_title'   => $smallbiz_startup_pages_title,
               'post_content' => $smallbiz_startup_pages_content,
               'post_status'  => 'publish',
               'post_author'  => 1,
               'post_name'    => 'pages'
           );
           $smallbiz_startup_pages_id = wp_insert_post($smallbiz_startup_pages);
           // Add Pages Page to Menu
           wp_update_nav_menu_item($smallbiz_startup_menu_id, 0, array(
               'menu-item-title' => __('Pages', 'smallbiz-startup'),
               'menu-item-classes' => 'pages',
               'menu-item-url' => home_url('/pages/'),
               'menu-item-status' => 'publish',
               'menu-item-object-id' => $smallbiz_startup_pages_id,
               'menu-item-object' => 'page',
               'menu-item-type' => 'post_type'
           ));

           // Create About Us Page
           $smallbiz_startup_about_title = 'About Us';
           $smallbiz_startup_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>';
           $smallbiz_startup_about = array(
               'post_type'    => 'page',
               'post_title'   => $smallbiz_startup_about_title,
               'post_content' => $smallbiz_startup_about_content,
               'post_status'  => 'publish',
               'post_author'  => 1,
               'post_name'    => 'about-us'
           );
           $smallbiz_startup_about_id = wp_insert_post($smallbiz_startup_about);
           // Add About Us Page to Menu
           wp_update_nav_menu_item($smallbiz_startup_menu_id, 0, array(
               'menu-item-title' => __('About Us', 'smallbiz-startup'),
               'menu-item-classes' => 'about-us',
               'menu-item-url' => home_url('/about-us/'),
               'menu-item-status' => 'publish',
               'menu-item-object-id' => $smallbiz_startup_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( $smallbiz_startup_bpmenulocation ) ) {
               $smallbiz_startup_locations = get_theme_mod( 'nav_menu_locations' );
               if ( empty( $smallbiz_startup_locations ) ) {
                   $smallbiz_startup_locations = array();
               }
               $smallbiz_startup_locations[ $smallbiz_startup_bpmenulocation ] = $smallbiz_startup_menu_id;
               set_theme_mod( 'nav_menu_locations', $smallbiz_startup_locations );
           }
       }

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

        //Header Section
        set_theme_mod( 'smallbiz_startup_email_address', 'example@gmail.com');
        set_theme_mod( 'smallbiz_startup_contact_us_text', 'CONTACT US');
        
        //Social Media Section
        set_theme_mod( 'smallbiz_startup_fb_link', 'https://www.facebook.com');
        set_theme_mod( 'smallbiz_startup_twitt_link', 'https://www.twitter.com');
        set_theme_mod( 'smallbiz_startup_linked_link', 'https://www.linkedin.com');
        set_theme_mod( 'smallbiz_startup_insta_link', 'https://www.instagram.com');
        set_theme_mod( 'smallbiz_startup_youtube_link', 'https://www.youtube.com');

        //Slider Section
        set_theme_mod( 'smallbiz_startup_disabled_pgboxes_1', true);
        set_theme_mod( 'smallbiz_startup_pgboxes_title', 'LOREM IPSUM AMET');
        set_theme_mod( 'smallbiz_startup_button_text', 'GO NOW');
        
        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $smallbiz_startup_page_title, $smallbiz_startup_page_content = '' ) {
            $smallbiz_startup_args = array(
                'post_type'   => 'page',
                'title'       => $smallbiz_startup_page_title,
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'fields'      => 'ids'
            );
            $smallbiz_startup_query = new WP_Query( $smallbiz_startup_args );

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

        // Upload an image and set it as a featured image for a page
        function upload_service_image( $smallbiz_startup_image_url, $smallbiz_startup_post_id ) {
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
            require_once( ABSPATH . 'wp-admin/includes/media.php' );
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Upload image from URL
            $smallbiz_startup_image_id = media_sideload_image( $smallbiz_startup_image_url, $smallbiz_startup_post_id, null, 'id' );

            if ( !is_wp_error( $smallbiz_startup_image_id ) ) {
                // Set the uploaded image as the featured image for the page
                set_post_thumbnail( $smallbiz_startup_post_id, $smallbiz_startup_image_id );
                // Optionally, set the image as a theme mod
                set_theme_mod( 'smallbiz_startup_about_section_image', $smallbiz_startup_image_id );
                error_log('Image uploaded and set as theme mod and post thumbnail successfully.');
            } else {
                error_log('Failed to upload image: ' . $smallbiz_startup_image_url . ' - Error: ' . $smallbiz_startup_image_id->get_error_message() );
            }
        }

        // Get the absolute URL for theme images
        function get_theme_image_url( $smallbiz_startup_relative_path ) {
            return get_template_directory_uri() . $smallbiz_startup_relative_path;
        }

        // Create "What is Elusmod Tempor?" Page
        $smallbiz_startup_page_title = 'Elusmod Tempor';
        $smallbiz_startup_page_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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore';
        $smallbiz_startup_page_id = get_or_create_page_by_title( $smallbiz_startup_page_title, $smallbiz_startup_page_content );

        // Set theme mod and upload image for "What is Elusmod Tempor?"
        if ( $smallbiz_startup_page_id ) {
            set_theme_mod( 'smallbiz_startup_pageboxes', $smallbiz_startup_page_id );
            // Upload an image for this page (make sure the path is correct)
            $smallbiz_startup_image_url = get_theme_image_url( '/images/slider.png' );
            upload_service_image( $smallbiz_startup_image_url, $smallbiz_startup_page_id );
        } else {
            error_log('Failed to create or fetch the "What is Elusmod Tempor?" page.');
        }

        // Services Section   
        set_theme_mod( 'smallbiz_startup_hidepageboxes', true);
        set_theme_mod( 'smallbiz_startup_text', 'WE OFFERS YOU');
        set_theme_mod( 'smallbiz_startup_title', 'Our Services');

        $smallbiz_startup_about_page_title = 'Online Consulting';
        $smallbiz_startup_about_page_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore';
        $smallbiz_startup_about_page_id = get_or_create_page_by_title( $smallbiz_startup_about_page_title, $smallbiz_startup_about_page_content );

        // Set theme mod and upload image for About Us page
        if ( $smallbiz_startup_about_page_id ) {
            set_theme_mod( 'smallbiz_startup_pageboxes1', $smallbiz_startup_about_page_id );
            // Upload an image for About Us section (make sure the path is correct)
            $smallbiz_startup_image_url = get_theme_image_url( '/images/service1.png' );
            upload_service_image( $smallbiz_startup_image_url, $smallbiz_startup_about_page_id );
        } else {
            error_log('Failed to create or fetch the "About Us" page.');
        }

        $smallbiz_startup_about_page_title = 'Digital Marketing';
        $smallbiz_startup_about_page_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore';
        $smallbiz_startup_about_page_id = get_or_create_page_by_title( $smallbiz_startup_about_page_title, $smallbiz_startup_about_page_content );

        // Set theme mod and upload image for About Us page
        if ( $smallbiz_startup_about_page_id ) {
            set_theme_mod( 'smallbiz_startup_pageboxes2', $smallbiz_startup_about_page_id );
            // Upload an image for About Us section (make sure the path is correct)
            $smallbiz_startup_image_url = get_theme_image_url( '/images/service2.png' );
            upload_service_image( $smallbiz_startup_image_url, $smallbiz_startup_about_page_id );
        } else {
            error_log('Failed to create or fetch the "About Us" page.');
        }

        $smallbiz_startup_about_page_title = 'Branding';
        $smallbiz_startup_about_page_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore';
        $smallbiz_startup_about_page_id = get_or_create_page_by_title( $smallbiz_startup_about_page_title, $smallbiz_startup_about_page_content );

        // Set theme mod and upload image for About Us page
        if ( $smallbiz_startup_about_page_id ) {
            set_theme_mod( 'smallbiz_startup_pageboxes3', $smallbiz_startup_about_page_id );
            // Upload an image for About Us section (make sure the path is correct)
            $smallbiz_startup_image_url = get_theme_image_url( '/images/service3.png' );
            upload_service_image( $smallbiz_startup_image_url, $smallbiz_startup_about_page_id );
        } else {
            error_log('Failed to create or fetch the "About Us" page.');
        }

        // Show success message and the "View Site" button
        update_option('smallbiz_startup_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.', 'smallbiz-startup' ) . '</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('smallbiz_startup_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'smallbiz-startup' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'smallbiz-startup' ); ?></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.', 'smallbiz-startup' ); ?></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','smallbiz-startup'); ?>" 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>