From 764148247951b14c3b98d53c01ff1fc6977f9c1f Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Wed, 4 Oct 2023 18:45:16 +0000 Subject: [PATCH] Editor: Fix loading of assets in blocks in child themes where the directory name starts with the parent theme's directory name. Props lgladdy, masteradhoc, audrasjb, rajinsharwar, azaozz. Merges [56527] to the 6.3 branch. Fixes #59018. git-svn-id: https://develop.svn.wordpress.org/branches/6.3@56776 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 8 +++---- tests/phpunit/tests/blocks/register.php | 30 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 347839f09a..50c6c6806d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -150,8 +150,8 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) { * Determine if the block script was registered in a theme, by checking if the script path starts with either * the parent (template) or child (stylesheet) directory path. */ - $is_parent_theme_block = str_starts_with( $script_path_norm, $template_path_norm ); - $is_child_theme_block = str_starts_with( $script_path_norm, $stylesheet_path_norm ); + $is_parent_theme_block = str_starts_with( $script_path_norm, trailingslashit( $template_path_norm ) ); + $is_child_theme_block = str_starts_with( $script_path_norm, trailingslashit( $stylesheet_path_norm ) ); $is_theme_block = ( $is_parent_theme_block || $is_child_theme_block ); $script_uri = plugins_url( $script_path, $metadata['file'] ); @@ -261,8 +261,8 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) { // Determine if the block style was registered in a theme, by checking if the script path starts with either // the parent (template) or child (stylesheet) directory path. - $is_parent_theme_block = str_starts_with( $style_path_norm, $template_path_norm ); - $is_child_theme_block = str_starts_with( $style_path_norm, $stylesheet_path_norm ); + $is_parent_theme_block = str_starts_with( $style_path_norm, trailingslashit( $template_path_norm ) ); + $is_child_theme_block = str_starts_with( $style_path_norm, trailingslashit( $stylesheet_path_norm ) ); $is_theme_block = ( $is_parent_theme_block || $is_child_theme_block ); if ( $is_core_block ) { diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index f3617f5fad..ceee6c0538 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -270,6 +270,21 @@ class Tests_Blocks_Register extends WP_UnitTestCase { $result = register_block_script_handle( $metadata, 'script' ); $this->assertSame( 'unit-tests-test-block-script', $result ); + + // Test the behavior directly within the unit test + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['script'] ) ), + trailingslashit( wp_normalize_path( get_template_directory() ) ) + ) === 0 + ); + + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['script'] ) ), + trailingslashit( wp_normalize_path( get_stylesheet_directory() ) ) + ) === 0 + ); } /** @@ -438,6 +453,21 @@ class Tests_Blocks_Register extends WP_UnitTestCase { wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ), wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) ) ); + + // Test the behavior directly within the unit test + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['style'] ) ), + trailingslashit( wp_normalize_path( get_template_directory() ) ) + ) === 0 + ); + + $this->assertFalse( + strpos( + wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['style'] ) ), + trailingslashit( wp_normalize_path( get_stylesheet_directory() ) ) + ) === 0 + ); } /**