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