Make WordPress Core


Ignore:
Timestamp:
08/25/2024 11:47:01 PM (2 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-dependencies.php

    r56547 r58935  
    491491                return true;
    492492        }
     493
     494        /**
     495         * Get etag header for cache validation.
     496         *
     497         * @since 6.7.0
     498         *
     499         * @global string $wp_version The WordPress version string.
     500         *
     501         * @param string[] $load Array of script or style handles to load.
     502         * @return string Etag header.
     503         */
     504        public function get_etag( $load ) {
     505                /*
     506                 * Note: wp_get_wp_version() is not used here, as this file can be included
     507                 * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case
     508                 * wp-includes/functions.php is not loaded.
     509                 */
     510                global $wp_version;
     511
     512                $etag = "WP:{$wp_version};";
     513
     514                foreach ( $load as $handle ) {
     515                        if ( ! array_key_exists( $handle, $this->registered ) ) {
     516                                continue;
     517                        }
     518
     519                        $ver   = $this->registered[ $handle ]->ver ?? $wp_version;
     520                        $etag .= "{$handle}:{$ver};";
     521                }
     522
     523                /*
     524                 * This is not intended to be cryptographically secure, just a fast way to get
     525                 * a fixed length string based on the script versions. As this file does not
     526                 * load the full WordPress environment, it is not possible to use the salted
     527                 * wp_hash() function.
     528                 */
     529                return 'W/"' . md5( $etag ) . '"';
     530        }
    493531}
Note: See TracChangeset for help on using the changeset viewer.