Blocks: Align with Gutenberg the name of generated asset handle for core blocks

Related Gutenberg PR: https://github.com/WordPress/gutenberg/pull/25220.

It aligns with the latest changes added by aristath to the Gutenberg project. As part of styles splitting for core blocks, there was a special pattern introduced for how style handles are named. Ideally, we would apply it to all blocks but there might be some backward compatibility considerations so I left the handling for non-core blocks unchanged.

Props aristath.
See #50328.



git-svn-id: https://develop.svn.wordpress.org/trunk@49850 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski 2020-12-21 11:37:30 +00:00
parent 91a6444e50
commit 3d43e57237
2 changed files with 32 additions and 0 deletions

View File

@ -71,6 +71,14 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
* @return string Generated asset name for the block's field.
*/
function generate_block_asset_handle( $block_name, $field_name ) {
if ( 0 === strpos( $block_name, 'core/' ) ) {
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
if ( 0 === strpos( $field_name, 'editor' ) ) {
$asset_handle .= '-editor';
}
return $asset_handle;
}
$field_mappings = array(
'editorScript' => 'editor-script',
'script' => 'script',

View File

@ -144,6 +144,30 @@ class WP_Test_Block_Register extends WP_UnitTestCase {
);
}
/**
* @ticket 50328
*/
function test_generate_block_asset_handle_core_block() {
$block_name = 'core/paragraph';
$this->assertSame(
'wp-block-paragraph-editor',
generate_block_asset_handle( $block_name, 'editorScript' )
);
$this->assertSame(
'wp-block-paragraph',
generate_block_asset_handle( $block_name, 'script' )
);
$this->assertSame(
'wp-block-paragraph-editor',
generate_block_asset_handle( $block_name, 'editorStyle' )
);
$this->assertSame(
'wp-block-paragraph',
generate_block_asset_handle( $block_name, 'style' )
);
}
/**
* @ticket 50263
*/