From 37c80a7b4bbf6222188b68caebf1438cc9443dc0 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 11 Mar 2021 06:57:28 -0600 Subject: [PATCH 1/7] Replaces filemtime when registering package styles. For production, uses GUTENBERG_VERSION. For dev, uses microtime(). --- lib/client-assets.php | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index ef74ce9e025126..e624a3c83d7ecc 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -285,13 +285,16 @@ function gutenberg_register_packages_scripts( $scripts ) { * @param WP_Styles $styles WP_Styles instance. */ function gutenberg_register_packages_styles( $styles ) { + // When in development mode, use a current time as the asset version; else, use the plugin's version as the asset version. + $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; + // Editor Styles. gutenberg_override_style( $styles, 'wp-block-editor', gutenberg_url( 'build/block-editor/style.css' ), array( 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/editor/style.css' ) + $version ); $styles->add_data( 'wp-block-editor', 'rtl', 'replace' ); @@ -300,7 +303,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-editor', gutenberg_url( 'build/editor/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ), - filemtime( gutenberg_dir_path() . 'build/editor/style.css' ) + $version ); $styles->add_data( 'wp-editor', 'rtl', 'replace' ); @@ -309,7 +312,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-edit-post', gutenberg_url( 'build/edit-post/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ), - filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' ) + $version ); $styles->add_data( 'wp-edit-post', 'rtl', 'replace' ); @@ -318,7 +321,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-components', gutenberg_url( 'build/components/style.css' ), array( 'dashicons' ), - filemtime( gutenberg_dir_path() . 'build/components/style.css' ) + $version ); $styles->add_data( 'wp-components', 'rtl', 'replace' ); @@ -328,7 +331,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-block-library', gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ), array(), - filemtime( gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' ) + $version ); $styles->add_data( 'wp-block-library', 'rtl', 'replace' ); $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' ); @@ -338,7 +341,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-format-library', gutenberg_url( 'build/format-library/style.css' ), array( 'wp-block-editor', 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/format-library/style.css' ) + $version ); $styles->add_data( 'wp-format-library', 'rtl', 'replace' ); @@ -360,7 +363,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-edit-blocks', gutenberg_url( 'build/block-library/editor.css' ), $wp_edit_blocks_dependencies, - filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' ) + $version ); $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' ); @@ -369,7 +372,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-nux', gutenberg_url( 'build/nux/style.css' ), array( 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/nux/style.css' ) + $version ); $styles->add_data( 'wp-nux', 'rtl', 'replace' ); @@ -378,7 +381,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-block-library-theme', gutenberg_url( 'build/block-library/theme.css' ), array(), - filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' ) + $version ); $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' ); @@ -387,7 +390,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-list-reusable-blocks', gutenberg_url( 'build/list-reusable-blocks/style.css' ), array( 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' ) + $version ); $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' ); @@ -396,7 +399,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-edit-navigation', gutenberg_url( 'build/edit-navigation/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), - filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' ) + $version ); $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' ); @@ -405,7 +408,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-edit-site', gutenberg_url( 'build/edit-site/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), - filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' ) + $version ); $styles->add_data( 'wp-edit-site', 'rtl', 'replace' ); @@ -414,7 +417,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-edit-widgets', gutenberg_url( 'build/edit-widgets/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks' ), - filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' ) + $version ); $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' ); @@ -423,7 +426,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-block-directory', gutenberg_url( 'build/block-directory/style.css' ), array( 'wp-block-editor', 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' ) + $version ); $styles->add_data( 'wp-block-directory', 'rtl', 'replace' ); @@ -432,7 +435,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-customize-widgets', gutenberg_url( 'build/customize-widgets/style.css' ), array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), - filemtime( gutenberg_dir_path() . 'build/customize-widgets/style.css' ) + $version ); $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' ); @@ -441,7 +444,7 @@ function gutenberg_register_packages_styles( $styles ) { 'wp-reusable-blocks', gutenberg_url( 'build/reusable-blocks/style.css' ), array( 'wp-components' ), - filemtime( gutenberg_dir_path() . 'build/reusable-blocks/style.css' ) + $version ); $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' ); } From a53fafe6a1e073ea8b2df86fbc0a578501301ecd Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 11 Mar 2021 07:00:53 -0600 Subject: [PATCH 2/7] Replaces filemtime when registering package scripts. For production, uses GUTENBERG_VERSION. For dev, uses microtime(). --- lib/client-assets.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index e624a3c83d7ecc..c491d92cd685b6 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -233,6 +233,10 @@ function gutenberg_register_vendor_scripts( $scripts ) { * @param WP_Scripts $scripts WP_Scripts instance. */ function gutenberg_register_packages_scripts( $scripts ) { + // Defines default version. + // When in development mode, use a current time as the asset version; else, use the plugin's version as the asset version. + $default_version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { // Prefix `wp-` to package directory to get script handle. // For example, `…/build/a11y/index.js` becomes `wp-a11y`. @@ -244,7 +248,7 @@ function gutenberg_register_packages_scripts( $scripts ) { ? require( $asset_file ) : null; $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); - $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path ); + $version = isset( $asset['version'] ) ? $asset['version'] : $default_version; // Add dependencies that cannot be detected and generated by build tools. switch ( $handle ) { From 30eaa13b594c26acb4478c6c7a703a816523d1a1 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 11 Mar 2021 07:02:39 -0600 Subject: [PATCH 3/7] Improves comments. --- lib/client-assets.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index c491d92cd685b6..af0f12de68c3e1 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -234,7 +234,8 @@ function gutenberg_register_vendor_scripts( $scripts ) { */ function gutenberg_register_packages_scripts( $scripts ) { // Defines default version. - // When in development mode, use a current time as the asset version; else, use the plugin's version as the asset version. + // For development, uses the current time (in microseconds) as the asset version. + // For production, uses the plugin's version as the asset version. $default_version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { @@ -289,7 +290,8 @@ function gutenberg_register_packages_scripts( $scripts ) { * @param WP_Styles $styles WP_Styles instance. */ function gutenberg_register_packages_styles( $styles ) { - // When in development mode, use a current time as the asset version; else, use the plugin's version as the asset version. + // For development, uses the current time (in microseconds) as the asset version. + // For production, uses the plugin's version as the asset version. $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; // Editor Styles. From e415075b1861337b3488b12f67e7ab70450ed26f Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 11 Mar 2021 07:40:47 -0600 Subject: [PATCH 4/7] Use time() instead of microtime(). --- lib/client-assets.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index af0f12de68c3e1..a53036a348a502 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -236,7 +236,7 @@ function gutenberg_register_packages_scripts( $scripts ) { // Defines default version. // For development, uses the current time (in microseconds) as the asset version. // For production, uses the plugin's version as the asset version. - $default_version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; + $default_version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : GUTENBERG_VERSION; foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { // Prefix `wp-` to package directory to get script handle. @@ -292,7 +292,7 @@ function gutenberg_register_packages_scripts( $scripts ) { function gutenberg_register_packages_styles( $styles ) { // For development, uses the current time (in microseconds) as the asset version. // For production, uses the plugin's version as the asset version. - $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? microtime() : GUTENBERG_VERSION; + $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : GUTENBERG_VERSION; // Editor Styles. gutenberg_override_style( From f31f27bb2dbf4f1c7d5d8be8b5a4ef924d135b4a Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Fri, 12 Mar 2021 08:04:08 -0600 Subject: [PATCH 5/7] For consistency, use SCRIPT_DEBUG as dev/prod flag. --- lib/client-assets.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index a53036a348a502..ff3d8eed8e97d0 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -236,7 +236,7 @@ function gutenberg_register_packages_scripts( $scripts ) { // Defines default version. // For development, uses the current time (in microseconds) as the asset version. // For production, uses the plugin's version as the asset version. - $default_version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : GUTENBERG_VERSION; + $default_version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : GUTENBERG_VERSION; foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { // Prefix `wp-` to package directory to get script handle. @@ -292,7 +292,7 @@ function gutenberg_register_packages_scripts( $scripts ) { function gutenberg_register_packages_styles( $styles ) { // For development, uses the current time (in microseconds) as the asset version. // For production, uses the plugin's version as the asset version. - $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : GUTENBERG_VERSION; + $version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : GUTENBERG_VERSION; // Editor Styles. gutenberg_override_style( From 352e06ae161ddb9f738e82320203e8d20874c464 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 16 Mar 2021 12:27:47 -0500 Subject: [PATCH 6/7] Check if GUTENBERG_VERSION is defined. Switches the check around to determine if in production mode: - GUTENBERG_VERSION is defined - SCRIPT_DEBUG is not defined || is not true. Else, defaults to time(). --- lib/client-assets.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index ff3d8eed8e97d0..e54ef9ce2970c3 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -233,10 +233,9 @@ function gutenberg_register_vendor_scripts( $scripts ) { * @param WP_Scripts $scripts WP_Scripts instance. */ function gutenberg_register_packages_scripts( $scripts ) { - // Defines default version. - // For development, uses the current time (in microseconds) as the asset version. - // For production, uses the plugin's version as the asset version. - $default_version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : GUTENBERG_VERSION; + // When in production, use the plugin's version as the default asset version; + // else (for development or test) default to use the current time. + $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { // Prefix `wp-` to package directory to get script handle. @@ -290,9 +289,10 @@ function gutenberg_register_packages_scripts( $scripts ) { * @param WP_Styles $styles WP_Styles instance. */ function gutenberg_register_packages_styles( $styles ) { - // For development, uses the current time (in microseconds) as the asset version. - // For production, uses the plugin's version as the asset version. - $version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : GUTENBERG_VERSION; + // When in production, use the plugin's version as the asset version; + // else (for development or test) default to use the current time. + $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); + // Editor Styles. gutenberg_override_style( From 49695e1799763e9f35f951a52fe6e6b292f29ad9 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 16 Mar 2021 12:48:31 -0500 Subject: [PATCH 7/7] Removes extra empty line (for phpcs). --- lib/client-assets.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index e54ef9ce2970c3..f5d881902b9a1f 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -293,7 +293,6 @@ function gutenberg_register_packages_styles( $styles ) { // else (for development or test) default to use the current time. $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); - // Editor Styles. gutenberg_override_style( $styles,