PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Core / Routing / UrlParser.php

UrlParser.php in Yatra – Travel Booking & Tour Operator Software trunk, at app/Core/Routing/UrlParser.php

91 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Core\Routing;
6
7 /**
8 * URL Parser for extracting clean request paths from WordPress requests
9 *
10 * Handles URL parsing logic that was duplicated across all handler methods
11 */
12 class UrlParser
13 {
14 /**
15 * Get the clean request path from current WordPress request
16 *
17 * @return string Clean request path without site subdirectory
18 */
19 public static function getCleanRequestPath(): string
20 {
21 global $wp;
22
23 $request_path = '';
24
25 // Method 1: Get from $wp->request if available
26 if (isset($wp) && isset($wp->request)) {
27 $request_path = trim((string) $wp->request, '/');
28 }
29
30 // Method 2: Fallback to parsing REQUEST_URI
31 if (empty($request_path)) {
32 $request_uri = $_SERVER['REQUEST_URI'] ?? '';
33 $parsed_uri = wp_parse_url($request_uri);
34 $path = $parsed_uri['path'] ?? '';
35 $path = trim($path, '/');
36
37 // Remove site subdirectory if WordPress is installed in subdir
38 $home_path = wp_parse_url(home_url('/'), PHP_URL_PATH);
39 $home_path = $home_path ? trim($home_path, '/') : '';
40 if ($home_path && str_starts_with($path, $home_path)) {
41 $path = trim(substr($path, strlen($home_path)), '/');
42 }
43
44 $request_path = $path;
45 }
46
47 /**
48 * Normalize the storefront path segment Yatra uses for routing (no leading slash, site subdirectory removed).
49 * Map custom public URLs onto Yatra paths before {@see PrettyRouteMatcher} runs, e.g. `regions/norway` → `destination/norway`.
50 *
51 * @param string $request_path Already normalized relative path or empty string.
52 */
53 return (string) apply_filters('yatra_frontend_request_path', $request_path);
54 }
55
56 /**
57 * Extract slug from URL path using pattern matching
58 *
59 * @param string $path Request path
60 * @param string $base Base path to match against
61 * @return string|null Extracted slug or null if no match
62 */
63 public static function extractSlugFromPath(string $path, string $base): ?string
64 {
65 $escaped_base = preg_quote($base, '/');
66 $pattern = '/^' . $escaped_base . '\/([^\/]+)\/?$/';
67
68 if (preg_match($pattern, $path, $matches)) {
69 return $matches[1];
70 }
71
72 return null;
73 }
74
75 /**
76 * Extract token from URL path using pattern matching
77 *
78 * @param string $path Request path
79 * @param string $pattern Regex pattern to match
80 * @return string|null Extracted token or null if no match
81 */
82 public static function extractTokenFromPath(string $path, string $pattern): ?string
83 {
84 if (preg_match($pattern, $path, $matches)) {
85 return $matches[1];
86 }
87
88 return null;
89 }
90 }
91