1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2018 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
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A collection of tools dealing with urls |
16
|
|
|
*/ |
17
|
|
|
class Url { |
18
|
|
|
/** |
19
|
|
|
* Get the path for the request relative to the home url |
20
|
|
|
* |
21
|
|
|
* @param RequestInterface $request |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
3 |
|
public static function getPath( RequestInterface $request ) { |
25
|
3 |
|
$url = $request->getUrl(); |
26
|
3 |
|
$relative_url = substr( $url, strlen( home_url( '/' ) ) ); |
27
|
3 |
|
$relative_url = static::addLeadingSlash( $relative_url ); |
28
|
3 |
|
$relative_url = preg_replace( '~\?.*~', '', $relative_url ); |
29
|
3 |
|
$relative_url = static::addTrailingSlash( $relative_url ); |
30
|
3 |
|
return $relative_url; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Ensure url has a leading slash |
35
|
|
|
* |
36
|
|
|
* @param string $url |
37
|
|
|
* @param boolean $leave_blank |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
1 |
|
public static function addLeadingSlash( $url, $leave_blank = false ) { |
41
|
1 |
|
if ( $leave_blank && $url === '' ) { |
42
|
1 |
|
return ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return '/' . static::removeLeadingSlash( $url ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Ensure url does not have a leading slash |
50
|
|
|
* |
51
|
|
|
* @param string $url |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
public static function removeLeadingSlash( $url ) { |
55
|
1 |
|
return preg_replace( '/^\/+/', '', $url ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Ensure url has a trailing slash |
60
|
|
|
* |
61
|
|
|
* @param string $url |
62
|
|
|
* @param boolean $leave_blank |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public static function addTrailingSlash( $url, $leave_blank = false ) { |
66
|
1 |
|
if ( $leave_blank && $url === '' ) { |
67
|
|
|
return ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return trailingslashit( $url ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Ensure url does not have a trailing slash |
75
|
|
|
* |
76
|
|
|
* @param string $url |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public static function removeTrailingSlash( $url ) { |
80
|
1 |
|
return untrailingslashit( $url ); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|