<?php //to use wp udpate plugin
  $blogging_hub_home_id=''; $blogging_hub_blog_id=''; $blogging_hub_page_id=''; $blogging_hub_about_id='';


  // Function to check if a page with a specific title exists
  function page_exists_by_title($blogging_hub_title) {
    $page_query = new WP_Query(array(
        'post_type'   => 'page',
        'title'       => $blogging_hub_title,
        'post_status' => 'publish',
        'numberposts' => 1
    ));
    
    if ($page_query->have_posts()) {
        // Return the ID of the first matching page
        $page = $page_query->posts[0];
        return $page->ID;
    }
  
    return false; // Return false if no page found
  }

  //Homepage
  $blogging_hub_home_title = 'Home';
  if (!page_exists_by_title($blogging_hub_home_title)) {
    $blogging_hub_home_content = '';
    $blogging_hub_home = array(
      'post_type'    => 'page',
      'post_title'   => $blogging_hub_home_title,
      'post_content' => $blogging_hub_home_content,
      'post_status'  => 'publish',
      'post_author'  => 1,
      'post_name'    => 'home'
    );

    $blogging_hub_home_id = wp_insert_post($blogging_hub_home);
    
    // Set the home page template
    add_post_meta($blogging_hub_home_id, '_wp_page_template', 'apex-library/homepage-template.php');
    
    // Set the static front page
    update_option('page_on_front', $blogging_hub_home_id);
    update_option('show_on_front', 'page');

  }else {
    // Get the ID of the existing page
    $blogging_hub_home_id = page_exists_by_title($blogging_hub_home_title);

    // Set the home page template
    add_post_meta($blogging_hub_home_id, '_wp_page_template', 'apex-library/homepage-template.php');
    
    // Set the static front page
    update_option('page_on_front', $blogging_hub_home_id);
    update_option('show_on_front', 'page');
  }

  // Create a posts page and assign the template
  $blogging_hub_blog_title = 'Blog';
  $blogging_hub_blog_check = get_page_by_path('blog');
  if (!$blogging_hub_blog_check) {
    $blogging_hub_blog = array(
      'post_type'    => 'page',
      'post_title'   => $blogging_hub_blog_title,
      'post_status'  => 'publish',
      'post_author'  => 1,
      'post_name'    => 'blog' // Unique slug for the blog page
    );
    $blogging_hub_blog_id = wp_insert_post($blogging_hub_blog);

    // Set the posts page
    if (!is_wp_error($blogging_hub_blog_id)) {
      update_option('page_for_posts', $blogging_hub_blog_id);
    }
  }
  
  // Create a Page if it doesn't exist
  if ( !page_exists_by_title('Page') ) {
    $blogging_hub_page_title = 'Page';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'page'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);
  }else {
    // Get the ID of the existing page
    $blogging_hub_ot_page = page_exists_by_title('Page');
  }

  // Create a About if it doesn't exist
  if ( !page_exists_by_title('About') ) {
    $blogging_hub_page_title = 'About';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'about'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);
  }else {
    // Get the ID of the existing page
    $blogging_hub_ot_page = page_exists_by_title('About');
  }

  // Create a Services if it doesn't exist
  if ( !page_exists_by_title('Services') ) {
    $blogging_hub_page_title = 'Services';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'services'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);
  }else {
    // Get the ID of the existing page
    $blogging_hub_ot_page = page_exists_by_title('Services');
  }

  // Create a Contact if it doesn't exist
  if ( !page_exists_by_title('Contact') ) {
    $blogging_hub_page_title = 'Contact';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'contact'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);
  }else {
    // Get the ID of the existing page
    $blogging_hub_ot_page = page_exists_by_title('Contact');
  }

  if ( !page_exists_by_title('Submenu Page One') ) {
    $blogging_hub_page_title = 'Submenu Page One';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semelTe obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel.Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel.';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'submenu-page-one'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);

    // Set the page template
    add_post_meta($blogging_hub_page_id, '_wp_page_template', 'apex-library/left-sidebar.php');
  }else {
    // Get the ID of the existing page
    $blogging_hub_ot_page = page_exists_by_title('Submenu Page One');
  }

  if ( !page_exists_by_title('Subemenu Page Two') ) {
    $blogging_hub_page_title = 'Subemenu Page Two';
    $blogging_hub_content = 'Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semelTe obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel.Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi. Sed quis scit si forte quod esset optima res pro me. sicut ea quae sentio. Qui vellem cadunt off ius desk ejus! Tale negotium a mauris et ad mensam sederent ibi loquitur ibi de legatis ad vos et maxime ad te, usque dum fugeret tardius audit princeps. Bene tamen fiduciam Ego got off semel.';

    $blogging_hub_ot_page = array(
      'post_type'     => 'page',
      'post_title'    => $blogging_hub_page_title,
      'post_content'  => $blogging_hub_content,
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_name'     => 'submenu-page-two'
    );
    $blogging_hub_page_id = wp_insert_post($blogging_hub_ot_page);

    // Set the page template
    add_post_meta($blogging_hub_page_id, '_wp_page_template', 'apex-library/right-sidebar.php');
    }else {
      // Get the ID of the existing page
      $blogging_hub_ot_page = page_exists_by_title('Subemenu Page Two');
    }


  // ------- Create Left Menu --------
  $blogging_hub_menuname =  'Main Menu';
  $blogging_hub_bpmenulocation = 'primary_menu';
  $blogging_hub_menu_exists = wp_get_nav_menu_object( $blogging_hub_menuname );

  if (!$blogging_hub_menu_exists) {
    // Create the menu
    $blogging_hub_menu_id = wp_create_nav_menu($blogging_hub_menuname);

    // Add the HOME item
    wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'  => __('Home', 'blogging-hub'),
        'menu-item-classes' => 'home',
        'menu-item-url'     => home_url('/index.php/home/'),
        'menu-item-status'  => 'publish'
    ));

    // Add the PAGE item
    $parent_page_item_id = wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'  => __('Blogs', 'blogging-hub'),
        'menu-item-classes' => 'blog',
        'menu-item-url'     => home_url('/index.php/blog/'),
        'menu-item-status'  => 'publish'
    ));

    // Add the PAGE item
    $parent_page_item_id = wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'  => __('Pages', 'blogging-hub'),
        'menu-item-classes' => 'page',
        'menu-item-url'     => home_url('/index.php/page/'),
        'menu-item-status'  => 'publish'
    ));

    // Add the Submenu Page One item as a child of PAGE
    wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'   => __('Submenu Page One', 'blogging-hub'),
        'menu-item-classes' => 'submenu-page-one',
        'menu-item-url'     => home_url('/index.php/submenu-page-one/'),
        'menu-item-status'  => 'publish',
        'menu-item-parent-id' => $parent_page_item_id
    ));

    // Add the Submenu Page Two item as a child of PAGE
    wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'   => __('Submenu Page Two', 'blogging-hub'),
        'menu-item-classes' => 'submenu-page-two',
        'menu-item-url'     => home_url('/index.php/submenu-page-two/'),
        'menu-item-status'  => 'publish',
        'menu-item-parent-id' => $parent_page_item_id
    ));

    wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'  => __('About', 'blogging-hub'),
        'menu-item-classes' => 'about',
        'menu-item-url'     => home_url('/index.php/about/'),
        'menu-item-status'  => 'publish'
    ));

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

    wp_update_nav_menu_item($blogging_hub_menu_id, 0, array(
        'menu-item-title'  => __('Contact', 'blogging-hub'),
        'menu-item-classes' => 'contact',
        'menu-item-url'     => home_url('/index.php/contact/'),
        'menu-item-status'  => 'publish'
    ));
    
    // Assign the menu to the desired location if not already assigned
    if (!has_nav_menu($blogging_hub_bpmenulocation)) {
        $blogging_hub_locations = get_theme_mod('nav_menu_locations');
        $blogging_hub_locations[$blogging_hub_bpmenulocation] = $blogging_hub_menu_id;
        set_theme_mod('nav_menu_locations', $blogging_hub_locations);
    }
  }
     
  // --------Header------------------------

    set_theme_mod( 'blogging_hub_header_search_hide', true );
    set_theme_mod( 'blogging_hub_topbar_email', 'blogging@example.com' );
    set_theme_mod( 'blogging_hub_categories_hide', true );
    set_theme_mod( 'blogging_hub_blog_hero_hide', true );
    set_theme_mod( 'blogging_hub_popular_review_hide', true );

    set_theme_mod( 'blogging_hub_facebook_url', '#' );
    set_theme_mod( 'blogging_hub_twitter_url', '#' );
    set_theme_mod( 'blogging_hub_instagram_url', '#' );
    set_theme_mod( 'blogging_hub_youtube_url', '#' );
    set_theme_mod( 'blogging_hub_linkedin_url', '#' );
    set_theme_mod( 'blogging_hub_pinterest_url', '#' );

    set_theme_mod( 'blogging_hub_popular_review_title', 'Most Popular Review' ); 
    set_theme_mod( 'blogging_hub_popular_review_view_all_btn', 'View All' );
    set_theme_mod( 'blogging_hub_popular_review_count', 5 );

    set_theme_mod( 'blogging_hub_categories_title', 'Categories' );
    set_theme_mod( 'blogging_hub_categories_count', 5 );

    set_theme_mod( 'blogging_hub_blog_hero_count_category', 'banner' );
    set_theme_mod( 'blogging_hub_blog_hero_count', 3 );
    set_theme_mod( 'blogging_hub_trending_title', 'Trending Blog' );

  //-------------- Demo Posts Import ----------------------
  if ( ! function_exists( 'blogging_hub_get_existing_post_id_by_title' ) ) {
    function blogging_hub_get_existing_post_id_by_title( $post_title ) {
      if ( ! function_exists( 'post_exists' ) ) {
        require_once ABSPATH . 'wp-admin/includes/post.php';
      }

      $existing_post_id = post_exists( $post_title, '', '', 'post' );
      return $existing_post_id ? (int) $existing_post_id : 0;
    }
  }

  if ( ! function_exists( 'blogging_hub_find_theme_image_path' ) ) {
    function blogging_hub_find_theme_image_path( $post_title ) {
      $images_dir = trailingslashit( get_template_directory() ) . 'assets/images/';

      $legacy_slug    = trim( str_replace( ' ', '-', $post_title ) );
      $sanitized_slug = sanitize_title( $post_title );
      $candidates     = array_unique(
        array(
          $legacy_slug . '.png',
          $legacy_slug . '.jpg',
          $legacy_slug . '.jpeg',
          $legacy_slug . '.webp',
          $sanitized_slug . '.png',
          $sanitized_slug . '.jpg',
          $sanitized_slug . '.jpeg',
          $sanitized_slug . '.webp',
        )
      );

      foreach ( $candidates as $candidate_file ) {
        if ( empty( $candidate_file ) ) {
          continue;
        }

        $full_path = $images_dir . $candidate_file;
        if ( file_exists( $full_path ) ) {
          return $full_path;
        }
      }

      return '';
    }
  }

  if ( ! function_exists( 'blogging_hub_attach_theme_image_to_post' ) ) {
    function blogging_hub_attach_theme_image_to_post( $source_path, $post_id, $attachment_title = '' ) {
      if ( empty( $source_path ) || ! file_exists( $source_path ) ) {
        return 0;
      }

      $source_contents = file_get_contents( $source_path );
      if ( false === $source_contents ) {
        return 0;
      }

      $filename  = basename( $source_path );
      $file_bits = wp_upload_bits( $filename, null, $source_contents );

      if ( ! empty( $file_bits['error'] ) ) {
        return 0;
      }

      $wp_filetype = wp_check_filetype( $filename, null );
      $attachment  = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title'     => ! empty( $attachment_title ) ? wp_strip_all_tags( $attachment_title ) : sanitize_file_name( $filename ),
        'post_content'   => '',
        'post_status'    => 'inherit',
      );

      $attach_id = wp_insert_attachment( $attachment, $file_bits['file'], $post_id );
      if ( is_wp_error( $attach_id ) ) {
        return 0;
      }

      if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
        require_once ABSPATH . 'wp-admin/includes/image.php';
      }

      $attach_data = wp_generate_attachment_metadata( $attach_id, $file_bits['file'] );
      wp_update_attachment_metadata( $attach_id, $attach_data );

      return (int) $attach_id;
    }
  }

  $blogging_hub_demo_posts = array(
    'Movies' => array(
      'Best Action Movies That Keep You Hooked',
    ),
    'Travels' => array(
      'What I Always Pack for Stress-Free Travel with Toddlers',
      'Best Fast Food In New York City USA ‘French Fries’',
    ),
    'Foods' => array(
      'Snacks That Saved My Sanity on Long Flights',
      'Easy 30-Minute Dinner Recipes for Families',
    ),
    'Technology' => array(
      'Cybersecurity Tips to Protect Your',
      'The scientists that deserve recognition the field of biology',
    ),
    'Sports' => array(
      '10 Quick & Healthy Breakfast Ideas for Busy Mornings ',
    ),
  );

  foreach ( $blogging_hub_demo_posts as $category_name => $demo_post_titles ) {
    $category_description = 'Lorem ipsum dolor sit amet';
    $existing_term        = term_exists( $category_name, 'category' );

    if ( ! $existing_term ) {
      $term_result = wp_insert_term(
        $category_name,
        'category',
        array(
          'description' => $category_description,
          'slug'        => sanitize_title( $category_name ),
        )
      );

      if ( is_wp_error( $term_result ) || empty( $term_result['term_id'] ) ) {
        continue;
      }

      $term_id = (int) $term_result['term_id'];
    } else {
      $term_id = is_array( $existing_term ) ? (int) $existing_term['term_id'] : (int) $existing_term;
    }

    foreach ( $demo_post_titles as $demo_post_title ) {
      $post_content = 'Te obtinuit ut adepto satis somno.';
      $post_id      = blogging_hub_get_existing_post_id_by_title( $demo_post_title );

      if ( ! $post_id ) {
        $inserted_post = wp_insert_post(
          array(
            'post_title'   => wp_strip_all_tags( $demo_post_title ),
            'post_content' => $post_content,
            'post_status'  => 'publish',
            'post_type'    => 'post',
          ),
          true
        );

        if ( is_wp_error( $inserted_post ) || ! $inserted_post ) {
          continue;
        }

        $post_id = (int) $inserted_post;
      }

      wp_set_post_terms( $post_id, array( $term_id ), 'category', true );

      if ( ! has_post_thumbnail( $post_id ) ) {
        $image_path = blogging_hub_find_theme_image_path( $demo_post_title );
        if ( ! empty( $image_path ) ) {
          $attach_id = blogging_hub_attach_theme_image_to_post( $image_path, $post_id, $demo_post_title );
          if ( $attach_id ) {
            set_post_thumbnail( $post_id, $attach_id );
          }
        }
      }
    }
  }
    
?>