Leverage Script API strategy feature to defer front end scripts in WP >=6.3 (#40686)

* Leverage strategy API to defer scripts from header rather than placing in footer

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Adam Silverstein 2023-10-19 01:45:37 -06:00 committed by GitHub
parent e302d4cac8
commit fe6e43b438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Use the Script API strategy feature to defer front-end scripts in WordPress 6.3+

View File

@ -120,7 +120,7 @@ class WC_Frontend_Scripts {
* @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
* @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
*/
private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = array( 'strategy' => 'defer' ) ) {
self::$scripts[] = $handle;
wp_register_script( $handle, $path, $deps, $version, $in_footer );
}
@ -135,7 +135,7 @@ class WC_Frontend_Scripts {
* @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
* @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
*/
private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = array( 'strategy' => 'defer' ) ) {
if ( ! in_array( $handle, self::$scripts, true ) && $path ) {
self::register_script( $handle, $path, $deps, $version, $in_footer );
}