Make WordPress Core


Ignore:
Timestamp:
07/18/2023 12:00:49 AM (20 months ago)
Author:
flixos90
Message:

Editor: Fix bug where it was not possible to style custom block elements in theme.json.

This changeset resolves a bug where WordPress would only allow HTML elements within core's own blocks to be styled in theme.json. Prior to this change, any theme.json rules applying to elements in custom blocks were ignored. With this fix it is now possible to style third-party block elements in theme.json.

Props flixos90, azaozz, costdev, glendaviesnz, spacedmonkey, oandregal.
Fixes #57868.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php

    r54703 r56254  
    140140    }
    141141
     142    /**
     143     * @ticket 57868
     144     */
     145    public function test_third_party_blocks_inline_styles_for_elements_get_rendered_when_per_block() {
     146        $this->set_up_third_party_block();
     147        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     148
     149        wp_register_style( 'global-styles', false, array(), true, true );
     150        wp_enqueue_style( 'global-styles' );
     151        wp_add_global_styles_for_blocks();
     152
     153        $actual = get_echo( 'wp_print_styles' );
     154
     155        $this->assertStringContainsString(
     156            '.wp-block-my-third-party-block cite{color: white;}',
     157            $actual
     158        );
     159    }
     160
     161    /**
     162     * @ticket 57868
     163     */
     164    public function test_third_party_blocks_inline_styles_for_elements_get_rendered() {
     165        wp_register_style( 'global-styles', false, array(), true, true );
     166        wp_enqueue_style( 'global-styles' );
     167        wp_add_global_styles_for_blocks();
     168
     169        $actual = get_echo( 'wp_print_styles' );
     170
     171        $this->assertStringContainsString(
     172            '.wp-block-my-third-party-block cite{color: white;}',
     173            $actual
     174        );
     175    }
     176
     177    /**
     178     * @ticket 57868
     179     *
     180     * @dataProvider data_wp_get_block_name_from_theme_json_path
     181     *
     182     * @param array  $path     An array of keys describing the path to a property in theme.json.
     183     * @param string $expected The expected block name.
     184     */
     185    public function test_wp_get_block_name_from_theme_json_path( $path, $expected ) {
     186        $block_name = wp_get_block_name_from_theme_json_path( $path );
     187        $this->assertSame( $expected, $block_name );
     188    }
     189
     190    /**
     191     * Data provider.
     192     *
     193     * @return array[]
     194     */
     195    public function data_wp_get_block_name_from_theme_json_path() {
     196        return array(
     197            'core block styles'             => array(
     198                array( 'styles', 'blocks', 'core/navigation' ),
     199                'core/navigation',
     200            ),
     201            'core block element styles'     => array(
     202                array( 'styles', 'blocks', 'core/navigation', 'elements', 'link' ),
     203                'core/navigation',
     204            ),
     205            'custom block styles'           => array(
     206                array( 'styles', 'blocks', 'my/third-party-block' ),
     207                'my/third-party-block',
     208            ),
     209            'custom block element styles'   => array(
     210                array( 'styles', 'blocks', 'my/third-party-block', 'elements', 'cite' ),
     211                'my/third-party-block',
     212            ),
     213            'custom block wrong format'     => array(
     214                array( 'styles', 'my/third-party-block' ),
     215                '',
     216            ),
     217            'invalid path but works for BC' => array(
     218                array( 'something', 'core/image' ),
     219                'core/image',
     220            ),
     221        );
     222    }
     223
    142224    private function set_up_third_party_block() {
    143225        switch_theme( 'block-theme' );
Note: See TracChangeset for help on using the changeset viewer.