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.

Built from https://develop.svn.wordpress.org/trunk@38100


git-svn-id: http://core.svn.wordpress.org/trunk@38041 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2016-07-19 02:35:31 +00:00
parent 01b9ca2292
commit a693987b91
2 changed files with 15 additions and 19 deletions

View File

@ -2799,7 +2799,7 @@ function wp_site_icon() {
*/ */
function wp_resource_hints() { function wp_resource_hints() {
$hints = array( $hints = array(
'dns-prefetch' => wp_resource_hints_scripts_styles(), 'dns-prefetch' => wp_dependencies_unique_hosts(),
'preconnect' => array( 's.w.org' ), 'preconnect' => array( 's.w.org' ),
'prefetch' => array(), 'prefetch' => array(),
'prerender' => array(), 'prerender' => array(),
@ -2851,33 +2851,29 @@ function wp_resource_hints() {
} }
/** /**
* Adds dns-prefetch for all scripts and styles enqueued from external hosts. * Returns a list of unique hosts of all enqueued scripts and styles.
* *
* @since 4.6.0 * @since 4.6.0
*
* @return array A list of unique hosts of enqueued scripts and styles.
*/ */
function wp_resource_hints_scripts_styles() { function wp_dependencies_unique_hosts() {
global $wp_scripts, $wp_styles; global $wp_scripts, $wp_styles;
$unique_hosts = array(); $unique_hosts = array();
if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) { foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) {
foreach ( $wp_scripts->registered as $registered_script ) { if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) {
$parsed = wp_parse_url( $registered_script->src ); foreach ( $dependencies->queue as $handle ) {
/* @var _WP_Dependency $dependency */
$dependency = $dependencies->registered[ $handle ];
$parsed = wp_parse_url( $dependency->src );
if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) {
$unique_hosts[] = $parsed['host']; $unique_hosts[] = $parsed['host'];
} }
} }
} }
if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
foreach ( $wp_styles->registered as $registered_style ) {
$parsed = wp_parse_url( $registered_style->src );
if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_hosts ) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) {
$unique_hosts[] = $parsed['host'];
}
}
} }
return $unique_hosts; return $unique_hosts;

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.6-beta3-38099'; $wp_version = '4.6-beta3-38100';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.