WordPress.org

Make WordPress Core

Changeset 38036


Ignore:
Timestamp:
07/12/2016 11:31:58 AM (4 years ago)
Author:
ocean90
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r38029 r38036  
    28162816         */
    28172817        $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
    2818         $urls = array_unique( $urls );
    2819 
    2820         foreach ( $urls as $url ) {
     2818
     2819        foreach ( $urls as $key => $url ) {
    28212820            $url = esc_url( $url, array( 'http', 'https' ) );
     2821            if ( ! $url ) {
     2822                unset( $urls[ $key ] );
     2823                continue;
     2824            }
    28222825
    28232826            if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
    28242827                $parsed = wp_parse_url( $url );
    28252828                if ( empty( $parsed['host'] ) ) {
     2829                    unset( $urls[ $key ] );
    28262830                    continue;
    28272831                }
    28282832
    2829                 if ( ! empty( $parsed['scheme'] ) ) {
     2833                if ( 'dns-prefetch' === $relation_type ) {
     2834                    $url = '//' . $parsed['host'];
     2835                } else if ( ! empty( $parsed['scheme'] ) ) {
    28302836                    $url = $parsed['scheme'] . '://' . $parsed['host'];
    28312837                } else {
     
    28342840            }
    28352841
    2836             printf( "<link rel='%s' href='%s'>\r\n", $relation_type, $url );
     2842            $urls[ $key ] = $url;
     2843        }
     2844
     2845        $urls = array_unique( $urls );
     2846
     2847        foreach ( $urls as $url ) {
     2848            printf( "<link rel='%s' href='%s'>\n", $relation_type, $url );
    28372849        }
    28382850    }
  • trunk/tests/phpunit/tests/general/resourceHints.php

    r37951 r38036  
    3232
    3333    function test_should_have_defaults_on_frontend() {
    34         $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
     34        $expected = "<link rel='preconnect' href='http://s.w.org'>\n";
    3535
    3636        $this->expectOutputString( $expected );
     
    4040
    4141    function test_dns_prefetching() {
    42         $expected = "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
    43                     "<link rel='dns-prefetch' href='https://google.com'>\r\n" .
    44                     "<link rel='dns-prefetch' href='make.wordpress.org'>\r\n" .
    45                     "<link rel='preconnect' href='http://s.w.org'>\r\n";
     42        $expected = "<link rel='dns-prefetch' href='//wordpress.org'>\n" .
     43                    "<link rel='dns-prefetch' href='//google.com'>\n" .
     44                    "<link rel='dns-prefetch' href='//make.wordpress.org'>\n" .
     45                    "<link rel='preconnect' href='http://s.w.org'>\n";
    4646
    4747        add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
     
    5757        if ( 'dns-prefetch' === $method ) {
    5858            $hints[] = 'http://wordpress.org';
     59            $hints[] = 'https://wordpress.org';
     60            $hints[] = 'htps://wordpress.org'; // Invalid URLs should be skipped.
    5961            $hints[] = 'https://google.com';
    6062            $hints[] = '//make.wordpress.org';
     63            $hints[] = 'https://wordpress.org/plugins/';
    6164        }
    6265
     
    6568
    6669    function test_prerender() {
    67         $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
    68                     "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
    69                     "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
    70                     "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
     70        $expected = "<link rel='preconnect' href='http://s.w.org'>\n" .
     71                    "<link rel='prerender' href='https://make.wordpress.org/great-again'>\n" .
     72                    "<link rel='prerender' href='http://jobs.wordpress.net'>\n" .
     73                    "<link rel='prerender' href='//core.trac.wordpress.org'>\n";
    7174
    7275        add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 );
     
    8487            $hints[] = 'http://jobs.wordpress.net';
    8588            $hints[] = '//core.trac.wordpress.org';
     89            $hints[] = 'htps://wordpress.org'; // Invalid URLs should be skipped.
    8690        }
    8791
     
    9094
    9195    function test_parse_url_dns_prefetch() {
    92         $expected = "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n" .
    93                     "<link rel='preconnect' href='http://s.w.org'>\r\n";
     96        $expected = "<link rel='dns-prefetch' href='//make.wordpress.org'>\n" .
     97                    "<link rel='preconnect' href='http://s.w.org'>\n";
    9498
    9599        add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
     
    111115
    112116    function test_dns_prefetch_styles() {
    113         $expected = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
    114                     "<link rel='preconnect' href='http://s.w.org'>\r\n";
     117        $expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" .
     118                    "<link rel='preconnect' href='http://s.w.org'>\n";
    115119
    116120        $args = array(
     
    130134
    131135    function test_dns_prefetch_scripts() {
    132         $expected = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
    133                     "<link rel='preconnect' href='http://s.w.org'>\r\n";
     136        $expected = "<link rel='dns-prefetch' href='//fonts.googleapis.com'>\n" .
     137                    "<link rel='preconnect' href='http://s.w.org'>\n";
    134138
    135139        $args = array(
Note: See TracChangeset for help on using the changeset viewer.