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

    function technical_blogging_create_customizer_nav_menu() {
        // ------- Create Nav Menu --------
        $technical_blogging_menuname = 'Primary';
        $technical_blogging_menulocation = 'primary';
        $technical_blogging_menu_exists = wp_get_nav_menu_object($technical_blogging_menuname);

        if (!$technical_blogging_menu_exists) {
            $technical_blogging_menu_id = wp_create_nav_menu($technical_blogging_menuname);

            wp_update_nav_menu_item($technical_blogging_menu_id, 0, array(
                'menu-item-title' => __('Home', 'technical-blogging'),
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
            ));

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

            wp_update_nav_menu_item($technical_blogging_menu_id, 0, array(
                'menu-item-title' => __('Services', 'technical-blogging'),
                'menu-item-url' => home_url('/index.php/services/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($technical_blogging_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'technical-blogging'),
                'menu-item-url' => home_url('/index.php/pages/'),
                'menu-item-status' => 'publish',
            ));

            wp_update_nav_menu_item($technical_blogging_menu_id, 0, array(
                'menu-item-title' => __('Blog', 'technical-blogging'),
                'menu-item-url' => home_url('/index.php/blog/'),
                'menu-item-status' => 'publish',
            ));

            // Set menu to location
            $technical_blogging_locations = get_theme_mod('nav_menu_locations');
            if (!is_array($technical_blogging_locations)) {
                $technical_blogging_locations = array();
            }
            $technical_blogging_locations[$technical_blogging_menulocation] = $technical_blogging_menu_id;
            set_theme_mod('nav_menu_locations', $technical_blogging_locations);
        }
    }

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

        foreach ($technical_blogging_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 --------
        technical_blogging_create_customizer_nav_menu();

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

        $technical_blogging_home_id = create_demo_page('Home', '', 'home/home.php');
        update_option('page_on_front', $technical_blogging_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
        technical_blogging_create_customizer_nav_menu();

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

        set_theme_mod('technical_blogging_topbar_text', 'TRENDING');
        set_theme_mod('technical_blogging_topbar_marquee_text', 'Quisque ut dolor gravida, placerat libero vel, euismod.');
        set_theme_mod('technical_blogging_header_subscribe_btn_link', '#');        
        set_theme_mod('technical_blogging_social_media1_heading', 'www.facebook.com');
        set_theme_mod('technical_blogging_social_media2_heading', 'www.instagram.com');
        set_theme_mod('technical_blogging_social_media3_heading', 'www.twitter.com');
        set_theme_mod('technical_blogging_social_media4_heading', 'www.youtube.com');

        // Create a category for the post (optional)
        $technical_blogging_demo_category = wp_create_category('Advertisement');

        // Create demo post
        $technical_blogging_demo_post = array(
            'post_title'    => 'Quisque ut dolor gravida, placerat libero vel, euismod.',
            'post_content'  => 'Lorem ipsum dolor sit amet...',
            'post_status'   => 'publish',
            'post_type'     => 'post',
            'post_category' => array($technical_blogging_demo_category)
        );

        $technical_blogging_demo_post_id = wp_insert_post($technical_blogging_demo_post);

        // Add a featured image to the post
        $image_path = get_template_directory() . '/img/ads-demo.png'; // Replace with your image path
        $image_name = 'ads-demo.png';
        $upload_dir = wp_upload_dir();
        $image_data = file_get_contents($image_path);
        $unique_file_name = wp_unique_filename($upload_dir['path'], $image_name);
        $filename = basename($unique_file_name);

        // Create the full path to save the file
        if (wp_mkdir_p($upload_dir['path'])) {
            $file = $upload_dir['path'] . '/' . $filename;
        } else {
            $file = $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, $image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

        // Check and insert attachment
        $wp_filetype = wp_check_filetype($filename, null);
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title'     => sanitize_file_name($filename),
            'post_content'   => '',
            'post_status'    => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $file, $technical_blogging_demo_post_id);

        // Include image functions and generate metadata
        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($technical_blogging_demo_post_id, $attach_id);

        // Set the post ID in the theme mod for use in your ads box
        set_theme_mod('technical_blogging_ads_post_setting', $technical_blogging_demo_post_id);



        // ------- Banner Section --------

        $technical_blogging_top_post_category_1 = wp_create_category('Tech'); 
    set_theme_mod('technical_blogging_top_post_category_1', get_cat_name($technical_blogging_top_post_category_1));


    set_theme_mod( 'technical_blogging_banner_number1', '3' );

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

      $title = 'The Rise of Quantum Computing: What It Means for Developers';
      $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
      $technical_blogging_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($technical_blogging_top_post_category_1) 
      );

      $technical_blogging_banner1_post_id = wp_insert_post($technical_blogging_post);
      $technical_blogging_banner1_post_image_url = get_template_directory()."/img/slider$i.png"; // file path

      $technical_blogging_banner1_image_name = 'slider'.$i.'.png';
      $technical_blogging_banner1_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $technical_blogging_banner1_image_data       = file_get_contents($technical_blogging_banner1_post_image_url); 
       
      // Get image data
      $technical_blogging_banner1_unique_file_name = wp_unique_filename( $technical_blogging_banner1_upload_dir['path'], $technical_blogging_banner1_image_name ); 
      // Generate unique name
      $filename= basename( $technical_blogging_banner1_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $technical_blogging_banner1_upload_dir['path'] ) ) {
          $file = $technical_blogging_banner1_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $technical_blogging_banner1_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, $technical_blogging_banner1_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $technical_blogging_banner1_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $technical_blogging_banner1_attachment, $file, $technical_blogging_banner1_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( $technical_blogging_banner1_post_id, $attach_id );

          set_theme_mod( 'technical_blogging_banner_button_text', 'Read More' );

    }

// 2nd post
        $technical_blogging_top_post_category_2 = wp_create_category('Apps'); 
    set_theme_mod('technical_blogging_top_post_category_2', get_cat_name($technical_blogging_top_post_category_2));   

    set_theme_mod( 'technical_blogging_banner_number2', '3' );

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

      $title = 'Mastering Docker for Application Deployment';
      $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
      $technical_blogging_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($technical_blogging_top_post_category_2) 
      );

      $technical_blogging_banner2_post_id = wp_insert_post($technical_blogging_post);
      $technical_blogging_banner2_post_image_url = get_template_directory()."/img/slider-right$i.png"; // file path

      $technical_blogging_banner2_image_name = 'slider-right'.$i.'.png';
      $technical_blogging_banner2_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $technical_blogging_banner2_image_data       = file_get_contents($technical_blogging_banner2_post_image_url); 
       
      // Get image data
      $technical_blogging_banner2_unique_file_name = wp_unique_filename( $technical_blogging_banner2_upload_dir['path'], $technical_blogging_banner2_image_name ); 
      // Generate unique name
      $filename= basename( $technical_blogging_banner2_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $technical_blogging_banner2_upload_dir['path'] ) ) {
          $file = $technical_blogging_banner2_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $technical_blogging_banner2_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, $technical_blogging_banner2_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $technical_blogging_banner2_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $technical_blogging_banner2_attachment, $file, $technical_blogging_banner2_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( $technical_blogging_banner2_post_id, $attach_id );

    }
    // 3rd post    

        $technical_blogging_top_post_category_3 = wp_create_category('AI'); 
    set_theme_mod('technical_blogging_top_post_category_3', get_cat_name($technical_blogging_top_post_category_3));


    set_theme_mod( 'technical_blogging_banner_number3', '3' );

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

      $title = 'How AI is Revolutionizing Cybersecurity in 2025';
      $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
      $technical_blogging_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($technical_blogging_top_post_category_3) 
      );

      $technical_blogging_banner3_post_id = wp_insert_post($technical_blogging_post);
      $technical_blogging_banner3_post_image_url = get_template_directory()."/img/slider-btm$i.png"; // file path

      $technical_blogging_banner3_image_name = 'slider-btm'.$i.'.png';
      $technical_blogging_banner3_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $technical_blogging_banner3_image_data       = file_get_contents($technical_blogging_banner3_post_image_url); 
       
      // Get image data
      $technical_blogging_banner3_unique_file_name = wp_unique_filename( $technical_blogging_banner3_upload_dir['path'], $technical_blogging_banner3_image_name ); 
      // Generate unique name
      $filename= basename( $technical_blogging_banner3_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $technical_blogging_banner3_upload_dir['path'] ) ) {
          $file = $technical_blogging_banner3_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $technical_blogging_banner3_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, $technical_blogging_banner3_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $technical_blogging_banner3_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $technical_blogging_banner3_attachment, $file, $technical_blogging_banner3_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( $technical_blogging_banner3_post_id, $attach_id );

    }

        // ------- Featured topics Section --------

    set_theme_mod('technical_blogging_featured_section_bg_image', get_template_directory_uri() . '/img/feature-bg.jpg');
    set_theme_mod( 'technical_blogging_featured_topics_main_heading', '“FEATURED TOPICS”' );
 $technical_blogging_feature_category = wp_create_category('Tech'); 
    set_theme_mod('technical_blogging_feature_category', get_cat_name($technical_blogging_feature_category));

     $technical_blogging_featured_headings = array('Exploring Rust: The Future of Safe and Fast Programming', 'Top 10 VS Code to Boost Developer Productivity');

    set_theme_mod( 'technical_blogging_featured_topics_number', '2' );

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

      $title = $technical_blogging_featured_headings[$i - 1];
      $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
      $technical_blogging_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($technical_blogging_feature_category) 
      );

      $technical_blogging_featured_post_id = wp_insert_post($technical_blogging_post);
      $technical_blogging_featured_post_image_url = get_template_directory()."/img/ftr-left$i.jpg"; // file path

      $technical_blogging_featured_image_name = 'ftr-left'.$i.'.jpg';
      $technical_blogging_featured_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $technical_blogging_featured_image_data       = file_get_contents($technical_blogging_featured_post_image_url); 
       
      // Get image data
      $technical_blogging_featured_unique_file_name = wp_unique_filename( $technical_blogging_featured_upload_dir['path'], $technical_blogging_featured_image_name ); 
      // Generate unique name
      $filename= basename( $technical_blogging_featured_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $technical_blogging_featured_upload_dir['path'] ) ) {
          $file = $technical_blogging_featured_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $technical_blogging_featured_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, $technical_blogging_featured_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $technical_blogging_featured_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $technical_blogging_featured_attachment, $file, $technical_blogging_featured_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( $technical_blogging_featured_post_id, $attach_id );

    }

// featured 2nd

 $technical_blogging_selected_category2 = wp_create_category('Tech2'); 
    set_theme_mod('technical_blogging_selected_category2', get_cat_name($technical_blogging_selected_category2));

    $technical_blogging_featured_headings2 = array('5G Networks and the Edge: Driving Innovation in IoT', 'Navigating the Challenges of Migrating to the Cloud');

    set_theme_mod( 'technical_blogging_featured_topics_number2', '2' );

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

      $title = $technical_blogging_featured_headings2[$i - 1];
      $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
      $technical_blogging_post = array(
       'post_title'    => wp_strip_all_tags( $title ),
       'post_content'  => $content,
       'post_status'   => 'publish',
       'post_type'     => 'post',
       'post_category' => array($technical_blogging_selected_category2) 
      );

      $technical_blogging_featured_post_id = wp_insert_post($technical_blogging_post);
      $technical_blogging_featured_post_image_url = get_template_directory()."/img/ftr-right$i.jpg"; // file path

      $technical_blogging_featured_image_name = 'ftr-right'.$i.'.jpg';
      $technical_blogging_featured_upload_dir       = wp_upload_dir(); 
      // Set upload folder
      $technical_blogging_featured_image_data       = file_get_contents($technical_blogging_featured_post_image_url); 
       
      // Get image data
      $technical_blogging_featured_unique_file_name = wp_unique_filename( $technical_blogging_featured_upload_dir['path'], $technical_blogging_featured_image_name ); 
      // Generate unique name
      $filename= basename( $technical_blogging_featured_unique_file_name ); 
      // Create image file name
      // Check folder permission and define file location
      if( wp_mkdir_p( $technical_blogging_featured_upload_dir['path'] ) ) {
          $file = $technical_blogging_featured_upload_dir['path'] . '/' . $filename;
      } else {
          $file = $technical_blogging_featured_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, $technical_blogging_featured_image_data, FS_CHMOD_FILE ) ) {
            wp_die( 'Error saving file!' );
        }

      $wp_filetype = wp_check_filetype( $filename, null );
      $technical_blogging_featured_attachment = array(
          'post_mime_type' => $wp_filetype['type'],
          'post_title'     => sanitize_file_name( $filename ),
          'post_content'   => '',
          'post_status'    => 'inherit'
      );
      $attach_id = wp_insert_attachment( $technical_blogging_featured_attachment, $file, $technical_blogging_featured_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( $technical_blogging_featured_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.', 'technical-blogging'); ?>
            <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', 'technical-blogging'); ?>">
            </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>