diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
index 2bcdb43..6d0fdd8 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(), |
| 2803 | | 'preconnect' => array( 's.w.org' ), |
| | 2802 | 'dns-prefetch' => wp_dependencies_unique_hosts(), |
| | 2803 | 'preconnect' => array( |
| | 2804 | // Loop below will trim URLs to host only. |
| | 2805 | /** This filter is documented in wp-includes/formatting.php */ |
| | 2806 | apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/72x72/' ), |
| | 2807 | /** This filter is documented in wp-includes/formatting.php */ |
| | 2808 | apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/svg/' ), |
| | 2809 | ), |
| 2804 | 2810 | 'prefetch' => array(), |
| 2805 | 2811 | 'prerender' => array(), |
| 2806 | 2812 | ); |
| … |
… |
function wp_resource_hints() { |
| 2851 | 2857 | } |
| 2852 | 2858 | |
| 2853 | 2859 | /** |
| 2854 | | * Adds dns-prefetch for all scripts and styles enqueued from external hosts. |
| | 2860 | * Returns a list of unique hosts of all enqueued scripts and styles. |
| 2855 | 2861 | * |
| 2856 | 2862 | * @since 4.6.0 |
| | 2863 | * |
| | 2864 | * @return array A list of unique hosts of enqueued scripts and styles. |
| 2857 | 2865 | */ |
| 2858 | | function wp_resource_hints_scripts_styles() { |
| | 2866 | function wp_dependencies_unique_hosts() { |
| 2859 | 2867 | global $wp_scripts, $wp_styles; |
| 2860 | 2868 | |
| 2861 | 2869 | $unique_hosts = array(); |
| 2862 | 2870 | |
| 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 ); |
| | 2871 | foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) { |
| | 2872 | if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) { |
| | 2873 | foreach ( $dependencies->queue as $handle ) { |
| | 2874 | /* @var _WP_Dependency $dependency */ |
| | 2875 | $dependency = $dependencies->registered[ $handle ]; |
| | 2876 | $parsed = wp_parse_url( $dependency->src ); |
| 2866 | 2877 | |
| 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']; |
| | 2878 | if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { |
| | 2879 | $unique_hosts[] = $parsed['host']; |
| | 2880 | } |
| 2879 | 2881 | } |
| 2880 | 2882 | } |
| 2881 | 2883 | } |
diff --git tests/phpunit/tests/general/resourceHints.php tests/phpunit/tests/general/resourceHints.php
index 7041a61..c7fb227 100644
|
|
|
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | function test_should_have_defaults_on_frontend() { |
| 34 | | $expected = "<link rel='preconnect' href='http://s.w.org'>\n"; |
| | 34 | $expected = "<link rel='preconnect' href='https://s.w.org'>\n"; |
| 35 | 35 | |
| 36 | 36 | $this->expectOutputString( $expected ); |
| 37 | 37 | |
| … |
… |
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 42 | 42 | $expected = "<link rel='dns-prefetch' href='//wordpress.org'>\n" . |
| 43 | 43 | "<link rel='dns-prefetch' href='//google.com'>\n" . |
| 44 | 44 | "<link rel='dns-prefetch' href='//make.wordpress.org'>\n" . |
| 45 | | "<link rel='preconnect' href='http://s.w.org'>\n"; |
| | 45 | "<link rel='preconnect' href='https://s.w.org'>\n"; |
| 46 | 46 | |
| 47 | 47 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 ); |
| 48 | 48 | |
| … |
… |
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | function test_prerender() { |
| 70 | | $expected = "<link rel='preconnect' href='http://s.w.org'>\n" . |
| | 70 | $expected = "<link rel='preconnect' href='https://s.w.org'>\n" . |
| 71 | 71 | "<link rel='prerender' href='https://make.wordpress.org/great-again'>\n" . |
| 72 | 72 | "<link rel='prerender' href='http://jobs.wordpress.net'>\n" . |
| 73 | 73 | "<link rel='prerender' href='//core.trac.wordpress.org'>\n"; |
| … |
… |
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 94 | 94 | |
| 95 | 95 | function test_parse_url_dns_prefetch() { |
| 96 | 96 | $expected = "<link rel='dns-prefetch' href='//make.wordpress.org'>\n" . |
| 97 | | "<link rel='preconnect' href='http://s.w.org'>\n"; |
| | 97 | "<link rel='preconnect' href='https://s.w.org'>\n"; |
| 98 | 98 | |
| 99 | 99 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 ); |
| 100 | 100 | |
| … |
… |
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 115 | 115 | |
| 116 | 116 | function test_dns_prefetch_styles() { |
| 117 | 117 | $expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" . |
| 118 | | "<link rel='preconnect' href='http://s.w.org'>\n"; |
| | 118 | "<link rel='preconnect' href='https://s.w.org'>\n"; |
| 119 | 119 | |
| 120 | 120 | $args = array( |
| 121 | 121 | 'family' => 'Open+Sans:400', |
| … |
… |
class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 134 | 134 | |
| 135 | 135 | function test_dns_prefetch_scripts() { |
| 136 | 136 | $expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" . |
| 137 | | "<link rel='preconnect' href='http://s.w.org'>\n"; |
| | 137 | "<link rel='preconnect' href='https://s.w.org'>\n"; |
| 138 | 138 | |
| 139 | 139 | $args = array( |
| 140 | 140 | 'family' => 'Open+Sans:400', |
| … |
… |
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='https://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 | } |