Skip to content

Commit

Permalink
Use existing filter over new action
Browse files Browse the repository at this point in the history
This is really an action (side effect) but we
don't need to add a new action if we use
the filter
  • Loading branch information
sirreal committed Jan 4, 2024
1 parent 7bcb68a commit 58c66d8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 58c66d8

Please sign in to comment.