@@ -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