From 6b1035a1f3323c1f8f5f184600f682b19e6417ae Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 27 Dec 2023 19:46:10 +0100 Subject: [PATCH 01/27] Handle scriptModule, module, viewModule --- src/wp-includes/blocks.php | 117 ++++++++++++++++++++++++ src/wp-includes/class-wp-block-type.php | 24 +++++ src/wp-includes/class-wp-block.php | 12 +++ 3 files changed, 153 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b7c2c2c433190..5f48c2bd42bcf 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -59,6 +59,9 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) { } $field_mappings = array( + 'editorModule' => 'editor-module', + 'module' => 'module', + 'viewModule' => 'view-module', 'editorScript' => 'editor-script', 'script' => 'script', 'viewScript' => 'view-script', @@ -122,6 +125,83 @@ function get_block_asset_url( $path ) { return plugins_url( basename( $path ), $path ); } +/** + * Finds a script handle for the selected block metadata field. It detects + * when a path to file was provided and optionally finds a corresponding asset + * file with details necessary to register the script under automatically + * generated handle name. It returns unprocessed script handle otherwise. + * + * @since 5.5.0 + * @since 6.1.0 Added `$index` parameter. + * @since 6.5.0 The asset file is optional. + * + * @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|false Script handle provided directly or created through + * script's registration, or false on failure. + */ +function register_block_module_handle( $metadata, $field_name, $index = 0 ) { + if ( empty( $metadata[ $field_name ] ) ) { + return false; + } + + $module_handle = $metadata[ $field_name ]; + if ( is_array( $module_handle ) ) { + if ( empty( $module_handle[ $index ] ) ) { + return false; + } + $module_handle = $module_handle[ $index ]; + } + + $module_path = remove_block_asset_path_prefix( $module_handle ); + if ( $module_handle === $module_path ) { + return $module_handle; + } + + $path = dirname( $metadata['file'] ); + $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); + $module_handle = generate_block_asset_handle( $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(); + $result = wp_register_module( + $module_handle, + $module_uri, + $module_dependencies, + isset( $module_asset['version'] ) ? $module_asset['version'] : false, + ); + + if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $module_dependencies, true ) ) { + // script translations? + wp_set_script_translations( $module_handle, $metadata['textdomain'] ); + } + + return $module_handle; +} + /** * Finds a script handle for the selected block metadata field. It detects * when a path to file was provided and optionally finds a corresponding asset @@ -490,6 +570,43 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + $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++ ) { + $result = register_block_module_handle( + $metadata, + $metadata_field_name, + $index + ); + if ( $result ) { + $processed_modules[] = $result; + } + } + } else { + $result = register_block_module_handle( + $metadata, + $metadata_field_name + ); + if ( $result ) { + $processed_modules[] = $result; + } + } + $settings[ $settings_field_name ] = $processed_modules; + } + } + $style_fields = array( 'editorStyle' => 'editor_style_handles', 'style' => 'style_handles', diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 33825a7888320..49a4f5f5eb803 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -226,6 +226,30 @@ class WP_Block_Type { */ public $view_script_handles = array(); + /** + * Block type editor only module handles. + * + * @since 6.5.0 + * @var string[] + */ + public $editor_module_handles = array(); + + /** + * Block type front end and editor module handles. + * + * @since 6.5.0 + * @var string[] + */ + public $module_handles = array(); + + /** + * Block type front end only module handles. + * + * @since 6.5.0 + * @var string[] + */ + public $view_module_handles = array(); + /** * Block type editor only style handles. * diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index a965ef13727e1..c31c9b29ecd20 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -470,6 +470,18 @@ public function render( $options = array() ) { } } + if ( ( ! empty( $this->block_type->module_handles ) ) ) { + foreach ( $this->block_type->module_handles as $module_handle ) { + wp_enqueue_module( $module_handle ); + } + } + + if ( ! empty( $this->block_type->view_module_handles ) ) { + foreach ( $this->block_type->view_module_handles as $view_module_handle ) { + wp_enqueue_module( $view_module_handle ); + } + } + if ( ( ! empty( $this->block_type->style_handles ) ) ) { foreach ( $this->block_type->style_handles as $style_handle ) { wp_enqueue_style( $style_handle ); From ad91ee923667afc5949bf457df3e9e5186dd560c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 11 Jan 2024 16:09:43 +0100 Subject: [PATCH 02/27] Remove editorModule, module support --- src/wp-includes/blocks.php | 4 ---- src/wp-includes/class-wp-block-type.php | 16 ---------------- src/wp-includes/class-wp-block.php | 6 ------ 3 files changed, 26 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 5f48c2bd42bcf..6fdd81a8fc580 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -59,8 +59,6 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) { } $field_mappings = array( - 'editorModule' => 'editor-module', - 'module' => 'module', 'viewModule' => 'view-module', 'editorScript' => 'editor-script', 'script' => 'script', @@ -571,8 +569,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } $module_fields = array( - 'editorModule' => 'editor_module_handles', - 'module' => 'module_handles', 'viewModule' => 'view_module_handles', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 49a4f5f5eb803..ceccf9df62f4c 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -226,22 +226,6 @@ class WP_Block_Type { */ public $view_script_handles = array(); - /** - * Block type editor only module handles. - * - * @since 6.5.0 - * @var string[] - */ - public $editor_module_handles = array(); - - /** - * Block type front end and editor module handles. - * - * @since 6.5.0 - * @var string[] - */ - public $module_handles = array(); - /** * Block type front end only module handles. * diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index c31c9b29ecd20..8b10f489362cb 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -470,12 +470,6 @@ public function render( $options = array() ) { } } - if ( ( ! empty( $this->block_type->module_handles ) ) ) { - foreach ( $this->block_type->module_handles as $module_handle ) { - wp_enqueue_module( $module_handle ); - } - } - if ( ! empty( $this->block_type->view_module_handles ) ) { foreach ( $this->block_type->view_module_handles as $view_module_handle ) { wp_enqueue_module( $view_module_handle ); From f94fe1638836b9a706b1494f4480d0a2e418fdcb Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 11 Jan 2024 16:32:16 +0100 Subject: [PATCH 03/27] Use module_id(s) instead of _handle(s) --- src/wp-includes/blocks.php | 30 ++++++++++++------------- src/wp-includes/class-wp-block-type.php | 4 ++-- src/wp-includes/class-wp-block.php | 6 ++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 6fdd81a8fc580..b57b1e6b3ce35 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -140,27 +140,27 @@ function get_block_asset_url( $path ) { * @return string|false Script handle provided directly or created through * script's registration, or false on failure. */ -function register_block_module_handle( $metadata, $field_name, $index = 0 ) { +function register_block_module_id( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } - $module_handle = $metadata[ $field_name ]; - if ( is_array( $module_handle ) ) { - if ( empty( $module_handle[ $index ] ) ) { + $module_id = $metadata[ $field_name ]; + if ( is_array( $module_id ) ) { + if ( empty( $module_id[ $index ] ) ) { return false; } - $module_handle = $module_handle[ $index ]; + $module_id = $module_id[ $index ]; } - $module_path = remove_block_asset_path_prefix( $module_handle ); - if ( $module_handle === $module_path ) { - return $module_handle; + $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_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index ); + $module_id = generate_block_asset_handle( $metadata['name'], $field_name, $index ); $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) ); @@ -186,7 +186,7 @@ function register_block_module_handle( $metadata, $field_name, $index = 0 ) { $module_asset = require $module_asset_path; $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); $result = wp_register_module( - $module_handle, + $module_id, $module_uri, $module_dependencies, isset( $module_asset['version'] ) ? $module_asset['version'] : false, @@ -194,10 +194,10 @@ function register_block_module_handle( $metadata, $field_name, $index = 0 ) { if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $module_dependencies, true ) ) { // script translations? - wp_set_script_translations( $module_handle, $metadata['textdomain'] ); + wp_set_script_translations( $module_id, $metadata['textdomain'] ); } - return $module_handle; + return $module_id; } /** @@ -569,7 +569,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } $module_fields = array( - 'viewModule' => 'view_module_handles', + 'viewModule' => 'view_module_ids', ); foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { @@ -581,7 +581,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $processed_modules = array(); if ( is_array( $modules ) ) { for ( $index = 0; $index < count( $modules ); $index++ ) { - $result = register_block_module_handle( + $result = register_block_module_id( $metadata, $metadata_field_name, $index @@ -591,7 +591,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } } else { - $result = register_block_module_handle( + $result = register_block_module_id( $metadata, $metadata_field_name ); diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index ceccf9df62f4c..7c0b7e1a1cf31 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -227,12 +227,12 @@ class WP_Block_Type { public $view_script_handles = array(); /** - * Block type front end only module handles. + * Block type front end only module IDs. * * @since 6.5.0 * @var string[] */ - public $view_module_handles = array(); + public $view_module_ids = array(); /** * Block type editor only style handles. diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 8b10f489362cb..779052083efbb 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -470,9 +470,9 @@ public function render( $options = array() ) { } } - if ( ! empty( $this->block_type->view_module_handles ) ) { - foreach ( $this->block_type->view_module_handles as $view_module_handle ) { - wp_enqueue_module( $view_module_handle ); + if ( ! empty( $this->block_type->view_module_ids ) ) { + foreach ( $this->block_type->view_module_ids as $view_module_id ) { + wp_enqueue_module( $view_module_id ); } } From 51f8671cd8ec7e8a130d74d8102f4e6664c622e7 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 13:45:53 +0100 Subject: [PATCH 04/27] Update for changes to module function names --- src/wp-includes/blocks.php | 2 +- src/wp-includes/class-wp-block.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b57b1e6b3ce35..87c37b26ac364 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -185,7 +185,7 @@ function register_block_module_id( $metadata, $field_name, $index = 0 ) { $module_asset = require $module_asset_path; $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); - $result = wp_register_module( + $result = wp_register_script_module( $module_id, $module_uri, $module_dependencies, diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 779052083efbb..d3101677cc5e1 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -472,7 +472,7 @@ public function render( $options = array() ) { if ( ! empty( $this->block_type->view_module_ids ) ) { foreach ( $this->block_type->view_module_ids as $view_module_id ) { - wp_enqueue_module( $view_module_id ); + wp_enqueue_script_module( $view_module_id ); } } From 0be47441184be773f3416c67a56ba0578e6b5032 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 15:00:22 +0100 Subject: [PATCH 05/27] Update function names and docblocks --- src/wp-includes/blocks.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 87c37b26ac364..d69ecacf5c9b9 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -36,6 +36,7 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) { * * @since 5.5.0 * @since 6.1.0 Added `$index` parameter. + * @since 6.5.0 Add support for `viewModule` field. * * @param string $block_name Name of the block. * @param string $field_name Name of the metadata field. @@ -124,23 +125,20 @@ function get_block_asset_url( $path ) { } /** - * Finds a script handle for the selected block metadata field. It detects - * when a path to file was provided and optionally finds a corresponding asset - * file with details necessary to register the script under automatically + * Finds a script 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 script module under an automatically * generated handle name. It returns unprocessed script handle otherwise. * - * @since 5.5.0 - * @since 6.1.0 Added `$index` parameter. - * @since 6.5.0 The asset file is optional. + * @since 6.5.0 * * @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|false Script handle provided directly or created through - * script's registration, or false on failure. + * @return string|false Script module ID or false on failure. */ -function register_block_module_id( $metadata, $field_name, $index = 0 ) { +function register_block_script_module_id( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } @@ -392,7 +390,7 @@ function get_block_metadata_i18n_schema() { * @since 6.1.0 Added support for `render` field. * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. - * @since 6.5.0 Added support for `allowedBlocks` and `viewStyle` fields. + * @since 6.5.0 Added support for `allowedBlocks`, `viewModule`, and `viewStyle` fields. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -581,7 +579,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $processed_modules = array(); if ( is_array( $modules ) ) { for ( $index = 0; $index < count( $modules ); $index++ ) { - $result = register_block_module_id( + $result = register_block_script_module_id( $metadata, $metadata_field_name, $index @@ -591,7 +589,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } } else { - $result = register_block_module_id( + $result = register_block_script_module_id( $metadata, $metadata_field_name ); From 0f7283d3076f6987e4623e29f3ced4e5a7e3648f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 15:08:15 +0100 Subject: [PATCH 06/27] Fix some "script" references --- src/wp-includes/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d69ecacf5c9b9..0cc09b1df1e5b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -128,14 +128,14 @@ function get_block_asset_url( $path ) { * Finds a script 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 script module under an automatically - * generated handle name. It returns unprocessed script handle otherwise. + * generated handle name. It returns unprocessed script module ID otherwise. * * @since 6.5.0 * * @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. + * @param int $index Optional. Index of the script module ID to register when multiple + * items passed. Default 0. * @return string|false Script module ID or false on failure. */ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) { From 8e4f1a797c963024e390e488decf0e1e975de9ff Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 15:09:56 +0100 Subject: [PATCH 07/27] Clean up some dead code and lints --- src/wp-includes/blocks.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 0cc09b1df1e5b..2de19c6f01f87 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -183,18 +183,14 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) { $module_asset = require $module_asset_path; $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); - $result = wp_register_script_module( + + wp_register_script_module( $module_id, $module_uri, $module_dependencies, - isset( $module_asset['version'] ) ? $module_asset['version'] : false, + isset( $module_asset['version'] ) ? $module_asset['version'] : false ); - if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $module_dependencies, true ) ) { - // script translations? - wp_set_script_translations( $module_id, $metadata['textdomain'] ); - } - return $module_id; } From 3e51c6fc756b45fbf344c86c1f555948013cdb35 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 16:16:52 +0100 Subject: [PATCH 08/27] Rename to `viewScriptModule` --- src/wp-includes/blocks.php | 13 ++++++------- src/wp-includes/class-wp-block-type.php | 4 ++-- src/wp-includes/class-wp-block.php | 6 +++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 2de19c6f01f87..e97ec10ad119f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -36,7 +36,7 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) { * * @since 5.5.0 * @since 6.1.0 Added `$index` parameter. - * @since 6.5.0 Add support for `viewModule` field. + * @since 6.5.0 Add support for `viewScriptModule` field. * * @param string $block_name Name of the block. * @param string $field_name Name of the metadata field. @@ -60,12 +60,12 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) { } $field_mappings = array( - 'viewModule' => 'view-module', 'editorScript' => 'editor-script', - 'script' => 'script', - 'viewScript' => 'view-script', 'editorStyle' => 'editor-style', + 'script' => 'script', 'style' => 'style', + 'viewScript' => 'view-script', + 'viewScriptModule' => 'view-script-module', 'viewStyle' => 'view-style', ); $asset_handle = str_replace( '/', '-', $block_name ) . @@ -386,7 +386,7 @@ function get_block_metadata_i18n_schema() { * @since 6.1.0 Added support for `render` field. * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. - * @since 6.5.0 Added support for `allowedBlocks`, `viewModule`, and `viewStyle` fields. + * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -563,10 +563,9 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } $module_fields = array( - 'viewModule' => 'view_module_ids', + '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 ]; } diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 7c0b7e1a1cf31..63496d41029e3 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -227,12 +227,12 @@ class WP_Block_Type { public $view_script_handles = array(); /** - * Block type front end only module IDs. + * Block type front end only script module IDs. * * @since 6.5.0 * @var string[] */ - public $view_module_ids = array(); + public $view_script_module_ids = array(); /** * Block type editor only style handles. diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index d3101677cc5e1..4369223f42959 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -470,9 +470,9 @@ public function render( $options = array() ) { } } - if ( ! empty( $this->block_type->view_module_ids ) ) { - foreach ( $this->block_type->view_module_ids as $view_module_id ) { - wp_enqueue_script_module( $view_module_id ); + if ( ! empty( $this->block_type->view_script_module_ids ) ) { + foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { + wp_enqueue_script_module( $view_script_module_id ); } } From fb36f1f33758579225db4661528dedf4ad38b85d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 17:30:10 +0100 Subject: [PATCH 09/27] Add to view_script_module_ids to block type REST API --- .../class-wp-rest-block-types-controller.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 7bf2f6af4af2e..75903a1ffd160 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -291,6 +291,7 @@ public function prepare_item_for_response( $item, $request ) { 'editor_script_handles', 'script_handles', 'view_script_handles', + 'view_script_modules_ids', 'editor_style_handles', 'style_handles', 'view_style_handles', @@ -584,6 +585,16 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'view_script_module_ids' => array( + 'description' => __( 'Public facing script module IDs.' ), + 'type' => array( 'array' ), + 'default' => array(), + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'editor_style_handles' => array( 'description' => __( 'Editor style handles.' ), 'type' => array( 'array' ), From 8df5bc058d2740f0ac88bc6c07499b086afbbe72 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Jan 2024 17:46:15 +0100 Subject: [PATCH 10/27] Add tests --- tests/phpunit/data/blocks/notice/block.json | 1 + tests/phpunit/tests/blocks/register.php | 102 ++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index 3e198145bdb49..7ccaef2d1312d 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -68,6 +68,7 @@ "editorScript": "tests-notice-editor-script", "script": "tests-notice-script", "viewScript": [ "tests-notice-view-script", "tests-notice-view-script-2" ], + "viewScriptModule": [ "tests-notice-view-script-module", "tests-notice-view-script-module-2" ], "editorStyle": "tests-notice-editor-style", "style": [ "tests-notice-style", "tests-notice-style-2" ], "viewStyle": [ "tests-notice-view-style" ], diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 3ee56bc9e5470..961c52455f943 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -138,6 +138,7 @@ public function test_removes_block_asset_path_prefix_and_current_directory() { /** * @ticket 50263 + * @ticket 60233 */ public function test_generate_block_asset_handle() { $block_name = 'unit-tests/my-block'; @@ -154,6 +155,18 @@ public function test_generate_block_asset_handle() { 'unit-tests-my-block-view-script-100', generate_block_asset_handle( $block_name, 'viewScript', 99 ) ); + $this->assertSame( + 'unit-tests-my-block-view-script-module', + generate_block_asset_handle( $block_name, 'viewScriptModule' ) + ); + $this->assertSame( + 'unit-tests-my-block-view-script-module-2', + generate_block_asset_handle( $block_name, 'viewScriptModule', 1 ) + ); + $this->assertSame( + 'unit-tests-my-block-view-script-module-100', + generate_block_asset_handle( $block_name, 'viewScriptModule', 99 ) + ); $this->assertSame( 'unit-tests-my-block-editor-style-2', generate_block_asset_handle( $block_name, 'editorStyle', 1 ) @@ -172,6 +185,7 @@ public function test_generate_block_asset_handle() { /** * @ticket 50328 + * @ticket 60233 */ public function test_generate_block_asset_handle_core_block() { $block_name = 'core/paragraph'; @@ -188,6 +202,18 @@ public function test_generate_block_asset_handle_core_block() { 'wp-block-paragraph-view-100', generate_block_asset_handle( $block_name, 'viewScript', 99 ) ); + $this->assertSame( + 'unit-tests-my-block-view-script-module', + generate_block_asset_handle( $block_name, 'viewScriptModule' ) + ); + $this->assertSame( + 'unit-tests-my-block-view-script-module-2', + generate_block_asset_handle( $block_name, 'viewScriptModule', 1 ) + ); + $this->assertSame( + 'unit-tests-my-block-view-script-module-100', + generate_block_asset_handle( $block_name, 'viewScriptModule', 99 ) + ); $this->assertSame( 'wp-block-paragraph-editor-2', generate_block_asset_handle( $block_name, 'editorStyle', 1 ) @@ -231,6 +257,77 @@ public function test_wrong_array_index_do_not_register_block_script_handle() { $this->assertFalse( $result ); } + /** + * @expectedIncorrectUsage register_block_script_module_id + * @ticket 60233 + */ + public function test_missing_asset_file_register_block_script_module_id() { + $metadata = array( + 'file' => __FILE__, + 'name' => 'unit-tests/test-block', + 'viewScriptModule' => 'file:./blocks/notice/missing-asset.js', + ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); + + $this->assertFalse( $result ); + } + + /** + * @ticket 60233 + */ + public function test_handle_passed_register_block_script_module_id() { + $metadata = array( + 'viewScriptModule' => 'test-id', + ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); + + $this->assertSame( 'test-script-handle', $result ); + } + + /** + * @ticket 60233 + */ + public function test_handles_passed_register_block_script_module_ids() { + $metadata = array( + 'script' => array( 'test-id', 'test-id-1' ), + ); + + $result = register_block_script_handle( $metadata, 'viewScriptModule' ); + $this->assertSame( 'test-id', $result ); + + $result = register_block_script_handle( $metadata, 'viewScriptModule', 1 ); + $this->assertSame( 'test-id-2', $result, 1 ); + } + + /** + * @ticket 60233 + */ + public function test_success_register_block_script_module_id() { + $metadata = array( + 'file' => DIR_TESTDATA . '/blocks/notice/block.json', + 'name' => 'unit-tests/test-block', + 'viewScriptModule' => 'file:./block.js', + ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); + + $this->assertSame( 'tests-notice-view-script-module', $result ); + + // Test the behavior directly within the unit test + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['viewScriptModule'] ) ), + trailingslashit( wp_normalize_path( get_template_directory() ) ) + ) === 0 + ); + + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['viewScriptModule'] ) ), + trailingslashit( wp_normalize_path( get_stylesheet_directory() ) ) + ) === 0 + ); + } + /** * @ticket 50263 */ @@ -751,6 +848,7 @@ public function data_register_block_registers_with_args_override_returns_false_w * @ticket 50328 * @ticket 57585 * @ticket 59797 + * @ticket 60233 */ public function test_block_registers_with_metadata_fixture() { $result = register_block_type_from_metadata( @@ -853,6 +951,10 @@ public function test_block_registers_with_metadata_fixture() { array( 'tests-notice-view-script', 'tests-notice-view-script-2' ), $result->view_script_handles ); + $this->assertSameSets( + array( 'tests-notice-view-script-module', 'tests-notice-view-script-module-2' ), + $result->view_script_module_ids + ); $this->assertSameSets( array( 'tests-notice-editor-style' ), $result->editor_style_handles From da148adc9606560b337b4d187dede59fada159cd Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 14:03:56 +0100 Subject: [PATCH 11/27] Test fixes --- tests/phpunit/tests/blocks/register.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 961c52455f943..4827b801e94c8 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -289,14 +289,14 @@ public function test_handle_passed_register_block_script_module_id() { */ public function test_handles_passed_register_block_script_module_ids() { $metadata = array( - 'script' => array( 'test-id', 'test-id-1' ), + 'script' => array( 'test-id', 'test-id-other' ), ); - $result = register_block_script_handle( $metadata, 'viewScriptModule' ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); $this->assertSame( 'test-id', $result ); - $result = register_block_script_handle( $metadata, 'viewScriptModule', 1 ); - $this->assertSame( 'test-id-2', $result, 1 ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule', 1 ); + $this->assertSame( 'test-id-other', $result ); } /** @@ -342,14 +342,14 @@ public function test_handle_passed_register_block_script_handle() { public function test_handles_passed_register_block_script_handles() { $metadata = array( - 'script' => array( 'test-script-handle', 'test-script-handle-2' ), + 'script' => array( 'test-script-handle', 'test-script-handle-other' ), ); $result = register_block_script_handle( $metadata, 'script' ); $this->assertSame( 'test-script-handle', $result ); $result = register_block_script_handle( $metadata, 'script', 1 ); - $this->assertSame( 'test-script-handle-2', $result, 1 ); + $this->assertSame( 'test-script-handle-other', $result ); } /** From 84d84988dddf72a41cad2a31e38661f4bad49718 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 14:06:58 +0100 Subject: [PATCH 12/27] lints --- tests/phpunit/tests/blocks/register.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 4827b801e94c8..1adb07969eee9 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -263,8 +263,8 @@ public function test_wrong_array_index_do_not_register_block_script_handle() { */ public function test_missing_asset_file_register_block_script_module_id() { $metadata = array( - 'file' => __FILE__, - 'name' => 'unit-tests/test-block', + 'file' => __FILE__, + 'name' => 'unit-tests/test-block', 'viewScriptModule' => 'file:./blocks/notice/missing-asset.js', ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); @@ -304,8 +304,8 @@ public function test_handles_passed_register_block_script_module_ids() { */ public function test_success_register_block_script_module_id() { $metadata = array( - 'file' => DIR_TESTDATA . '/blocks/notice/block.json', - 'name' => 'unit-tests/test-block', + 'file' => DIR_TESTDATA . '/blocks/notice/block.json', + 'name' => 'unit-tests/test-block', 'viewScriptModule' => 'file:./block.js', ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); From 9705feb10f927bd389b5540afa0e2dc45140ed3e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 14:11:06 +0100 Subject: [PATCH 13/27] Add more tests --- tests/phpunit/tests/blocks/register.php | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1adb07969eee9..3969aeb94b20b 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -257,6 +257,45 @@ public function test_wrong_array_index_do_not_register_block_script_handle() { $this->assertFalse( $result ); } + /** + * @ticket 60233 + */ + public function test_field_not_found_register_block_script_module_id() { + $result = register_block_script_module_id( array(), 'viewScriptModule' ); + + $this->assertFalse( $result ); + } + + /** + * @ticket 60233 + */ + public function test_empty_string_value_do_not_register_block_script_module_id() { + $metadata = array( 'viewScriptHandle' => '' ); + $result = register_block_script_module_id( $metadata, 'viewScriptHandle' ); + + $this->assertFalse( $result ); + } + + /** + * @ticket 60233 + */ + public function test_empty_array_value_do_not_register_block_script_module_id() { + $metadata = array( 'viewScriptModule' => array() ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); + + $this->assertFalse( $result ); + } + + /** + * @ticket 60233 + */ + public function test_wrong_array_index_do_not_register_block_script_module_id() { + $metadata = array( 'viewScriptModule' => array( 'test-module_id' ) ); + $result = register_block_script_module_id( $metadata, 'script', 1 ); + + $this->assertFalse( $result ); + } + /** * @expectedIncorrectUsage register_block_script_module_id * @ticket 60233 From 876a507cee7ad7ebe8741337b392213ef1540476 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 14:16:32 +0100 Subject: [PATCH 14/27] REST tests --- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 6e314e6af4ab7..bdf1eec08b51d 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -261,6 +261,7 @@ public function test_get_item_invalid() { $this->assertSameSets( array(), $data['editor_script_handles'] ); $this->assertSameSets( array(), $data['script_handles'] ); $this->assertSameSets( array(), $data['view_script_handles'] ); + $this->assertSameSets( array(), $data['view_script_module_ids'] ); $this->assertSameSets( array(), $data['editor_style_handles'] ); $this->assertSameSets( array(), $data['style_handles'] ); $this->assertFalse( $data['is_dynamic'] ); @@ -339,6 +340,7 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['editor_script_handles'] ); $this->assertSameSets( array(), $data['script_handles'] ); $this->assertSameSets( array(), $data['view_script_handles'] ); + $this->assertSameSets( array(), $data['view_script_module_ids'] ); $this->assertSameSets( array(), $data['editor_style_handles'] ); $this->assertSameSets( array(), $data['style_handles'] ); $this->assertFalse( $data['is_dynamic'] ); @@ -586,6 +588,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'editor_script_handles', $properties ); $this->assertArrayHasKey( 'script_handles', $properties ); $this->assertArrayHasKey( 'view_script_handles', $properties ); + $this->assertArrayHasKey( 'view_script_module_ids', $properties ); $this->assertArrayHasKey( 'editor_style_handles', $properties ); $this->assertArrayHasKey( 'style_handles', $properties ); $this->assertArrayHasKey( 'view_style_handles', $properties, 'schema must contain view_style_handles' ); @@ -718,6 +721,7 @@ protected function check_block_type_object( $block_type, $data, $links ) { 'editor_script_handles', 'script_handles', 'view_script_handles', + 'view_script_module_ids', 'editor_style_handles', 'style_handles', // Deprecated fields. From 3712584e95dec729e7eb755a029d75d3b768dc9a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 14:34:09 +0100 Subject: [PATCH 15/27] Fix typo in REST --- .../rest-api/endpoints/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 75903a1ffd160..e6f69a3bd2e65 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -291,7 +291,7 @@ public function prepare_item_for_response( $item, $request ) { 'editor_script_handles', 'script_handles', 'view_script_handles', - 'view_script_modules_ids', + 'view_script_module_ids', 'editor_style_handles', 'style_handles', 'view_style_handles', From b9af0e99d6fb7c8b365eba1d189edadaf077088f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 15:50:43 +0100 Subject: [PATCH 16/27] Fix tests --- tests/phpunit/tests/blocks/register.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 3969aeb94b20b..afeab5edaf4fb 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -203,15 +203,15 @@ public function test_generate_block_asset_handle_core_block() { generate_block_asset_handle( $block_name, 'viewScript', 99 ) ); $this->assertSame( - 'unit-tests-my-block-view-script-module', + 'wp-block-paragraph-view-script-module', generate_block_asset_handle( $block_name, 'viewScriptModule' ) ); $this->assertSame( - 'unit-tests-my-block-view-script-module-2', + 'wp-block-paragraph-view-script-module-2', generate_block_asset_handle( $block_name, 'viewScriptModule', 1 ) ); $this->assertSame( - 'unit-tests-my-block-view-script-module-100', + 'wp-block-paragraph-view-script-module-100', generate_block_asset_handle( $block_name, 'viewScriptModule', 99 ) ); $this->assertSame( @@ -316,11 +316,11 @@ public function test_missing_asset_file_register_block_script_module_id() { */ public function test_handle_passed_register_block_script_module_id() { $metadata = array( - 'viewScriptModule' => 'test-id', + 'viewScriptModule' => 'test-script-module-id', ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); - $this->assertSame( 'test-script-handle', $result ); + $this->assertSame( 'test-script-module-id', $result ); } /** @@ -328,7 +328,7 @@ public function test_handle_passed_register_block_script_module_id() { */ public function test_handles_passed_register_block_script_module_ids() { $metadata = array( - 'script' => array( 'test-id', 'test-id-other' ), + 'viewScriptModule' => array( 'test-id', 'test-id-other' ), ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); @@ -349,7 +349,7 @@ public function test_success_register_block_script_module_id() { ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); - $this->assertSame( 'tests-notice-view-script-module', $result ); + $this->assertSame( 'unit-tests-test-block-view-script-module', $result ); // Test the behavior directly within the unit test $this->assertFalse( From 29edd408df7b9cc37db457e06fdb2f08f2efa76f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 15:51:08 +0100 Subject: [PATCH 17/27] Core viewScriptModule should be suffixed with `-script-module` --- src/wp-includes/blocks.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index e97ec10ad119f..53d77d464e540 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -53,6 +53,9 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) { if ( str_starts_with( $field_name, 'view' ) ) { $asset_handle .= '-view'; } + if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) { + $asset_handle .= '-script-module'; + } if ( $index > 0 ) { $asset_handle .= '-' . ( $index + 1 ); } From 9ea7f4e7959a2dcfeea58a6d3540d11e8406551f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 15:57:45 +0100 Subject: [PATCH 18/27] Split and expand core block handle module tests --- tests/phpunit/tests/blocks/register.php | 43 ++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index afeab5edaf4fb..20101404ab928 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -185,7 +185,6 @@ public function test_generate_block_asset_handle() { /** * @ticket 50328 - * @ticket 60233 */ public function test_generate_block_asset_handle_core_block() { $block_name = 'core/paragraph'; @@ -202,6 +201,35 @@ public function test_generate_block_asset_handle_core_block() { 'wp-block-paragraph-view-100', generate_block_asset_handle( $block_name, 'viewScript', 99 ) ); + $this->assertSame( + 'wp-block-paragraph-editor-2', + generate_block_asset_handle( $block_name, 'editorStyle', 1 ) + ); + $this->assertSame( + 'wp-block-paragraph', + generate_block_asset_handle( $block_name, 'style' ) + ); + } + + /** + * @ticket 60233 + */ + public function test_generate_block_asset_handle_core_block_module() { + $block_name = 'core/paragraph'; + + $this->assertSame( + 'wp-block-paragraph-editor-script-module', + generate_block_asset_handle( $block_name, 'editorScriptModule' ) + ); + $this->assertSame( + 'wp-block-paragraph-editor-script-module-2', + generate_block_asset_handle( $block_name, 'editorScriptModule', 1 ) + ); + $this->assertSame( + 'wp-block-paragraph-editor-script-module-100', + generate_block_asset_handle( $block_name, 'editorScriptModule', 99 ) + ); + $this->assertSame( 'wp-block-paragraph-view-script-module', generate_block_asset_handle( $block_name, 'viewScriptModule' ) @@ -214,13 +242,18 @@ public function test_generate_block_asset_handle_core_block() { 'wp-block-paragraph-view-script-module-100', generate_block_asset_handle( $block_name, 'viewScriptModule', 99 ) ); + $this->assertSame( - 'wp-block-paragraph-editor-2', - generate_block_asset_handle( $block_name, 'editorStyle', 1 ) + 'wp-block-paragraph-script-module', + generate_block_asset_handle( $block_name, 'scriptModule' ) ); $this->assertSame( - 'wp-block-paragraph', - generate_block_asset_handle( $block_name, 'style' ) + 'wp-block-paragraph-script-module-2', + generate_block_asset_handle( $block_name, 'scriptModule', 1 ) + ); + $this->assertSame( + 'wp-block-paragraph-script-module-100', + generate_block_asset_handle( $block_name, 'scriptModule', 99 ) ); } From a960f65239ab58a2cacf56169e82ebdab5d5fe7f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 16:07:38 +0100 Subject: [PATCH 19/27] Fix since docblock description --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 53d77d464e540..b0a6aca2a8d6e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -36,7 +36,7 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) { * * @since 5.5.0 * @since 6.1.0 Added `$index` parameter. - * @since 6.5.0 Add support for `viewScriptModule` field. + * @since 6.5.0 Added support for `viewScriptModule` field. * * @param string $block_name Name of the block. * @param string $field_name Name of the metadata field. From 1a1f4e30605b8a368c577837a32e06cb6f06c9cc Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Jan 2024 16:09:06 +0100 Subject: [PATCH 20/27] Add since docblock to REST response --- .../rest-api/endpoints/class-wp-rest-block-types-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index e6f69a3bd2e65..9577ebe8008e7 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -240,6 +240,7 @@ public function get_item( $request ) { * @since 5.5.0 * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support. * @since 6.3.0 Added `selectors` field. + * @since 6.5.0 Added `view_script_module_ids` field. * * @param WP_Block_Type $item Block type data. * @param WP_REST_Request $request Full details about the request. From 00fbf399c9f233ca0dae8ac56d0e5eb7a9293de1 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 5 Feb 2024 13:25:35 +0100 Subject: [PATCH 21/27] Update REST property count in tests (again) --- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index bdf1eec08b51d..6ae8954abf1be 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -564,7 +564,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 32, $properties ); + $this->assertCount( 33, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'title', $properties ); From 9da386d8250b689baaf27f8afdf00cf516a4548e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 7 Feb 2024 12:21:12 +0100 Subject: [PATCH 22/27] Fix alignment --- src/wp-includes/blocks.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b0a6aca2a8d6e..b5658ac0b8827 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -63,13 +63,13 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) { } $field_mappings = array( - 'editorScript' => 'editor-script', - 'editorStyle' => 'editor-style', - 'script' => 'script', - 'style' => 'style', - 'viewScript' => 'view-script', + 'editorScript' => 'editor-script', + 'editorStyle' => 'editor-style', + 'script' => 'script', + 'style' => 'style', + 'viewScript' => 'view-script', 'viewScriptModule' => 'view-script-module', - 'viewStyle' => 'view-style', + 'viewStyle' => 'view-style', ); $asset_handle = str_replace( '/', '-', $block_name ) . '-' . $field_mappings[ $field_name ]; From 2d3a37ffabc3b01e50416927ee5a019b3137e001 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 7 Feb 2024 16:26:49 +0100 Subject: [PATCH 23/27] Make module asset file optional See #5799 / https://core.trac.wordpress.org/ticket/60460 --- src/wp-includes/blocks.php | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b5658ac0b8827..9c32f8e166aa3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -166,25 +166,10 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) { 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_asset = @include $module_asset_path; $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array(); wp_register_script_module( From 21120749d9fb89663b1a9941206d55aa9a71179a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 7 Feb 2024 16:30:04 +0100 Subject: [PATCH 24/27] Fix typo in test --- tests/phpunit/tests/blocks/register.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 20101404ab928..8a0d4658e7cde 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -303,8 +303,8 @@ public function test_field_not_found_register_block_script_module_id() { * @ticket 60233 */ public function test_empty_string_value_do_not_register_block_script_module_id() { - $metadata = array( 'viewScriptHandle' => '' ); - $result = register_block_script_module_id( $metadata, 'viewScriptHandle' ); + $metadata = array( 'viewScriptModule' => '' ); + $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); $this->assertFalse( $result ); } From fc338e613102a0b408c9519013698e5a44bdacea Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 7 Feb 2024 16:32:11 +0100 Subject: [PATCH 25/27] Update test for no-asset-file case --- tests/phpunit/tests/blocks/register.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 8a0d4658e7cde..7ac046f147955 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -330,7 +330,6 @@ public function test_wrong_array_index_do_not_register_block_script_module_id() } /** - * @expectedIncorrectUsage register_block_script_module_id * @ticket 60233 */ public function test_missing_asset_file_register_block_script_module_id() { @@ -341,7 +340,7 @@ public function test_missing_asset_file_register_block_script_module_id() { ); $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); - $this->assertFalse( $result ); + $this->assertSame( 'unit-tests-test-block-view-script-module', $result ); } /** From a225da18d498b76688ae7f96752db6a9fb0c4133 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 10:32:51 +0100 Subject: [PATCH 26/27] Require module asset file only if it exists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9c32f8e166aa3..eec6645cef328 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -169,7 +169,7 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) { $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) ); $module_uri = get_block_asset_url( $module_path_norm ); - $module_asset = @include $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 d2a5c7151df987ddb344d696e969038831f16a8e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 11:02:41 +0100 Subject: [PATCH 27/27] Update documentation for module registration --- src/wp-includes/blocks.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index eec6645cef328..47620d7f808fc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -129,9 +129,10 @@ function get_block_asset_url( $path ) { /** * Finds a script 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 script module under an automatically - * generated handle name. It returns unprocessed script module ID otherwise. + * when a path to file was provided and optionally finds a corresponding asset + * file with details necessary to register the script module under with an + * automatically generated module ID. It returns unprocessed script module + * ID otherwise. * * @since 6.5.0 *