-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
777a70a
Move redundant functionality into 6.5-compat
sirreal 2e274b0
fix alignment
sirreal 93ef50d
Move into existing conditional block
sirreal d2d38f5
Stop requiring asset file
sirreal cd12d21
Run "good" version first
sirreal 5380af8
move todo
sirreal f82922e
Compat changes for REST API
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
); | ||
foreach ( $module_fields as $metadata_field_name => $settings_field_name ) { | ||
if ( ! empty( $settings[ $metadata_field_name ] ) ) { | ||
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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', | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 handlesviewScriptModule
.