| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 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 |
public static function getPath( RequestInterface $request, $home_url = '' ) { |
| 28 |
$parsed_request = wp_parse_url( $request->getUrl() ); |
| 29 |
$parsed_home = wp_parse_url( $home_url ? $home_url : home_url( '/' ) ); |
| 30 |
|
| 31 |
$request_path = Arr::get( $parsed_request, 'path', '/' ); |
| 32 |
$request_path = static::removeTrailingSlash( $request_path ); |
| 33 |
$request_path = static::addLeadingSlash( $request_path ); |
| 34 |
|
| 35 |
if ( $parsed_request['host'] !== $parsed_home['host'] ) { |
| 36 |
return $request_path; |
| 37 |
} |
| 38 |
|
| 39 |
$home_path = Arr::get( $parsed_home, 'path', '/' ); |
| 40 |
$home_path = static::removeTrailingSlash( $home_path ); |
| 41 |
$home_path = static::addLeadingSlash( $home_path ); |
| 42 |
$path = $request_path; |
| 43 |
|
| 44 |
if ( strpos( $request_path, $home_path ) === 0 ) { |
| 45 |
$path = substr( $request_path, strlen( $home_path ) ); |
| 46 |
} |
| 47 |
|
| 48 |
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 |
public static function addLeadingSlash( $url, $leave_blank = false ) { |
| 59 |
if ( $leave_blank && $url === '' ) { |
| 60 |
return ''; |
| 61 |
} |
| 62 |
|
| 63 |
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 |
public static function removeLeadingSlash( $url ) { |
| 73 |
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 |
public static function addTrailingSlash( $url, $leave_blank = false ) { |
| 84 |
if ( $leave_blank && $url === '' ) { |
| 85 |
return ''; |
| 86 |
} |
| 87 |
|
| 88 |
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 |
public static function removeTrailingSlash( $url ) { |
| 98 |
return untrailingslashit( $url ); |
| 99 |
} |
| 100 |
} |
| 101 |
|