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

        // If the demo import is completed, display the "View Site" button
        if ($roofing_installation_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.', 'roofing-installation' ) . '</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 Roofing Installation
    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
                $roofing_installation_plugin_slug = 'classic-blog-grid';
                $roofing_installation_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $roofing_installation_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $roofing_installation_installed_plugins = get_plugins();
                    if ( ! isset( $roofing_installation_installed_plugins[ $roofing_installation_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
                        $roofing_installation_upgrader = new Plugin_Upgrader();
                        $roofing_installation_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( $roofing_installation_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $roofing_installation_menuname = 'Primary Menu'; 
        $roofing_installation_bpmenulocation = 'primary';
        $roofing_installation_menu_exists = wp_get_nav_menu_object($roofing_installation_menuname);

        if (!$roofing_installation_menu_exists) {
            // Create a new menu
            $roofing_installation_menu_id = wp_create_nav_menu($roofing_installation_menuname);

            // Define pages to be created
            $roofing_installation_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>'
                ),
                'projects' => array(
                    'title' => 'Projects',
                    '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' => '<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>'
                ),
                'contact' => array(
                    'title' => 'Contact',
                    '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>'
                ),
            );

            $roofing_installation_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($roofing_installation_pages as $roofing_installation_slug => $roofing_installation_data) {
                $roofing_installation_existing_page = get_page_by_path($roofing_installation_slug);

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

                    $roofing_installation_page_id = wp_insert_post($roofing_installation_page_data);

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

                // Store the page IDs
                $roofing_installation_page_ids[$roofing_installation_slug] = $roofing_installation_page_id;
            }

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

            // Define menu items
            $roofing_installation_menu_items = array(
                'home',
                'about',
                'services',
                'projects',
                'blog',
                'pages',
                'contact'
            );

            // Add menu items dynamically
            foreach ($roofing_installation_menu_items as $roofing_installation_slug) {
                wp_update_nav_menu_item($roofing_installation_menu_id, 0, array(
                    'menu-item-title' => esc_html($roofing_installation_pages[$roofing_installation_slug]['title']),
                    'menu-item-url' => get_permalink($roofing_installation_page_ids[$roofing_installation_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $roofing_installation_page_ids[$roofing_installation_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $roofing_installation_locations = get_theme_mod('nav_menu_locations', array());
            $roofing_installation_locations[$roofing_installation_bpmenulocation] = $roofing_installation_menu_id;
            set_theme_mod('nav_menu_locations', $roofing_installation_locations);
        }

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

        //Header Section
        set_theme_mod('roofing_installation_enable_topbar', true);        
        set_theme_mod('roofing_installation_topbar_text', 'Welcome to RoofGuard');   
        set_theme_mod('roofing_installation_email_address', 'RoofGuard@Example.com'); 
        set_theme_mod('roofing_installation_phone_number', '+1 123 456 7890'); 
        set_theme_mod( 'roofing_installation_facebook_url', 'https://www.facebook.com');
        set_theme_mod( 'roofing_installation_instagram_url', 'https://www.instagram.com');
        set_theme_mod( 'roofing_installation_google_url', 'https://www.googleplus.com');
        set_theme_mod( 'roofing_installation_youtube_url', 'https://www.youtube.com');

       //Slider Section
       set_theme_mod( 'roofing_installation_banner', true);
       set_theme_mod( 'roofing_installation_button_text', 'Get a Free Roof Inspection');

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $roofing_installation_page_title, $roofing_installation_page_content = '' ) {
            $roofing_installation_args = array(
                'post_type'      => 'page',
                'title'          => $roofing_installation_page_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $roofing_installation_query = new WP_Query( $roofing_installation_args );

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

        // Create Page
        $roofing_installation_page_title = 'Protect What Matters Most – Quality Roofing Solutions';
        $roofing_installation_page_content = 'From new installations to storm damage repairs, we deliver premium roofing with unmatched reliability';
        $roofing_installation_page_id = get_or_create_page_by_title( $roofing_installation_page_title, $roofing_installation_page_content );

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

        $roofing_installation_image_url = get_template_directory_uri().'/images/slider.png';
        $roofing_installation_image_id = media_sideload_image($roofing_installation_image_url, $roofing_installation_page_id, null, 'id');
        if (!is_wp_error($roofing_installation_image_id)) {
            // Set the downloaded image as the post's featured image
            set_post_thumbnail($roofing_installation_page_id, $roofing_installation_image_id);
        }  

       //Our Services
       set_theme_mod('roofing_installation_disabled_services_section', true);  
       set_theme_mod('roofing_installation_services_text', 'Our Roofing Services');
       set_theme_mod('roofing_installation_services_title', 'From Installations to Repairs — We\'ve Got You Covered');

       $roofing_installation_service_titles = array('Residential Roof Installation', 'Storm Damage Restoration', 'Commercial Roofing', 'Roof Repair & Leak Fixing', 'Roof Maintenance Plans');
       $roofing_installation_image_urls = array(
           get_template_directory_uri() . '/images/service1.png',
           get_template_directory_uri() . '/images/service2.png',
           get_template_directory_uri() . '/images/service3.png',
           get_template_directory_uri() . '/images/service4.png',
           get_template_directory_uri() . '/images/service5.png'
       );
       $roofing_installation_service_icons = array(
            'fa-solid fa-home',              
            'fa-solid fa-person-shelter',          
            'fa-solid fa-building',          
            'fa-solid fa-screwdriver-wrench',  
            'fa-solid fa-clipboard-check'     
        );    

       // Loop to create posts for each service
        for ($roofing_installation_i = 0; $roofing_installation_i < 5; $roofing_installation_i++) {
           $roofing_installation_title = $roofing_installation_service_titles[$roofing_installation_i];
           $roofing_installation_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.';
           $roofing_installation_icon = $roofing_installation_service_icons[$roofing_installation_i];

           // Create the post object
           $roofing_installation_post_id = wp_insert_post(array(
               'post_title'    => wp_strip_all_tags($roofing_installation_title),
               'post_content'  => $roofing_installation_content,
               'post_status'   => 'publish',
               'post_type'     => 'post',
           ));

           // If the post was successfully created
           if ($roofing_installation_post_id && !is_wp_error($roofing_installation_post_id)) {
               // Set the theme mod for selecting this post
               set_theme_mod('roofing_installation_select_post' . ($roofing_installation_i + 1), $roofing_installation_post_id);

               // Set the icon as a theme mod
               set_theme_mod('roofing_installation_select_services_icon' . ($roofing_installation_i + 1), $roofing_installation_icon);

               // Handle the featured image using media_sideload_image
               $roofing_installation_image_url = $roofing_installation_image_urls[$roofing_installation_i];
               $roofing_installation_image_id = media_sideload_image($roofing_installation_image_url, $roofing_installation_post_id, null, 'id');

               if (!is_wp_error($roofing_installation_image_id)) {
                   set_post_thumbnail($roofing_installation_post_id, $roofing_installation_image_id);
               } else {
                   error_log("Failed to download image for $roofing_installation_title: " . $roofing_installation_image_id->get_error_message());
               }
           } else {
               error_log("Failed to create post: " . print_r($roofing_installation_post_id, true));
           }
        }

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



 