<?php 
if (isset($_GET['import-demo']) && $_GET['import-demo'] == true) {

    function feminine_style_coach_import_demo_content() {
        
        // Display the preloader only for plugin installation
        echo '<div id="plugin-loader" style="display: flex; align-items: center; justify-content: center; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255, 255, 255, 0.4); z-index: 9999;">
                <img src="' . esc_url(get_template_directory_uri()) . '/assets/images/loader.png" alt="Loading..." width="60" height="60" />
              </div>';

        // Define the plugins you want to install and activate
        $plugins = array(
            array(
                'slug' => 'contact-form-7',
                'file' => 'contact-form-7/wp-contact-form-7.php',
                'url'  => 'https://downloads.wordpress.org/plugin/contact-form-7.latest-stable.zip'
            ),
            array(
                'slug' => 'advanced-appointment-booking-scheduling',
                'file' => 'advanced-appointment-booking-scheduling/advanced-appointment-booking.php',
                'url'  => 'https://downloads.wordpress.org/plugin/advanced-appointment-booking-scheduling.zip'
            ),
        );

        // Include required files for plugin installation
        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');

        // Loop through each plugin
        foreach ($plugins as $plugin) {
            $plugin_file = WP_PLUGIN_DIR . '/' . $plugin['file'];

            // Check if the plugin is installed
            if (!file_exists($plugin_file)) {
                // If the plugin is not installed, download and install it
                $upgrader = new Plugin_Upgrader();
                $result = $upgrader->install($plugin['url']);

                // Check for installation errors
                if (is_wp_error($result)) {
                    error_log('Plugin installation failed: ' . $plugin['slug'] . ' - ' . $result->get_error_message());
                    echo 'Error installing plugin: ' . esc_html($plugin['slug']) . ' - ' . esc_html($result->get_error_message());
                    continue;
                }
            }

            // If the plugin exists but is not active, activate it
            if (file_exists($plugin_file) && !is_plugin_active($plugin['file'])) {
                $result = activate_plugin($plugin['file']);

                // Check for activation errors
                if (is_wp_error($result)) {
                    error_log('Plugin activation failed: ' . $plugin['slug'] . ' - ' . $result->get_error_message());
                    echo 'Error activating plugin: ' . esc_html($plugin['slug']) . ' - ' . esc_html($result->get_error_message());
                }
            }
        }

        // Hide the preloader after the process is complete
        echo '<script type="text/javascript">
                window.onload = function() {
                    document.getElementById("plugin-loader").style.display = "none"; // Hide only plugin preloader
                };
              </script>';


        add_filter( 'woocommerce_prevent_automatic_wizard_redirect', '__return_true' );
    }

    // Call the import function
    feminine_style_coach_import_demo_content();

     // ------- Create Nav Menu --------
    // Define menu names and locations
        $feminine_style_coach_menuname_left = 'Primary Left Menu';
        $feminine_style_coach_bpmenulocation_left = 'primary-1';
        $feminine_style_coach_menuname_right = 'Primary Right Menu';
        $feminine_style_coach_bpmenulocation_right = 'primary-2';

        // Check if the left menu exists
        $feminine_style_coach_left_menu_exists = wp_get_nav_menu_object($feminine_style_coach_menuname_left);

        if (!$feminine_style_coach_left_menu_exists) {
            $feminine_style_coach_left_menu_id = wp_create_nav_menu($feminine_style_coach_menuname_left);

            // Create Home Page
            $feminine_style_coach_home_title = 'Home';
            $feminine_style_coach_home = array(
                'post_type' => 'page',
                'post_title' => $feminine_style_coach_home_title,
                'post_content' => '',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'home'
            );
            $feminine_style_coach_home_id = wp_insert_post($feminine_style_coach_home);
            
            // Assign Home Page Template
            add_post_meta($feminine_style_coach_home_id, '_wp_page_template', 'page-template/front-page.php');

            // Update options to set Home Page as the front page
            update_option('page_on_front', $feminine_style_coach_home_id);
            update_option('show_on_front', 'page');

            // Add Home Page to Left Menu
            wp_update_nav_menu_item($feminine_style_coach_left_menu_id, 0, array(
                'menu-item-title' => __('Home', 'feminine-style-coach'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'Pages' to Left Menu
            $feminine_style_coach_pages_title = 'Pages';
            $feminine_style_coach_pages_content = '...'; // Your dummy content
            $feminine_style_coach_pages = array(
                'post_type' => 'page',
                'post_title' => $feminine_style_coach_pages_title,
                'post_content' => $feminine_style_coach_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'pages'
            );
            $feminine_style_coach_pages_id = wp_insert_post($feminine_style_coach_pages);
            wp_update_nav_menu_item($feminine_style_coach_left_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'feminine-style-coach'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'About Us' to Left Menu
            $feminine_style_coach_about_title = 'About Us';
            $feminine_style_coach_about_content = '...'; // Your dummy content
            $feminine_style_coach_about = array(
                'post_type' => 'page',
                'post_title' => $feminine_style_coach_about_title,
                'post_content' => $feminine_style_coach_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'about-us'
            );
            $feminine_style_coach_about_id = wp_insert_post($feminine_style_coach_about);
            wp_update_nav_menu_item($feminine_style_coach_left_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'feminine-style-coach'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign Left Menu to its location
            if (!has_nav_menu($feminine_style_coach_bpmenulocation_left)) {
                $locations = get_theme_mod('nav_menu_locations');
                if (empty($locations)) {
                    $locations = array();
                }
                $locations[$feminine_style_coach_bpmenulocation_left] = $feminine_style_coach_left_menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
        }

        // Check if the right menu exists
        $feminine_style_coach_right_menu_exists = wp_get_nav_menu_object($feminine_style_coach_bpmenulocation_right);

        if (!$feminine_style_coach_right_menu_exists) {
            $feminine_style_coach_right_menu_id = wp_create_nav_menu($feminine_style_coach_bpmenulocation_right);

            // Add Home Page to Right Menu (can be the same page or different logic if needed)
            wp_update_nav_menu_item($feminine_style_coach_right_menu_id, 0, array(
                'menu-item-title' => __('Home', 'feminine-style-coach'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_home_id, // Reuse the same home ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'Pages' to Right Menu
            wp_update_nav_menu_item($feminine_style_coach_right_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'feminine-style-coach'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_pages_id, // Reuse the same page ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Add 'About Us' to Right Menu
            wp_update_nav_menu_item($feminine_style_coach_right_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'feminine-style-coach'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $feminine_style_coach_about_id, // Reuse the same about ID
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign Right Menu to its location
            if (!has_nav_menu($feminine_style_coach_bpmenulocation_right)) {
                $locations = get_theme_mod('nav_menu_locations');
                if (empty($locations)) {
                    $locations = array();
                }
                $locations[$feminine_style_coach_bpmenulocation_right] = $feminine_style_coach_right_menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
        }

        //---Header--//
        set_theme_mod('feminine_style_coach_location', '2118 Thornridge Cir. Syracuse, Connecticut 35624');
        set_theme_mod('feminine_style_coach_mail', 'feminine@example.com');
        set_theme_mod('feminine_style_coach_call', '+18000-1340-12485');

        // Slider Section
        set_theme_mod('feminine_style_coach_slider_arrows', true);
        set_theme_mod('feminine_style_coach_slider_short_heading', 'Live A Life With A Passion');

        for ($i = 1; $i <= 4; $i++) {
            $feminine_style_coach_title = 'Career And Success For Women In Business.';
            $feminine_style_coach_content = 'Lorem Ipsum Dolor Sit Amet, Consectetur Adipiscing Elit, Sed Do Eiusmod Tempor Incididunt Ut Labore Et Dolore Magna Aliqua.';

            // Create post object
            $my_post = array(
                'post_title'    => wp_strip_all_tags($feminine_style_coach_title),
                'post_content'  => $feminine_style_coach_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
            );

            // Insert the post into the database
            $post_id = wp_insert_post($my_post);

            if ($post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('feminine_style_coach_slider_page' . $i, $post_id);

                $image_url = get_template_directory_uri() . '/assets/images/slider-img.png';
                $image_id = media_sideload_image($image_url, $post_id, null, 'id');

                if (!is_wp_error($image_id)) {
                    // Set the downloaded image as the post's featured image
                    set_post_thumbnail($post_id, $image_id);
                }
            }
        }

         // Our Services Section //
            set_theme_mod('feminine_style_coach_coaching_tittle', 'Coaching Benefits');
            set_theme_mod('feminine_style_coach_coaching_section_category', 'postcategory1');

            // Define post category names and post titles
            $feminine_style_coach_category_names = array('postcategory1');
            $feminine_style_coach_title_array = array(
                array(
                    "Achieve Life Goals",
                    "Ask Consultancy",
                    "Self Development",
                )
            );

            $feminine_style_coach_content = 'Consectetur Adipiscing Elit, Sed Do Eiusmod Tempor Incididunt Ut Labore Et Dolore Magna Aliqua. Ut Enim Ad Minim Veniam, Quis Nostrud.';

            foreach ($feminine_style_coach_category_names as $feminine_style_coach_index => $feminine_style_coach_category_name) {
                // Create or retrieve the post category term ID
                $feminine_style_coach_term = term_exists($feminine_style_coach_category_name, 'category');
                if ($feminine_style_coach_term === 0 || $feminine_style_coach_term === null) {
                    // If the term does not exist, create it
                    $feminine_style_coach_term = wp_insert_term($feminine_style_coach_category_name, 'category');
                }
                if (is_wp_error($feminine_style_coach_term)) {
                    error_log('Error creating category: ' . $feminine_style_coach_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                // Ensure we get the term ID
                $feminine_style_coach_term_id = is_array($feminine_style_coach_term) ? $feminine_style_coach_term['term_id'] : $feminine_style_coach_term;

                // Get titles for this category
                $feminine_style_coach_titles = $feminine_style_coach_title_array[$feminine_style_coach_index];

                for ($feminine_style_coach_i = 0; $feminine_style_coach_i < count($feminine_style_coach_titles); $feminine_style_coach_i++) {
                    // Create post content
                    $feminine_style_coach_title = $feminine_style_coach_titles[$feminine_style_coach_i];

                    // Create post post object
                    $feminine_style_coach_my_post = array(
                        'post_title'    => wp_strip_all_tags($feminine_style_coach_title),
                        'post_content'  => $feminine_style_coach_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $feminine_style_coach_post_id = wp_insert_post($feminine_style_coach_my_post);

                    if (is_wp_error($feminine_style_coach_post_id)) {
                        error_log('Error creating post: ' . $feminine_style_coach_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }

                    // Assign the category to the post
                    wp_set_post_categories($feminine_style_coach_post_id, array((int)$feminine_style_coach_term_id));

                    // Handle the featured image using media_sideload_image
                    $feminine_style_coach_image_url = get_template_directory_uri() . '/assets/images/post-img' . ($feminine_style_coach_i + 1) . '.png';
                    $feminine_style_coach_image_id = media_sideload_image($feminine_style_coach_image_url, $feminine_style_coach_post_id, null, 'id');

                    if (is_wp_error($feminine_style_coach_image_id)) {
                        error_log('Error downloading image: ' . $feminine_style_coach_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }

                    // Assign featured image to post
                    set_post_thumbnail($feminine_style_coach_post_id, $feminine_style_coach_image_id);
                }
            }  


            // newsletter section
            set_theme_mod('feminine_style_coach_newsletter_main_heading', 'Get A Free Update On Your Email.');
            set_theme_mod('feminine_style_coach_newsletter_text', 'Signup Our Newsletter To Get Update information,news & Insight.');
            // Set default shortcodes for each form (replace these with your actual form shortcodes)
            set_theme_mod('feminine_style_coach_newsletter_form_shortcode', '');

            // Set default values for the contact form settings for Amusement Park Contact Form
            $cf7title = 'Amusement Park Contact Form'; // Modify the title as needed
            $cf7content = '[email* your-email placeholder "Your Email Address"] [submit "Send"]';

            // Insert the contact form post
            $cf7_post = array(
                'post_title'   => wp_strip_all_tags($cf7title),
                'post_content' => $cf7content,
                'post_status'  => 'publish',
                'post_type'    => 'wpcf7_contact_form',
            );

            // Insert post and get post ID
            $cf7post_id = wp_insert_post($cf7_post);

            // Check if the post insertion was successful
            if (!is_wp_error($cf7post_id)) {
                // Add form content to the post meta
                add_post_meta($cf7post_id, "_form", $cf7content);

                // Prepare email settings for the contact form
                $cf7mail_data = array(
                    'subject'         => '[_site_title] "[your-subject]"',
                    'sender'          => '[_site_title] <support@example.com>',
                    'body'            => 'From: [your-name] <[your-email]>\nSubject: [your-subject]\nMessage Body:\n[your-message]\n\n--\nThis e-mail was sent from a contact form on [_site_title] ([_site_url])',
                    'recipient'       => '[_site_admin_email]',
                    'additional_headers' => 'Reply-To: [your-email]',
                    'attachments'     => '',
                    'use_html'        => 0,
                    'exclude_blank'   => 0
                );

                // Add email data to the post meta
                add_post_meta($cf7post_id, "_mail", $cf7mail_data);

                // Generate the contact form shortcode
                $cf7shortcode = '[contact-form-7 id="' . $cf7post_id . '" title="' . $cf7title . '"]';

                // Save the shortcode in the theme settings
                set_theme_mod('feminine_style_coach_newsletter_form_shortcode', $cf7shortcode);
            } else {
                // Handle errors, if any
                echo 'Error creating the form post: ' . $cf7post_id->get_error_message();
            }

            // Display the contact form using the saved shortcode
            $cf7_shortcode = get_theme_mod('feminine_style_coach_newsletter_form_shortcode');

            // Check if the shortcode exists in the theme settings
            if ($cf7_shortcode) {
                echo 'Generated shortcode: ' . $cf7_shortcode . '<br>';
                echo do_shortcode($cf7_shortcode); // Output the contact form using the shortcode
            } else {
                echo 'Error: Contact form shortcode not found in theme mod.';
        }   


    }
?>