-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Theme.json: Allow shared block style variations via separate theme.json files #57787
Changes from all commits
3153e0e
8b985b7
278801f
bc1c5bb
7753561
f8897fe
30a5a27
51bb763
b10f7ad
29125ef
c66f888
88c7e68
d7cdf3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,7 +225,8 @@ protected static function has_same_registered_blocks( $origin ) { | |
* @since 5.8.0 | ||
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed. | ||
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports. | ||
* @since 6.5.0 Theme data will now also include block style variations that were registered with a style object. | ||
* @since 6.5.0 Theme data will now also include block style variations that | ||
* were registered with a style object or included via a standalone file. | ||
* | ||
* @param array $deprecated Deprecated. Not used. | ||
* @param array $options { | ||
|
@@ -373,9 +374,53 @@ public static function get_theme_data( $deprecated = array(), $options = array() | |
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); | ||
|
||
if ( $options['with_block_style_variations'] ) { | ||
// Absorb block style variations that were registered with a style object. | ||
$block_style_variations_data = WP_Theme_JSON_Gutenberg::get_from_block_styles_registry(); | ||
$with_block_style_variations = new WP_Theme_JSON_Gutenberg( $block_style_variations_data ); | ||
$with_theme_supports->merge( $with_block_style_variations ); | ||
|
||
// Resolve shared block style variations that were bundled in the | ||
// theme via standalone theme.json files. | ||
$shared_block_style_variations = static::get_style_variations( '/block-styles' ); | ||
$variations_data = array(); | ||
$registry = WP_Block_Styles_Registry::get_instance(); | ||
|
||
foreach ( $shared_block_style_variations as $variation ) { | ||
if ( empty( $variation['supportedBlockTypes'] ) || empty( $variation['styles'] ) ) { | ||
continue; | ||
} | ||
|
||
$variation_slug = _wp_to_kebab_case( $variation['title'] ); | ||
|
||
// If it proves desirable, block style variations could include | ||
// custom settings which can be included here. | ||
foreach ( $variation['supportedBlockTypes'] as $block_type ) { | ||
// Automatically register the block style variation if it | ||
// hasn't been already. | ||
$registered_styles = $registry->get_registered_styles_for_block( $block_type ); | ||
if ( ! array_key_exists( $variation_slug, $registered_styles ) ) { | ||
gutenberg_register_block_style( | ||
$block_type, | ||
array( | ||
'name' => $variation_slug, | ||
'label' => $variation['title'], | ||
) | ||
); | ||
} | ||
Comment on lines
+398
to
+409
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should block style variations defined within these standalone files be registered for blocks automatically? Child themes wouldn't be able to deregister block style variations coming from standalone files via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes it is a little limiting, but it is already documented that "The function unregister_block_style only unregisters styles that were registered on the server using register_block_style" so there would be no change. |
||
|
||
$path = array( $block_type, 'variations', $variation_slug ); | ||
_wp_array_set( $variations_data, $path, $variation['styles'] ); | ||
} | ||
} | ||
|
||
if ( ! empty( $variations_data ) ) { | ||
$variations_theme_json_data = array( | ||
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, | ||
'styles' => array( 'blocks' => $variations_data ), | ||
); | ||
$with_shared_variations = new WP_Theme_JSON_Gutenberg( $variations_theme_json_data ); | ||
$with_theme_supports->merge( $with_shared_variations ); | ||
} | ||
} | ||
|
||
$with_theme_supports->merge( static::$theme ); | ||
|
@@ -743,14 +788,16 @@ private static function recursively_iterate_json( $dir ) { | |
* Returns the style variations defined by the theme (parent and child). | ||
* | ||
* @since 6.2.0 Returns parent theme variations if theme is a child. | ||
* @since 6.5.0 Added configurable directory to allow block style variations | ||
* to reside in a different directory to theme style variations. | ||
* | ||
* @return array | ||
*/ | ||
public static function get_style_variations() { | ||
public static function get_style_variations( $dir = 'styles' ) { | ||
$variation_files = array(); | ||
$variations = array(); | ||
$base_directory = get_stylesheet_directory() . '/styles'; | ||
$template_directory = get_template_directory() . '/styles'; | ||
$base_directory = get_stylesheet_directory() . '/' . $dir; | ||
$template_directory = get_template_directory() . '/' . $dir; | ||
if ( is_dir( $base_directory ) ) { | ||
$variation_files = static::recursively_iterate_json( $base_directory ); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": 2, | ||
"supportedBlockTypes": [ "core/group", "core/columns", "core/media-text" ], | ||
"styles": { | ||
"color": { | ||
"background": "darkcyan", | ||
"text": "aliceblue" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Theme Name: Block Theme Child With Block Style Variations Theme | ||
Theme URI: https://wordpress.org/ | ||
Description: For testing purposes only. | ||
Template: block-theme | ||
Version: 1.0.0 | ||
Text Domain: block-theme-child-with-block-style-variations | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/theme.json", | ||
"version": 2 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": 2, | ||
"supportedBlockTypes": [ "core/group", "core/columns" ], | ||
"styles": { | ||
"color": { | ||
"background": "indigo", | ||
"text": "plum" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": 2, | ||
"supportedBlockTypes": [ "core/group", "core/columns" ], | ||
"styles": { | ||
"color": { | ||
"background": "midnightblue", | ||
"text": "lightblue" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the moment, the shared block style variations are being looked for in a
block-styles
directory within the theme. As with theme style variations understyles
, this takes into account child theme variations overriding a parent's definition.Is there a better location to store these block style variations? It seemed keeping them separate from theme style variations made sense to limit further updates to ensure block style variations weren't accidentally included with theme styles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it needs to be a different directory than the existing styles, but the naming is trickier.