Skip to content

Commit

Permalink
Blocks: Align with Gutenberg the name of generated asset handle for c…
Browse files Browse the repository at this point in the history
…ore blocks

Related Gutenberg PR: WordPress/gutenberg#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
  • Loading branch information
gziolo committed Dec 21, 2020
1 parent 91a6444 commit 3d43e57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ function test_generate_block_asset_handle() {
);
}

/**
* @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
*/
Expand Down

0 comments on commit 3d43e57

Please sign in to comment.