diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index cc1ac60667773..04f99e4e2bf46 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -370,7 +370,16 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) { if ( is_rtl() && file_exists( $rtl_file ) ) { wp_style_add_data( $style_handle_name, 'rtl', 'replace' ); - wp_style_add_data( $style_handle_name, 'suffix', $suffix ); + /* + * Core block files use a '.min' suffix (e.g. style.min.css) when + * SCRIPT_DEBUG is false, so the suffix must be stored so that + * WP_Styles::do_item() can construct the correct RTL URL via str_replace. + * Non-core block files registered from block.json never use a '.min' + * suffix, so storing a non-empty suffix would cause str_replace() to + * search for '.min.css' in a filename that only contains '.css', + * silently failing to rewrite the URL to its RTL counterpart. + */ + wp_style_add_data( $style_handle_name, 'suffix', $is_core_block ? $suffix : '' ); wp_style_add_data( $style_handle_name, 'path', $rtl_file ); } } diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index d2a836ba85e68..48608ade25657 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -843,6 +843,7 @@ public function test_success_register_block_style_handle() { * * @ticket 56325 * @ticket 56797 + * @ticket 61625 * * @covers ::register_block_style_handle */ @@ -890,6 +891,44 @@ public function test_register_block_style_handle_should_load_rtl_stylesheets_for ); } + /** + * Tests that register_block_style_handle() always stores an empty suffix for + * non-core blocks, so that WP_Styles::do_item() can correctly rewrite the + * stylesheet URL to its RTL counterpart. + * + * Non-core block files registered via block.json never use a '.min' suffix, + * so storing a non-empty suffix causes str_replace() to search for '.min.css' + * in a filename that only contains '.css', silently leaving the LTR URL + * unchanged on RTL sites. + * + * @ticket 61625 + * + * @covers ::register_block_style_handle + */ + public function test_register_block_style_handle_rtl_suffix_is_empty_for_non_core_blocks() { + global $wp_locale; + + $metadata = array( + 'file' => DIR_TESTDATA . '/blocks/notice/block.json', + 'name' => 'tests/test-block-rtl-suffix', + 'style' => 'file:./block.css', + ); + + $orig_text_dir = $wp_locale->text_direction; + $wp_locale->text_direction = 'rtl'; + + register_block_style_handle( $metadata, 'style' ); + $extra_suffix = wp_styles()->get_data( 'tests-test-block-rtl-suffix-style', 'suffix' ); + + $wp_locale->text_direction = $orig_text_dir; + + $this->assertSame( + '', + $extra_suffix, + 'Non-core block RTL styles must have an empty suffix so the LTR URL is correctly rewritten to the RTL URL, regardless of SCRIPT_DEBUG.' + ); + } + /** * @ticket 56664 */