@@ -185,8 +185,14 @@ private function show_legend( $items ) {
185185 }
186186
187187 public function install ( $ args , $ assoc_args ) {
188- $ successes = 0 ;
189- $ errors = 0 ;
188+ $ successes = 0 ;
189+ $ errors = 0 ;
190+ $ custom_slug = Utils \get_flag_value ( $ assoc_args , 'slug ' , false );
191+
192+ if ( $ custom_slug && count ( $ args ) > 1 ) {
193+ WP_CLI ::error ( 'The --slug option can only be used when installing a single item. ' );
194+ }
195+
190196 foreach ( $ args as $ slug ) {
191197
192198 if ( empty ( $ slug ) ) {
@@ -263,10 +269,32 @@ public function install( $args, $assoc_args ) {
263269 $ file_upgrader = $ this ->get_upgrader ( $ assoc_args );
264270
265271 $ filter = false ;
266- // If a GitHub URL, do some guessing as to the correct plugin/theme directory.
267- if ( $ is_remote && 'github.com ' === Utils \parse_url ( $ slug , PHP_URL_HOST )
272+ if ( $ custom_slug ) {
273+ // If --slug is specified, rename the extracted directory to the provided slug.
274+ $ filter = function ( $ source ) use ( $ custom_slug ) {
275+ /**
276+ * @var \WP_Filesystem_Base $wp_filesystem
277+ */
278+ global $ wp_filesystem ;
279+
280+ $ source_dir = Utils \basename ( $ source ); // `$source` is trailing-slashed path to the unzipped archive directory.
281+ if ( $ source_dir === $ custom_slug ) {
282+ return $ source ;
283+ }
284+ $ new_path = substr_replace ( $ source , $ custom_slug , (int ) strrpos ( $ source , $ source_dir ), strlen ( $ source_dir ) );
285+
286+ if ( $ wp_filesystem ->move ( $ source , $ new_path ) ) {
287+ WP_CLI ::log ( sprintf ( "Renamed '%s' to '%s'. " , $ source_dir , $ custom_slug ) );
288+ return $ new_path ;
289+ }
290+
291+ return new WP_Error ( 'wpcli_install_slug ' , "Couldn't rename to ' {$ custom_slug }'. " );
292+ };
293+ add_filter ( 'upgrader_source_selection ' , $ filter , 10 );
294+ } elseif ( $ is_remote && 'github.com ' === Utils \parse_url ( $ slug , PHP_URL_HOST )
268295 // Don't attempt to rename ZIPs uploaded to the releases page or coming from a raw source.
269296 && ! preg_match ( '#github\.com/[^/]+/[^/]+/(?:releases/download|raw)/# ' , $ slug ) ) {
297+ // If a GitHub URL, do some guessing as to the correct plugin/theme directory.
270298
271299 $ filter = function ( $ source ) use ( $ slug ) {
272300 /**
0 commit comments