request if available if (isset($wp) && isset($wp->request)) { $request_path = trim((string) $wp->request, '/'); } // Method 2: Fallback to parsing REQUEST_URI if (empty($request_path)) { $request_uri = $_SERVER['REQUEST_URI'] ?? ''; $parsed_uri = wp_parse_url($request_uri); $path = $parsed_uri['path'] ?? ''; $path = trim($path, '/'); // Remove site subdirectory if WordPress is installed in subdir $home_path = wp_parse_url(home_url('/'), PHP_URL_PATH); $home_path = $home_path ? trim($home_path, '/') : ''; if ($home_path && str_starts_with($path, $home_path)) { $path = trim(substr($path, strlen($home_path)), '/'); } $request_path = $path; } /** * Normalize the storefront path segment Yatra uses for routing (no leading slash, site subdirectory removed). * Map custom public URLs onto Yatra paths before {@see PrettyRouteMatcher} runs, e.g. `regions/norway` → `destination/norway`. * * @param string $request_path Already normalized relative path or empty string. */ return (string) apply_filters('yatra_frontend_request_path', $request_path); } /** * Extract slug from URL path using pattern matching * * @param string $path Request path * @param string $base Base path to match against * @return string|null Extracted slug or null if no match */ public static function extractSlugFromPath(string $path, string $base): ?string { $escaped_base = preg_quote($base, '/'); $pattern = '/^' . $escaped_base . '\/([^\/]+)\/?$/'; if (preg_match($pattern, $path, $matches)) { return $matches[1]; } return null; } /** * Extract token from URL path using pattern matching * * @param string $path Request path * @param string $pattern Regex pattern to match * @return string|null Extracted token or null if no match */ public static function extractTokenFromPath(string $path, string $pattern): ?string { if (preg_match($pattern, $path, $matches)) { return $matches[1]; } return null; } }