Skip to content

Commit

Permalink
Theme Json: Don't output double selectors for elements inside blocks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed May 9, 2022
1 parent 1397b15 commit 709aabc
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/compat/wordpress-5.9/class-wp-theme-json-5-9.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ protected static function get_blocks_metadata() {
foreach ( static::ELEMENTS as $el_name => $el_selector ) {
$element_selector = array();
foreach ( $block_selectors as $selector ) {
if ( $selector === $el_selector ) {
$element_selector = array( $el_selector );
break;
}

$element_selector[] = $selector . ' ' . $el_selector;
}
static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @access private
*/
class WP_Theme_JSON_Gutenberg extends WP_Theme_JSON_5_9 {
class WP_Theme_JSON_6_0 extends WP_Theme_JSON_5_9 {
/**
* Metadata for style properties.
*
Expand Down
90 changes: 90 additions & 0 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* WP_Theme_JSON_Gutenberg class
*
* @package gutenberg
*/

/**
* Class that encapsulates the processing of structures that adhere to the theme.json spec.
*
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
* This is a low-level API that may need to do breaking changes. Please,
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
*
* @access private
*/
class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
/**
* Returns the metadata for each block.
*
* Example:
*
* {
* 'core/paragraph': {
* 'selector': 'p',
* 'elements': {
* 'link' => 'link selector',
* 'etc' => 'element selector'
* }
* },
* 'core/heading': {
* 'selector': 'h1',
* 'elements': {}
* },
* 'core/image': {
* 'selector': '.wp-block-image',
* 'duotone': 'img',
* 'elements': {}
* }
* }
*
* @return array Block metadata.
*/
protected static function get_blocks_metadata() {
if ( null !== static::$blocks_metadata ) {
return static::$blocks_metadata;
}

static::$blocks_metadata = array();

$registry = WP_Block_Type_Registry::get_instance();
$blocks = $registry->get_all_registered();
foreach ( $blocks as $block_name => $block_type ) {
if (
isset( $block_type->supports['__experimentalSelector'] ) &&
is_string( $block_type->supports['__experimentalSelector'] )
) {
static::$blocks_metadata[ $block_name ]['selector'] = $block_type->supports['__experimentalSelector'];
} else {
static::$blocks_metadata[ $block_name ]['selector'] = '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) );
}

if (
isset( $block_type->supports['color']['__experimentalDuotone'] ) &&
is_string( $block_type->supports['color']['__experimentalDuotone'] )
) {
static::$blocks_metadata[ $block_name ]['duotone'] = $block_type->supports['color']['__experimentalDuotone'];
}

// Assign defaults, then overwrite those that the block sets by itself.
// If the block selector is compounded, will append the element to each
// individual block selector.
$block_selectors = explode( ',', static::$blocks_metadata[ $block_name ]['selector'] );
foreach ( static::ELEMENTS as $el_name => $el_selector ) {
$element_selector = array();
foreach ( $block_selectors as $selector ) {
if ( $selector === $el_selector ) {
$element_selector = array( $el_selector );
break;
}

$element_selector[] = $selector . ' ' . $el_selector;
}
static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
}
}

return static::$blocks_metadata;
}
}
19 changes: 19 additions & 0 deletions lib/experimental/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* WP_Theme_JSON_Gutenberg class
*
* @package gutenberg
*/

/**
* Class that encapsulates the processing of structures that adhere to the theme.json spec.
*
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
* This is a low-level API that may need to do breaking changes. Please,
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
*
* @access private
*/
class WP_Theme_JSON_Gutenberg extends WP_Theme_JSON_6_1 {

}
4 changes: 3 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.0/blocks.php';
require __DIR__ . '/compat/wordpress-6.0/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.0/functions.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.0/block-template.php';
Expand All @@ -123,11 +123,13 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.1 compat.
require __DIR__ . '/compat/wordpress-6.1/blocks.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php';

// Experimental features.
remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WP 6.0's stopgap handler for Webfonts API.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/register-webfonts-from-theme-json.php';
require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php';
require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php';
require __DIR__ . '/experimental/class-wp-webfonts.php';
require __DIR__ . '/experimental/class-wp-webfonts-provider.php';
Expand Down

0 comments on commit 709aabc

Please sign in to comment.