Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewScriptModule: 6.5 compatibility changes #58832

Merged
merged 7 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions lib/compat/wordpress-6.5/scripts-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,95 @@ function wp_script_modules(): WP_Script_Modules {
return $wp_script_modules;
}
wp_script_modules()->add_hooks();

/**
* Add module fields from block metadata to WP_Block_Type settings.
*
* This filter allows us to register modules from block metadata and attach additional fields to
* WP_Block_Type instances.
*
* @param array $settings Array of determined settings for registering a block type.
* @param array $metadata Metadata provided for registering a block type.
*/
function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) {
$module_fields = array(
'viewScriptModule' => 'view_script_module_ids',
);
foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
if ( ! empty( $settings[ $metadata_field_name ] ) ) {
$metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
}
if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
$modules = $metadata[ $metadata_field_name ];
$processed_modules = array();
if ( is_array( $modules ) ) {
for ( $index = 0; $index < count( $modules ); $index++ ) {
$processed_modules[] = gutenberg_register_block_module_id(
$metadata,
$metadata_field_name,
$index
);
}
} else {
$processed_modules[] = gutenberg_register_block_module_id(
$metadata,
$metadata_field_name
);
}
$settings[ $settings_field_name ] = $processed_modules;
}
}

return $settings;
}

add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 );

/**
* Enqueue modules associated with the block.
*
* @param string $block_content The block content.
* @param array $parsed_block The full block, including name and attributes.
* @param WP_Block $block_instance The block instance.
*/
function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) {
$block_type = $block_instance->block_type;

if ( ! empty( $block_type->view_script_module_ids ) ) {
foreach ( $block_type->view_script_module_ids as $module_id ) {
wp_enqueue_script_module( $module_id );
}
}

return $block_content;
}

add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 );

/**
* Registers a REST field for block types to provide view script module IDs.
*
* Adds the `view_script_module_ids` and `view_module_ids` (deprecated) field to block type objects in the REST API, which
* lists the script module IDs for any script modules associated with the
* block's viewScriptModule key.
*/
function gutenberg_register_view_script_module_ids_rest_field() {
register_rest_field(
'block-type',
'view_script_module_ids',
array(
'get_callback' => function ( $item ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] );
if ( isset( $block_type->view_script_module_ids ) ) {
return $block_type->view_script_module_ids;
}
return array();
},
)
);
}

add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' );
}

if ( ! function_exists( 'wp_register_script_module' ) ) {
Expand Down
64 changes: 6 additions & 58 deletions lib/experimental/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
* @param array $settings Array of determined settings for registering a block type.
* @param array $metadata Metadata provided for registering a block type.
*/
function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) {
function gutenberg_filter_block_type_metadata_settings_register_view_module( $settings, $metadata = null ) {
$module_fields = array(
// @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule).
'viewModule' => 'view_script_module_ids',
'viewScriptModule' => 'view_script_module_ids',
'viewModule' => 'view_script_module_ids',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles the deprecated viewModule field. Core >= 6.5 handles viewScriptModule.

);
foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
if ( ! empty( $settings[ $metadata_field_name ] ) ) {
Expand Down Expand Up @@ -42,28 +41,7 @@ function gutenberg_filter_block_type_metadata_settings_register_modules( $settin
return $settings;
}

add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 );

/**
* Enqueue modules associated with the block.
*
* @param string $block_content The block content.
* @param array $parsed_block The full block, including name and attributes.
* @param WP_Block $block_instance The block instance.
*/
function gutenberg_filter_render_block_enqueue_view_script_modules( $block_content, $parsed_block, $block_instance ) {
$block_type = $block_instance->block_type;

if ( ! empty( $block_type->view_script_module_ids ) ) {
foreach ( $block_type->view_script_module_ids as $module_id ) {
wp_enqueue_script_module( $module_id );
}
}

return $block_content;
}

add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 );
add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_view_module', 20, 2 );

/**
* Finds a module ID for the selected block metadata field. It detects
Expand Down Expand Up @@ -102,25 +80,9 @@ function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0
$module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index );
$module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) );

if ( empty( $module_asset_path ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
// This string is from WordPress Core. See `register_block_script_handle`.
// Translators: This is a translation from WordPress Core (default). No need to translate.
__( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.', 'default' ),
$module_asset_raw_path,
$field_name,
$metadata['name']
),
'6.5.0'
);
return false;
}
Comment on lines -105 to -119
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In WordPress trunk we no longer require the asset file to be present.


$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
$module_uri = get_block_asset_url( $module_path_norm );
$module_asset = require $module_asset_path;
$module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
$module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();

wp_register_script_module(
Expand Down Expand Up @@ -180,7 +142,7 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in
* lists the script module IDs for any script modules associated with the
* block's viewScriptModule key.
*/
function gutenberg_register_view_script_module_ids_rest_field() {
function gutenberg_register_view_module_ids_rest_field() {
// @todo remove view_module_ids support in Gutenberg >= 17.8 (replaced by view_script_module_ids).
register_rest_field(
'block-type',
Expand All @@ -195,23 +157,9 @@ function gutenberg_register_view_script_module_ids_rest_field() {
},
)
);

register_rest_field(
'block-type',
'view_script_module_ids',
array(
'get_callback' => function ( $item ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] );
if ( isset( $block_type->view_script_module_ids ) ) {
return $block_type->view_script_module_ids;
}
return array();
},
)
);
Comment on lines -199 to -211
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved to trunk or the compat file.

}

add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' );
add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' );

/**
* Registers the module if no module with that module identifier has already
Expand Down
Loading