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

        // If the demo import is completed, display the "View Site" button
        if ($classic_law_firm_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-law-firm' ) . '</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">';
            // 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_law_firm_plugin_slug = 'classic-blog-grid';
                $classic_law_firm_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_law_firm_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_law_firm_installed_plugins = get_plugins();
                    if ( ! isset( $classic_law_firm_installed_plugins[ $classic_law_firm_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_law_firm_upgrader = new Plugin_Upgrader();
                        $classic_law_firm_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_law_firm_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $classic_law_firm_menuname = 'Primary Menu';
        $classic_law_firm_bpmenulocation = 'primary';
        $classic_law_firm_menu_exists = wp_get_nav_menu_object( $classic_law_firm_menuname );
    
        if ( !$classic_law_firm_menu_exists ) {
            $classic_law_firm_menu_id = wp_create_nav_menu( $classic_law_firm_menuname );

            // Create Home Page
            $classic_law_firm_home_title = 'Home';
            $classic_law_firm_home = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $classic_law_firm_home_id = wp_insert_post($classic_law_firm_home);
            // Assign Home Page Template
            add_post_meta($classic_law_firm_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', $classic_law_firm_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($classic_law_firm_menu_id, 0, array(
                'menu-item-title' => __('Home', 'classic-law-firm'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_law_firm_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $classic_law_firm_pages_title = 'Services';
            $classic_law_firm_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>';
            $classic_law_firm_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_pages_title,
                'post_content' => $classic_law_firm_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'services'
            );
            $classic_law_firm_pages_id = wp_insert_post($classic_law_firm_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_law_firm_menu_id, 0, array(
                'menu-item-title' => __('Services', 'classic-law-firm'),
                'menu-item-classes' => 'services',
                'menu-item-url' => home_url('/services/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_law_firm_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $classic_law_firm_about_title = 'About Us';
            $classic_law_firm_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.';
            $classic_law_firm_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_about_title,
                'post_content' => $classic_law_firm_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $classic_law_firm_about_id = wp_insert_post($classic_law_firm_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($classic_law_firm_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'classic-law-firm'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_law_firm_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $classic_law_firm_pages_title = 'Case Studies';
            $classic_law_firm_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>';
            $classic_law_firm_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_pages_title,
                'post_content' => $classic_law_firm_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'case-studies'
            );
            $classic_law_firm_pages_id = wp_insert_post($classic_law_firm_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_law_firm_menu_id, 0, array(
                'menu-item-title' => __('Case Studies', 'classic-law-firm'),
                'menu-item-classes' => 'case-studies',
                'menu-item-url' => home_url('/case-studies/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_law_firm_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Blogs Page with Dummy Content
            $classic_law_firm_about_title = 'Blog';
            $classic_law_firm_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...';

            $classic_law_firm_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_about_title,
                'post_content' => $classic_law_firm_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blog' 
            );

            $classic_law_firm_about_id = wp_insert_post($classic_law_firm_about);

            // Add Blogs Page to Menu
            wp_update_nav_menu_item( $classic_law_firm_menu_id, 0, array(
                'menu-item-title'     => __( 'Blog', 'classic-law-firm' ),
                'menu-item-classes'  => 'blog',
                'menu-item-object-id'=> $classic_law_firm_about_id,
                'menu-item-object'   => 'page',
                'menu-item-type'     => 'post_type',
                'menu-item-status'   => 'publish'
            ));

            update_option( 'page_for_posts', $classic_law_firm_about_id );

            // Create a new Page 
            $classic_law_firm_pages_title = 'Contact Us';
            $classic_law_firm_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>';
            $classic_law_firm_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_law_firm_pages_title,
                'post_content' => $classic_law_firm_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'contact-us'
            );
            $classic_law_firm_pages_id = wp_insert_post($classic_law_firm_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_law_firm_menu_id, 0, array(
                'menu-item-title' => __('Contact Us', 'classic-law-firm'),
                'menu-item-classes' => 'contact-us',
                'menu-item-url' => home_url('/contact-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_law_firm_pages_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( $classic_law_firm_bpmenulocation ) ) {
                $classic_law_firm_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $classic_law_firm_locations ) ) {
                    $classic_law_firm_locations = array();
                }
                $classic_law_firm_locations[ $classic_law_firm_bpmenulocation ] = $classic_law_firm_menu_id;
                set_theme_mod( 'nav_menu_locations', $classic_law_firm_locations );
            }
        }

        //Logo
        set_theme_mod( 'classic_law_firm_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));
       
        //Topbar Section
        set_theme_mod( 'classic_law_firm_location_title','ADDRESS :');
        set_theme_mod( 'classic_law_firm_address_location','New York,US');
        set_theme_mod( 'classic_law_firm_track_locatin_url','#');
        set_theme_mod( 'classic_law_firm_email_title','EMAIL :');
        set_theme_mod( 'classic_law_firm_email_address','info@example.com');
        set_theme_mod( 'classic_law_firm_phone_title','PHONE :');
        set_theme_mod( 'classic_law_firm_phone_number','+123 456 7891');
        set_theme_mod( 'classic_law_firm_fb_link','#');
        set_theme_mod( 'classic_law_firm_youtube_link','#');
        set_theme_mod( 'classic_law_firm_instagram_link','#');
        set_theme_mod( 'classic_law_firm_linkedin_link','#');
        set_theme_mod( 'classic_law_firm_twitter_link','#');
        set_theme_mod( 'classic_law_firm_header_btn_text','Free Consultant');
        set_theme_mod( 'classic_law_firm_header_btn_url','#');

        //Slider Section
        set_theme_mod( 'classic_law_firm_slider_small_title','Beside Your As Your Friends');
        set_theme_mod( 'classic_law_firm_button_text','Our Best Services');
        set_theme_mod( 'classic_law_firm_button_link_slider','#');

        // Create the 'Laws' category and retrieve its ID
        $classic_law_firm_slider_category_id = wp_create_category('Laws');
    
        // Set the slider category in theme mods
        set_theme_mod('classic_law_firm_slider_cat', 'Laws');

        $classic_law_firm_titles = array(
            'Your Partner in Overcoming Challenges',
            'Your Rights with Power Commitment',
            'Finest & Strongest Law Firm In The World'
        );
        // Create three posts and assign them to the 'Laws' category
        // Define the image URLs
        for ($classic_law_firm_i = 1; $classic_law_firm_i <= 3; $classic_law_firm_i++) {
            $classic_law_firm_title = $classic_law_firm_titles[$classic_law_firm_i - 1];           
            $classic_law_firm_content = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout';
            
            // Create the post object
            $classic_law_firm_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_law_firm_title),
                'post_content'  => $classic_law_firm_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_law_firm_slider_category_id),  // Use the category ID
            );

            // Insert the post into the database
            $classic_law_firm_post_id = wp_insert_post( $classic_law_firm_my_post );
 
            if ($classic_law_firm_post_id) {
              // Set the theme mod for the slider page
              set_theme_mod('classic_law_firm_slider_cat' . $classic_law_firm_i, $classic_law_firm_post_id);

               $classic_law_firm_image_url = get_template_directory_uri().'/images/slider'.$classic_law_firm_i.'.png';

             $classic_law_firm_image_id = media_sideload_image($classic_law_firm_image_url, $classic_law_firm_post_id, null, 'id');

                 if (!is_wp_error($classic_law_firm_image_id)) {
                     // Set the downloaded image as the post's featured image
                     set_post_thumbnail($classic_law_firm_post_id, $classic_law_firm_image_id);
                 }
            }
        }
              
        //Lawyers Section
        set_theme_mod( 'classic_law_firm_lawyer_text','Our Lawyers');
        set_theme_mod( 'classic_law_firm_lawyer_title','A Passion For Justice, Our Practice Areas');

        $classic_law_firm_lawyers = array(
            array(
                'img' => esc_url(get_template_directory_uri() . '/images/lawyer1.png'),
                'name' => 'Sarah Johnson',
                'url' => '#',
                'category' => 'Lawyer',
            ),
            array(
                'img' => esc_url(get_template_directory_uri() . '/images/lawyer2.png'),
                'name' => 'Cindy Chow',
                'url' => '#',
                'category' => 'Lawyer',
            ),
            array(
                'img' => esc_url(get_template_directory_uri() . '/images/lawyer3.png'),
                'name' => 'Peter Parker',
                'url' => '#',
                'category' => 'Lawyer',
            ),
        );
    
        // Loop through the array to set theme mods
        foreach ($classic_law_firm_lawyers as $classic_law_firm_index => $classic_law_firm_lawyer) {
            $classic_law_firm_i = $classic_law_firm_index + 1; // Convert 0-based index to 1-based
    
            set_theme_mod('classic_law_firm_lawyer_img' . $classic_law_firm_i, $classic_law_firm_lawyer['img']);
            set_theme_mod('classic_law_firm_lawyer_name' . $classic_law_firm_i, $classic_law_firm_lawyer['name']);
            set_theme_mod('classic_law_firm_lawyer_name_url' . $classic_law_firm_i, $classic_law_firm_lawyer['url']);
            set_theme_mod('classic_law_firm_lawyer_category' . $classic_law_firm_i, $classic_law_firm_lawyer['category']);
        }

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