Make WordPress Core

Changeset 38256 for branches/4.6


Ignore:
Timestamp:
08/13/2016 06:39:39 PM (8 years ago)
Author:
ocean90
Message:

Script Loader: Fix protocol-relative URLs for the preconnect relation type.

wp_resource_hints() parses the URL for the preconnect and dns-prefetch relation types to ensure correct values for both. While protocol-relative URLs are supported for dns-prefetch, the double slash was lost for preconnect.

Merge of [38255] to the 4.6 branch.

Props swissspidy, peterwilsoncc.
Props azaozz for review.
See #37652.

Location:
branches/4.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6

  • branches/4.6/src/wp-includes/general-template.php

    r38174 r38256  
    28402840                }
    28412841
    2842                 if ( 'dns-prefetch' === $relation_type ) {
    2843                     $url = '//' . $parsed['host'];
    2844                 } else if ( ! empty( $parsed['scheme'] ) ) {
     2842                if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
    28452843                    $url = $parsed['scheme'] . '://' . $parsed['host'];
    28462844                } else {
    2847                     $url = $parsed['host'];
     2845                    // Use protocol-relative URLs for dns-prefetch or if scheme is missing.
     2846                    $url = '//' . $parsed['host'];
    28482847                }
    28492848            }
  • branches/4.6/tests/phpunit/tests/general/resourceHints.php

    r38174 r38256  
    6767    }
    6868
     69    /**
     70     * @ticket 37652
     71     */
     72    function test_preconnect() {
     73        $expected = "<link rel='dns-prefetch' href='//s.w.org'>\n" .
     74                    "<link rel='preconnect' href='//wordpress.org'>\n" .
     75                    "<link rel='preconnect' href='https://make.wordpress.org'>\n" .
     76                    "<link rel='preconnect' href='http://google.com'>\n" .
     77                    "<link rel='preconnect' href='http://w.org'>\n";
     78
     79        add_filter( 'wp_resource_hints', array( $this, '_add_preconnect_domains' ), 10, 2 );
     80
     81        $actual = get_echo( 'wp_resource_hints' );
     82
     83        remove_filter( 'wp_resource_hints', array( $this, '_add_preconnect_domains' ) );
     84
     85        $this->assertEquals( $expected, $actual );
     86    }
     87
     88    function _add_preconnect_domains( $hints, $method ) {
     89        if ( 'preconnect' === $method ) {
     90            $hints[] = '//wordpress.org';
     91            $hints[] = 'https://make.wordpress.org';
     92            $hints[] = 'htps://example.com'; // Invalid URLs should be skipped.
     93            $hints[] = 'http://google.com';
     94            $hints[] = 'w.org';
     95        }
     96
     97        return $hints;
     98    }
     99
    69100    function test_prerender() {
    70101        $expected = "<link rel='dns-prefetch' href='//s.w.org'>\n" .
     
    177208        $this->assertEquals( $expected, $actual );
    178209    }
     210
     211    /**
     212     * @ticket 37652
     213     */
     214    function test_malformed_urls() {
     215        $expected = "<link rel='dns-prefetch' href='//s.w.org'>\n";
     216
     217        // Errant colon.
     218        add_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_errant_colon' ), 10, 2 );
     219        $actual = get_echo( 'wp_resource_hints' );
     220        remove_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_errant_colon' ) );
     221        $this->assertEquals( $expected, $actual );
     222
     223        // Unsupported Scheme.
     224        add_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_unsupported_scheme' ), 10, 2 );
     225        $actual = get_echo( 'wp_resource_hints' );
     226        remove_filter( 'wp_resource_hints', array( $this, '_add_malformed_url_unsupported_scheme' ) );
     227        $this->assertEquals( $expected, $actual );
     228    }
     229
     230    function _add_malformed_url_errant_colon( $hints, $method ) {
     231        if ( 'preconnect' === $method ) {
     232            $hints[] = '://core.trac.wordpress.org/ticket/37652';
     233        }
     234
     235        return $hints;
     236    }
     237
     238    function _add_malformed_url_unsupported_scheme( $hints, $method ) {
     239        if ( 'preconnect' === $method ) {
     240            $hints[] = 'git://develop.git.wordpress.org/';
     241        }
     242
     243        return $hints;
     244    }
    179245}
Note: See TracChangeset for help on using the changeset viewer.