Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4674,12 +4674,14 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) {
/*
* If the URL doesn't appear to contain a scheme, we presume
* it needs http:// prepended (unless it's a relative link
* starting with /, # or ?, or a PHP file).
* starting with /, # or ?, or a PHP file). If the first item
* in $protocols is 'https', then https:// is prepended.
*/
if ( ! str_contains( $url, ':' ) && ! in_array( $url[0], array( '/', '#', '?' ), true ) &&
! preg_match( '/^[a-z0-9-]+?\.php/i', $url )
) {
$url = 'http://' . $url;
$scheme = ( is_array( $protocols ) && 'https' === reset( $protocols ) ) ? 'https://' : 'http://';
$url = $scheme . $url;
}

// Replace ampersands and single quotes only when displaying.
Expand Down
34 changes: 33 additions & 1 deletion tests/phpunit/tests/formatting/escUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,48 @@ public function test_encoding() {
}

/**
* @ticket 23605
* @ticket 52886
*
* @covers ::wp_allowed_protocols
*/
public function test_protocol() {
$this->assertSame( 'http://example.com', esc_url( 'http://example.com' ) );
$this->assertSame( '', esc_url( 'nasty://example.com/' ) );
$this->assertSame(
'',
'https://example.com',
esc_url(
'example.com',
array(
'https',
)
)
);
$this->assertSame(
'http://example.com',
esc_url(
'example.com',
array(
'http',
)
)
);
$this->assertSame(
'https://example.com',
esc_url(
'example.com',
array(
'https',
'http',
)
)
);
$this->assertSame(
'http://example.com',
esc_url(
'example.com',
array(
'http',
'https',
)
)
Expand Down
Loading