/* BEGIN SaudiBusRental Sitemap Folder Assets - SAFE ADDON */

if ( ! function_exists( 'sbr_safe_current_sitemap_folder' ) ) {
  function sbr_safe_current_sitemap_folder() {
    $request_path = '';

    if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] !== '' ) {
      $request_uri  = wp_unslash( $_SERVER['REQUEST_URI'] );
      $request_path = parse_url( $request_uri, PHP_URL_PATH );
      $request_path = trim( (string) $request_path, '/' );
    }

    if ( $request_path === '' ) {
      return 'sitemap-core';
    }

    $parts = explode( '/', $request_path );
    $slug  = sanitize_title( end( $parts ) );

    $core_slugs = array(
      'about-saudi-bus-rental',
      'company-profile',
      'request-a-quote',
      'contact-us',
      'reviews',
      'gallery',
      'service-areas',
      'fleet',
      'privacy-policy',
      'terms-and-conditions',
      'cancellation-policy',
    );

    if ( in_array( $slug, $core_slugs, true ) ) {
      return 'sitemap-core';
    }

    $sitemap_map = array(
      // Future examples:
      // 'bus-rental-saudi-arabia'       => 'sitemap-bus-rental-main',
      // 'staff-transport-saudi-arabia'  => 'sitemap-staff-transport',
      // 'labor-transport-saudi-arabia'  => 'sitemap-labor-transport',
      // 'airport-transfer-saudi-arabia' => 'sitemap-airport-transfer',
    );

    foreach ( $sitemap_map as $prefix => $folder_name ) {
      $prefix      = trim( (string) $prefix, '/' );
      $folder_name = sanitize_file_name( (string) $folder_name );

      if ( $prefix === '' || $folder_name === '' ) {
        continue;
      }

      if (
        $request_path === $prefix ||
        strpos( $request_path, $prefix . '/' ) === 0 ||
        $slug === $prefix
      ) {
        return $folder_name;
      }
    }

    return 'sitemap-core';
  }
}

if ( ! function_exists( 'sbr_safe_enqueue_sitemap_assets' ) ) {
  function sbr_safe_enqueue_sitemap_assets() {
    if ( is_admin() ) {
      return;
    }

    $folder = sbr_safe_current_sitemap_folder();
    $folder = sanitize_file_name( $folder );

    if ( $folder === '' ) {
      $folder = 'sitemap-core';
    }

    $base_relative = 'sitemaps/' . $folder . '/';

    $css_relative = $base_relative . 'css/style.css';
    $css_absolute = ABSPATH . $css_relative;

    $js_relative = $base_relative . 'js/main.js';
    $js_absolute = ABSPATH . $js_relative;

    if ( file_exists( $css_absolute ) && is_readable( $css_absolute ) ) {
      wp_enqueue_style(
        'sbr-' . $folder . '-style',
        home_url( '/' . $css_relative ),
        array(),
        (string) filemtime( $css_absolute )
      );
    }

    if ( file_exists( $js_absolute ) && is_readable( $js_absolute ) ) {
      wp_enqueue_script(
        'sbr-' . $folder . '-main',
        home_url( '/' . $js_relative ),
        array(),
        (string) filemtime( $js_absolute ),
        true
      );
    }
  }
}

add_action( 'wp_enqueue_scripts', 'sbr_safe_enqueue_sitemap_assets', 35 );

/* END SaudiBusRental Sitemap Folder Assets - SAFE ADDON */<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://saudibusrental.net/sitemap-bus-rental-main.xml</loc><lastmod>2026-06-24T14:31:44+00:00</lastmod></sitemap><sitemap><loc>https://saudibusrental.net/sitemap-core.xml</loc><lastmod>2026-06-24T14:24:25+00:00</lastmod></sitemap></sitemapindex>