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

        // If the demo import is completed, display the "View Site" button
        if ($web_startup_agency_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.', 'web-startup-agency' ) . '</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
    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
            $web_startup_agency_plugin_slug = 'classic-blog-grid';
            $web_startup_agency_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $web_startup_agency_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $web_startup_agency_installed_plugins = get_plugins();
                if ( ! isset( $web_startup_agency_installed_plugins[ $web_startup_agency_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
                    $web_startup_agency_upgrader = new Plugin_Upgrader();
                    $web_startup_agency_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( $web_startup_agency_plugin_file );
            }
        }

        // ------- Create Main Menu --------
        $web_startup_agency_menuname = 'Primary Menu';
        $web_startup_agency_bpmenulocation = 'primary';
        $web_startup_agency_menu_exists = wp_get_nav_menu_object( $web_startup_agency_menuname );
    
        if ( !$web_startup_agency_menu_exists ) {
            $web_startup_agency_menu_id = wp_create_nav_menu( $web_startup_agency_menuname );

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

            // Create a new Page 
            $web_startup_agency_pages_title = 'Pages';
            $web_startup_agency_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>';
            $web_startup_agency_pages = array(
                'post_type'    => 'page',
                'post_title'   => $web_startup_agency_pages_title,
                'post_content' => $web_startup_agency_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $web_startup_agency_pages_id = wp_insert_post($web_startup_agency_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($web_startup_agency_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'web-startup-agency'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $web_startup_agency_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $web_startup_agency_about_title = 'About Us';
            $web_startup_agency_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>';
            $web_startup_agency_about = array(
                'post_type'    => 'page',
                'post_title'   => $web_startup_agency_about_title,
                'post_content' => $web_startup_agency_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $web_startup_agency_about_id = wp_insert_post($web_startup_agency_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($web_startup_agency_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'web-startup-agency'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $web_startup_agency_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( $web_startup_agency_bpmenulocation ) ) {
                $web_startup_agency_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $web_startup_agency_locations ) ) {
                    $web_startup_agency_locations = array();
                }
                $web_startup_agency_locations[ $web_startup_agency_bpmenulocation ] = $web_startup_agency_menu_id;
                set_theme_mod( 'nav_menu_locations', $web_startup_agency_locations );
            }
        }

        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( 'web_startup_agency_hide_categorysec', true);
        set_theme_mod( 'web_startup_agency_button_text', 'Read More');
        
        // Create the 'startup' category and retrieve its ID
        $web_startup_agency_slider_category_id = wp_create_category('startup');

        // Set the category in theme mods for the slider section
        set_theme_mod('web_startup_agency_pageboxes', $web_startup_agency_slider_category_id);

        // Titles for the three demo posts
        $web_startup_agency_titles = array(
            'Lets improve your Business Results',
            'We Help Startups Grow Online',
            'Digital Solutions for Your Brand'
        );

        // Create three demo posts and assign them to the 'Party' category
        for ($web_startup_agency_i = 1; $web_startup_agency_i <= 3; $web_startup_agency_i++) {
            $web_startup_agency_title = $web_startup_agency_titles[$web_startup_agency_i - 1];
            $web_startup_agency_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.';

            // Prepare the post object
            $web_startup_agency_my_post = array(
                'post_title'    => wp_strip_all_tags($web_startup_agency_title),
                'post_content'  => $web_startup_agency_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($web_startup_agency_slider_category_id),
            );

            // Insert the post into the database
            $web_startup_agency_post_id = wp_insert_post($web_startup_agency_my_post);

            // If the post was successfully created, set the featured image
            if ($web_startup_agency_post_id && !is_wp_error($web_startup_agency_post_id)) {
                // Set the image URL based on the current slider index
                $web_startup_agency_image_url = esc_url(get_stylesheet_directory_uri() . '/images/Slider' . $web_startup_agency_i . '.png');
                $web_startup_agency_upload_dir = wp_upload_dir();

                // Download the image data using wp_remote_get()
                $web_startup_agency_response = wp_remote_get($web_startup_agency_image_url);
                if (!is_wp_error($web_startup_agency_response)) {
                    $web_startup_agency_image_data = wp_remote_retrieve_body($web_startup_agency_response);

                    if (!empty($web_startup_agency_image_data)) {
                        // Handle the file upload process
                        $web_startup_agency_image_name = 'slider' . $web_startup_agency_i . '.png';
                        $web_startup_agency_unique_file_name = wp_unique_filename($web_startup_agency_upload_dir['path'], $web_startup_agency_image_name);
                        $web_startup_agency_file = $web_startup_agency_upload_dir['path'] . '/' . $web_startup_agency_unique_file_name;

                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($web_startup_agency_file, $web_startup_agency_image_data);

                        // Check file type and prepare for attachment
                        $web_startup_agency_wp_filetype = wp_check_filetype($web_startup_agency_unique_file_name, null);
                        $web_startup_agency_attachment = array(
                            'post_mime_type' => $web_startup_agency_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($web_startup_agency_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );

                        // Insert the image into the media library and set it as the post's featured image
                        $web_startup_agency_attach_id = wp_insert_attachment($web_startup_agency_attachment, $web_startup_agency_file, $web_startup_agency_post_id);
                        $web_startup_agency_attach_data = wp_generate_attachment_metadata($web_startup_agency_attach_id, $web_startup_agency_file);
                        wp_update_attachment_metadata($web_startup_agency_attach_id, $web_startup_agency_attach_data);
                        set_post_thumbnail($web_startup_agency_post_id, $web_startup_agency_attach_id);
                    }
                }
            }
        }

        // Services Section   
        set_theme_mod( 'smallbiz_startup_hidepageboxes', true);
        set_theme_mod( 'smallbiz_startup_text', 'SOME PICTURES OF');
        set_theme_mod( 'smallbiz_startup_title', 'LATEST PROJECTS');

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $page_title, $page_content = '' ) {
            $args = array(
                'post_type'   => 'page',
                'title'       => $page_title,
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'fields'      => 'ids'
            );
            $query = new WP_Query( $args );

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

        // Upload an image and set it as a featured image for a page
        function upload_service_image( $image_url, $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
            $image_id = media_sideload_image( $image_url, $post_id, null, 'id' );

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

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

        $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_pageboxes1', $smallbiz_startup_about_page_id );
            // Upload an image for About Us section (make sure the path is correct)
            $image_url = get_theme_file_uri( '/images/service1.png' );
            upload_service_image( $image_url, $smallbiz_startup_about_page_id );
        } else {
            error_log('Failed to create or fetch the "About Us" page.');
        }

        $smallbiz_startup_about_page_title = 'Market Analysis';
        $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)
            $image_url = get_theme_file_uri( '/images/service2.png' );
            upload_service_image( $image_url, $smallbiz_startup_about_page_id );
        } else {
            error_log('Failed to create or fetch the "About Us" page.');
        }

        $smallbiz_startup_about_page_title = 'Offline 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_pageboxes3', $smallbiz_startup_about_page_id );
            // Upload an image for About Us section (make sure the path is correct)
            $image_url = get_theme_file_uri( '/images/service3.png' );
            upload_service_image( $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('web_startup_agency_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.', 'web-startup-agency' ) . '</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('web_startup_agency_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'web-startup-agency' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'web-startup-agency' ); ?></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.', 'web-startup-agency' ); ?></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','web-startup-agency'); ?>" 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>