Make WordPress Core

Changeset 38100


Ignore:
Timestamp:
07/19/2016 02:34:42 AM (9 years ago)
Author:
peterwilsoncc
Message:

Script Loader: Limit resource hinting to enqueued assets.

Externally hosted script and style dependencies trigger dns-prefetch hinting only when enqueued. This removed a bug in which hinting was added on registration.

Renames the function wp_resource_hints_scripts_styles to wp_dependencies_unique_hosts as the function provides the hosts, not the hinting.

Props swissspidy.
Fixes #37385.

Location:
trunk
Files:
2 edited

Legend:

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

    r38036 r38100  
    28002800function wp_resource_hints() {
    28012801    $hints = array(
    2802         'dns-prefetch' => wp_resource_hints_scripts_styles(),
     2802        'dns-prefetch' => wp_dependencies_unique_hosts(),
    28032803        'preconnect'   => array( 's.w.org' ),
    28042804        'prefetch'     => array(),
     
    28522852
    28532853/**
    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.
    28552855 *
    28562856 * @since 4.6.0
    2857  */
    2858 function wp_resource_hints_scripts_styles() {
     2857 *
     2858 * @return array A list of unique hosts of enqueued scripts and styles.
     2859 */
     2860function wp_dependencies_unique_hosts() {
    28592861    global $wp_scripts, $wp_styles;
    28602862
    28612863    $unique_hosts = array();
    28622864
    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 );
    2866 
    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'];
     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 );
     2871
     2872                if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) {
     2873                    $unique_hosts[] = $parsed['host'];
     2874                }
    28792875            }
    28802876        }
  • trunk/tests/phpunit/tests/general/resourceHints.php

    r38036 r38100  
    151151    }
    152152
     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    }
    153166}
Note: See TracChangeset for help on using the changeset viewer.