Make WordPress Core

Ticket #34292: 34292.11.diff

File 34292.11.diff, 5.8 KB (added by peterwilsoncc, 9 years ago)
  • src/wp-includes/general-template.php

    diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
    index 2bcdb43..6d0fdd8 100644
    function wp_site_icon() { 
    27992799 */
    28002800function wp_resource_hints() {
    28012801        $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                ),
    28042810                'prefetch'     => array(),
    28052811                'prerender'    => array(),
    28062812        );
    function wp_resource_hints() { 
    28512857}
    28522858
    28532859/**
    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.
    28552861 *
    28562862 * @since 4.6.0
     2863 *
     2864 * @return array A list of unique hosts of enqueued scripts and styles.
    28572865 */
    2858 function wp_resource_hints_scripts_styles() {
     2866function wp_dependencies_unique_hosts() {
    28592867        global $wp_scripts, $wp_styles;
    28602868
    28612869        $unique_hosts = array();
    28622870
    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 );
    28662877
    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                                }
    28792881                        }
    28802882                }
    28812883        }
  • tests/phpunit/tests/general/resourceHints.php

    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 { 
    3131        }
    3232
    3333        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";
    3535
    3636                $this->expectOutputString( $expected );
    3737
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    4242                $expected = "<link rel='dns-prefetch' href='//wordpress.org'>\n" .
    4343                                        "<link rel='dns-prefetch' href='//google.com'>\n" .
    4444                                        "<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";
    4646
    4747                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
    4848
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    6767        }
    6868
    6969        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" .
    7171                                        "<link rel='prerender' href='https://make.wordpress.org/great-again'>\n" .
    7272                                        "<link rel='prerender' href='http://jobs.wordpress.net'>\n" .
    7373                                        "<link rel='prerender' href='//core.trac.wordpress.org'>\n";
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    9494
    9595        function test_parse_url_dns_prefetch() {
    9696                $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";
    9898
    9999                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
    100100
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    115115
    116116        function test_dns_prefetch_styles() {
    117117                $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";
    119119
    120120                $args = array(
    121121                        'family' => 'Open+Sans:400',
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    134134
    135135        function test_dns_prefetch_scripts() {
    136136                $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";
    138138
    139139                $args = array(
    140140                        'family' => 'Open+Sans:400',
    class Tests_WP_Resource_Hints extends WP_UnitTestCase { 
    150150                $this->assertEquals( $expected, $actual );
    151151        }
    152152
     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        }
    153166}