mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Editor: Fix block editor styles being registered with frontend stylesheets.
This changeset fixes a bug where WordPress core's own block editor styles (`wp-block-{$block_name}-editor`) were referencing the same CSS files as their frontend equivalents (`wp-block-{$block_name}`). This would result in incorrect frontend styles potentially being loaded in the block editor.
Tests for the related logic have been added.
Props flixos90, joemcgill, mukesh27, spacedmonkey.
Fixes #58605.
git-svn-id: https://develop.svn.wordpress.org/trunk@56005 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
15a9bcad35
commit
915db312ab
@ -219,7 +219,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
// Check whether styles should have a ".min" suffix or not.
|
||||
$suffix = SCRIPT_DEBUG ? '' : '.min';
|
||||
if ( $is_core_block ) {
|
||||
$style_path = "style$suffix.css";
|
||||
$style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
|
||||
}
|
||||
|
||||
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
|
||||
@ -239,7 +239,8 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
if ( $is_theme_block ) {
|
||||
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
|
||||
} elseif ( $is_core_block ) {
|
||||
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
|
||||
// All possible $style_path variants for core blocks are hard-coded above.
|
||||
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $style_path );
|
||||
}
|
||||
} else {
|
||||
$style_uri = false;
|
||||
|
||||
@ -322,6 +322,76 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58605
|
||||
*
|
||||
* @dataProvider data_register_block_style_handle_uses_correct_core_stylesheet
|
||||
*
|
||||
* @param string $block_json_path Path to the `block.json` file, relative to ABSPATH.
|
||||
* @param string $style_field Either 'style' or 'editorStyle'.
|
||||
* @param string|bool $expected_path Expected path of registered stylesheet, relative to ABSPATH.
|
||||
*/
|
||||
public function test_register_block_style_handle_uses_correct_core_stylesheet( $block_json_path, $style_field, $expected_path ) {
|
||||
$metadata_file = ABSPATH . $block_json_path;
|
||||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
|
||||
|
||||
$block_name = str_replace( 'core/', '', $metadata['name'] );
|
||||
|
||||
// Normalize metadata similar to `register_block_type_from_metadata()`.
|
||||
$metadata['file'] = wp_normalize_path( realpath( $metadata_file ) );
|
||||
if ( ! isset( $metadata['style'] ) ) {
|
||||
$metadata['style'] = "wp-block-$block_name";
|
||||
}
|
||||
if ( ! isset( $metadata['editorStyle'] ) ) {
|
||||
$metadata['editorStyle'] = "wp-block-{$block_name}-editor";
|
||||
}
|
||||
|
||||
// Ensure block assets are separately registered.
|
||||
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
|
||||
|
||||
/*
|
||||
* Account for minified asset path and ensure the file exists.
|
||||
* This may not be the case in the testing environment since it requires the build process to place them.
|
||||
*/
|
||||
if ( is_string( $expected_path ) ) {
|
||||
$expected_path = str_replace( '.css', wp_scripts_get_suffix() . '.css', $expected_path );
|
||||
self::touch( ABSPATH . $expected_path );
|
||||
}
|
||||
|
||||
$result = register_block_style_handle( $metadata, $style_field );
|
||||
$this->assertSame( $metadata[ $style_field ], $result, 'Core block registration failed' );
|
||||
if ( $expected_path ) {
|
||||
$this->assertStringEndsWith( $expected_path, wp_styles()->registered[ $result ]->src, 'Core block stylesheet path incorrect' );
|
||||
} else {
|
||||
$this->assertFalse( wp_styles()->registered[ $result ]->src, 'Core block stylesheet src should be false' );
|
||||
}
|
||||
}
|
||||
|
||||
public function data_register_block_style_handle_uses_correct_core_stylesheet() {
|
||||
return array(
|
||||
'block with style' => array(
|
||||
WPINC . '/blocks/archives/block.json',
|
||||
'style',
|
||||
WPINC . '/blocks/archives/style.css',
|
||||
),
|
||||
'block with editor style' => array(
|
||||
WPINC . '/blocks/archives/block.json',
|
||||
'editorStyle',
|
||||
WPINC . '/blocks/archives/editor.css',
|
||||
),
|
||||
'block without style' => array(
|
||||
WPINC . '/blocks/widget-group/block.json',
|
||||
'style',
|
||||
false,
|
||||
),
|
||||
'block without editor style' => array(
|
||||
WPINC . '/blocks/widget-group/block.json',
|
||||
'editorStyle',
|
||||
false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user