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


    function babysitter_childcare_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.8); 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' => '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">
                document.getElementById("plugin-loader").style.display = "none";
              </script>';

        // Add filter to skip WooCommerce setup wizard after activation
        add_filter('woocommerce_prevent_automatic_wizard_redirect', '__return_true');
    }

    // Call the import function
    babysitter_childcare_import_demo_content();

    // ------- Create Nav Menu --------
$babysitter_childcare_menuname = 'Main Menus';
$babysitter_childcare_bpmenulocation = 'primary-menu';
$babysitter_childcare_menu_exists = wp_get_nav_menu_object($babysitter_childcare_menuname);

if (!$babysitter_childcare_menu_exists) {
    $babysitter_childcare_menu_id = wp_create_nav_menu($babysitter_childcare_menuname);

    // Create Home Page
    $babysitter_childcare_home_title = 'Home';
    $babysitter_childcare_home = array(
        'post_type' => 'page',
        'post_title' => $babysitter_childcare_home_title,
        'post_content' => '',
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'home'
    );
    $babysitter_childcare_home_id = wp_insert_post($babysitter_childcare_home);

    // Assign Home Page Template
    add_post_meta($babysitter_childcare_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', $babysitter_childcare_home_id);
    update_option('show_on_front', 'page');

    // Add Home Page to Menu
    wp_update_nav_menu_item($babysitter_childcare_menu_id, 0, array(
        'menu-item-title' => __('Home', 'babysitter-childcare'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url('/'),
        'menu-item-status' => 'publish',
        'menu-item-object-id' => $babysitter_childcare_home_id,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type'
    ));

    // Create About Us Page with Dummy Content
    $babysitter_childcare_about_title = 'About Us';
    $babysitter_childcare_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>

             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.<br> 

                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br> 

                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
    $babysitter_childcare_about = array(
        'post_type' => 'page',
        'post_title' => $babysitter_childcare_about_title,
        'post_content' => $babysitter_childcare_about_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'about-us'
    );
    $babysitter_childcare_about_id = wp_insert_post($babysitter_childcare_about);

    // Add About Us Page to Menu
    wp_update_nav_menu_item($babysitter_childcare_menu_id, 0, array(
        'menu-item-title' => __('About Us', 'babysitter-childcare'),
        'menu-item-classes' => 'about-us',
        'menu-item-url' => home_url('/about-us/'),
        'menu-item-status' => 'publish',
        'menu-item-object-id' => $babysitter_childcare_about_id,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type'
    ));

    // Create Services Page with Dummy Content
    $babysitter_childcare_services_title = 'Services';
    $babysitter_childcare_services_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>

             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.<br> 

                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br> 

                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
    $babysitter_childcare_services = array(
        'post_type' => 'page',
        'post_title' => $babysitter_childcare_services_title,
        'post_content' => $babysitter_childcare_services_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'services'
    );
    $babysitter_childcare_services_id = wp_insert_post($babysitter_childcare_services);

    // Add Services Page to Menu
    wp_update_nav_menu_item($babysitter_childcare_menu_id, 0, array(
        'menu-item-title' => __('Services', 'babysitter-childcare'),
        'menu-item-classes' => 'services',
        'menu-item-url' => home_url('/services/'),
        'menu-item-status' => 'publish',
        'menu-item-object-id' => $babysitter_childcare_services_id,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type'
    ));

    // Create Pages Page with Dummy Content
    $babysitter_childcare_pages_title = 'Pages';
    $babysitter_childcare_pages_content = '<h2>Our Pages</h2>
    <p>Explore all the pages we have on our website. Find information about our services, company, and more.</p>';
    $babysitter_childcare_pages = array(
        'post_type' => 'page',
        'post_title' => $babysitter_childcare_pages_title,
        'post_content' => $babysitter_childcare_pages_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'pages'
    );
    $babysitter_childcare_pages_id = wp_insert_post($babysitter_childcare_pages);

    // Add Pages Page to Menu
    wp_update_nav_menu_item($babysitter_childcare_menu_id, 0, array(
        'menu-item-title' => __('Pages', 'babysitter-childcare'),
        'menu-item-classes' => 'pages',
        'menu-item-url' => home_url('/pages/'),
        'menu-item-status' => 'publish',
        'menu-item-object-id' => $babysitter_childcare_pages_id,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type'
    ));

    // Create Contact Page with Dummy Content
    $babysitter_childcare_contact_title = 'Contact';
    $babysitter_childcare_contact_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>

             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.<br> 

                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br> 

                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
    $babysitter_childcare_contact = array(
        'post_type' => 'page',
        'post_title' => $babysitter_childcare_contact_title,
        'post_content' => $babysitter_childcare_contact_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'contact'
    );
    $babysitter_childcare_contact_id = wp_insert_post($babysitter_childcare_contact);

    // Add Contact Page to Menu
    wp_update_nav_menu_item($babysitter_childcare_menu_id, 0, array(
        'menu-item-title' => __('Contact', 'babysitter-childcare'),
        'menu-item-classes' => 'contact',
        'menu-item-url' => home_url('/contact/'),
        'menu-item-status' => 'publish',
        'menu-item-object-id' => $babysitter_childcare_contact_id,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type'
    ));

    // Set the menu location if it's not already set
    if (!has_nav_menu($babysitter_childcare_bpmenulocation)) {
        $locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
        if (empty($locations)) {
            $locations = array();
        }
        $locations[$babysitter_childcare_bpmenulocation] = $babysitter_childcare_menu_id;
        set_theme_mod('nav_menu_locations', $locations);
    }
}

        //---Header--//
        set_theme_mod('babysitter_childcare_header_button', 'Get A Quote');
        set_theme_mod('babysitter_childcare_header_link', '#');
        set_theme_mod('babysitter_childcare_phone', '+123-456-789-00');

        // Banner Section
        set_theme_mod('babysitter_childcare_about_enable', 'true');

        set_theme_mod('babysitter_childcare_slider_short_heading', 'Hey Busy Parents');
        set_theme_mod('babysitter_childcare_slider_heading', 'Connect With Top babysitter In Your Area');
        set_theme_mod('babysitter_childcare_banner_btn_text1', 'Find a Sitter');
        set_theme_mod('babysitter_childcare_banner_btn_link1', '#');
        set_theme_mod('babysitter_childcare_banner_btn_text2', 'Book Appointment');
        set_theme_mod('babysitter_childcare_banner_btn_link2', '#');


        set_theme_mod('babysitter_childcare_banner_image1',  get_template_directory_uri().'/assets/images/banner1.png' );
        set_theme_mod('babysitter_childcare_banner_image2',  get_template_directory_uri().'/assets/images/banner2.png' );
        set_theme_mod('babysitter_childcare_banner_image3',  get_template_directory_uri().'/assets/images/banner3.png' );
        set_theme_mod('babysitter_childcare_banner_image4',  get_template_directory_uri().'/assets/images/banner4.png' );

        set_theme_mod('babysitter_childcare_total_no_babysitter_center', '300+');
        set_theme_mod('babysitter_childcare_total_no_happy_parents', '5K');
        set_theme_mod('babysitter_childcare_total_no_babysitter', '300+');


        // About Section
        set_theme_mod('babysitter_childcare_about_short_heading', 'About Us');

        set_theme_mod('babysitter_childcare_abt_image', get_template_directory_uri().'/assets/images/about-bg.png' );

        set_theme_mod('babysitter_childcare_customer_review', '5.0 (245 review)');
        set_theme_mod('babysitter_childcare_trusted_heading', 'Trusted by mom and professionals');
        set_theme_mod('babysitter_childcare_moms_enrolled_counter', '25k');
        set_theme_mod('babysitter_childcare_integrated_counter', '100+');
        set_theme_mod('babysitter_childcare_professionals', '645+');

        // Create About page and set the featured image
        $babysitter_childcare_abt_title = 'Get the right care for your child';
        $babysitter_childcare_abt_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</br>quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

        $my_post = array(
            'post_title'    => wp_strip_all_tags($babysitter_childcare_abt_title),
            'post_content'  => $babysitter_childcare_abt_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
        );

        $post_id = wp_insert_post($my_post);

        if ($post_id) {
            set_theme_mod('babysitter_childcare_about_page', $post_id);
            $image_url = get_template_directory_uri() . '/assets/images/about-img.png';
            $image_id = media_sideload_image($image_url, $post_id, null, 'id');
            if (!is_wp_error($image_id)) {
                set_post_thumbnail($post_id, $image_id);
            }
        }

        // Define post category names
        $babysitter_childcare_category_names = array('postcategory1');
        set_theme_mod('babysitter_childcare_about_catData', 'postcategory1');

        foreach ($babysitter_childcare_category_names as $babysitter_childcare_category_name) {
            // Check if the category exists, and insert if necessary
            $babysitter_childcare_term = term_exists($babysitter_childcare_category_name, 'category');
            
            if (!$babysitter_childcare_term) {
                $babysitter_childcare_term = wp_insert_term($babysitter_childcare_category_name, 'category');
            }

            // Handle term insertion errors
            if (is_wp_error($babysitter_childcare_term)) {
                error_log('Error creating category: ' . $babysitter_childcare_term->get_error_message());
                continue;
            }

            // Ensure the term ID is retrieved correctly
            $babysitter_childcare_term_id = is_array($babysitter_childcare_term) ? $babysitter_childcare_term['term_id'] : $babysitter_childcare_term;

            // Loop for creating posts under the category
            for ($babysitter_childcare_i = 0; $babysitter_childcare_i < 3; $babysitter_childcare_i++) {
                $babysitter_childcare_my_post = array(
                    'post_title'   => 'Post ' . ($babysitter_childcare_i + 1),
                    'post_content' => 'This is the content for post ' . ($babysitter_childcare_i + 1),
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );

                $babysitter_childcare_post_id = wp_insert_post($babysitter_childcare_my_post);
                if (is_wp_error($babysitter_childcare_post_id)) {
                    error_log('Error creating post: ' . $babysitter_childcare_post_id->get_error_message());
                    continue;
                }

                // Assign the category to the post
                wp_set_post_categories($babysitter_childcare_post_id, array($babysitter_childcare_term_id));

                // Handle featured image
                $babysitter_childcare_image_url = get_template_directory_uri() . '/assets/images/post-img' . ($babysitter_childcare_i + 1) . '.png';
                $babysitter_childcare_image_id = media_sideload_image($babysitter_childcare_image_url, $babysitter_childcare_post_id, null, 'id');

                if (!is_wp_error($babysitter_childcare_image_id)) {
                    set_post_thumbnail($babysitter_childcare_post_id, $babysitter_childcare_image_id);
                }
            }
        }


    }

?>