Skip to content

Commit

Permalink
Make child themes inherit parent's style variations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jan 24, 2023
1 parent 91b86d8 commit f3cc87c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
56 changes: 42 additions & 14 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,33 +664,61 @@ public static function clean_cached_data() {
static::$i18n_schema = null;
}

/**
* Returns an array of all nested json files within a given directory.
*
* @since 6.2.0
*
* @param string $dir The directory to recursively iterate and list files of.
* @return array The merged array.
*/
private static function recursively_iterate_json( $dir ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
$nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
return $nested_json_files;
}


/**
* Returns the style variations defined by the theme.
*
* @since 6.0.0
* @since 6.2.0 Returns parent theme variations if theme is a child.
*
* @return array
*/
public static function get_style_variations() {
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$variation_files = array();
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$template_directory = get_template_directory() . '/styles';
if ( is_dir( $base_directory ) ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
$nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
ksort( $nested_html_files );
foreach ( $nested_html_files as $path => $file ) {
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
if ( empty( $variation['title'] ) ) {
$variation['title'] = basename( $path, '.json' );
$variation_files = static::recursively_iterate_json( $base_directory );
}
if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
$variation_files_parent = static::recursively_iterate_json( $template_directory );
// If the child and parent variation file basename are the same, only include the child theme's.
foreach ( $variation_files_parent as $parent_path => $parent ) {
foreach ( $variation_files as $child_path => $child ) {
if ( basename( $parent_path ) === basename( $child_path ) ) {
unset( $variation_files_parent[ $parent_path ] );
}
$variations[] = $variation;
}
}
$variation_files = array_merge( $variation_files, $variation_files_parent );
}
ksort( $variation_files );
foreach ( $variation_files as $path => $file ) {
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
if ( empty( $variation['title'] ) ) {
$variation['title'] = basename( $path, '.json' );
}
$variations[] = $variation;
}
}
return $variations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/post-title": {
"color": {
"palette": [
{
"slug": "dark",
"name": "Dark",
"color": "#010101"
}
]
}
}
}
}
}
18 changes: 18 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/styles/variation-b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/post-title": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f1f1f1"
}
]
}
}
}
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@
"area": "header"
}
]
}
}
33 changes: 19 additions & 14 deletions tests/phpunit/tests/theme/wpThemeJsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function test_translations_are_applied() {
);
$this->assertSame(
'Wariant motywu blokowego',
$style_variations[0]['title']
$style_variations[1]['title']
);
}

Expand Down Expand Up @@ -920,10 +920,10 @@ public function test_get_style_variations_returns_all_variations() {
$expected_settings = array(
array(
'version' => 2,
'title' => 'variation-a',
'title' => 'variation-b',
'settings' => array(
'blocks' => array(
'core/paragraph' => array(
'core/post-title' => array(
'color' => array(
'palette' => array(
'theme' => array(
Expand All @@ -941,20 +941,25 @@ public function test_get_style_variations_returns_all_variations() {
),
array(
'version' => 2,
'title' => 'variation-b',
'title' => 'Block theme variation',
'settings' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'foreground',
'name' => 'Foreground',
'color' => '#3F67C6',
),
),
),
),
),
'styles' => array(
'blocks' => array(
'core/post-title' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f1f1f1',
),
),
),
'typography' => array(
'fontWeight' => '700',
),
),
),
Expand Down

0 comments on commit f3cc87c

Please sign in to comment.