Script Loader: Refactor Etag generation for concatenated assets.

Move Etag HTTP header generation in `load-scripts.php` and `load-styles.php` to `WP_Dependencies`.

Introduces the method `WP_Dependencies::get_etag()` and associated unit tests.

Follow up to [57943].

Props vrajadas, martinkrcho, mukesh27.
Fixes #61485.

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


git-svn-id: http://core.svn.wordpress.org/trunk@58331 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-08-25 23:48:13 +00:00
parent fd94bc45c6
commit 0735fa8ce0
4 changed files with 41 additions and 37 deletions

View File

@ -52,24 +52,7 @@ wp_default_scripts( $wp_scripts );
wp_default_packages_vendor( $wp_scripts );
wp_default_packages_scripts( $wp_scripts );
$etag = "WP:{$wp_version};";
foreach ( $load as $handle ) {
if ( ! array_key_exists( $handle, $wp_scripts->registered ) ) {
continue;
}
$ver = $wp_scripts->registered[ $handle ]->ver ? $wp_scripts->registered[ $handle ]->ver : $wp_version;
$etag .= "{$handle}:{$ver};";
}
/*
* This is not intended to be cryptographically secure, just a fast way to get
* a fixed length string based on the script versions. As this file does not
* load the full WordPress environment, it is not possible to use the salted
* wp_hash() function.
*/
$etag = 'W/"' . md5( $etag ) . '"';
$etag = $wp_scripts->get_etag( $load );
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag ) {
header( "$protocol 304 Not Modified" );

View File

@ -55,24 +55,7 @@ $out = '';
$wp_styles = new WP_Styles();
wp_default_styles( $wp_styles );
$etag = "WP:{$wp_version};";
foreach ( $load as $handle ) {
if ( ! array_key_exists( $handle, $wp_styles->registered ) ) {
continue;
}
$ver = $wp_styles->registered[ $handle ]->ver ? $wp_styles->registered[ $handle ]->ver : $wp_version;
$etag .= "{$handle}:{$ver};";
}
/*
* This is not intended to be cryptographically secure, just a fast way to get
* a fixed length string based on the script versions. As this file does not
* load the full WordPress environment, it is not possible to use the salted
* wp_hash() function.
*/
$etag = 'W/"' . md5( $etag ) . '"';
$etag = $wp_styles->get_etag( $wp_version, $load );
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag ) {
header( "$protocol 304 Not Modified" );

View File

@ -490,4 +490,42 @@ class WP_Dependencies {
return true;
}
/**
* Get etag header for cache validation.
*
* @since 6.7.0
*
* @global string $wp_version The WordPress version string.
*
* @param string[] $load Array of script or style handles to load.
* @return string Etag header.
*/
public function get_etag( $load ) {
/*
* Note: wp_get_wp_version() is not used here, as this file can be included
* via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case
* wp-includes/functions.php is not loaded.
*/
global $wp_version;
$etag = "WP:{$wp_version};";
foreach ( $load as $handle ) {
if ( ! array_key_exists( $handle, $this->registered ) ) {
continue;
}
$ver = $this->registered[ $handle ]->ver ?? $wp_version;
$etag .= "{$handle}:{$ver};";
}
/*
* This is not intended to be cryptographically secure, just a fast way to get
* a fixed length string based on the script versions. As this file does not
* load the full WordPress environment, it is not possible to use the salted
* wp_hash() function.
*/
return 'W/"' . md5( $etag ) . '"';
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-alpha-58934';
$wp_version = '6.7-alpha-58935';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.