From 6e3c2755227be5039cc461dc46bf6e45dae7875a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 28 Dec 2023 20:59:05 +0100 Subject: [PATCH 01/20] Working implementation with caveats --- .../modules/class-gutenberg-modules.php | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 5aa012f8e2f2c5..5c5804fd7e3709 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -280,3 +280,161 @@ function gutenberg_dequeue_module( $module_identifier ) { // Prints the script that loads the import map polyfill in the footer. add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 ); + +function gutenberg_filter_block_type_metadata_settings( $settings, $metadata = null ) { + $module_fields = array( + 'editorModule' => 'editor_module_handles', + 'module' => 'module_handles', + 'viewModule' => 'view_module_handles', + ); + 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_handle( + $metadata, + $metadata_field_name, + $index + ); + } + } else { + $processed_modules[] = gutenberg_register_block_module_handle( + $metadata, + $metadata_field_name + ); + } + $settings[ $settings_field_name ] = $processed_modules; + } + } + + return $settings; +} + +add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings', 10, 2 ); + +/* @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 ); + } + } +} ); + +/** + * Finds a module ID for the selected block metadata field. It detects + * when a path to file was provided and finds a corresponding asset file + * with details necessary to register the module under an automatically + * generated module ID. + * + * @param array $metadata Block metadata. + * @param string $field_name Field name to pick from metadata. + * @param int $index Optional. Index of the script to register when multiple items passed. + * Default 0. + * @return string Module ID. + */ +function gutenberg_register_block_module_handle( $metadata, $field_name, $index = 0 ) { + if ( empty( $metadata[ $field_name ] ) ) { + return false; + } + + $module_id = $metadata[ $field_name ]; + if ( is_array( $module_id ) ) { + if ( empty( $module_id[ $index ] ) ) { + return false; + } + $module_id = $module_id[ $index ]; + } + + $module_path = remove_block_asset_path_prefix( $module_id ); + if ( $module_id === $module_path ) { + return $module_id; + } + + $path = dirname( $metadata['file'] ); + $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); + $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( + /* translators: 1: Asset file location, 2: Field name, 3: Block name. */ + __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ), + $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_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); + + gutenberg_register_module( + $module_id, + $module_uri, + $module_dependencies, + isset( $module_asset['version'] ) ? $module_asset['version'] : false, + ); + + // @todo This in_array may not work with module_dependencies. + // @todo @wordpress/i18n isn't module compatible anyways… + if ( ! empty( $metadata['textdomain'] ) && in_array( '@wordpress/i18n', $module_dependencies, true ) ) { + wp_set_script_translations( $module_id, $metadata['textdomain'] ); + } + + return $module_id; +} + +/** + * Generates the module ID for an asset based on the name of the block + * and the field name provided. + * + * @param string $block_name Name of the block. + * @param string $field_name Name of the metadata field. + * @param int $index Optional. Index of the asset when multiple items passed. + * Default 0. + * @return string Generated module ID for the block's field. + */ +function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $index = 0 ) { + if ( str_starts_with( $block_name, 'core/' ) ) { + $asset_handle = str_replace( 'core/', 'wp-block-', $block_name ); + if ( str_starts_with( $field_name, 'editor' ) ) { + $asset_handle .= '-editor'; + } + if ( str_starts_with( $field_name, 'view' ) ) { + $asset_handle .= '-view'; + } + if ( $index > 0 ) { + $asset_handle .= '-' . ( $index + 1 ); + } + return $asset_handle; + } + + $field_mappings = array( + 'editorModule' => 'editor-module', + 'module' => 'module', + 'viewModule' => 'view-module', + ); + $asset_handle = str_replace( '/', '-', $block_name ) . + '-' . $field_mappings[ $field_name ]; + if ( $index > 0 ) { + $asset_handle .= '-' . ( $index + 1 ); + } + return $asset_handle; +} From c74482efbc9595c8fa839fb96e1bbabdc5f986d3 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 28 Dec 2023 21:10:22 +0100 Subject: [PATCH 02/20] 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 5c5804fd7e3709..6494134ca2a3c8 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -281,7 +281,7 @@ function gutenberg_dequeue_module( $module_identifier ) { // Prints the script that loads the import map polyfill in the footer. add_action( 'wp_head', 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', @@ -316,16 +316,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 From d440ae952314fa89ac49af85da5a4cfe4bda64a8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 28 Dec 2023 21:13:43 +0100 Subject: [PATCH 03/20] Fix lints --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 6494134ca2a3c8..7fc2424ff2a2b4 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -400,7 +400,7 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index $module_id, $module_uri, $module_dependencies, - isset( $module_asset['version'] ) ? $module_asset['version'] : false, + isset( $module_asset['version'] ) ? $module_asset['version'] : false ); // @todo This in_array may not work with module_dependencies. From e54781c1e0b6708a6602d97ebd66f04db3ca331c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 29 Dec 2023 15:19:31 +0100 Subject: [PATCH 04/20] Reuse translation from core --- lib/experimental/modules/class-gutenberg-modules.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 7fc2424ff2a2b4..a717e97d592dfe 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -379,8 +379,8 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: Asset file location, 2: Field name, 3: Block name. */ - __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ), + // This is a translation from core. See `register_block_script_handle`jk. + __( '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'] From ec363f31b1e39e665a7ddb1232c1a1082e51015b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 29 Dec 2023 15:19:44 +0100 Subject: [PATCH 05/20] Fix lints --- lib/experimental/modules/class-gutenberg-modules.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index a717e97d592dfe..7496585a00995d 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -390,9 +390,8 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index return false; } - $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) ); - $module_uri = get_block_asset_url( $module_path_norm ); - + $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_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); From 4090db5c95cacf04130d9d014a80a5e13ac16ec8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 29 Dec 2023 15:19:57 +0100 Subject: [PATCH 06/20] Comment out useles i18n code --- lib/experimental/modules/class-gutenberg-modules.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 7496585a00995d..6fac0833d7813b 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -404,9 +404,9 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index // @todo This in_array may not work with module_dependencies. // @todo @wordpress/i18n isn't module compatible anyways… - if ( ! empty( $metadata['textdomain'] ) && in_array( '@wordpress/i18n', $module_dependencies, true ) ) { - wp_set_script_translations( $module_id, $metadata['textdomain'] ); - } + // if ( ! empty( $metadata['textdomain'] ) && in_array( '@wordpress/i18n', $module_dependencies, true ) ) { + // wp_set_script_translations( $module_id, $metadata['textdomain'] ); + // } return $module_id; } From b252ef17a725652c37558bc08d753515c25de608 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 16:55:46 +0100 Subject: [PATCH 07/20] Remove module, editorModule handling --- lib/experimental/modules/class-gutenberg-modules.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 6fac0833d7813b..2766e6d3385397 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -283,12 +283,9 @@ function gutenberg_dequeue_module( $module_identifier ) { function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { $module_fields = array( - 'editorModule' => 'editor_module_handles', - 'module' => 'module_handles', - 'viewModule' => 'view_module_handles', + 'viewModule' => 'view_module_handles', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { - if ( ! empty( $settings[ $metadata_field_name ] ) ) { $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; } @@ -321,12 +318,6 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin 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 ); - } - } - if ( ! empty( $block_type->view_module_handles ) ) { foreach ( $block_type->view_module_handles as $module_id ) { gutenberg_enqueue_module( $module_id ); From 3e7b171daf77e2097a9be32bda1c4696c4b32534 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 16:56:01 +0100 Subject: [PATCH 08/20] Add docblocks --- .../modules/class-gutenberg-modules.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 2766e6d3385397..28fb6da7761a98 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -281,6 +281,15 @@ function gutenberg_dequeue_module( $module_identifier ) { // Prints the script that loads the import map polyfill in the footer. add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 ); +/** + * 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( 'viewModule' => 'view_module_handles', @@ -315,6 +324,13 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin 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 $block The full block, including name and attributes. + * @param WP_Block $instance The block instance. + */ function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) { $block_type = $block_instance->block_type; From 4a4313d65d13d89e98226e5d90716248596dd869 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 16:57:09 +0100 Subject: [PATCH 09/20] Punctuation --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 28fb6da7761a98..5dfe89386ad742 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -282,7 +282,7 @@ function gutenberg_dequeue_module( $module_identifier ) { add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 ); /** - * Add module fields from block metadata to WP_Block_Type settings + * 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. From d0b816cc5279dcfb521121c2adf1700d855e35a7 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Fri, 5 Jan 2024 17:07:24 +0100 Subject: [PATCH 10/20] Fix VIM leak --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 5dfe89386ad742..c7bfbaa9fd2a3b 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -386,7 +386,7 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index _doing_it_wrong( __FUNCTION__, sprintf( - // This is a translation from core. See `register_block_script_handle`jk. + // This is a translation from core. See `register_block_script_handle`. __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.', 'default' ), $module_asset_raw_path, $field_name, From ba349e5b0b3a247773022b87006663fd449353b6 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 16:55:46 +0100 Subject: [PATCH 11/20] Remove module, editorModule handling --- lib/experimental/modules/class-gutenberg-modules.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index c7bfbaa9fd2a3b..95493ef09623f9 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -444,8 +444,6 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in } $field_mappings = array( - 'editorModule' => 'editor-module', - 'module' => 'module', 'viewModule' => 'view-module', ); $asset_handle = str_replace( '/', '-', $block_name ) . From a639b69c808362589ccecf9a3829347c0a6256de Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 17:28:56 +0100 Subject: [PATCH 12/20] Better docblocks --- lib/experimental/modules/class-gutenberg-modules.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 95493ef09623f9..3a052ac1db0e44 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -351,13 +351,15 @@ function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $pa * with details necessary to register the module under an automatically * generated module ID. * + * This is analogous to the `register_block_script_handle` in WordPress Core. + * * @param array $metadata Block metadata. * @param string $field_name Field name to pick from metadata. * @param int $index Optional. Index of the script to register when multiple items passed. * Default 0. * @return string Module ID. */ -function gutenberg_register_block_module_handle( $metadata, $field_name, $index = 0 ) { +function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } @@ -422,6 +424,8 @@ function gutenberg_register_block_module_handle( $metadata, $field_name, $index * Generates the module ID for an asset based on the name of the block * and the field name provided. * + * This is analogous to the `generate_block_asset_handle` in WordPress Core. + * * @param string $block_name Name of the block. * @param string $field_name Name of the metadata field. * @param int $index Optional. Index of the asset when multiple items passed. From b29d3b1d7889b5479f417255a359c831a483ebde Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 17:29:19 +0100 Subject: [PATCH 13/20] cleanup --- .../modules/class-gutenberg-modules.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 3a052ac1db0e44..245337b32ed7f5 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -380,15 +380,14 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 $path = dirname( $metadata['file'] ); $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); $module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index ); - $module_asset_path = wp_normalize_path( - realpath( $module_asset_raw_path ) - ); + $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) ); if ( empty( $module_asset_path ) ) { _doing_it_wrong( __FUNCTION__, sprintf( - // This is a translation from core. See `register_block_script_handle`. + // 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, @@ -411,12 +410,6 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 isset( $module_asset['version'] ) ? $module_asset['version'] : false ); - // @todo This in_array may not work with module_dependencies. - // @todo @wordpress/i18n isn't module compatible anyways… - // if ( ! empty( $metadata['textdomain'] ) && in_array( '@wordpress/i18n', $module_dependencies, true ) ) { - // wp_set_script_translations( $module_id, $metadata['textdomain'] ); - // } - return $module_id; } From 5b35851a64d1ef318eaf906f57779aeebf005b28 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 17:39:11 +0100 Subject: [PATCH 14/20] lint --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 245337b32ed7f5..bbfbce56f129dc 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -441,7 +441,7 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in } $field_mappings = array( - 'viewModule' => 'view-module', + 'viewModule' => 'view-module', ); $asset_handle = str_replace( '/', '-', $block_name ) . '-' . $field_mappings[ $field_name ]; From f38ce2aa0272a4e39ca7476049b81c255470caee Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Jan 2024 17:56:30 +0100 Subject: [PATCH 15/20] Module IDs not handles --- lib/experimental/modules/class-gutenberg-modules.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index bbfbce56f129dc..ca5fb7d9dbd793 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -292,7 +292,7 @@ function gutenberg_dequeue_module( $module_identifier ) { */ function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { $module_fields = array( - 'viewModule' => 'view_module_handles', + 'viewModule' => 'view_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { if ( ! empty( $settings[ $metadata_field_name ] ) ) { @@ -303,14 +303,14 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin $processed_modules = array(); if ( is_array( $modules ) ) { for ( $index = 0; $index < count( $modules ); $index++ ) { - $processed_modules[] = gutenberg_register_block_module_handle( + $processed_modules[] = gutenberg_register_block_module_id( $metadata, $metadata_field_name, $index ); } } else { - $processed_modules[] = gutenberg_register_block_module_handle( + $processed_modules[] = gutenberg_register_block_module_id( $metadata, $metadata_field_name ); @@ -334,8 +334,8 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) { $block_type = $block_instance->block_type; - if ( ! empty( $block_type->view_module_handles ) ) { - foreach ( $block_type->view_module_handles as $module_id ) { + if ( ! empty( $block_type->view_module_ids ) ) { + foreach ( $block_type->view_module_ids as $module_id ) { gutenberg_enqueue_module( $module_id ); } } From 41ee64c8e06e9d037e8664f0cd1a9ae91cb0630f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 8 Jan 2024 15:52:59 +0100 Subject: [PATCH 16/20] Add view_module_ids to block-type REST API --- .../modules/class-gutenberg-modules.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index ca5fb7d9dbd793..d7a4f4442a5d94 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -450,3 +450,21 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in } return $asset_handle; } + +function gutenberg_register_view_module_ids_rest_field() { + register_rest_field( + 'block-type', + 'view_module_ids', + array( + 'get_callback' => function ( $object ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $object['name'] ); + if ( isset( $block_type->view_module_ids ) ) { + return $block_type->view_module_ids; + } + return array(); + }, + ) + ); +} + +add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' ); From 02994e9f591d62869a1076a765de207bda649602 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 8 Jan 2024 16:57:36 +0100 Subject: [PATCH 17/20] Lint fix --- lib/experimental/modules/class-gutenberg-modules.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index d7a4f4442a5d94..134a82f81fc9b8 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -456,8 +456,8 @@ function gutenberg_register_view_module_ids_rest_field() { 'block-type', 'view_module_ids', array( - 'get_callback' => function ( $object ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $object['name'] ); + 'get_callback' => function ( $item ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); if ( isset( $block_type->view_module_ids ) ) { return $block_type->view_module_ids; } From 34b5b442ba1a3a9be04b025dc91b9461b8ed1ff9 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 9 Jan 2024 16:42:33 +0100 Subject: [PATCH 18/20] Switch from id to identifier --- .../modules/class-gutenberg-modules.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 134a82f81fc9b8..f4dc325dd6bbb1 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -292,7 +292,7 @@ function gutenberg_dequeue_module( $module_identifier ) { */ function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { $module_fields = array( - 'viewModule' => 'view_module_ids', + 'viewModule' => 'view_module_identifiers', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { if ( ! empty( $settings[ $metadata_field_name ] ) ) { @@ -303,14 +303,14 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin $processed_modules = array(); if ( is_array( $modules ) ) { for ( $index = 0; $index < count( $modules ); $index++ ) { - $processed_modules[] = gutenberg_register_block_module_id( + $processed_modules[] = gutenberg_register_block_module_identifier( $metadata, $metadata_field_name, $index ); } } else { - $processed_modules[] = gutenberg_register_block_module_id( + $processed_modules[] = gutenberg_register_block_module_identifier( $metadata, $metadata_field_name ); @@ -334,9 +334,9 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) { $block_type = $block_instance->block_type; - if ( ! empty( $block_type->view_module_ids ) ) { - foreach ( $block_type->view_module_ids as $module_id ) { - gutenberg_enqueue_module( $module_id ); + if ( ! empty( $block_type->view_module_identifiers ) ) { + foreach ( $block_type->view_module_identifiers as $module_identifier ) { + gutenberg_enqueue_module( $module_identifier ); } } @@ -359,27 +359,27 @@ function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $pa * Default 0. * @return string Module ID. */ -function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 ) { +function gutenberg_register_block_module_identifier( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } - $module_id = $metadata[ $field_name ]; - if ( is_array( $module_id ) ) { - if ( empty( $module_id[ $index ] ) ) { + $module_identifier = $metadata[ $field_name ]; + if ( is_array( $module_identifier ) ) { + if ( empty( $module_identifier[ $index ] ) ) { return false; } - $module_id = $module_id[ $index ]; + $module_identifier = $module_identifier[ $index ]; } - $module_path = remove_block_asset_path_prefix( $module_id ); - if ( $module_id === $module_path ) { - return $module_id; + $module_path = remove_block_asset_path_prefix( $module_identifier ); + if ( $module_identifier === $module_path ) { + return $module_identifier; } $path = dirname( $metadata['file'] ); $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); - $module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index ); + $module_identifier = gutenberg_generate_block_asset_module_identifier( $metadata['name'], $field_name, $index ); $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) ); if ( empty( $module_asset_path ) ) { @@ -404,13 +404,13 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); gutenberg_register_module( - $module_id, + $module_identifier, $module_uri, $module_dependencies, isset( $module_asset['version'] ) ? $module_asset['version'] : false ); - return $module_id; + return $module_identifier; } /** @@ -425,7 +425,7 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 * Default 0. * @return string Generated module ID for the block's field. */ -function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $index = 0 ) { +function gutenberg_generate_block_asset_module_identifier( $block_name, $field_name, $index = 0 ) { if ( str_starts_with( $block_name, 'core/' ) ) { $asset_handle = str_replace( 'core/', 'wp-block-', $block_name ); if ( str_starts_with( $field_name, 'editor' ) ) { @@ -451,15 +451,15 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in return $asset_handle; } -function gutenberg_register_view_module_ids_rest_field() { +function gutenberg_register_view_module_identifiers_rest_field() { register_rest_field( 'block-type', - 'view_module_ids', + 'view_module_identifiers', array( 'get_callback' => function ( $item ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); - if ( isset( $block_type->view_module_ids ) ) { - return $block_type->view_module_ids; + if ( isset( $block_type->view_module_identifiers ) ) { + return $block_type->view_module_identifiers; } return array(); }, @@ -467,4 +467,4 @@ function gutenberg_register_view_module_ids_rest_field() { ); } -add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' ); +add_action( 'rest_api_init', 'gutenberg_register_view_module_identifiers_rest_field' ); From 4cb4530508c8bd92a5ce2b49941d999889f8c77f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 9 Jan 2024 16:55:25 +0100 Subject: [PATCH 19/20] Revert "Switch from id to identifier" This reverts commit 34b5b442ba1a3a9be04b025dc91b9461b8ed1ff9. --- .../modules/class-gutenberg-modules.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index f4dc325dd6bbb1..134a82f81fc9b8 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -292,7 +292,7 @@ function gutenberg_dequeue_module( $module_identifier ) { */ function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) { $module_fields = array( - 'viewModule' => 'view_module_identifiers', + 'viewModule' => 'view_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { if ( ! empty( $settings[ $metadata_field_name ] ) ) { @@ -303,14 +303,14 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin $processed_modules = array(); if ( is_array( $modules ) ) { for ( $index = 0; $index < count( $modules ); $index++ ) { - $processed_modules[] = gutenberg_register_block_module_identifier( + $processed_modules[] = gutenberg_register_block_module_id( $metadata, $metadata_field_name, $index ); } } else { - $processed_modules[] = gutenberg_register_block_module_identifier( + $processed_modules[] = gutenberg_register_block_module_id( $metadata, $metadata_field_name ); @@ -334,9 +334,9 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) { $block_type = $block_instance->block_type; - if ( ! empty( $block_type->view_module_identifiers ) ) { - foreach ( $block_type->view_module_identifiers as $module_identifier ) { - gutenberg_enqueue_module( $module_identifier ); + if ( ! empty( $block_type->view_module_ids ) ) { + foreach ( $block_type->view_module_ids as $module_id ) { + gutenberg_enqueue_module( $module_id ); } } @@ -359,27 +359,27 @@ function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $pa * Default 0. * @return string Module ID. */ -function gutenberg_register_block_module_identifier( $metadata, $field_name, $index = 0 ) { +function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } - $module_identifier = $metadata[ $field_name ]; - if ( is_array( $module_identifier ) ) { - if ( empty( $module_identifier[ $index ] ) ) { + $module_id = $metadata[ $field_name ]; + if ( is_array( $module_id ) ) { + if ( empty( $module_id[ $index ] ) ) { return false; } - $module_identifier = $module_identifier[ $index ]; + $module_id = $module_id[ $index ]; } - $module_path = remove_block_asset_path_prefix( $module_identifier ); - if ( $module_identifier === $module_path ) { - return $module_identifier; + $module_path = remove_block_asset_path_prefix( $module_id ); + if ( $module_id === $module_path ) { + return $module_id; } $path = dirname( $metadata['file'] ); $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); - $module_identifier = gutenberg_generate_block_asset_module_identifier( $metadata['name'], $field_name, $index ); + $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 ) ) { @@ -404,13 +404,13 @@ function gutenberg_register_block_module_identifier( $metadata, $field_name, $in $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); gutenberg_register_module( - $module_identifier, + $module_id, $module_uri, $module_dependencies, isset( $module_asset['version'] ) ? $module_asset['version'] : false ); - return $module_identifier; + return $module_id; } /** @@ -425,7 +425,7 @@ function gutenberg_register_block_module_identifier( $metadata, $field_name, $in * Default 0. * @return string Generated module ID for the block's field. */ -function gutenberg_generate_block_asset_module_identifier( $block_name, $field_name, $index = 0 ) { +function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $index = 0 ) { if ( str_starts_with( $block_name, 'core/' ) ) { $asset_handle = str_replace( 'core/', 'wp-block-', $block_name ); if ( str_starts_with( $field_name, 'editor' ) ) { @@ -451,15 +451,15 @@ function gutenberg_generate_block_asset_module_identifier( $block_name, $field_n return $asset_handle; } -function gutenberg_register_view_module_identifiers_rest_field() { +function gutenberg_register_view_module_ids_rest_field() { register_rest_field( 'block-type', - 'view_module_identifiers', + 'view_module_ids', array( 'get_callback' => function ( $item ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); - if ( isset( $block_type->view_module_identifiers ) ) { - return $block_type->view_module_identifiers; + if ( isset( $block_type->view_module_ids ) ) { + return $block_type->view_module_ids; } return array(); }, @@ -467,4 +467,4 @@ function gutenberg_register_view_module_identifiers_rest_field() { ); } -add_action( 'rest_api_init', 'gutenberg_register_view_module_identifiers_rest_field' ); +add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' ); From 781b4685546203c2611c0ea22ae02f55bcaf4b37 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 9 Jan 2024 17:00:24 +0100 Subject: [PATCH 20/20] Fix lint --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 134a82f81fc9b8..f773e2c998c237 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -457,7 +457,7 @@ function gutenberg_register_view_module_ids_rest_field() { 'view_module_ids', array( 'get_callback' => function ( $item ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); if ( isset( $block_type->view_module_ids ) ) { return $block_type->view_module_ids; }