-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module
#72449
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
Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module
#72449
Changes from all commits
04f073d
d14f94b
ac4e28e
67db04c
ffc4cc4
4d07b05
5e78cd1
e519c02
1b164c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/10324 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/72340 | ||
| * https://github.com/WordPress/gutenberg/pull/72340 | ||
| * https://github.com/WordPress/gutenberg/pull/72449 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -686,79 +686,84 @@ function gutenberg_default_script_modules() { | |
| 'fetchpriority' => 'low', | ||
| ); | ||
|
|
||
| gutenberg_register_interactive_script_module_id( $script_module_id ); | ||
|
|
||
| $path = gutenberg_url( "build-module/{$file_name}" ); | ||
| 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>. | ||
| } | ||
| } | ||
| remove_action( 'wp_default_scripts', 'wp_default_script_modules' ); | ||
| add_action( 'wp_default_scripts', 'gutenberg_default_script_modules' ); | ||
|
|
||
| /* | ||
| * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. | ||
| * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes, | ||
| * which are in continuous development and generally ahead of Core. | ||
| */ | ||
| remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' ); | ||
| remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 ); | ||
| if ( version_compare( get_bloginfo( 'version' ), '6.9.0', '<' ) ) { | ||
| /** | ||
| * Access the shared static variable for interactive script modules. | ||
| * | ||
| * @param string|null $script_module_id The script module ID to register, or null to get the list. | ||
| * @return array<string, true> Associative array of script module ID => true. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to make this abundantly clear that this "script module ID" is the ID for the |
||
| */ | ||
| function gutenberg_interactive_script_modules_registry( $script_module_id = null ) { | ||
| static $interactive_script_modules = array( | ||
| '@wordpress/block-library/navigation/view-js-module' => true, | ||
| '@wordpress/block-library/query/view-js-module' => true, | ||
| '@wordpress/block-library/image/view-js-module' => true, | ||
| ); | ||
| if ( null !== $script_module_id ) { | ||
| $interactive_script_modules[ $script_module_id . '-js-module' ] = true; | ||
| } | ||
| return $interactive_script_modules; | ||
| } | ||
|
|
||
| // Enqueue stored styles. | ||
| add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' ); | ||
| add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 ); | ||
| /** | ||
| * Adds `data-wp-router-options` attribute to script modules registered as interactive. | ||
| * | ||
| * @param array<string, string|true>|mixed $attributes Script attributes. | ||
| * @return array<string, string|true> Filtered script attributes. | ||
| */ | ||
| function gutenberg_script_module_add_router_options_attributes( $attributes ): array { | ||
| if ( ! is_array( $attributes ) ) { | ||
| return $attributes; | ||
| } | ||
| if ( isset( $attributes['id'] ) && array_key_exists( $attributes['id'], gutenberg_interactive_script_modules_registry() ) ) { | ||
| $attributes['data-wp-router-options'] = wp_json_encode( array( 'loadOnClientNavigation' => true ) ); | ||
| } | ||
| return $attributes; | ||
| } | ||
| add_filter( 'wp_script_attributes', 'gutenberg_script_module_add_router_options_attributes' ); | ||
|
|
||
| /** | ||
| * Access the shared static variable for interactive script modules. | ||
| * | ||
| * @param string|null $script_module_id The script module ID to register, or null to get the list. | ||
| * @return array Associative array of script module ID => true. | ||
| */ | ||
| function gutenberg_interactive_script_modules_registry( $script_module_id = null ) { | ||
| static $interactive_script_modules = array(); | ||
| function gutenberg_script_module_add_load_on_client_navigation_to_script_modules( $parsed_block ) { | ||
| if ( ! isset( $parsed_block['blockName'] ) ) { | ||
| return $parsed_block; | ||
| } | ||
|
|
||
| if ( null !== $script_module_id ) { | ||
| $interactive_script_modules[ $script_module_id ] = true; | ||
| } | ||
| $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); | ||
|
|
||
| return $interactive_script_modules; | ||
| } | ||
| $supports_interactivity = isset( $block_type->supports['interactivity'] ); | ||
| $supports_interactivity_array = $supports_interactivity && is_array( $block_type->supports['interactivity'] ); | ||
| $is_fully_interactive = $supports_interactivity && true === $block_type->supports['interactivity']; | ||
| $is_supports_interactive = $supports_interactivity_array && isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive']; | ||
| $is_supports_client_navigation = $supports_interactivity_array && isset( $block_type->supports['interactivity']['clientNavigation'] ) && true === $block_type->supports['interactivity']['clientNavigation']; | ||
|
|
||
| /** | ||
| * Register a script module ID for interactive blocks. | ||
| * | ||
| * @param string $script_module_id The script module ID. | ||
| */ | ||
| function gutenberg_register_interactive_script_module_id( $script_module_id ) { | ||
| gutenberg_interactive_script_modules_registry( $script_module_id ); | ||
| } | ||
| if ( $is_fully_interactive || ( $is_supports_interactive && $is_supports_client_navigation ) ) { | ||
| foreach ( $block_type->view_script_module_ids as $script_module_id ) { | ||
| gutenberg_interactive_script_modules_registry( $script_module_id ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is adding the script to the static array inside of |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of interactive script module IDs. | ||
| * | ||
| * @return array Associative array of script module ID => true. | ||
| */ | ||
| function gutenberg_get_interactive_script_module_ids() { | ||
| return gutenberg_interactive_script_modules_registry(); | ||
| return $parsed_block; | ||
| } | ||
| add_action( 'render_block_data', 'gutenberg_script_module_add_load_on_client_navigation_to_script_modules', 10, 1 ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds `data-wp-router-options` attribute to script modules registered as interactive. | ||
| * | ||
| * @param array $args The script module attributes. | ||
| * @param string $id The script module ID. | ||
| * @return array Filtered script module attributes. | ||
| * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. | ||
| * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes, | ||
| * which are in continuous development and generally ahead of Core . | ||
| */ | ||
| function gutenberg_script_module_add_router_options_attributes( $args, $id ) { | ||
| // Check if this script module ID is registered as interactive. | ||
| $interactive_modules = gutenberg_get_interactive_script_module_ids(); | ||
|
|
||
| if ( isset( $interactive_modules[ $id ] ) ) { | ||
| $args['data-wp-router-options'] = '{ "loadOnClientNavigation": true }'; | ||
| } | ||
| return $args; | ||
| } | ||
| remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' ); | ||
| remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 ); | ||
|
|
||
| add_filter( 'wp_script_module_attributes', 'gutenberg_script_module_add_router_options_attributes', 10, 2 ); | ||
| // Enqueue stored styles. | ||
| add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' ); | ||
| add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 ); | ||
|
|
||
| add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_latex_to_mathml_loader' ); | ||
| function gutenberg_enqueue_latex_to_mathml_loader() { | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this file can actually be deleted entirely now?