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

        // If the demo import is completed, display the "View Site" button
        if ($classic_wedding_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-wedding' ) . '</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 Classic Wedding
    if ( isset( $_POST['submit'] ) ) {

        // ------- Create Main Menu --------
        $classic_wedding_menuname = 'Primary Menu';
        $classic_wedding_bpmenulocation = 'primary';
        $classic_wedding_menu_exists = wp_get_nav_menu_object( $classic_wedding_menuname );
    
        if ( !$classic_wedding_menu_exists ) {
            $classic_wedding_menu_id = wp_create_nav_menu( $classic_wedding_menuname );

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

            // Create a new Page 
            $classic_wedding_pages_title = 'Pages';
            $classic_wedding_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_wedding_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_wedding_pages_title,
                'post_content' => $classic_wedding_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $classic_wedding_pages_id = wp_insert_post($classic_wedding_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_wedding_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'classic-wedding'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_wedding_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page 
            $classic_wedding_about_title = 'About Us';
            $classic_wedding_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>';
            $classic_wedding_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_wedding_about_title,
                'post_content' => $classic_wedding_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $classic_wedding_about_id = wp_insert_post($classic_wedding_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($classic_wedding_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'classic-wedding'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_wedding_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( $classic_wedding_bpmenulocation ) ) {
                $classic_wedding_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $classic_wedding_locations ) ) {
                    $classic_wedding_locations = array();
                }
                $classic_wedding_locations[ $classic_wedding_bpmenulocation ] = $classic_wedding_menu_id;
                set_theme_mod( 'nav_menu_locations', $classic_wedding_locations );
            }
        }

        //Header Section
        set_theme_mod( 'classic_wedding_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));
        set_theme_mod( 'classic_wedding_rsvp_text', 'RSVP');
        set_theme_mod( 'classic_wedding_rsvp_link', '#');
        set_theme_mod( 'classic_wedding_header_text', 'Olivia & Enrico wedding 15 December 2021 New York');
       
        //Slider Section
        set_theme_mod( 'classic_wedding_button_text', 'Book Now');
        set_theme_mod( 'classic_wedding_button_link_slider', '#');
        set_theme_mod( 'classic_wedding_slider_count', '1');

        // Create the 'Wedding' category and retrieve its ID
        $classic_wedding_slider_category_id = wp_create_category('Wedding');

        set_theme_mod('classic_wedding_slidersection', $classic_wedding_slider_category_id); 
        
        // Create three demo posts and assign them to the 'Wedding' category
        for ($classic_wedding_i = 1; $classic_wedding_i <= 3; $classic_wedding_i++) {
            $classic_wedding_title = 'Franklin & Juliet';
            $classic_wedding_content = '01.01.2021';

            // Prepare the post object
            $classic_wedding_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_wedding_title),
                'post_content'  => $classic_wedding_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_wedding_slider_category_id),
            );
    
            // Insert the post into the database
            $classic_wedding_post_id = wp_insert_post($classic_wedding_my_post);
    
            // If the post was successfully created, set the featured image
            if ($classic_wedding_post_id && !is_wp_error($classic_wedding_post_id)) {
                // Set the image URL
                $classic_wedding_image_url = esc_url(get_template_directory_uri() . '/images/slider.png');
                $classic_wedding_upload_dir = wp_upload_dir();
    
                // Download the image data using wp_remote_get()
                $classic_wedding_response = wp_remote_get($classic_wedding_image_url);
                if (!is_wp_error($classic_wedding_response)) {
                    $classic_wedding_image_data = wp_remote_retrieve_body($classic_wedding_response);
    
                    if (!empty($classic_wedding_image_data)) {
                        // Handle the file upload process
                        $classic_wedding_image_name = 'blog' . $classic_wedding_i . '.png';
                        $classic_wedding_unique_file_name = wp_unique_filename($classic_wedding_upload_dir['path'], $classic_wedding_image_name);
                        $classic_wedding_file = $classic_wedding_upload_dir['path'] . '/' . $classic_wedding_unique_file_name;
    
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($classic_wedding_file, $classic_wedding_image_data);
    
                        // Check file type and prepare for attachment
                        $classic_wedding_wp_filetype = wp_check_filetype($classic_wedding_unique_file_name, null);
                        $classic_wedding_attachment = array(
                            'post_mime_type' => $classic_wedding_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($classic_wedding_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
    
                        // Insert the image into the media library and set it as the post's featured image
                        $classic_wedding_attach_id = wp_insert_attachment($classic_wedding_attachment, $classic_wedding_file, $classic_wedding_post_id);
                        $classic_wedding_attach_data = wp_generate_attachment_metadata($classic_wedding_attach_id, $classic_wedding_file);
                        wp_update_attachment_metadata($classic_wedding_attach_id, $classic_wedding_attach_data);
                        set_post_thumbnail($classic_wedding_post_id, $classic_wedding_attach_id);
                    }
                }
            }
        }

        //About Us Section
        set_theme_mod( 'classic_wedding_pgboxes_title', 'Hello Friends & Family'); 

        // Create the 'About Us' page
        $classic_wedding_about_us_page_data = array(
            'post_title'   => 'Hello Friends & Family',
            'post_content' => 'We are Getting Married',
            'post_status'  => 'publish',
            'post_type'    => 'page',
        );

        $classic_wedding_about_us_page_id = wp_insert_post($classic_wedding_about_us_page_data);

        if (!is_wp_error($classic_wedding_about_us_page_id)) {
            // Assign the created page to the 'About Us' section in Customizer
            set_theme_mod('classic_wedding_about_us', $classic_wedding_about_us_page_id);

            // Set the featured image for the 'About Us' page
            $classic_wedding_image_url = get_template_directory_uri() . '/images/slider.png';
            $classic_wedding_image_id = media_sideload_image($classic_wedding_image_url, $classic_wedding_about_us_page_id, null, 'id');

            if (!is_wp_error($classic_wedding_image_id)) {
                set_post_thumbnail($classic_wedding_about_us_page_id, $classic_wedding_image_id);
            }
        }

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