From 777a70a2e6d6daa64f33d05ad0152897094bc98d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 12:43:34 +0100 Subject: [PATCH 1/7] Move redundant functionality into 6.5-compat --- lib/compat/wordpress-6.5/scripts-modules.php | 66 ++++++++++++++++++++ lib/experimental/script-modules.php | 29 ++------- 2 files changed, 70 insertions(+), 25 deletions(-) diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index 740a16581ce7f1..d2838211e964c7 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -118,3 +118,69 @@ function wp_dequeue_script_module( string $id ) { wp_script_modules()->dequeue( $id ); } } + +if ( ! function_exists( 'wp_script_modules' ) ) { + /** + * Add module fields from block metadata to WP_Block_Type settings. + * + * This filter allows us to register modules from block metadata and attach additional fields to + * WP_Block_Type instances. + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + */ + function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { + $module_fields = array( + 'viewScriptModule' => 'view_script_module_ids', + ); + foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { + if ( ! empty( $settings[ $metadata_field_name ] ) ) { + $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; + } + if ( ! empty( $metadata[ $metadata_field_name ] ) ) { + $modules = $metadata[ $metadata_field_name ]; + $processed_modules = array(); + if ( is_array( $modules ) ) { + for ( $index = 0; $index < count( $modules ); $index++ ) { + $processed_modules[] = gutenberg_register_block_module_id( + $metadata, + $metadata_field_name, + $index + ); + } + } else { + $processed_modules[] = gutenberg_register_block_module_id( + $metadata, + $metadata_field_name + ); + } + $settings[ $settings_field_name ] = $processed_modules; + } + } + + return $settings; + } + + add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 11, 2 ); + + /** + * Enqueue modules associated with the block. + * + * @param string $block_content The block content. + * @param array $parsed_block The full block, including name and attributes. + * @param WP_Block $block_instance The block instance. + */ + function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) { + $block_type = $block_instance->block_type; + + if ( ! empty( $block_type->view_script_module_ids ) ) { + foreach ( $block_type->view_script_module_ids as $module_id ) { + wp_enqueue_script_module( $module_id ); + } + } + + return $block_content; + } + + add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); +} diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index b24164d4d35feb..6650448ddeb73f 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -5,14 +5,14 @@ * This filter allows us to register modules from block metadata and attach additional fields to * WP_Block_Type instances. * + * @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule). + * * @param array $settings Array of determined settings for registering a block type. * @param array $metadata Metadata provided for registering a block type. */ -function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { +function gutenberg_filter_block_type_metadata_settings_register_view_module( $settings, $metadata = null ) { $module_fields = array( - // @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule). 'viewModule' => 'view_script_module_ids', - 'viewScriptModule' => 'view_script_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { if ( ! empty( $settings[ $metadata_field_name ] ) ) { @@ -42,28 +42,7 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin return $settings; } -add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 ); - -/** - * Enqueue modules associated with the block. - * - * @param string $block_content The block content. - * @param array $parsed_block The full block, including name and attributes. - * @param WP_Block $block_instance The block instance. - */ -function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) { - $block_type = $block_instance->block_type; - - if ( ! empty( $block_type->view_script_module_ids ) ) { - foreach ( $block_type->view_script_module_ids as $module_id ) { - wp_enqueue_script_module( $module_id ); - } - } - - return $block_content; -} - -add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); +add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_view_module', 10, 2 ); /** * Finds a module ID for the selected block metadata field. It detects From 2e274b01b07c9f2b834389882f3f08e162e268d9 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 13:17:43 +0100 Subject: [PATCH 2/7] fix alignment --- lib/experimental/script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 6650448ddeb73f..6881b1899489c2 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -12,7 +12,7 @@ */ function gutenberg_filter_block_type_metadata_settings_register_view_module( $settings, $metadata = null ) { $module_fields = array( - 'viewModule' => 'view_script_module_ids', + 'viewModule' => 'view_script_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { if ( ! empty( $settings[ $metadata_field_name ] ) ) { From 93ef50d80a39a98c17d84618e2d87eb2726d51b5 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 14:27:46 +0100 Subject: [PATCH 3/7] Move into existing conditional block --- lib/compat/wordpress-6.5/scripts-modules.php | 130 +++++++++---------- 1 file changed, 64 insertions(+), 66 deletions(-) diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index d2838211e964c7..e97ad66108f45c 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -28,6 +28,70 @@ function wp_script_modules(): WP_Script_Modules { return $wp_script_modules; } wp_script_modules()->add_hooks(); + + /** + * Add module fields from block metadata to WP_Block_Type settings. + * + * This filter allows us to register modules from block metadata and attach additional fields to + * WP_Block_Type instances. + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + */ + function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { + $module_fields = array( + 'viewScriptModule' => 'view_script_module_ids', + ); + foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { + if ( ! empty( $settings[ $metadata_field_name ] ) ) { + $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; + } + if ( ! empty( $metadata[ $metadata_field_name ] ) ) { + $modules = $metadata[ $metadata_field_name ]; + $processed_modules = array(); + if ( is_array( $modules ) ) { + for ( $index = 0; $index < count( $modules ); $index++ ) { + $processed_modules[] = gutenberg_register_block_module_id( + $metadata, + $metadata_field_name, + $index + ); + } + } else { + $processed_modules[] = gutenberg_register_block_module_id( + $metadata, + $metadata_field_name + ); + } + $settings[ $settings_field_name ] = $processed_modules; + } + } + + return $settings; + } + + add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 11, 2 ); + + /** + * Enqueue modules associated with the block. + * + * @param string $block_content The block content. + * @param array $parsed_block The full block, including name and attributes. + * @param WP_Block $block_instance The block instance. + */ + function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) { + $block_type = $block_instance->block_type; + + if ( ! empty( $block_type->view_script_module_ids ) ) { + foreach ( $block_type->view_script_module_ids as $module_id ) { + wp_enqueue_script_module( $module_id ); + } + } + + return $block_content; + } + + add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); } if ( ! function_exists( 'wp_register_script_module' ) ) { @@ -118,69 +182,3 @@ function wp_dequeue_script_module( string $id ) { wp_script_modules()->dequeue( $id ); } } - -if ( ! function_exists( 'wp_script_modules' ) ) { - /** - * Add module fields from block metadata to WP_Block_Type settings. - * - * This filter allows us to register modules from block metadata and attach additional fields to - * WP_Block_Type instances. - * - * @param array $settings Array of determined settings for registering a block type. - * @param array $metadata Metadata provided for registering a block type. - */ - function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { - $module_fields = array( - 'viewScriptModule' => 'view_script_module_ids', - ); - foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { - if ( ! empty( $settings[ $metadata_field_name ] ) ) { - $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; - } - if ( ! empty( $metadata[ $metadata_field_name ] ) ) { - $modules = $metadata[ $metadata_field_name ]; - $processed_modules = array(); - if ( is_array( $modules ) ) { - for ( $index = 0; $index < count( $modules ); $index++ ) { - $processed_modules[] = gutenberg_register_block_module_id( - $metadata, - $metadata_field_name, - $index - ); - } - } else { - $processed_modules[] = gutenberg_register_block_module_id( - $metadata, - $metadata_field_name - ); - } - $settings[ $settings_field_name ] = $processed_modules; - } - } - - return $settings; - } - - add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 11, 2 ); - - /** - * Enqueue modules associated with the block. - * - * @param string $block_content The block content. - * @param array $parsed_block The full block, including name and attributes. - * @param WP_Block $block_instance The block instance. - */ - function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) { - $block_type = $block_instance->block_type; - - if ( ! empty( $block_type->view_script_module_ids ) ) { - foreach ( $block_type->view_script_module_ids as $module_id ) { - wp_enqueue_script_module( $module_id ); - } - } - - return $block_content; - } - - add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); -} From d2d38f56003cea9c6bbbd41784fad83986740840 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 14:28:08 +0100 Subject: [PATCH 4/7] Stop requiring asset file --- lib/experimental/script-modules.php | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 6881b1899489c2..bc8773a460735d 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -81,25 +81,9 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 $module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index ); $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) ); - if ( empty( $module_asset_path ) ) { - _doing_it_wrong( - __FUNCTION__, - sprintf( - // This string is from WordPress Core. See `register_block_script_handle`. - // Translators: This is a translation from WordPress Core (default). No need to translate. - __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.', 'default' ), - $module_asset_raw_path, - $field_name, - $metadata['name'] - ), - '6.5.0' - ); - return false; - } - $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) ); $module_uri = get_block_asset_url( $module_path_norm ); - $module_asset = require $module_asset_path; + $module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array(); $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); wp_register_script_module( From cd12d217d73909de4cd039a855650b99127a3590 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 14:40:33 +0100 Subject: [PATCH 5/7] Run "good" version first --- lib/compat/wordpress-6.5/scripts-modules.php | 2 +- lib/experimental/script-modules.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index e97ad66108f45c..ecfb4155550091 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -70,7 +70,7 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin return $settings; } - add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 11, 2 ); + add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 ); /** * Enqueue modules associated with the block. diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index bc8773a460735d..4f43e39d48458f 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -42,7 +42,7 @@ function gutenberg_filter_block_type_metadata_settings_register_view_module( $se return $settings; } -add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_view_module', 10, 2 ); +add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_view_module', 20, 2 ); /** * Finds a module ID for the selected block metadata field. It detects From 5380af8dc70e9e439bd5d47d23e42f374682a551 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 14:42:46 +0100 Subject: [PATCH 6/7] move todo --- lib/experimental/script-modules.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 4f43e39d48458f..f5cba1914e3d6e 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -5,13 +5,12 @@ * This filter allows us to register modules from block metadata and attach additional fields to * WP_Block_Type instances. * - * @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule). - * * @param array $settings Array of determined settings for registering a block type. * @param array $metadata Metadata provided for registering a block type. */ function gutenberg_filter_block_type_metadata_settings_register_view_module( $settings, $metadata = null ) { $module_fields = array( + // @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule). 'viewModule' => 'view_script_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { From f82922e902d0f1fe87615a3d3c8d5a54e1747755 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 15:45:28 +0100 Subject: [PATCH 7/7] Compat changes for REST API --- lib/compat/wordpress-6.5/scripts-modules.php | 25 ++++++++++++++++++++ lib/experimental/script-modules.php | 18 ++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index ecfb4155550091..8fd8978d9e8f5d 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -92,6 +92,31 @@ function gutenberg_filter_render_block_enqueue_view_script_modules( $block_conte } add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); + + /** + * Registers a REST field for block types to provide view script module IDs. + * + * Adds the `view_script_module_ids` and `view_module_ids` (deprecated) field to block type objects in the REST API, which + * lists the script module IDs for any script modules associated with the + * block's viewScriptModule key. + */ + function gutenberg_register_view_script_module_ids_rest_field() { + register_rest_field( + 'block-type', + 'view_script_module_ids', + array( + 'get_callback' => function ( $item ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); + if ( isset( $block_type->view_script_module_ids ) ) { + return $block_type->view_script_module_ids; + } + return array(); + }, + ) + ); + } + + add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' ); } if ( ! function_exists( 'wp_register_script_module' ) ) { diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index f5cba1914e3d6e..e22b757c119501 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -142,7 +142,7 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in * lists the script module IDs for any script modules associated with the * block's viewScriptModule key. */ -function gutenberg_register_view_script_module_ids_rest_field() { +function gutenberg_register_view_module_ids_rest_field() { // @todo remove view_module_ids support in Gutenberg >= 17.8 (replaced by view_script_module_ids). register_rest_field( 'block-type', @@ -157,23 +157,9 @@ function gutenberg_register_view_script_module_ids_rest_field() { }, ) ); - - register_rest_field( - 'block-type', - 'view_script_module_ids', - array( - 'get_callback' => function ( $item ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); - if ( isset( $block_type->view_script_module_ids ) ) { - return $block_type->view_script_module_ids; - } - return array(); - }, - ) - ); } -add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' ); +add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' ); /** * Registers the module if no module with that module identifier has already