From 58c66d88003f94a6c570f3820d559a52270c1f8e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 28 Dec 2023 21:10:22 +0100 Subject: [PATCH] Use existing filter over new action This is really an action (side effect) but we don't need to add a new action if we use the filter --- .../modules/class-gutenberg-modules.php | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index c97835cee090e7..5b1f94d32e5391 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -275,7 +275,7 @@ function gutenberg_dequeue_module( $module_identifier ) { // Prints the script that loads the import map polyfill in the footer. add_action( 'wp_footer', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 ); -function gutenberg_filter_block_type_metadata_settings( $settings, $metadata = null ) { +function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { $module_fields = array( 'editorModule' => 'editor_module_handles', 'module' => 'module_handles', @@ -310,16 +310,27 @@ function gutenberg_filter_block_type_metadata_settings( $settings, $metadata = n return $settings; } -add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings', 10, 2 ); +add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 ); + +function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) { + $block_type = $block_instance->block_type; + + if ( ! empty( $block_type->module_handles ) ) { + foreach ( $block_type->module_handles as $module_id ) { + gutenberg_enqueue_module( $module_id ); + } + } -/* @todo This doesn't exist in core, we'll need to add it or find another way… */ -add_action( 'block_type_render_enqueue', function ( ?WP_Block_Type $block_type ) { if ( ! empty( $block_type->view_module_handles ) ) { - foreach ( $block_type->view_module_handles as $view_module_handle ) { - gutenberg_enqueue_module( $view_module_handle ); + foreach ( $block_type->view_module_handles as $module_id ) { + gutenberg_enqueue_module( $module_id ); } } -} ); + + return $block_content; +} + +add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_modules', 10, 3 ); /** * Finds a module ID for the selected block metadata field. It detects