Skip to content

Commit 2f16c14

Browse files
schlesseraclaude
andcommitted
Install the parent of a cached child theme
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 46ce957 commit 2f16c14

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎features/behat-steps.feature‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,15 @@ Feature: Test that WP-CLI Behat steps work as expected
494494
And these installed themes:
495495
"""
496496
twentysixteen
497+
oceanly-green
497498
"""
498499
And these installed plugins:
499500
"""
500501
wordpress-importer --version=0.8
501502
"""
502503
And a non-existent wp-content/themes/twentysixteen directory
504+
And a non-existent wp-content/themes/oceanly-green directory
505+
And a non-existent wp-content/themes/oceanly directory
503506
And a non-existent wp-content/plugins/wordpress-importer directory
504507
And a wp-content/mu-plugins/block-http-requests.php file:
505508
"""
@@ -512,7 +515,11 @@ Feature: Test that WP-CLI Behat steps work as expected
512515
);
513516
"""
514517

515-
When these installed and active themes:
518+
When these installed themes:
519+
"""
520+
oceanly-green
521+
"""
522+
And these installed and active themes:
516523
"""
517524
twentysixteen
518525
"""
@@ -532,6 +539,8 @@ Feature: Test that WP-CLI Behat steps work as expected
532539
status,active
533540
version,0.8
534541
"""
542+
And the wp-content/themes/oceanly-green/style.css file should exist
543+
And the wp-content/themes/oceanly/style.css file should exist
535544

536545
@require-wp
537546
Scenario: Test custom wp-content directory

‎src/Context/FeatureContext.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,12 @@ public function install_extensions( $type, array $lines, $activate ): void {
18991899
self::remove_dir( "$target_dir/$slug" );
19001900
mkdir( "$target_dir/$slug" );
19011901
self::copy_dir( $cache_path, "$target_dir/$slug" );
1902+
1903+
// Installing a child theme also installs its parent.
1904+
$parent = 'theme' === $type ? self::get_parent_theme( "$target_dir/$slug" ) : '';
1905+
if ( '' !== $parent && ! is_dir( "$target_dir/$parent" ) ) {
1906+
$this->install_extensions( 'theme', [ $parent ], false );
1907+
}
19021908
} else {
19031909
$uncached[ $flags ][] = $slug;
19041910
}
@@ -1911,6 +1917,11 @@ public function install_extensions( $type, array $lines, $activate ): void {
19111917
if ( is_dir( "$target_dir/$slug" ) ) {
19121918
self::store_in_extension_cache( "$target_dir/$slug", self::get_extension_cache_path( $type, $slug, (string) $flags ) );
19131919
}
1920+
1921+
$parent = 'theme' === $type && is_dir( "$target_dir/$slug" ) ? self::get_parent_theme( "$target_dir/$slug" ) : '';
1922+
if ( '' !== $parent && is_dir( "$target_dir/$parent" ) && ! is_dir( self::get_extension_cache_path( 'theme', $parent, '' ) ) ) {
1923+
self::store_in_extension_cache( "$target_dir/$parent", self::get_extension_cache_path( 'theme', $parent, '' ) );
1924+
}
19141925
}
19151926
}
19161927

@@ -1947,6 +1958,22 @@ private function get_extension_dir( $type ): ?string {
19471958
return null;
19481959
}
19491960

1961+
/**
1962+
* Get the parent of a theme from the `Template` header of its stylesheet, or '' if it has none.
1963+
*
1964+
* @param string $theme_dir
1965+
*/
1966+
private static function get_parent_theme( $theme_dir ): string {
1967+
$stylesheet = "$theme_dir/style.css";
1968+
if ( ! is_file( $stylesheet ) ) {
1969+
return '';
1970+
}
1971+
1972+
$header = (string) file_get_contents( $stylesheet, false, null, 0, 8192 );
1973+
1974+
return preg_match( '/^[ \t\/*#@]*Template:(.*)$/mi', $header, $matches ) ? trim( $matches[1] ) : '';
1975+
}
1976+
19501977
/**
19511978
* @param string $type
19521979
* @param string $slug

0 commit comments

Comments
 (0)