Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactivity API: Use modules instead of scripts in the frontend #56143

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1f3f99d
Bundle `@wordpress/interactivity` as an ES module
luisherranz Nov 9, 2023
8fe36f9
Add the class with a basic API
luisherranz Nov 13, 2023
68692d1
Make it work with the Navigation block
luisherranz Nov 13, 2023
0215330
Add versions
luisherranz Nov 13, 2023
9c5ea5d
Register `@wordpress/interactivity` module
luisherranz Nov 13, 2023
3ffc77c
Add query and image blocks
luisherranz Nov 13, 2023
7cc08c8
Add id with module identifier to the enqueued modules
luisherranz Nov 13, 2023
11b4e1b
Add requried $usage for admin/frontend
luisherranz Nov 14, 2023
c080fa7
Avoid the use of enqueue to register modules
luisherranz Nov 14, 2023
bd0a881
Refactor versions
luisherranz Nov 14, 2023
e0201ce
Switch from `both` to `['admin', 'frontend']`
luisherranz Nov 14, 2023
89f6be0
Improve comments
luisherranz Nov 14, 2023
64fd8ac
Add an optional static dependency graph
luisherranz Nov 14, 2023
6a21c10
Add static and dynamic dependencies in the server
luisherranz Nov 14, 2023
0e6a178
Add the file and search blocks
luisherranz Nov 14, 2023
211d1d5
Move $version out of $args to match wp_register_script
luisherranz Nov 15, 2023
400afda
Improve version logic
luisherranz Nov 15, 2023
87809c5
Add polyfill
luisherranz Nov 17, 2023
43ff14a
Fix $version using its own arg in register calls
luisherranz Nov 20, 2023
6d8d965
Add unit tests
cbravobernal Nov 27, 2023
823fcfa
Refactor tests, add test get import map
cbravobernal Nov 27, 2023
81c014e
Cleaning data
cbravobernal Nov 27, 2023
5488eab
Use gutenberg_url()
luisherranz Nov 28, 2023
4e6c5da
Use wp_print_script_tag
luisherranz Nov 28, 2023
829c6ec
Fix DocBlock on get_import_map()
luisherranz Nov 28, 2023
77cd95e
Merge branch 'trunk' into experiment/modules-and-import-maps-api-requ…
luisherranz Nov 28, 2023
366ad42
Load navigation module only on Gutenberg plugin
luisherranz Nov 28, 2023
4e7a3ef
Load query module only on Gutenberg plugin
luisherranz Nov 28, 2023
3e8d5fd
Load search module only on Gutenberg plugin
luisherranz Nov 28, 2023
46c9a65
Load file module only on Gutenberg plugin
luisherranz Nov 28, 2023
9f185d8
Load image module only on Gutenberg plugin
luisherranz Nov 28, 2023
df56463
Make registration optional
luisherranz Nov 28, 2023
0c6237a
Improve navigation logic
luisherranz Nov 28, 2023
2a2eeda
Remove unnecessary check
luisherranz Nov 28, 2023
5926282
Fix missing view file
luisherranz Nov 28, 2023
6b932fe
Don't print the import map if it's empty
luisherranz Nov 28, 2023
1b176d0
Load the importmap polyfill locally
luisherranz Nov 28, 2023
b627d73
Move the es-modules-shims package to the top
luisherranz Nov 28, 2023
0affd41
Use the public functions in the tests
luisherranz Nov 28, 2023
3cd253a
Update test to be more output oriented
cbravobernal Nov 29, 2023
148f1dd
Remove we from comments.
cbravobernal Nov 29, 2023
4135f22
Merge branch 'trunk' into experiment/modules-and-import-maps-api-requ…
luisherranz Nov 29, 2023
876fdb1
Start using modules in the interactivity e2e tests
luisherranz Nov 29, 2023
88fcee8
Update package-lock.json
luisherranz Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre

- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments
Expand Down
33 changes: 33 additions & 0 deletions lib/experimental/interactivity-api/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Interactive modules.
*
* @package Gutenberg
* @subpackage Interactivity API
*/

/**
* Register the `@wordpress/interactivity` module.
*/
function gutenberg_register_interactivity_module() {
gutenberg_register_module(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a way in our code to have an array of "esm modules" and have these be registered automatically (similarly to the other types of packages we have: non modules)

'@wordpress/interactivity',
'/wp-content/plugins/gutenberg/build/interactivity/index.min.js',
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
array(),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);

// TODO: Move this to a local file and replace with a simpler version that
// only provides support for import maps.
wp_enqueue_script(
'es-module-shims',
'https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js',
array(),
null,
array(
'strategy' => 'defer',
)
);
}

add_action( 'wp_enqueue_scripts', 'gutenberg_register_interactivity_module' );
40 changes: 0 additions & 40 deletions lib/experimental/interactivity-api/scripts.php

This file was deleted.

186 changes: 186 additions & 0 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php
/**
* Modules API: Gutenberg_Modules class.
*
* Native support for ES Modules and Import Maps.
*
* @package Gutenberg
* @subpackage Modules
*/

/**
* Gutenberg_Modules class
*/
class Gutenberg_Modules {
/**
* An array of registered modules, keyed by module identifier.
*
* @var array
*/
private static $registered = array();

/**
* An array of queued modules.
*
* @var string[]
*/
private static $enqueued = array();

/**
* Registers the module if no module with that module identifier already
* exists.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $dependencies Optional. An array of module identifiers of the static and dynamic dependencies of this module. It can be an indexed array, in which case all the dependencies are static, or it can be an associative array, in which case it has to contain the keys `static` and `dynamic`.
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
* @param string|bool|null $version Optional. String specifying module version number. It is added to the URL as a query string for cache busting purposes. If SCRIPT_DEBUG is true, a timestamp is used. If it is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
*/
public static function register( $module_identifier, $src, $dependencies = array(), $version = false ) {
// Register the module if it's not already registered.
if ( ! isset( self::$registered[ $module_identifier ] ) ) {
$deps = array(
'static' => isset( $dependencies['static'] ) || isset( $dependencies['dynamic'] ) ? $dependencies['static'] ?? array() : $dependencies,
'dynamic' => isset( $dependencies['dynamic'] ) ? $dependencies['dynamic'] : array(),
);

self::$registered[ $module_identifier ] = array(
'src' => $src,
'version' => $version,
'dependencies' => $deps,
);
}
}

/**
* Enqueues a module in the page.
*
* @param string $module_identifier The identifier of the module.
*/
public static function enqueue( $module_identifier ) {
// Add the module to the queue if it's not already there.
if ( ! in_array( $module_identifier, self::$enqueued, true ) ) {
self::$enqueued[] = $module_identifier;
}
}

/**
* Returns the import map array.
*
* @return string The import map.
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function get_import_map() {
$imports = array();
foreach ( self::get_dependencies( self::$enqueued, array( 'static', 'dynamic' ) ) as $module_identifier => $module ) {
$imports[ $module_identifier ] = $module['src'] . self::get_version_query_string( $module['version'] );
}
return array( 'imports' => $imports );
}

/**
* Prints the import map.
*/
public static function print_import_map() {
echo '<script type="importmap">' . wp_json_encode( self::get_import_map(), JSON_HEX_TAG | JSON_HEX_AMP ) . '</script>';
}

/**
* Prints all the enqueued modules using <script type="module">.
*/
public static function print_enqueued_modules() {
foreach ( self::$enqueued as $module_identifier ) {
if ( isset( self::$registered[ $module_identifier ] ) ) {
$module = self::$registered[ $module_identifier ];
echo '<script type="module" src="' . $module['src'] . self::get_version_query_string( $module['version'] ) . '" id="' . $module_identifier . '"></script>';
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/**
* Prints the link tag with rel="modulepreload" for all the static
* dependencies of the enqueued modules.
*/
public static function print_module_preloads() {
foreach ( self::get_dependencies( self::$enqueued, array( 'static' ) ) as $dependency_identifier => $module ) {
echo '<link rel="modulepreload" href="' . $module['src'] . self::get_version_query_string( $module['version'] ) . '" id="' . $dependency_identifier . '">';
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* 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 $version The version of the module.
* @return string A string presenting the version.
*/
private static function get_version_query_string( $version ) {
if ( SCRIPT_DEBUG ) {
return '?ver=' . time();
} elseif ( false === $version ) {
return '?ver=' . get_bloginfo( 'version' );
} elseif ( null !== $version ) {
return '?ver=' . $version;
}
return '';
}

/**
* Returns all unique static and/or dynamic dependencies for the received modules. It's
* recursive, so it will also get the static or dynamic dependencies of the dependencies.
*
* @param array $module_identifiers The identifiers of the modules to get dependencies for.
* @param array $types The type of dependencies to retrieve. It can be `static`, `dynamic` or both.
* @return array The array containing the unique dependencies of the modules.
*/
private static function get_dependencies( $module_identifiers, $types = array( 'static', 'dynamic' ) ) {
return array_reduce(
$module_identifiers,
function ( $dependency_modules, $module_identifier ) use ( $types ) {
if ( ! isset( self::$registered[ $module_identifier ] ) ) {
return $dependency_modules;
}

$dependencies = array();
foreach ( $types as $type ) {
$dependencies = array_merge( $dependencies, self::$registered[ $module_identifier ]['dependencies'][ $type ] );
}
$dependencies = array_unique( $dependencies );
$dependency_modules = array_intersect_key( self::$registered, array_flip( $dependencies ) );

return array_merge( $dependency_modules, $dependency_modules, self::get_dependencies( $dependencies, $types ) );
},
array()
);
}
}

/**
* Registers a JavaScript module. It will be added to the import map.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $dependencies Optional. An array of module identifiers of the static and dynamic dependencies of this module. It can be an indexed array, in which case all the dependencies are static, or it can be an associative array, in which case it has to contain the keys `static` and `dynamic`.
* @param string|bool|null $version Optional. String specifying module version number. It is added to the URL as a query string for cache busting purposes. If SCRIPT_DEBUG is true, a timestamp is used. If it is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
*/
function gutenberg_register_module( $module_identifier, $src, $dependencies = array(), $version = false ) {
Gutenberg_Modules::register( $module_identifier, $src, $dependencies, $version );
}

/**
* Enqueues a JavaScript module. It will be added to both the import map and a
* script tag with the "module" type.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
*/
function gutenberg_enqueue_module( $module_identifier ) {
Gutenberg_Modules::enqueue( $module_identifier );
}

// Prints the import map in the head tag.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map' ) );

// Prints the enqueued modules in the head tag.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_enqueued_modules' ) );

// Prints the preloaded modules in the head tag.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_module_preloads' ) );
4 changes: 3 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function gutenberg_is_experiment_enabled( $name ) {

require __DIR__ . '/experimental/interactivity-api/class-wp-interactivity-store.php';
require __DIR__ . '/experimental/interactivity-api/store.php';
require __DIR__ . '/experimental/interactivity-api/scripts.php';
require __DIR__ . '/experimental/interactivity-api/modules.php';
require __DIR__ . '/experimental/interactivity-api/class-wp-directive-processor.php';
require __DIR__ . '/experimental/interactivity-api/class-wp-directive-context.php';
require __DIR__ . '/experimental/interactivity-api/directive-processing.php';
Expand All @@ -148,6 +148,8 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/interactivity-api/directives/wp-style.php';
require __DIR__ . '/experimental/interactivity-api/directives/wp-text.php';

require __DIR__ . '/experimental/modules/class-gutenberg-modules.php';

// Fonts API / Font Face.
remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WordPress 6.0's stopgap handler.

Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
},
"interactivity": true
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-file-editor",
"style": "wp-block-file"
}
22 changes: 9 additions & 13 deletions packages/block-library/src/file/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,8 @@
*/
function render_block_core_file( $attributes, $content, $block ) {
$should_load_view_script = ! empty( $attributes['displayPreview'] );
$view_js_file = 'wp-block-file-view';
// If the script already exists, there is no point in removing it from viewScript.
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( ! $should_load_view_script && in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $should_load_view_script && ! in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
}
if ( $should_load_view_script ) {
gutenberg_enqueue_module( '@wordpress/block-library/file-block' );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like complex logic, do we expect any interactive block to write this kind of code? Is there a way to streamline that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar feedback before WP 6.4 Beta 1: WordPress/wordpress-develop#5262 (comment).

Follow-up ticket in Trac: https://core.trac.wordpress.org/ticket/59462.


// Update object's aria-label attribute if present in block HTML.
Expand Down Expand Up @@ -96,5 +85,12 @@ function register_block_core_file() {
'render_callback' => 'render_block_core_file',
)
);

gutenberg_register_module(
'@wordpress/block-library/file-block',
'/wp-content/plugins/gutenberg/build/interactivity/file.min.js',
array( '@wordpress/interactivity' ),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);
}
add_action( 'init', 'register_block_core_file' );
4 changes: 2 additions & 2 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
}
},
"supports": {
"interactivity": true,
"anchor": true,
"color": {
"text": false,
Expand Down Expand Up @@ -130,6 +131,5 @@
{ "name": "rounded", "label": "Rounded" }
],
"editorStyle": "wp-block-image-editor",
"style": "wp-block-image",
"viewScript": "file:./view.min.js"
"style": "wp-block-image"
}
20 changes: 8 additions & 12 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ function render_block_core_image( $attributes, $content, $block ) {
$link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
$lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );

$view_js_file_handle = 'wp-block-image-view';
$script_handles = $block->block_type->view_script_handles;

/*
* If the lightbox is enabled and the image is not linked, add the filter
* and the JavaScript view file.
Expand All @@ -50,11 +47,7 @@ function render_block_core_image( $attributes, $content, $block ) {
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
$block->block_type->supports['interactivity'] = true;

if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) );
}
gutenberg_enqueue_module( '@wordpress/block-library/image' );

/*
* This render needs to happen in a filter with priority 15 to ensure
Expand All @@ -71,10 +64,6 @@ function render_block_core_image( $attributes, $content, $block ) {
* other Image blocks.
*/
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) );
}
}

return $processor->get_updated_html();
Expand Down Expand Up @@ -362,5 +351,12 @@ function register_block_core_image() {
'render_callback' => 'render_block_core_image',
)
);

gutenberg_register_module(
'@wordpress/block-library/image',
'/wp-content/plugins/gutenberg/build/interactivity/image.min.js',
array( '@wordpress/interactivity' ),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);
}
add_action( 'init', 'register_block_core_image' );
Loading
Loading