Skip to content

Commit

Permalink
Refactor versions
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Nov 14, 2023
1 parent c080fa7 commit bd0a881
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
5 changes: 4 additions & 1 deletion lib/experimental/interactivity-api/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
);
}

Expand Down
45 changes: 26 additions & 19 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : '',
);
}
}
Expand All @@ -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 );
}
}

Expand All @@ -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 '<script type="module" src="' . $module['src'] . $version . '" id="' . $module_identifier . '"></script>';
}
}
Expand All @@ -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;
}
Expand All @@ -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 '';
}
}

/**
Expand Down Expand Up @@ -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 );
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
5 changes: 4 additions & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit bd0a881

Please sign in to comment.