This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WP_URL.php
66 lines (60 loc) · 1.73 KB
/
WP_URL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
use Rowbot\URL\URL;
/**
* An abstraction to make swapping the URL parser easier later on.
* We do not need it in the long run. It adds extra overhead of the
* function call – let's remove it once the URL parser is decided.
*/
class WP_URL {
static public function parse($url, $base=null) {
return URL::parse($url, $base) ?? false;
}
/*
* Prepends a protocol to any matched URL without the double slash.
*
* Imagine we have a base URL of `https://example.com` and a text like `Visit myblog.com`.
* This Processor would match `myblog.com` as a URL candidate and then the parser
* would parse it as `https://example.com/myblog.com`, which is not what a user would expect.
*
* To get `https://myblog.com`, we need to prepend a protocol and turn that candidate into
* `https://myblog.com` before parsing.
*/
static public function ensure_protocol($raw_url, $protocol='https') {
if ( ! self::has_double_slash($raw_url) ) {
$raw_url = $protocol . '://' . $raw_url;
}
return $raw_url;
}
/**
* This method only considers http and https protocols.
*/
static public function has_double_slash($raw_url) {
return (
(
// Protocol-relative URLs.
strlen( $raw_url ) > 2 &&
'/' === $raw_url[0] &&
'/' === $raw_url[1]
) || (
strlen( $raw_url ) > 7 &&
'h' === $raw_url[0] &&
't' === $raw_url[1] &&
't' === $raw_url[2] &&
'p' === $raw_url[3] &&
':' === $raw_url[4] &&
'/' === $raw_url[5] &&
'/' === $raw_url[6]
) || (
strlen( $raw_url ) > 8 &&
'h' === $raw_url[0] &&
't' === $raw_url[1] &&
't' === $raw_url[2] &&
'p' === $raw_url[3] &&
's' === $raw_url[4] &&
':' === $raw_url[5] &&
'/' === $raw_url[6] &&
'/' === $raw_url[7]
)
);
}
}