From b92c1c58b3c14b578161de356d6afd861307bf1a Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 19 Jul 2016 02:34:42 +0000 Subject: [PATCH] Script Loader: Limit resource hinting to enqueued assets. Externally hosted script and style dependencies trigger `dns-prefetch` hinting only when enqueued. This removed a bug in which hinting was added on registration. Renames the function `wp_resource_hints_scripts_styles` to `wp_dependencies_unique_hosts` as the function provides the hosts, not the hinting. Props swissspidy. Fixes #37385. git-svn-id: https://develop.svn.wordpress.org/trunk@38100 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 32 ++++++++----------- tests/phpunit/tests/general/resourceHints.php | 13 ++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 2bcdb43801..ca48499ed3 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2799,7 +2799,7 @@ function wp_site_icon() { */ function wp_resource_hints() { $hints = array( - 'dns-prefetch' => wp_resource_hints_scripts_styles(), + 'dns-prefetch' => wp_dependencies_unique_hosts(), 'preconnect' => array( 's.w.org' ), 'prefetch' => array(), 'prerender' => array(), @@ -2851,31 +2851,27 @@ function wp_resource_hints() { } /** - * Adds dns-prefetch for all scripts and styles enqueued from external hosts. + * Returns a list of unique hosts of all enqueued scripts and styles. * * @since 4.6.0 + * + * @return array A list of unique hosts of enqueued scripts and styles. */ -function wp_resource_hints_scripts_styles() { +function wp_dependencies_unique_hosts() { global $wp_scripts, $wp_styles; $unique_hosts = array(); - if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) { - foreach ( $wp_scripts->registered as $registered_script ) { - $parsed = wp_parse_url( $registered_script->src ); + foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) { + if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) { + foreach ( $dependencies->queue as $handle ) { + /* @var _WP_Dependency $dependency */ + $dependency = $dependencies->registered[ $handle ]; + $parsed = wp_parse_url( $dependency->src ); - if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { - $unique_hosts[] = $parsed['host']; - } - } - } - - if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) { - foreach ( $wp_styles->registered as $registered_style ) { - $parsed = wp_parse_url( $registered_style->src ); - - if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { - $unique_hosts[] = $parsed['host']; + if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { + $unique_hosts[] = $parsed['host']; + } } } } diff --git a/tests/phpunit/tests/general/resourceHints.php b/tests/phpunit/tests/general/resourceHints.php index 7041a6130e..95958e7f6b 100644 --- a/tests/phpunit/tests/general/resourceHints.php +++ b/tests/phpunit/tests/general/resourceHints.php @@ -150,4 +150,17 @@ class Tests_WP_Resource_Hints extends WP_UnitTestCase { $this->assertEquals( $expected, $actual ); } + function test_dns_prefetch_scripts_does_not_included_registered_only() { + $expected = "\n"; + $unexpected = "\n"; + + wp_register_script( 'jquery-elsewhere', 'https://wordpress.org/wp-includes/js/jquery/jquery.js' ); + + $actual = get_echo( 'wp_resource_hints' ); + + wp_deregister_script( 'jquery-elsewhere' ); + + $this->assertEquals( $expected, $actual ); + $this->assertNotContains( $unexpected, $actual ); + } }