From bd0a881e2a9736d64af21b912a3871b7f291bb13 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Tue, 14 Nov 2023 11:22:19 +0100 Subject: [PATCH] Refactor versions --- .../interactivity-api/modules.php | 5 ++- .../modules/class-gutenberg-modules.php | 45 +++++++++++-------- packages/block-library/src/image/index.php | 5 ++- .../block-library/src/navigation/index.php | 5 ++- packages/block-library/src/query/index.php | 5 ++- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/experimental/interactivity-api/modules.php b/lib/experimental/interactivity-api/modules.php index eec49ec40d9a66..d67f225e58293f 100644 --- a/lib/experimental/interactivity-api/modules.php +++ b/lib/experimental/interactivity-api/modules.php @@ -13,7 +13,10 @@ function gutenberg_register_interactivity_module() { gutenberg_register_module( '@wordpress/interactivity', '/wp-content/plugins/gutenberg/build/interactivity/index.min.js', - 'frontend' + 'frontend', + array( + 'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ), + ) ); } diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index c500870dd46cec..652875cea2b756 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -43,9 +43,9 @@ public static function register( $module_identifier, $src, $usage, $args = array // Register the module if it's not already registered. if ( ! isset( self::$registered[ $module_identifier ] ) ) { self::$registered[ $module_identifier ] = array( - 'src' => $src, - 'usage' => $usage, - 'args' => $args, + 'src' => $src, + 'usage' => $usage, + 'version' => isset( $args['version'] ) ? $args['version'] : '', ); } } @@ -72,10 +72,9 @@ public static function get_import_map() { 'imports' => array(), ); - foreach ( self::$registered as $module_identifier => $module_data ) { - if ( self::appropriate_usage( $module_data['usage'] ) ) { - $version = SCRIPT_DEBUG ? '?ver=' . time() : '?ver=' . $module_data['args']['version'] || ''; - $import_map['imports'][ $module_identifier ] = $module_data['src'] . $version; + foreach ( self::$registered as $module_identifier => $module ) { + if ( self::get_appropriate_usage( $module['usage'] ) ) { + $import_map['imports'][ $module_identifier ] = $module['src'] . self::get_module_version( $module ); } } @@ -94,9 +93,9 @@ public static function print_import_map() { */ public static function print_enqueued_modules() { foreach ( self::$enqueued as $module_identifier ) { - if ( isset( self::$registered[ $module_identifier ] ) && self::appropriate_usage( self::$registered[ $module_identifier ]['usage'] ) ) { + if ( isset( self::$registered[ $module_identifier ] ) && self::get_appropriate_usage( self::$registered[ $module_identifier ]['usage'] ) ) { $module = self::$registered[ $module_identifier ]; - $version = SCRIPT_DEBUG ? '?ver=' . time() : '?ver=' . $module['args']['version'] || ''; + $version = self::get_module_version( $module ); echo ''; } } @@ -108,7 +107,7 @@ public static function print_enqueued_modules() { * @param string $usage Specifies the usage of the module. Can be 'admin', 'frontend', or 'both'. * @return bool Returns true if it's appropriate to load the module in the current WP context. */ - public static function appropriate_usage( $usage ) { + private static function get_appropriate_usage( $usage ) { if ( 'both' === $usage ) { return true; } @@ -120,6 +119,23 @@ public static function appropriate_usage( $usage ) { } return false; } + + /** + * Gets the module's version. It either returns a timestamp (if SCRIPT_DEBUG + * is true), the explicit version of the module if it is set and not false, or + * an empty string if none of the above conditions are met. + * + * @param array $module The data of the module. + * @return string A string presenting the version. + */ + private static function get_module_version( $module ) { + if ( SCRIPT_DEBUG ) { + return '?ver=' . time(); + } elseif ( $module['version'] ) { + return '?ver=' . $module['version']; + } + return ''; + } } /** @@ -149,15 +165,6 @@ function gutenberg_register_module( $module_identifier, $src, $usage, $args = ar * value if there is an existing one. * * @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map. - * @param string $src Optional. Full URL of the module, or path of the script relative to the WordPress root directory. - * @param array $args { - * Optional array of arguments. - * - * @type string|bool $ver Optional. String specifying script version number, if it has one, it 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 SCRIPT_DEBUG - * is set to true, it uses the timestamp instead. - * } */ function gutenberg_enqueue_module( $module_identifier ) { Gutenberg_Modules::enqueue( $module_identifier ); diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index 98d2a14a731489..99cbf2c880636d 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -355,7 +355,10 @@ function register_block_core_image() { gutenberg_register_module( '@wordpress/block-library/image', '/wp-content/plugins/gutenberg/build/interactivity/image.min.js', - 'frontend' + 'frontend', + array( + 'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ), + ) ); } add_action( 'init', 'register_block_core_image' ); diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index b8a4d7238a837a..d232f6953171fa 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -785,7 +785,10 @@ function register_block_core_navigation() { gutenberg_register_module( '@wordpress/block-library/navigation-block', '/wp-content/plugins/gutenberg/build/interactivity/navigation.min.js', - 'frontend' + 'frontend', + array( + 'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ), + ) ); } diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index adc07e6280c580..cb67904b6b3419 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -121,7 +121,10 @@ function register_block_core_query() { gutenberg_register_module( '@wordpress/block-library/query', '/wp-content/plugins/gutenberg/build/interactivity/query.min.js', - 'frontend' + 'frontend', + array( + 'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ), + ) ); } add_action( 'init', 'register_block_core_query' );