Make WordPress Core


Ignore:
Timestamp:
06/23/2023 06:59:53 PM (22 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/register.php

    r55673 r56005  
    324324
    325325    /**
     326     * @ticket 58605
     327     *
     328     * @dataProvider data_register_block_style_handle_uses_correct_core_stylesheet
     329     *
     330     * @param string      $block_json_path Path to the `block.json` file, relative to ABSPATH.
     331     * @param string      $style_field     Either 'style' or 'editorStyle'.
     332     * @param string|bool $expected_path   Expected path of registered stylesheet, relative to ABSPATH.
     333     */
     334    public function test_register_block_style_handle_uses_correct_core_stylesheet( $block_json_path, $style_field, $expected_path ) {
     335        $metadata_file = ABSPATH . $block_json_path;
     336        $metadata      = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
     337
     338        $block_name = str_replace( 'core/', '', $metadata['name'] );
     339
     340        // Normalize metadata similar to `register_block_type_from_metadata()`.
     341        $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) );
     342        if ( ! isset( $metadata['style'] ) ) {
     343            $metadata['style'] = "wp-block-$block_name";
     344        }
     345        if ( ! isset( $metadata['editorStyle'] ) ) {
     346            $metadata['editorStyle'] = "wp-block-{$block_name}-editor";
     347        }
     348
     349        // Ensure block assets are separately registered.
     350        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     351
     352        /*
     353         * Account for minified asset path and ensure the file exists.
     354         * This may not be the case in the testing environment since it requires the build process to place them.
     355         */
     356        if ( is_string( $expected_path ) ) {
     357            $expected_path = str_replace( '.css', wp_scripts_get_suffix() . '.css', $expected_path );
     358            self::touch( ABSPATH . $expected_path );
     359        }
     360
     361        $result = register_block_style_handle( $metadata, $style_field );
     362        $this->assertSame( $metadata[ $style_field ], $result, 'Core block registration failed' );
     363        if ( $expected_path ) {
     364            $this->assertStringEndsWith( $expected_path, wp_styles()->registered[ $result ]->src, 'Core block stylesheet path incorrect' );
     365        } else {
     366            $this->assertFalse( wp_styles()->registered[ $result ]->src, 'Core block stylesheet src should be false' );
     367        }
     368    }
     369
     370    public function data_register_block_style_handle_uses_correct_core_stylesheet() {
     371        return array(
     372            'block with style'           => array(
     373                WPINC . '/blocks/archives/block.json',
     374                'style',
     375                WPINC . '/blocks/archives/style.css',
     376            ),
     377            'block with editor style'    => array(
     378                WPINC . '/blocks/archives/block.json',
     379                'editorStyle',
     380                WPINC . '/blocks/archives/editor.css',
     381            ),
     382            'block without style'        => array(
     383                WPINC . '/blocks/widget-group/block.json',
     384                'style',
     385                false,
     386            ),
     387            'block without editor style' => array(
     388                WPINC . '/blocks/widget-group/block.json',
     389                'editorStyle',
     390                false,
     391            ),
     392        );
     393    }
     394
     395    /**
    326396     * @ticket 50263
    327397     */
Note: See TracChangeset for help on using the changeset viewer.