Skip to content

Commit 2c58fb5

Browse files
Copilotswissspidy
andcommitted
Add --slug option to plugin/theme install for directory renaming
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 5c3c007 commit 2c58fb5

5 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎features/plugin-install.feature‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,29 @@ Feature: Install WordPress plugins
472472
"""
473473
active-network
474474
"""
475+
476+
Scenario: Install plugin from a zip file with a custom --slug
477+
Given a WP install
478+
479+
When I run `wp plugin install https://github.com/wp-cli-test/generic-example-plugin/archive/refs/heads/master.zip --slug=my-custom-plugin`
480+
Then STDOUT should contain:
481+
"""
482+
Renamed 'generic-example-plugin-master' to 'my-custom-plugin'.
483+
"""
484+
And STDOUT should contain:
485+
"""
486+
Plugin installed successfully.
487+
"""
488+
And the wp-content/plugins/my-custom-plugin directory should exist
489+
And the wp-content/plugins/generic-example-plugin-master directory should not exist
490+
And the return code should be 0
491+
492+
Scenario: Error when --slug is used with multiple plugins
493+
Given a WP install
494+
495+
When I try `wp plugin install hello-dolly akismet --slug=my-plugin`
496+
Then STDERR should contain:
497+
"""
498+
Error: The --slug option can only be used when installing a single item.
499+
"""
500+
And the return code should be 1

‎features/theme-install.feature‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,29 @@ Feature: Install WordPress themes
186186
"""
187187
active
188188
"""
189+
190+
Scenario: Install theme from a zip file with a custom --slug
191+
Given a WP install
192+
193+
When I run `wp theme install https://github.com/wp-cli-test/generic-example-theme/archive/refs/heads/master.zip --slug=my-custom-theme`
194+
Then STDOUT should contain:
195+
"""
196+
Renamed 'generic-example-theme-master' to 'my-custom-theme'.
197+
"""
198+
And STDOUT should contain:
199+
"""
200+
Theme installed successfully.
201+
"""
202+
And the wp-content/themes/my-custom-theme directory should exist
203+
And the wp-content/themes/generic-example-theme-master directory should not exist
204+
And the return code should be 0
205+
206+
Scenario: Error when --slug is used with multiple themes
207+
Given a WP install
208+
209+
When I try `wp theme install twentytwelve twentyeleven --slug=my-theme`
210+
Then STDERR should contain:
211+
"""
212+
Error: The --slug option can only be used when installing a single item.
213+
"""
214+
And the return code should be 1

‎src/Plugin_Command.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,9 @@ protected function filter_item_list( $items, $args ) {
11651165
* [--with-dependencies]
11661166
* : If set, the command will also install all required dependencies of the plugin as specified in the 'Requires Plugins' header.
11671167
*
1168+
* [--slug=<slug>]
1169+
* : Use this as the target directory name when installing from a zip file. Cannot be used when installing multiple plugins.
1170+
*
11681171
* ## EXAMPLES
11691172
*
11701173
* # Install the latest version from wordpress.org and activate

‎src/Theme_Command.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ protected function filter_item_list( $items, $args ) {
612612
* [--insecure]
613613
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
614614
*
615+
* [--slug=<slug>]
616+
* : Use this as the target directory name when installing from a zip file. Cannot be used when installing multiple themes.
617+
*
615618
* ## EXAMPLES
616619
*
617620
* # Install the latest version from wordpress.org and activate

‎src/WP_CLI/CommandWithUpgrade.php‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)