@@ -1872,6 +1872,136 @@ public function download_wp( $subdir = '', $version = '' ): void {
18721872 }
18731873 }
18741874
1875+ /**
1876+ * Install plugins or themes, copying the ones from WordPress.org from a local cache when possible.
1877+ *
1878+ * @param string $type Either 'plugin' or 'theme'.
1879+ * @param array<string> $lines Slugs, optionally followed by `--version=<version>` and `--force`, or anything `wp plugin install` accepts.
1880+ * @param bool $activate Whether to activate them.
1881+ */
1882+ public function install_extensions ( $ type , array $ lines , $ activate ): void {
1883+ $ target_dir = $ this ->get_extension_dir ( $ type );
1884+ $ slugs = [];
1885+ $ uncached = [];
1886+ $ others = [];
1887+
1888+ foreach ( $ lines as $ line ) {
1889+ if ( ! preg_match ( '/^([a-z0-9][a-z0-9._-]*)(?:\s+--version=(\S+))?(\s+--force)?$/i ' , $ line , $ matches ) || null === $ target_dir ) {
1890+ $ others [] = $ line ;
1891+ continue ;
1892+ }
1893+
1894+ $ slug = $ matches [1 ];
1895+ $ flags = ( isset ( $ matches [2 ] ) && '' !== $ matches [2 ] ? ' --version= ' . escapeshellarg ( $ matches [2 ] ) : '' ) . ( empty ( $ matches [3 ] ) ? '' : ' --force ' );
1896+ $ slugs [] = $ slug ;
1897+
1898+ $ cache_path = self ::get_extension_cache_path ( $ type , $ slug , $ flags );
1899+ if ( is_dir ( $ cache_path ) ) {
1900+ self ::remove_dir ( "$ target_dir/ $ slug " );
1901+ mkdir ( "$ target_dir/ $ slug " );
1902+ self ::copy_dir ( $ cache_path , "$ target_dir/ $ slug " );
1903+
1904+ // Installing a child theme also installs its parent.
1905+ $ parent = 'theme ' === $ type ? self ::get_parent_theme ( "$ target_dir/ $ slug " ) : '' ;
1906+ if ( '' !== $ parent && ! is_dir ( "$ target_dir/ $ parent " ) ) {
1907+ $ this ->install_extensions ( 'theme ' , [ $ parent ], false );
1908+ }
1909+ } else {
1910+ $ uncached [ $ flags ][] = $ slug ;
1911+ }
1912+ }
1913+
1914+ foreach ( $ uncached as $ flags => $ uncached_slugs ) {
1915+ $ this ->proc ( "wp $ type install " . implode ( ' ' , array_map ( 'escapeshellarg ' , $ uncached_slugs ) ) . $ flags )->run_check ();
1916+
1917+ foreach ( $ uncached_slugs as $ slug ) {
1918+ if ( is_dir ( "$ target_dir/ $ slug " ) ) {
1919+ self ::store_in_extension_cache ( "$ target_dir/ $ slug " , self ::get_extension_cache_path ( $ type , $ slug , (string ) $ flags ) );
1920+ }
1921+
1922+ $ parent = 'theme ' === $ type && is_dir ( "$ target_dir/ $ slug " ) ? self ::get_parent_theme ( "$ target_dir/ $ slug " ) : '' ;
1923+ if ( '' !== $ parent && is_dir ( "$ target_dir/ $ parent " ) && ! is_dir ( self ::get_extension_cache_path ( 'theme ' , $ parent , '' ) ) ) {
1924+ self ::store_in_extension_cache ( "$ target_dir/ $ parent " , self ::get_extension_cache_path ( 'theme ' , $ parent , '' ) );
1925+ }
1926+ }
1927+ }
1928+
1929+ if ( $ others ) {
1930+ $ this ->proc ( "wp $ type install " . implode ( ' ' , $ others ) . ( $ activate ? ' --activate ' : '' ) )->run_check ();
1931+ }
1932+
1933+ if ( $ activate && $ slugs ) {
1934+ $ this ->proc ( "wp $ type activate " . implode ( ' ' , array_map ( 'escapeshellarg ' , $ slugs ) ) )->run_check ();
1935+ }
1936+ }
1937+
1938+ /**
1939+ * Get the directory plugins or themes are installed into, or null if it is not known.
1940+ *
1941+ * @param string $type Either 'plugin' or 'theme'.
1942+ */
1943+ private function get_extension_dir ( $ type ): ?string {
1944+ if ( ! isset ( $ this ->variables ['RUN_DIR ' ] ) ) {
1945+ return null ;
1946+ }
1947+
1948+ $ run_dir = $ this ->variables ['RUN_DIR ' ];
1949+ $ candidates = 'plugin ' === $ type
1950+ ? [ "$ run_dir/my-plugins " , "$ run_dir/wp-content/plugins " , "$ run_dir/WordPress/wp-content/plugins " ]
1951+ : [ "$ run_dir/my-content/themes " , "$ run_dir/wp-content/themes " , "$ run_dir/WordPress/wp-content/themes " ];
1952+
1953+ foreach ( $ candidates as $ candidate ) {
1954+ if ( is_dir ( $ candidate ) ) {
1955+ return $ candidate ;
1956+ }
1957+ }
1958+
1959+ return null ;
1960+ }
1961+
1962+ /**
1963+ * Get the parent of a theme from the `Template` header of its stylesheet, or '' if it has none.
1964+ *
1965+ * @param string $theme_dir
1966+ */
1967+ private static function get_parent_theme ( $ theme_dir ): string {
1968+ $ stylesheet = "$ theme_dir/style.css " ;
1969+ if ( ! is_file ( $ stylesheet ) ) {
1970+ return '' ;
1971+ }
1972+
1973+ $ header = (string ) file_get_contents ( $ stylesheet , false , null , 0 , 8192 );
1974+
1975+ return preg_match ( '/^[ \t\/*#@]*Template:(.*)$/mi ' , $ header , $ matches ) ? trim ( $ matches [1 ] ) : '' ;
1976+ }
1977+
1978+ /**
1979+ * @param string $type
1980+ * @param string $slug
1981+ * @param string $flags Install flags that change what gets installed.
1982+ */
1983+ private static function get_extension_cache_path ( $ type , $ slug , $ flags ): string {
1984+ $ key = implode ( ': ' , [ $ type , $ slug , $ flags , (string ) getenv ( 'WP_VERSION ' ), PHP_MAJOR_VERSION . '. ' . PHP_MINOR_VERSION ] );
1985+
1986+ return sys_get_temp_dir () . '/wp-cli-test-extension-cache/ ' . $ type . '- ' . $ slug . '- ' . substr ( md5 ( $ key ), 0 , 12 );
1987+ }
1988+
1989+ /**
1990+ * @param string $source_dir
1991+ * @param string $cache_path
1992+ */
1993+ private static function store_in_extension_cache ( $ source_dir , $ cache_path ): void {
1994+ $ temp_path = $ cache_path . '- ' . uniqid ( '' , true );
1995+ mkdir ( $ temp_path , 0777 , true );
1996+
1997+ self ::copy_dir ( $ source_dir , $ temp_path );
1998+
1999+ // Another process may have filled the cache in the meantime.
2000+ if ( is_dir ( $ cache_path ) || ! rename ( $ temp_path , $ cache_path ) ) {
2001+ self ::remove_dir ( $ temp_path );
2002+ }
2003+ }
2004+
18752005 /**
18762006 * Create a wp-config.php file.
18772007 *
0 commit comments