|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package WPEmerge |
|
4
|
|
|
* @author Atanas Angelov <[email protected]> |
|
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
|
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
|
7
|
|
|
* @link https://wpemerge.com/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WPEmerge\Helpers; |
|
11
|
|
|
|
|
12
|
|
|
use WPEmerge\Requests\RequestInterface; |
|
13
|
|
|
use WPEmerge\Support\Arr; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A collection of tools dealing with URLs. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Url { |
|
19
|
|
|
/** |
|
20
|
|
|
* Get the path for the request relative to the home url. |
|
21
|
|
|
* Works only with absolute URLs. |
|
22
|
|
|
* |
|
23
|
|
|
* @param RequestInterface $request |
|
24
|
|
|
* @param string $home_url |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
8 |
|
public static function getPath( RequestInterface $request, $home_url = '' ) { |
|
28
|
8 |
|
$parsed_request = wp_parse_url( $request->getUrl() ); |
|
29
|
8 |
|
$parsed_home = wp_parse_url( $home_url ? $home_url : home_url( '/' ) ); |
|
30
|
|
|
|
|
31
|
8 |
|
$request_path = Arr::get( $parsed_request, 'path', '/' ); |
|
32
|
8 |
|
$request_path = static::removeTrailingSlash( $request_path ); |
|
33
|
8 |
|
$request_path = static::addLeadingSlash( $request_path ); |
|
34
|
|
|
|
|
35
|
8 |
|
if ( $parsed_request['host'] !== $parsed_home['host'] ) { |
|
36
|
1 |
|
return $request_path; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
$home_path = Arr::get( $parsed_home, 'path', '/' ); |
|
40
|
7 |
|
$home_path = static::removeTrailingSlash( $home_path ); |
|
41
|
7 |
|
$home_path = static::addLeadingSlash( $home_path ); |
|
42
|
7 |
|
$path = $request_path; |
|
43
|
|
|
|
|
44
|
7 |
|
if ( strpos( $request_path, $home_path ) === 0 ) { |
|
45
|
6 |
|
$path = substr( $request_path, strlen( $home_path ) ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
7 |
|
return static::addLeadingSlash( $path ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Ensure url has a leading slash |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $url |
|
55
|
|
|
* @param boolean $leave_blank |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public static function addLeadingSlash( $url, $leave_blank = false ) { |
|
59
|
1 |
|
if ( $leave_blank && $url === '' ) { |
|
60
|
1 |
|
return ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return '/' . static::removeLeadingSlash( $url ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Ensure url does not have a leading slash |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $url |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public static function removeLeadingSlash( $url ) { |
|
73
|
1 |
|
return preg_replace( '/^\/+/', '', $url ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Ensure url has a trailing slash |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $url |
|
80
|
|
|
* @param boolean $leave_blank |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public static function addTrailingSlash( $url, $leave_blank = false ) { |
|
84
|
1 |
|
if ( $leave_blank && $url === '' ) { |
|
85
|
1 |
|
return ''; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return trailingslashit( $url ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Ensure url does not have a trailing slash |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $url |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public static function removeTrailingSlash( $url ) { |
|
98
|
1 |
|
return untrailingslashit( $url ); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|