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" );