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

    function legacy_medical_appointment_create_customizer_nav_menu() {
        // ------- Create Nav Menu --------
        $legacy_medical_appointment_menuname = 'Primary';
        $legacy_medical_appointment_menulocation = 'primary';
        $legacy_medical_appointment_menu_exists = wp_get_nav_menu_object($legacy_medical_appointment_menuname);

        if (!$legacy_medical_appointment_menu_exists) {
            $legacy_medical_appointment_menu_id = wp_create_nav_menu($legacy_medical_appointment_menuname);

            wp_update_nav_menu_item($legacy_medical_appointment_menu_id, 0, array(
                'menu-item-title' => __('Home', 'legacy-medical-appointment'),
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($legacy_medical_appointment_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'legacy-medical-appointment'),
                'menu-item-url' => home_url('/index.php/about-us/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($legacy_medical_appointment_menu_id, 0, array(
                'menu-item-title' => __('Services', 'legacy-medical-appointment'),
                'menu-item-url' => home_url('/index.php/services/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($legacy_medical_appointment_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'legacy-medical-appointment'),
                'menu-item-url' => home_url('/index.php/pages/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($legacy_medical_appointment_menu_id, 0, array(
                'menu-item-title' => __('Blog', 'legacy-medical-appointment'),
                'menu-item-url' => home_url('/index.php/blog/'),
                'menu-item-status' => 'publish',
            ));

            // Set menu to location
            $legacy_medical_appointment_locations = get_theme_mod('nav_menu_locations');
            if (!is_array($legacy_medical_appointment_locations)) {
                $legacy_medical_appointment_locations = array();
            }
            $legacy_medical_appointment_locations[$legacy_medical_appointment_menulocation] = $legacy_medical_appointment_menu_id;
            set_theme_mod('nav_menu_locations', $legacy_medical_appointment_locations);
        }
    }

    // POST and update the customizer and other related data of Legacy Medical Appointment
    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
        $legacy_medical_appointment_plugins = array(
            array(
                'slug' => 'classic-widgets',
                'file' => 'classic-widgets/classic-widgets.php',
                'download_url' => 'https://downloads.wordpress.org/plugin/classic-widgets.zip'
            )
        );

        foreach ($legacy_medical_appointment_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 --------
        legacy_medical_appointment_create_customizer_nav_menu();

        // ------- Create Pages --------
        function create_demo_page($title, $content = '', $template = '') {
            $page_id = legacy_medical_appointment_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;
        }
        

        $legacy_medical_appointment_home_id = create_demo_page('Home', '', 'home/home.php');
        update_option('page_on_front', $legacy_medical_appointment_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
        legacy_medical_appointment_create_customizer_nav_menu();

        // ------- Set Theme Mods --------

        set_theme_mod('legacy_medical_appointment_topbar_text', 'We are Industrial Cleaners');
        set_theme_mod('legacy_medical_appointment_topbar_email_id', 'info@example.com');
        set_theme_mod('legacy_medical_appointment_topbar_email_head', 'Email:');
        set_theme_mod('legacy_medical_appointment_topbar_career_button_link', '#');
        set_theme_mod('legacy_medical_appointment_topbar_service_button_link', '#');
        set_theme_mod('legacy_medical_appointment_topbar_contact_button_link', '#');
        set_theme_mod('legacy_medical_appointment_header_address', 'LOS ANGELES');
        set_theme_mod('legacy_medical_appointment_header_address_head', 'Location: US- ');
        set_theme_mod('legacy_medical_appointment_header_phone_number', '00222123333019');
        set_theme_mod('legacy_medical_appointment_header_phone_head', 'Call Us: ');

        // ------- Banner Section --------
       
        set_theme_mod("legacy_medical_appointment_banner_image", get_template_directory_uri() . "/img/banner.png");
        set_theme_mod("legacy_medical_appointment_banner_small_head", 'WELCOME TO OUR SITE');
        set_theme_mod("legacy_medical_appointment_banner_heading", 'Your Time, Your Health, Your Schedule.');
        set_theme_mod("legacy_medical_appointment_banner_text", 'Our long history and regular clients prove that our service is qualified.');
        set_theme_mod("legacy_medical_appointment_banner_button_link", '#');
        set_theme_mod("legacy_medical_appointment_banner_button_link2", '#');
        set_theme_mod("legacy_medical_appointment_banner_scedule_day", 'Sunday - Friday');
        set_theme_mod("legacy_medical_appointment_banner_schedule_time", '9:00 am - 9:00 pm');
        set_theme_mod("legacy_medical_appointment_banner_offer_head", 'Discount 25%off');
        set_theme_mod("legacy_medical_appointment_banner_offer_text", 'In First Medical Service for you');
        

        // ------- Latest news Section --------

        set_theme_mod('legacy_medical_appointment_latest_news_small_heading', 'OUR BLOG');
        set_theme_mod('legacy_medical_appointment_latest_news_main_heading', 'Our Latest Posts');
        $legacy_medical_appointment_selected_category = wp_create_category('Business'); 
    set_theme_mod('legacy_medical_appointment_selected_category', get_cat_name($legacy_medical_appointment_selected_category));

    set_theme_mod( 'legacy_medical_appointment_latest_news_number', '4' );

    for($i=1;$i<=4;$i++){

      $title = 'Lorem ipsum dolor sit amet, consectetur ';
      $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do minim eiusmod tempor incididunt ut afds labore et dolore magna aliqua. Ut enim ad quis nostrud exercitation ullamco laboris.';

      // Create post object
      $legacy_medical_appointment_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($legacy_medical_appointment_selected_category) 
      );

      $legacy_medical_appointment_latest_news_post_id = wp_insert_post($legacy_medical_appointment_post);
      $legacy_medical_appointment_latest_news_post_image_url = get_template_directory()."/img/blog$i.png"; // file path

      $legacy_medical_appointment_latest_news_image_name = 'blog'.$i.'.png';
      $legacy_medical_appointment_latest_news_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $legacy_medical_appointment_latest_news_image_data       = file_get_contents($legacy_medical_appointment_latest_news_post_image_url); 
       
      // Get image data
      $legacy_medical_appointment_latest_news_unique_file_name = wp_unique_filename( $legacy_medical_appointment_latest_news_upload_dir['path'], $legacy_medical_appointment_latest_news_image_name ); 
      // Generate unique name
      $filename= basename( $legacy_medical_appointment_latest_news_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $legacy_medical_appointment_latest_news_upload_dir['path'] ) ) {
          $file = $legacy_medical_appointment_latest_news_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $legacy_medical_appointment_latest_news_upload_dir['basedir'] . '/' . $filename;
      }

      // Create the image  file on the server
        if ( ! function_exists( 'WP_Filesystem' ) ) {
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
        }

        WP_Filesystem();
        global $wp_filesystem;

        if ( ! $wp_filesystem->put_contents( $file, $legacy_medical_appointment_latest_news_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $legacy_medical_appointment_latest_news_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $legacy_medical_appointment_latest_news_attachment, $file, $legacy_medical_appointment_latest_news_post_id );
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
          wp_update_attachment_metadata( $attach_id, $attach_data );
          set_post_thumbnail( $legacy_medical_appointment_latest_news_post_id, $attach_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.', 'legacy-medical-appointment'); ?>
            <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', 'legacy-medical-appointment'); ?>">
            </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>