From 0735fa8ce0e205613c478a58f016fdaa1b1b6395 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 23:48:13 +0000 Subject: [PATCH] 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 --- wp-admin/load-scripts.php | 19 +------------- wp-admin/load-styles.php | 19 +------------- wp-includes/class-wp-dependencies.php | 38 +++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/wp-admin/load-scripts.php b/wp-admin/load-scripts.php index b802394ece..e4b6a1ee5b 100644 --- a/wp-admin/load-scripts.php +++ b/wp-admin/load-scripts.php @@ -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" ); diff --git a/wp-admin/load-styles.php b/wp-admin/load-styles.php index 9e985569aa..083c3832eb 100644 --- a/wp-admin/load-styles.php +++ b/wp-admin/load-styles.php @@ -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" ); diff --git a/wp-includes/class-wp-dependencies.php b/wp-includes/class-wp-dependencies.php index e2bbf4ff98..ef9dfa7d5f 100644 --- a/wp-includes/class-wp-dependencies.php +++ b/wp-includes/class-wp-dependencies.php @@ -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 ) . '"'; + } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 007c511f7a..ce8d934093 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.