Make WordPress Core


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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.