diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
index 2bcdb43..ca48499 100644
|
|
function wp_site_icon() { |
2799 | 2799 | */ |
2800 | 2800 | function wp_resource_hints() { |
2801 | 2801 | $hints = array( |
2802 | | 'dns-prefetch' => wp_resource_hints_scripts_styles(), |
| 2802 | 'dns-prefetch' => wp_dependencies_unique_hosts(), |
2803 | 2803 | 'preconnect' => array( 's.w.org' ), |
2804 | 2804 | 'prefetch' => array(), |
2805 | 2805 | 'prerender' => array(), |
… |
… |
function wp_resource_hints() { |
2851 | 2851 | } |
2852 | 2852 | |
2853 | 2853 | /** |
2854 | | * Adds dns-prefetch for all scripts and styles enqueued from external hosts. |
| 2854 | * Returns a list of unique hosts of all enqueued scripts and styles. |
2855 | 2855 | * |
2856 | 2856 | * @since 4.6.0 |
| 2857 | * |
| 2858 | * @return array A list of unique hosts of enqueued scripts and styles. |
2857 | 2859 | */ |
2858 | | function wp_resource_hints_scripts_styles() { |
| 2860 | function wp_dependencies_unique_hosts() { |
2859 | 2861 | global $wp_scripts, $wp_styles; |
2860 | 2862 | |
2861 | 2863 | $unique_hosts = array(); |
2862 | 2864 | |
2863 | | if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) { |
2864 | | foreach ( $wp_scripts->registered as $registered_script ) { |
2865 | | $parsed = wp_parse_url( $registered_script->src ); |
| 2865 | foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) { |
| 2866 | if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) { |
| 2867 | foreach ( $dependencies->queue as $handle ) { |
| 2868 | /* @var _WP_Dependency $dependency */ |
| 2869 | $dependency = $dependencies->registered[ $handle ]; |
| 2870 | $parsed = wp_parse_url( $dependency->src ); |
2866 | 2871 | |
2867 | | if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { |
2868 | | $unique_hosts[] = $parsed['host']; |
2869 | | } |
2870 | | } |
2871 | | } |
2872 | | |
2873 | | if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) { |
2874 | | foreach ( $wp_styles->registered as $registered_style ) { |
2875 | | $parsed = wp_parse_url( $registered_style->src ); |
2876 | | |
2877 | | if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { |
2878 | | $unique_hosts[] = $parsed['host']; |
| 2872 | if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { |
| 2873 | $unique_hosts[] = $parsed['host']; |
| 2874 | } |
2879 | 2875 | } |
2880 | 2876 | } |
2881 | 2877 | } |
diff --git tests/phpunit/tests/general/resourceHints.php tests/phpunit/tests/general/resourceHints.php
index 7041a61..95958e7 100644
|
|
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
150 | 150 | $this->assertEquals( $expected, $actual ); |
151 | 151 | } |
152 | 152 | |
| 153 | function test_dns_prefetch_scripts_does_not_included_registered_only() { |
| 154 | $expected = "<link rel='preconnect' href='http://s.w.org'>\n"; |
| 155 | $unexpected = "<link rel='dns-prefetch' href='//wordpress.org'>\n"; |
| 156 | |
| 157 | wp_register_script( 'jquery-elsewhere', 'https://wordpress.org/wp-includes/js/jquery/jquery.js' ); |
| 158 | |
| 159 | $actual = get_echo( 'wp_resource_hints' ); |
| 160 | |
| 161 | wp_deregister_script( 'jquery-elsewhere' ); |
| 162 | |
| 163 | $this->assertEquals( $expected, $actual ); |
| 164 | $this->assertNotContains( $unexpected, $actual ); |
| 165 | } |
153 | 166 | } |