From 3d43e57237e97c6421bda61774b91cf2b6fa2d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Mon, 21 Dec 2020 11:37:30 +0000 Subject: [PATCH] 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 --- src/wp-includes/blocks.php | 8 ++++++++ tests/phpunit/tests/blocks/register.php | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f6e023c67f..8c272c088a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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', diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 392f7e2977..e9e607d89a 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -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 */