<div class="theme-offer">
<?php

    function unique_gift_shop_create_customizer_nav_menu() {
        // ------- Create Nav Menu --------
        $unique_gift_shop_menuname = 'Primary';
        $unique_gift_shop_menulocation = 'primary';
        $unique_gift_shop_menu_exists = wp_get_nav_menu_object($unique_gift_shop_menuname);

        if (!$unique_gift_shop_menu_exists) {
            $unique_gift_shop_menu_id = wp_create_nav_menu($unique_gift_shop_menuname);

            wp_update_nav_menu_item($unique_gift_shop_menu_id, 0, array(
                'menu-item-title' => __('Home', 'unique-gift-shop'),
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($unique_gift_shop_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'unique-gift-shop'),
                'menu-item-url' => home_url('/index.php/about-us/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($unique_gift_shop_menu_id, 0, array(
                'menu-item-title' => __('Services', 'unique-gift-shop'),
                'menu-item-url' => home_url('/index.php/services/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($unique_gift_shop_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'unique-gift-shop'),
                'menu-item-url' => home_url('/index.php/pages/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($unique_gift_shop_menu_id, 0, array(
                'menu-item-title' => __('Blog', 'unique-gift-shop'),
                'menu-item-url' => home_url('/index.php/blog/'),
                'menu-item-status' => 'publish',
            ));

            // Set menu to location
            $unique_gift_shop_locations = get_theme_mod('nav_menu_locations');
            if (!is_array($unique_gift_shop_locations)) {
                $unique_gift_shop_locations = array();
            }
            $unique_gift_shop_locations[$unique_gift_shop_menulocation] = $unique_gift_shop_menu_id;
            set_theme_mod('nav_menu_locations', $unique_gift_shop_locations);
        }
    }

    // POST and update the customizer and other related data of Unique Gift Shop
    if (isset($_POST['submit'])) {

        // -------- Plugin Installation and Activation (WooCommerce & Classic Widgets) -------- //
        include_once(ABSPATH . 'wp-admin/includes/plugin.php');
        include_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
        include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
        include_once(ABSPATH . 'wp-admin/includes/file.php');
        include_once(ABSPATH . 'wp-admin/includes/misc.php');

        // Plugin list
        $unique_gift_shop_plugins = array(
            array(
                'slug' => 'woocommerce',
                'file' => 'woocommerce/woocommerce.php',
                'download_url' => 'https://downloads.wordpress.org/plugin/woocommerce.zip'
            ),
            array(
                'slug' => 'classic-widgets',
                'file' => 'classic-widgets/classic-widgets.php',
                'download_url' => 'https://downloads.wordpress.org/plugin/classic-widgets.zip'
            )
        );

        foreach ($unique_gift_shop_plugins as $plugin) {
            $installed_plugins = get_plugins();

            // Install the plugin if it's not installed
            if (!isset($installed_plugins[$plugin['file']])) {
                $upgrader = new Plugin_Upgrader();
                $upgrader->install($plugin['download_url']);
            }

            // Activate the plugin if it's not active
            if (file_exists(WP_PLUGIN_DIR . '/' . $plugin['file']) && !is_plugin_active($plugin['file'])) {
                activate_plugin($plugin['file']);
            }
        }

        // ------- Create Menu --------
        unique_gift_shop_create_customizer_nav_menu();

        // ------- Create Pages --------
        function create_demo_page($title, $content = '', $template = '') {
            $page_id = unique_gift_shop_get_page_id_by_title($title);
        
            if (!$page_id) {
                $page_data = array(
                    'post_type'    => 'page',
                    'post_title'   => $title,
                    'post_content' => $content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                );
        
                $page_id = wp_insert_post($page_data);
        
                if ($template && !is_wp_error($page_id)) {
                    update_post_meta($page_id, '_wp_page_template', $template);
                }
            }
        
            return $page_id;
        }
        

        $unique_gift_shop_home_id = create_demo_page('Home', '', 'home/home.php');
        update_option('page_on_front', $unique_gift_shop_home_id);
        update_option('show_on_front', 'page');

        create_demo_page('Pages', '<p>Lorem Ipsum ...</p>');
        create_demo_page('About Us', '<p>Lorem Ipsum ...</p>');
        create_demo_page('Services', '<p>Service description...</p>');

        // Create blog page and assign it to display posts
        $blog_page_id = create_demo_page('Blog');
        update_option('page_for_posts', $blog_page_id);

        // Create the nav menu
        unique_gift_shop_create_customizer_nav_menu();

        // ------- Banner Section --------
        $unique_gift_shop_banner_headings = array('Elite Gifting', 'Elegant Gifting','Elite Gifting','Nice Gifting', 'Elegant Gifting',);
        set_theme_mod('unique_gift_shop_banner_slider_increase', 5);
        for ($i = 1; $i <= 5; $i++) {
            set_theme_mod("unique_gift_shop_banner_image$i", get_template_directory_uri() . "/img/banner$i.png");
            set_theme_mod("unique_gift_shop_banner_heading$i", $unique_gift_shop_banner_headings[$i - 1]);
            set_theme_mod("unique_gift_shop_banner_text$i", 'Lorem Ipsum is simply dummy text of the printing and typesetting industry');
            set_theme_mod("unique_gift_shop_banner_button_link$i", '#');

            set_theme_mod("unique_gift_shop_banner_ri8_text1$i", 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.');

            set_theme_mod("unique_gift_shop_banner_ri8_text2$i", 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.');
        }

        // ------- Product Section --------

       set_theme_mod('unique_gift_shop_product_main_heading', 'Shop Signature Gifts');
        set_theme_mod('unique_gift_shop_product_count', '4');

        // Create a new category if not exists
        $unique_gift_shop_product_category = wp_create_category('gift-product');
        set_theme_mod('unique_gift_shop_product_category', get_cat_name($unique_gift_shop_product_category));

        // Create WooCommerce product category
        wp_insert_term(
          'gift-product',
          'product_cat',
          array(
            'description' => '',
            'slug' => 'gift-product',
            'term_id' => 12,
            'term_taxonomy_id' => 34,
          )
        );

        if (class_exists('WooCommerce')) {
          // Required includes for image handling
          require_once(ABSPATH . 'wp-admin/includes/media.php');
          require_once(ABSPATH . 'wp-admin/includes/file.php');
          require_once(ABSPATH . 'wp-admin/includes/image.php');

          for ($i = 1; $i <= 4; $i++) {
            $title = 'Lorem Ipsum';
            $content = 'Lorem ipsum dolor sit amet, tetur piscing elit. Suspendisse smod congue bibendum.';

            // Create product post
            $unique_gift_shop_post = array(
              'post_title'   => wp_strip_all_tags($title),
              'post_content' => $content,
              'post_status'  => 'publish',
              'post_type'    => 'product',
            );

            $unique_gift_shop_product_id = wp_insert_post($unique_gift_shop_post);

            // Set product category
            $term = get_term_by('name', 'gift-product', 'product_cat');
            if ($term && !is_wp_error($term)) {
              wp_set_object_terms($unique_gift_shop_product_id, $term->term_id, 'product_cat');
            }

            // Set product prices
            update_post_meta($unique_gift_shop_product_id, '_price', '45.99');
            update_post_meta($unique_gift_shop_product_id, '_sale_price', '35.99');
            update_post_meta($unique_gift_shop_product_id, '_regular_price', '45.99');

            // Set product image via media_sideload_image
            $image_url = get_template_directory_uri() . '/img/prod' . $i . '.png';
            $attachment_id = media_sideload_image($image_url, $unique_gift_shop_product_id, null, 'id');

                if (!is_wp_error($attachment_id)) {
                set_post_thumbnail($unique_gift_shop_product_id, $attachment_id);
                }
            }
        }

        echo '<div class="success">Demo Import Successful</div>';
    }
?>

<ul>
    <li>
        <hr>
        <?php if (!isset($_POST['submit'])) : ?>
            <?php echo esc_html__('Click on the below button to get demo content installed.', 'unique-gift-shop'); ?>
            <br>
            <form id="demo-importer-form" action="" method="POST" onsubmit="return confirm('Do you really want to do this?');">
                <input class="run-btn" type="submit" name="submit" value="<?php echo esc_attr('Run Importer', 'unique-gift-shop'); ?>">
            </form>
        <?php else: ?>
            <div class="visit">
                <a href="<?php echo esc_url(home_url()); ?>" class="button button-primary button-large run-btn" style="margin-top: 10px;" target="_blank">View Site</a>
            </div>
        <?php endif; ?>
        <hr>
    </li>
</ul>
</div>