Skip to content

Commit d35fe7d

Browse files
Formatting: Allow specifying https:// as the default protocol in esc_url().
`esc_url()` will now prepend `https://` to the URL if it does not already contain a scheme and the first item in the `$protocols` array is `'https'`. Follow-up to [5088], [6015], [13299], [33851], [60672]. Props sabernhardt, mkaz, rachelbaker, audrasjb, costdev, aksl95, johnbillion, pcarvalho, SergeyBiryukov. Fixes #52886. git-svn-id: https://develop.svn.wordpress.org/trunk@60734 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d44463d commit d35fe7d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/wp-includes/formatting.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4642,6 +4642,8 @@ function esc_sql( $data ) {
46424642
* is applied to the returned cleaned URL.
46434643
*
46444644
* @since 2.8.0
4645+
* @since 6.9.0 Prepends `https://` to the URL if it does not already contain a scheme
4646+
* and the first item in `$protocols` is 'https'.
46454647
*
46464648
* @param string $url The URL to be cleaned.
46474649
* @param string[] $protocols Optional. An array of acceptable protocols.
@@ -4674,12 +4676,14 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) {
46744676
/*
46754677
* If the URL doesn't appear to contain a scheme, we presume
46764678
* it needs http:// prepended (unless it's a relative link
4677-
* starting with /, # or ?, or a PHP file).
4679+
* starting with /, # or ?, or a PHP file). If the first item
4680+
* in $protocols is 'https', then https:// is prepended.
46784681
*/
46794682
if ( ! str_contains( $url, ':' ) && ! in_array( $url[0], array( '/', '#', '?' ), true ) &&
46804683
! preg_match( '/^[a-z0-9-]+?\.php/i', $url )
46814684
) {
4682-
$url = 'http://' . $url;
4685+
$scheme = ( is_array( $protocols ) && 'https' === array_first( $protocols ) ) ? 'https://' : 'http://';
4686+
$url = $scheme . $url;
46834687
}
46844688

46854689
// Replace ampersands and single quotes only when displaying.

tests/phpunit/tests/formatting/escUrl.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,48 @@ public function test_encoding() {
9090
}
9191

9292
/**
93+
* @ticket 23605
94+
* @ticket 52886
95+
*
9396
* @covers ::wp_allowed_protocols
9497
*/
9598
public function test_protocol() {
9699
$this->assertSame( 'http://example.com', esc_url( 'http://example.com' ) );
97100
$this->assertSame( '', esc_url( 'nasty://example.com/' ) );
98101
$this->assertSame(
99-
'',
102+
'https://example.com',
103+
esc_url(
104+
'example.com',
105+
array(
106+
'https',
107+
)
108+
)
109+
);
110+
$this->assertSame(
111+
'http://example.com',
100112
esc_url(
101113
'example.com',
102114
array(
115+
'http',
116+
)
117+
)
118+
);
119+
$this->assertSame(
120+
'https://example.com',
121+
esc_url(
122+
'example.com',
123+
array(
124+
'https',
125+
'http',
126+
)
127+
)
128+
);
129+
$this->assertSame(
130+
'http://example.com',
131+
esc_url(
132+
'example.com',
133+
array(
134+
'http',
103135
'https',
104136
)
105137
)

0 commit comments

Comments
 (0)