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

        // If the demo import is completed, display the "View Site" button
        if ($cleaning_services_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.', 'cleaning-services-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 of Cleaning Services Agency
    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
            $cleaning_services_agency_plugin_slug = 'classic-blog-grid';
            $cleaning_services_agency_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $cleaning_services_agency_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $cleaning_services_agency_installed_plugins = get_plugins();
                if ( ! isset( $cleaning_services_agency_installed_plugins[ $cleaning_services_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
                    $cleaning_services_agency_upgrader = new Plugin_Upgrader();
                    $cleaning_services_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( $cleaning_services_agency_plugin_file );
            }
        }
    echo '</div>';
        // ------- Create Main Menu --------
        $cleaning_services_agency_menuname = 'Primary Menu';
        $cleaning_services_agency_bpmenulocation = 'primary';
        $cleaning_services_agency_menu_exists = wp_get_nav_menu_object( $cleaning_services_agency_menuname );
    
        if ( !$cleaning_services_agency_menu_exists ) {
            $cleaning_services_agency_menu_id = wp_create_nav_menu( $cleaning_services_agency_menuname );

            // Create Home Page
            $cleaning_services_agency_home_title = 'Home';
            $cleaning_services_agency_home = array(
                'post_type'    => 'page',
                'post_title'   => $cleaning_services_agency_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $cleaning_services_agency_home_id = wp_insert_post($cleaning_services_agency_home);
            // Assign Home Page Template
            add_post_meta($cleaning_services_agency_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', $cleaning_services_agency_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($cleaning_services_agency_menu_id, 0, array(
                'menu-item-title' => __('Home', 'cleaning-services-agency'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $cleaning_services_agency_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $cleaning_services_agency_about_title = 'About Us';
            $cleaning_services_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>';
            $cleaning_services_agency_about = array(
                'post_type'    => 'page',
                'post_title'   => $cleaning_services_agency_about_title,
                'post_content' => $cleaning_services_agency_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $cleaning_services_agency_about_id = wp_insert_post($cleaning_services_agency_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($cleaning_services_agency_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'cleaning-services-agency'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $cleaning_services_agency_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Services Page with Dummy Content
            $cleaning_services_agency_service_title = 'Services';
            $cleaning_services_agency_service_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>';
            $cleaning_services_agency_service = array(
                'post_type'    => 'page',
                'post_title'   => $cleaning_services_agency_service_title,
                'post_content' => $cleaning_services_agency_service_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'services'
            );
            $cleaning_services_agency_service_id = wp_insert_post($cleaning_services_agency_service);
            // Add Services Page to Menu
            wp_update_nav_menu_item($cleaning_services_agency_menu_id, 0, array(
                'menu-item-title' => __('Services', 'cleaning-services-agency'),
                'menu-item-classes' => 'services',
                'menu-item-url' => home_url('/services/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $cleaning_services_agency_service_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Pages Page with Dummy Content
            $cleaning_services_agency_pages_title = 'Pages';
            $cleaning_services_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>';
            $cleaning_services_agency_pages = array(
                'post_type'    => 'page',
                'post_title'   => $cleaning_services_agency_pages_title,
                'post_content' => $cleaning_services_agency_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $cleaning_services_agency_pages_id = wp_insert_post($cleaning_services_agency_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($cleaning_services_agency_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'cleaning-services-agency'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $cleaning_services_agency_service_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Blog Page 
            $cleaning_services_agency_blog_title = 'Blogs';
            $cleaning_services_agency_blog_content = '<p>Welcome to our blog section! Here you will find the latest updates, news, and insights.</p>';
            $cleaning_services_agency_blog_page = array(
                'post_type'    => 'page',
                'post_title'   => $cleaning_services_agency_blog_title,
                'post_content' => $cleaning_services_agency_blog_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blogs' // Slug updated to 'blogs'
            );
            $cleaning_services_agency_blog_id = wp_insert_post($cleaning_services_agency_blog_page);

            // Set the newly created page as the posts page (blog page)
            update_option('page_for_posts', $cleaning_services_agency_blog_id);

            // Add Blogs Page to Menu
            wp_update_nav_menu_item($cleaning_services_agency_menu_id, 0, array(
                'menu-item-title'    => __('Blogs', 'cleaning-services-agency'),
                'menu-item-classes'  => 'blogs',
                'menu-item-url'      => home_url('/blogs/'),
                'menu-item-status'   => 'publish',
                'menu-item-object-id'=> $cleaning_services_agency_blog_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( $cleaning_services_agency_bpmenulocation ) ) {
                $cleaning_services_agency_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $cleaning_services_agency_locations ) ) {
                    $cleaning_services_agency_locations = array();
                }
                $cleaning_services_agency_locations[ $cleaning_services_agency_bpmenulocation ] = $cleaning_services_agency_menu_id;
                set_theme_mod( 'nav_menu_locations', $cleaning_services_agency_locations );
            }
        }

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

        //Header Section
        set_theme_mod( 'cleaning_services_agency_topbar', true);
        set_theme_mod( 'cleaning_services_agency_email_address', 'info@example.com');
        set_theme_mod( 'cleaning_services_agency_time', 'Mon/Sat 9:00am to 7:00pm');
        set_theme_mod( 'cleaning_services_agency_phone_number', '1234 567 890');
        set_theme_mod( 'cleaning_services_agency_phone_text', 'Call Anytime For Booking');
        set_theme_mod( 'cleaning_services_agency_fb_link', 'https://www.facebook.com');
        set_theme_mod( 'cleaning_services_agency_twitt_link', 'https://www.twitter.com');
        set_theme_mod( 'cleaning_services_agency_insta_link', 'https://www.instagram.com');
        set_theme_mod( 'cleaning_services_agency_linkedin_link', 'https://www.linkedin.com');
        set_theme_mod( 'cleaning_services_agency_youtube_link', 'https://www.youtube.com');

        //Slider Section
        set_theme_mod( 'cleaning_services_agency_hide_categorysec', true);
        set_theme_mod( 'cleaning_services_agency_button_text', 'Appoinment');
        set_theme_mod( 'cleaning_services_agency_button2_text', 'Contacts');

        $cleaning_services_agency_slider_category_id = wp_create_category('Slider');
        set_theme_mod('cleaning_services_agency_slidersection', $cleaning_services_agency_slider_category_id);

        // Titles and content for posts
        $cleaning_services_agency_titles = array(
            'A Traditional Best Quality Cleaning',
            'Reliable Deep Cleaning Solutions',
            'Eco-Friendly Housekeeping Services'       
        );                      
        $cleaning_services_agency_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text.';

        // Insert cleaning services agency posts
        for ($cleaning_services_agency_i = 0; $cleaning_services_agency_i < 3; $cleaning_services_agency_i++) {
            set_theme_mod('cleaning_services_agency_title' . ($cleaning_services_agency_i + 1), $cleaning_services_agency_titles[$cleaning_services_agency_i]);

            $cleaning_services_agency_my_post = array(
                'post_title'    => wp_strip_all_tags($cleaning_services_agency_titles[$cleaning_services_agency_i]),
                'post_content'  => $cleaning_services_agency_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($cleaning_services_agency_slider_category_id),
            );

            $cleaning_services_agency_post_id = wp_insert_post($cleaning_services_agency_my_post);

            if (!is_wp_error($cleaning_services_agency_post_id)) {
                $cleaning_services_agency_image_url = get_template_directory_uri() . '/images/slider' . ($cleaning_services_agency_i + 1) . '.png';
                $cleaning_services_agency_image_id = media_sideload_image($cleaning_services_agency_image_url, $cleaning_services_agency_post_id, null, 'id');

                if (!is_wp_error($cleaning_services_agency_image_id)) {
                    set_post_thumbnail($cleaning_services_agency_post_id, $cleaning_services_agency_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $cleaning_services_agency_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($cleaning_services_agency_post_id, true));
            }
        }

        //Manage About Section
        set_theme_mod('cleaning_services_agency_disabled_pgboxes', true);

        $cleaning_services_agency_project_sec_category = wp_create_category('Cleaning');
        set_theme_mod('cleaning_services_agency_services_cat', $cleaning_services_agency_project_sec_category);

        // Titles and content for posts
        $cleaning_services_agency_titles = array(
            'Happy Customer',
            'Award Winner',
            'Team Member',
            'Year Experience'
        );                      
        $cleaning_services_agency_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text.';

        // Define different metadata for each post
        $cleaning_services_agency_custom_icons = array("fas fa-smile", "fas fa-trophy", "fas fa-users", "fas fa-trophy");
        $cleaning_services_agency_custom_numbers = array("921", "17", "330", "10");

        for ($cleaning_services_agency_i = 0; $cleaning_services_agency_i < 4; $cleaning_services_agency_i++) {
            set_theme_mod('cleaning_services_agency_title' . ($cleaning_services_agency_i + 1), $cleaning_services_agency_titles[$cleaning_services_agency_i]);

            $cleaning_services_agency_my_post = array(
                'post_title'    => wp_strip_all_tags($cleaning_services_agency_titles[$cleaning_services_agency_i]),
                'post_content'  => $cleaning_services_agency_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($cleaning_services_agency_project_sec_category),
            );

            $cleaning_services_agency_post_id = wp_insert_post($cleaning_services_agency_my_post);

            if (!is_wp_error($cleaning_services_agency_post_id)) {
                $cleaning_services_agency_image_url = get_template_directory_uri() . '/images/service' . ($cleaning_services_agency_i + 1) . '.png';
                $cleaning_services_agency_image_id = media_sideload_image($cleaning_services_agency_image_url, $cleaning_services_agency_post_id, null, 'id');

                if (!is_wp_error($cleaning_services_agency_image_id)) {
                    set_post_thumbnail($cleaning_services_agency_post_id, $cleaning_services_agency_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $cleaning_services_agency_post_id);
                }

                // Set different metadata
                update_post_meta($cleaning_services_agency_post_id, 'cleaning_services_agency_custom_icon', $cleaning_services_agency_custom_icons[$cleaning_services_agency_i]);
                update_post_meta($cleaning_services_agency_post_id, 'cleaning_services_agency_custom_number', $cleaning_services_agency_custom_numbers[$cleaning_services_agency_i]);
            } else {
                error_log('Failed to create post: ' . print_r($cleaning_services_agency_post_id, true));
            }
        }


         // Function to fetch or create a page using WP_Query
         function get_or_create_page_by_title( $cleaning_services_agency_page_title, $cleaning_services_agency_page_content = '' ) {
            $cleaning_services_agency_args = array(
                'post_type'      => 'page',
                'title'          => $cleaning_services_agency_page_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $cleaning_services_agency_query = new WP_Query( $cleaning_services_agency_args );

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

        // Create Page
        $cleaning_services_agency_page_title = 'About Me';
        $cleaning_services_agency_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, when an unknown printer took a galley of type and scrambled it to make a type specimen book.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 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 1960s 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.';
        $cleaning_services_agency_page_id = get_or_create_page_by_title( $cleaning_services_agency_page_title, $cleaning_services_agency_page_content );

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

        // Set default FontAwesome icons
        $cleaning_services_agency_default_icons = array(
            'fa-solid fa-warehouse',  
            'fa-solid fa-dolly', 
            'fa-solid fa-boxes-stacked' 
        );

        for ($cleaning_services_agency_i = 0; $cleaning_services_agency_i < 3; $cleaning_services_agency_i++) {
            set_theme_mod('cleaning_services_agency_services_icon' . ($cleaning_services_agency_i + 1), $cleaning_services_agency_default_icons[$cleaning_services_agency_i]);
        }

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