diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php
index bf0614bd22..2bcdb43801 100644
--- a/src/wp-includes/general-template.php
+++ b/src/wp-includes/general-template.php
@@ -2815,25 +2815,37 @@ function wp_resource_hints() {
* @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
*/
$urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
- $urls = array_unique( $urls );
- foreach ( $urls as $url ) {
+ foreach ( $urls as $key => $url ) {
$url = esc_url( $url, array( 'http', 'https' ) );
+ if ( ! $url ) {
+ unset( $urls[ $key ] );
+ continue;
+ }
if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
$parsed = wp_parse_url( $url );
if ( empty( $parsed['host'] ) ) {
+ unset( $urls[ $key ] );
continue;
}
- if ( ! empty( $parsed['scheme'] ) ) {
+ if ( 'dns-prefetch' === $relation_type ) {
+ $url = '//' . $parsed['host'];
+ } else if ( ! empty( $parsed['scheme'] ) ) {
$url = $parsed['scheme'] . '://' . $parsed['host'];
} else {
$url = $parsed['host'];
}
}
- printf( "\r\n", $relation_type, $url );
+ $urls[ $key ] = $url;
+ }
+
+ $urls = array_unique( $urls );
+
+ foreach ( $urls as $url ) {
+ printf( "\n", $relation_type, $url );
}
}
}
diff --git a/tests/phpunit/tests/general/resourceHints.php b/tests/phpunit/tests/general/resourceHints.php
index 351e6df917..7041a6130e 100644
--- a/tests/phpunit/tests/general/resourceHints.php
+++ b/tests/phpunit/tests/general/resourceHints.php
@@ -31,7 +31,7 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
}
function test_should_have_defaults_on_frontend() {
- $expected = "\r\n";
+ $expected = "\n";
$this->expectOutputString( $expected );
@@ -39,10 +39,10 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
}
function test_dns_prefetching() {
- $expected = "\r\n" .
- "\r\n" .
- "\r\n" .
- "\r\n";
+ $expected = "\n" .
+ "\n" .
+ "\n" .
+ "\n";
add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
@@ -56,18 +56,21 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
function _add_dns_prefetch_domains( $hints, $method ) {
if ( 'dns-prefetch' === $method ) {
$hints[] = 'http://wordpress.org';
+ $hints[] = 'https://wordpress.org';
+ $hints[] = 'htps://wordpress.org'; // Invalid URLs should be skipped.
$hints[] = 'https://google.com';
$hints[] = '//make.wordpress.org';
+ $hints[] = 'https://wordpress.org/plugins/';
}
return $hints;
}
function test_prerender() {
- $expected = "\r\n" .
- "\r\n" .
- "\r\n" .
- "\r\n";
+ $expected = "\n" .
+ "\n" .
+ "\n" .
+ "\n";
add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 );
@@ -83,14 +86,15 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
$hints[] = 'https://make.wordpress.org/great-again';
$hints[] = 'http://jobs.wordpress.net';
$hints[] = '//core.trac.wordpress.org';
+ $hints[] = 'htps://wordpress.org'; // Invalid URLs should be skipped.
}
return $hints;
}
function test_parse_url_dns_prefetch() {
- $expected = "\r\n" .
- "\r\n";
+ $expected = "\n" .
+ "\n";
add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
@@ -110,8 +114,8 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
}
function test_dns_prefetch_styles() {
- $expected = "\r\n" .
- "\r\n";
+ $expected = "\n" .
+ "\n";
$args = array(
'family' => 'Open+Sans:400',
@@ -129,8 +133,8 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
}
function test_dns_prefetch_scripts() {
- $expected = "\r\n" .
- "\r\n";
+ $expected = "\n" .
+ "\n";
$args = array(
'family' => 'Open+Sans:400',