Skip to content

Commit c83e48a

Browse files
committed
Make child themes inherit parent's style variations
1 parent 91b86d8 commit c83e48a

5 files changed

Lines changed: 98 additions & 29 deletions

File tree

src/wp-includes/class-wp-theme-json-resolver.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -664,33 +664,61 @@ public static function clean_cached_data() {
664664
static::$i18n_schema = null;
665665
}
666666

667+
/**
668+
* Returns an array of all nested json files within a given directory.
669+
*
670+
* @since 6.2.0
671+
*
672+
* @param string $dir The directory to recursively iterate and list files of.
673+
* @return array The merged array.
674+
*/
675+
private static function recursively_iterate_json( $dir ) {
676+
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
677+
$nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
678+
return $nested_json_files;
679+
}
680+
681+
667682
/**
668683
* Returns the style variations defined by the theme.
669684
*
670685
* @since 6.0.0
686+
* @since 6.2.0 Returns parent theme variations if theme is a child.
671687
*
672688
* @return array
673689
*/
674690
public static function get_style_variations() {
675-
$variations = array();
676-
$base_directory = get_stylesheet_directory() . '/styles';
691+
$variation_files = array();
692+
$variations = array();
693+
$base_directory = get_stylesheet_directory() . '/styles';
694+
$template_directory = get_template_directory() . '/styles';
677695
if ( is_dir( $base_directory ) ) {
678-
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
679-
$nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
680-
ksort( $nested_html_files );
681-
foreach ( $nested_html_files as $path => $file ) {
682-
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
683-
if ( is_array( $decoded_file ) ) {
684-
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
685-
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
686-
if ( empty( $variation['title'] ) ) {
687-
$variation['title'] = basename( $path, '.json' );
696+
$variation_files = static::recursively_iterate_json( $base_directory );
697+
}
698+
if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
699+
$variation_files_parent = static::recursively_iterate_json( $template_directory );
700+
// If the child and parent variation file basename are the same, only include the child theme's.
701+
foreach ( $variation_files_parent as $parent_path => $parent ) {
702+
foreach ( $variation_files as $child_path => $child ) {
703+
if ( basename( $parent_path ) === basename( $child_path ) ) {
704+
unset( $variation_files_parent[ $parent_path ] );
688705
}
689-
$variations[] = $variation;
690706
}
691707
}
708+
$variation_files = array_merge( $variation_files, $variation_files_parent );
709+
}
710+
ksort( $variation_files );
711+
foreach ( $variation_files as $path => $file ) {
712+
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
713+
if ( is_array( $decoded_file ) ) {
714+
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
715+
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
716+
if ( empty( $variation['title'] ) ) {
717+
$variation['title'] = basename( $path, '.json' );
718+
}
719+
$variations[] = $variation;
720+
}
692721
}
693722
return $variations;
694723
}
695-
696724
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 2,
3+
"settings": {
4+
"blocks": {
5+
"core/post-title": {
6+
"color": {
7+
"palette": [
8+
{
9+
"slug": "dark",
10+
"name": "Dark",
11+
"color": "#010101"
12+
}
13+
]
14+
}
15+
}
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 2,
3+
"settings": {
4+
"blocks": {
5+
"core/post-title": {
6+
"color": {
7+
"palette": [
8+
{
9+
"slug": "light",
10+
"name": "Light",
11+
"color": "#f1f1f1"
12+
}
13+
]
14+
}
15+
}
16+
}
17+
}
18+
}

tests/phpunit/data/themedir1/block-theme/theme.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,4 @@
124124
"area": "header"
125125
}
126126
]
127-
}
127+
}

tests/phpunit/tests/theme/wpThemeJsonResolver.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function test_translations_are_applied() {
235235
);
236236
$this->assertSame(
237237
'Wariant motywu blokowego',
238-
$style_variations[0]['title']
238+
$style_variations[1]['title']
239239
);
240240
}
241241

@@ -920,10 +920,10 @@ public function test_get_style_variations_returns_all_variations() {
920920
$expected_settings = array(
921921
array(
922922
'version' => 2,
923-
'title' => 'variation-a',
923+
'title' => 'variation-b',
924924
'settings' => array(
925925
'blocks' => array(
926-
'core/paragraph' => array(
926+
'core/post-title' => array(
927927
'color' => array(
928928
'palette' => array(
929929
'theme' => array(
@@ -941,20 +941,25 @@ public function test_get_style_variations_returns_all_variations() {
941941
),
942942
array(
943943
'version' => 2,
944-
'title' => 'variation-b',
944+
'title' => 'Block theme variation',
945945
'settings' => array(
946+
'color' => array(
947+
'palette' => array(
948+
'theme' => array(
949+
array(
950+
'slug' => 'foreground',
951+
'name' => 'Foreground',
952+
'color' => '#3F67C6',
953+
),
954+
),
955+
),
956+
),
957+
),
958+
'styles' => array(
946959
'blocks' => array(
947960
'core/post-title' => array(
948-
'color' => array(
949-
'palette' => array(
950-
'theme' => array(
951-
array(
952-
'slug' => 'light',
953-
'name' => 'Light',
954-
'color' => '#f1f1f1',
955-
),
956-
),
957-
),
961+
'typography' => array(
962+
'fontWeight' => '700',
958963
),
959964
),
960965
),

0 commit comments

Comments
 (0)