Skip to content

Commit 40f68a9

Browse files
t-hamanowestonruterluisherranzsamueljseayDAreRodz
authored
Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module (#72489) (#73512)
* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module * Use wp_json_encode() * Make gutenberg_script_module_add_router_options_attributes() more concise * Improve phpdoc return tags * Remove compat/wordpress-6.9/script-modules.php * Update backport changelog * (WIP) Use render_block_data to filter all blocks co-authored: @samueljseay * Adjust manual registration of blocks in e2e tests * We should not need any manual registration now. * Update changelog to match new wordpress-develop PR. * Fix merge conflict mistake * Remove redundant white space change * Fix merge conflict mistake * Move conditional code to compat, check for existence of new method when re-registering existing core blocks * Fix typo in code comment. * Simplify the compatibility check. --------- Co-authored-by: westonruter <[email protected]> Co-authored-by: luisherranz <[email protected]> Co-authored-by: samueljseay <[email protected]> Co-authored-by: DAreRodz <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: t-hamano <[email protected]>
1 parent 11dcec5 commit 40f68a9

File tree

7 files changed

+79
-162
lines changed

7 files changed

+79
-162
lines changed

backport-changelog/6.9/10324.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

backport-changelog/6.9/10357.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/10357
2+
3+
* https://github.com/WordPress/gutenberg/pull/72489

lib/client-assets.php

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,9 @@ function gutenberg_default_script_modules() {
686686
'fetchpriority' => 'low',
687687
);
688688

689-
gutenberg_register_interactive_script_module_id( $script_module_id );
689+
if ( str_starts_with( $script_module_id, '@wordpress/block-library' ) && method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
690+
wp_interactivity()->add_client_navigation_support_to_script_module( $script_module_id );
691+
}
690692

691693
$path = gutenberg_url( "build-module/{$file_name}" );
692694
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'], $args ); // The $args parameter is new as of WP 6.9 per <https://core.trac.wordpress.org/ticket/61734>.
@@ -695,7 +697,7 @@ function gutenberg_default_script_modules() {
695697
remove_action( 'wp_default_scripts', 'wp_default_script_modules' );
696698
add_action( 'wp_default_scripts', 'gutenberg_default_script_modules' );
697699

698-
/*
700+
/**
699701
* Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice.
700702
* This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes,
701703
* which are in continuous development and generally ahead of Core.
@@ -707,59 +709,6 @@ function gutenberg_default_script_modules() {
707709
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
708710
add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );
709711

710-
/**
711-
* Access the shared static variable for interactive script modules.
712-
*
713-
* @param string|null $script_module_id The script module ID to register, or null to get the list.
714-
* @return array Associative array of script module ID => true.
715-
*/
716-
function gutenberg_interactive_script_modules_registry( $script_module_id = null ) {
717-
static $interactive_script_modules = array();
718-
719-
if ( null !== $script_module_id ) {
720-
$interactive_script_modules[ $script_module_id ] = true;
721-
}
722-
723-
return $interactive_script_modules;
724-
}
725-
726-
/**
727-
* Register a script module ID for interactive blocks.
728-
*
729-
* @param string $script_module_id The script module ID.
730-
*/
731-
function gutenberg_register_interactive_script_module_id( $script_module_id ) {
732-
gutenberg_interactive_script_modules_registry( $script_module_id );
733-
}
734-
735-
/**
736-
* Get the list of interactive script module IDs.
737-
*
738-
* @return array Associative array of script module ID => true.
739-
*/
740-
function gutenberg_get_interactive_script_module_ids() {
741-
return gutenberg_interactive_script_modules_registry();
742-
}
743-
744-
/**
745-
* Adds `data-wp-router-options` attribute to script modules registered as interactive.
746-
*
747-
* @param array $args The script module attributes.
748-
* @param string $id The script module ID.
749-
* @return array Filtered script module attributes.
750-
*/
751-
function gutenberg_script_module_add_router_options_attributes( $args, $id ) {
752-
// Check if this script module ID is registered as interactive.
753-
$interactive_modules = gutenberg_get_interactive_script_module_ids();
754-
755-
if ( isset( $interactive_modules[ $id ] ) ) {
756-
$args['data-wp-router-options'] = '{ "loadOnClientNavigation": true }';
757-
}
758-
return $args;
759-
}
760-
761-
add_filter( 'wp_script_module_attributes', 'gutenberg_script_module_add_router_options_attributes', 10, 2 );
762-
763712
add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_latex_to_mathml_loader' );
764713
function gutenberg_enqueue_latex_to_mathml_loader() {
765714
wp_enqueue_script_module( '@wordpress/latex-to-mathml/loader' );
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/**
4+
* Client Assets compatibility functions for WordPress 6.9.
5+
*
6+
* Hooks into `render_block_data` to detect blocks that support preloading
7+
* during client side navigation (used by interactivity router). This is addressed in core in:
8+
* https://github.com/WordPress/wordpress-develop/pull/10357
9+
*
10+
* @package gutenberg
11+
*/
12+
13+
if ( ! method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
14+
/**
15+
* Access the shared static variable for interactive script modules.
16+
*
17+
* @param string|null $script_module_id The script module ID to register, or null to get the list.
18+
* @return array<string, true> Associative array of script module ID => true.
19+
*/
20+
function gutenberg_interactive_script_modules_registry( $script_module_id = null ) {
21+
static $interactive_script_modules = array(
22+
'@wordpress/block-library/navigation/view-js-module' => true,
23+
'@wordpress/block-library/query/view-js-module' => true,
24+
'@wordpress/block-library/image/view-js-module' => true,
25+
);
26+
if ( null !== $script_module_id ) {
27+
$interactive_script_modules[ $script_module_id . '-js-module' ] = true;
28+
}
29+
return $interactive_script_modules;
30+
}
31+
32+
/**
33+
* Adds `data-wp-router-options` attribute to script modules registered as interactive.
34+
*
35+
* @param array<string, string|true>|mixed $attributes Script attributes.
36+
* @return array<string, string|true> Filtered script attributes.
37+
*/
38+
function gutenberg_script_module_add_router_options_attributes( $attributes ): array {
39+
if ( ! is_array( $attributes ) ) {
40+
return $attributes;
41+
}
42+
if ( isset( $attributes['id'] ) && array_key_exists( $attributes['id'], gutenberg_interactive_script_modules_registry() ) ) {
43+
$attributes['data-wp-router-options'] = wp_json_encode( array( 'loadOnClientNavigation' => true ) );
44+
}
45+
return $attributes;
46+
}
47+
add_filter( 'wp_script_attributes', 'gutenberg_script_module_add_router_options_attributes' );
48+
49+
function gutenberg_script_module_add_load_on_client_navigation_to_script_modules( $parsed_block ) {
50+
if ( ! isset( $parsed_block['blockName'] ) ) {
51+
return $parsed_block;
52+
}
53+
54+
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
55+
56+
$supports_interactivity = isset( $block_type->supports['interactivity'] );
57+
$supports_interactivity_array = $supports_interactivity && is_array( $block_type->supports['interactivity'] );
58+
$is_fully_interactive = $supports_interactivity && true === $block_type->supports['interactivity'];
59+
$is_supports_interactive = $supports_interactivity_array && isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive'];
60+
$is_supports_client_navigation = $supports_interactivity_array && isset( $block_type->supports['interactivity']['clientNavigation'] ) && true === $block_type->supports['interactivity']['clientNavigation'];
61+
62+
if ( $is_fully_interactive || ( $is_supports_interactive && $is_supports_client_navigation ) ) {
63+
foreach ( $block_type->view_script_module_ids as $script_module_id ) {
64+
gutenberg_interactive_script_modules_registry( $script_module_id );
65+
}
66+
}
67+
68+
return $parsed_block;
69+
}
70+
add_action( 'render_block_data', 'gutenberg_script_module_add_load_on_client_navigation_to_script_modules', 10, 1 );
71+
}

lib/compat/wordpress-6.9/script-modules.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

lib/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function gutenberg_is_experiment_enabled( $name ) {
8080
// WordPress 6.9 compat.
8181
require __DIR__ . '/compat/wordpress-6.9/customizer-preview-custom-css.php';
8282
require __DIR__ . '/compat/wordpress-6.9/command-palette.php';
83-
require __DIR__ . '/compat/wordpress-6.9/script-modules.php';
83+
require __DIR__ . '/compat/wordpress-6.9/client-assets.php';
8484

8585
// WordPress 7.0 compat.
8686
require __DIR__ . '/compat/wordpress-7.0/php-only-blocks.php';

packages/e2e-tests/plugins/interactive-blocks.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ function () {
1818
foreach ( $block_json_files as $block_json_file ) {
1919
register_block_type( $block_json_file );
2020
}
21-
22-
// Manually register script modules for navigation tests.
23-
$test_router_script_modules = array(
24-
'test-router-script-modules-alpha-view-script-module',
25-
'test-router-script-modules-bravo-view-script-module',
26-
'test-router-script-modules-charlie-view-script-module',
27-
'test-router-script-modules-wrapper-view-script-module',
28-
);
29-
30-
foreach ( $test_router_script_modules as $module_id ) {
31-
gutenberg_register_interactive_script_module_id( $module_id );
32-
}
3321
}
3422

3523
/*

0 commit comments

Comments
 (0)