Make WordPress Core

Changeset 56254


Ignore:
Timestamp:
07/18/2023 12:00:49 AM (3 years 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/global-styles-and-settings.php

    r56249 r56254  
    321321                // The likes of block element styles from theme.json do not have  $metadata['name'] set.
    322322                if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
    323                         $result = array_values(
    324                                 array_filter(
    325                                         $metadata['path'],
    326                                         static function ( $item ) {
    327                                                 if ( str_contains( $item, 'core/' ) ) {
    328                                                         return true;
    329                                                 }
    330                                                 return false;
    331                                         }
    332                                 )
    333                         );
    334                         if ( isset( $result[0] ) ) {
    335                                 if ( str_starts_with( $result[0], 'core/' ) ) {
    336                                         $block_name        = str_replace( 'core/', '', $result[0] );
     323                        $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );
     324                        if ( $block_name ) {
     325                                if ( str_starts_with( $block_name, 'core/' ) ) {
     326                                        $block_name        = str_replace( 'core/', '', $block_name );
    337327                                        $stylesheet_handle = 'wp-block-' . $block_name;
    338328                                }
    … …  
    341331                }
    342332        }
     333}
     334
     335/**
     336 * Gets the block name from a given theme.json path.
     337 *
     338 * @since 6.3.0
     339 * @access private
     340 *
     341 * @param array $path An array of keys describing the path to a property in theme.json.
     342 * @return string Identified block name, or empty string if none found.
     343 */
     344function wp_get_block_name_from_theme_json_path( $path ) {
     345        // Block name is expected to be the third item after 'styles' and 'blocks'.
     346        if (
     347                count( $path ) >= 3
     348                && 'styles' === $path[0]
     349                && 'blocks' === $path[1]
     350                && str_contains( $path[2], '/' )
     351        ) {
     352                return $path[2];
     353        }
     354
     355        /*
     356         * As fallback and for backward compatibility, allow any core block to be
     357         * at any position.
     358         */
     359        $result = array_values(
     360                array_filter(
     361                        $path,
     362                        static function ( $item ) {
     363                                if ( str_contains( $item, 'core/' ) ) {
     364                                        return true;
     365                                }
     366                                return false;
     367                        }
     368                )
     369        );
     370        if ( isset( $result[0] ) ) {
     371                return $result[0];
     372        }
     373        return '';
    343374}
    344375
  • trunk/tests/phpunit/data/themedir1/block-theme/theme.json

    r55231 r56254  
    7474                                "color": {
    7575                                        "background": "hotpink"
     76                                },
     77                                "elements": {
     78                                        "cite": {
     79                                                "color": {
     80                                                        "text": "white"
     81                                                }
     82                                        }
    7683                                }
    7784                        }
  • 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.