mirror of
git://develop.git.wordpress.org/
synced 2025-02-12 02:44:22 +01:00
Resource Hints: Remove schemes from dns-prefetch
resource hint outputs.
"wordpress.org", "!http://wordpress.org", and "!https://wordpress.org" should all have the same DNS lookup. Also, replace `\r\n` with `\n` and ensure that invalid URLs are skipped. Props niallkennedy, peterwilsoncc. Fixes #37240. git-svn-id: https://develop.svn.wordpress.org/trunk@38036 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
e556fec595
commit
a27c61f7c2
@ -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( "<link rel='%s' href='%s'>\r\n", $relation_type, $url );
|
||||
$urls[ $key ] = $url;
|
||||
}
|
||||
|
||||
$urls = array_unique( $urls );
|
||||
|
||||
foreach ( $urls as $url ) {
|
||||
printf( "<link rel='%s' href='%s'>\n", $relation_type, $url );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
function test_should_have_defaults_on_frontend() {
|
||||
$expected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
|
||||
$expected = "<link rel='preconnect' href='http://s.w.org'>\n";
|
||||
|
||||
$this->expectOutputString( $expected );
|
||||
|
||||
@ -39,10 +39,10 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
function test_dns_prefetching() {
|
||||
$expected = "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
|
||||
"<link rel='dns-prefetch' href='https://google.com'>\r\n" .
|
||||
"<link rel='dns-prefetch' href='make.wordpress.org'>\r\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\r\n";
|
||||
$expected = "<link rel='dns-prefetch' href='//wordpress.org'>\n" .
|
||||
"<link rel='dns-prefetch' href='//google.com'>\n" .
|
||||
"<link rel='dns-prefetch' href='//make.wordpress.org'>\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\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 = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
|
||||
"<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
|
||||
"<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
|
||||
"<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
|
||||
$expected = "<link rel='preconnect' href='http://s.w.org'>\n" .
|
||||
"<link rel='prerender' href='https://make.wordpress.org/great-again'>\n" .
|
||||
"<link rel='prerender' href='http://jobs.wordpress.net'>\n" .
|
||||
"<link rel='prerender' href='//core.trac.wordpress.org'>\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 = "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\r\n";
|
||||
$expected = "<link rel='dns-prefetch' href='//make.wordpress.org'>\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\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 = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\r\n";
|
||||
$expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\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 = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\r\n";
|
||||
$expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" .
|
||||
"<link rel='preconnect' href='http://s.w.org'>\n";
|
||||
|
||||
$args = array(
|
||||
'family' => 'Open+Sans:400',
|
||||
|
Loading…
x
Reference in New Issue
Block a user