Skip to content

Commit dfacef0

Browse files
authored
Merge pull request #83 from wp-cli/issues-82-plugin-activate-error-bad-slug
Check args if optional and have all option.
2 parents 2ecec9a + c0b2a7c commit dfacef0

File tree

6 files changed

+275
-17
lines changed

6 files changed

+275
-17
lines changed

features/plugin-activate.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,21 @@ Feature: Activate WordPress plugins
6262
Plugin 'query-monitor' activated.
6363
"""
6464

65+
Scenario: Not giving a slug on activate should throw an error unless --all given
66+
When I try `wp plugin activate`
67+
Then the return code should be 1
68+
And STDERR should be:
69+
"""
70+
Error: Please specify one or more plugins, or use --all.
71+
"""
72+
And STDOUT should be empty
73+
74+
# But don't give an error if no plugins and --all given for BC.
75+
Given I run `wp plugin path`
76+
And save STDOUT as {PLUGIN_DIR}
77+
And an empty {PLUGIN_DIR} directory
78+
When I run `wp plugin activate --all`
79+
Then STDOUT should be:
80+
"""
81+
Success: No plugins installed.
82+
"""

features/plugin-deactivate.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ Feature: Deactivate WordPress plugins
5858
Plugin 'akismet' deactivated.
5959
"""
6060

61+
Scenario: Not giving a slug on deactivate should throw an error unless --all given
62+
When I try `wp plugin deactivate`
63+
Then the return code should be 1
64+
And STDERR should be:
65+
"""
66+
Error: Please specify one or more plugins, or use --all.
67+
"""
68+
And STDOUT should be empty
69+
70+
# But don't give an error if no plugins and --all given for BC.
71+
Given I run `wp plugin path`
72+
And save STDOUT as {PLUGIN_DIR}
73+
And an empty {PLUGIN_DIR} directory
74+
When I run `wp plugin deactivate --all`
75+
Then STDOUT should be:
76+
"""
77+
Success: No plugins installed.
78+
"""

features/plugin-update.feature

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,86 @@ Feature: Update WordPress plugins
108108
"""
109109
2.6.1
110110
"""
111+
112+
Scenario: Not giving a slug on update should throw an error unless --all given
113+
Given a WP install
114+
And I run `wp plugin path`
115+
And save STDOUT as {PLUGIN_DIR}
116+
And an empty {PLUGIN_DIR} directory
117+
118+
# No plugins installed. Don't give an error if --all given for BC.
119+
When I run `wp plugin update --all`
120+
Then STDOUT should be:
121+
"""
122+
Success: No plugins installed.
123+
"""
124+
125+
When I run `wp plugin update --version=0.6 --all`
126+
Then STDOUT should be:
127+
"""
128+
Success: No plugins installed.
129+
"""
130+
131+
# One plugin installed.
132+
Given I run `wp plugin install wordpress-importer --version=0.5 --force`
133+
134+
When I try `wp plugin update`
135+
Then the return code should be 1
136+
And STDERR should be:
137+
"""
138+
Error: Please specify one or more plugins, or use --all.
139+
"""
140+
And STDOUT should be empty
141+
142+
When I run `wp plugin update --all`
143+
Then STDOUT should contain:
144+
"""
145+
Success: Updated
146+
"""
147+
148+
When I run the previous command again
149+
Then STDOUT should be:
150+
"""
151+
Success: Plugin already updated.
152+
"""
153+
154+
# Note: if given version then re-installs.
155+
When I run `wp plugin update --version=0.6 --all`
156+
Then STDOUT should contain:
157+
"""
158+
Success: Installed 1 of 1 plugins.
159+
"""
160+
161+
When I run the previous command again
162+
Then STDOUT should contain:
163+
"""
164+
Success: Installed 1 of 1 plugins.
165+
"""
166+
167+
# Two plugins installed.
168+
Given I run `wp plugin install akismet --version=2.5.4`
169+
170+
When I run `wp plugin update --all`
171+
Then STDOUT should contain:
172+
"""
173+
Success: Updated
174+
"""
175+
176+
When I run the previous command again
177+
# BUG: note this message should be plural.
178+
Then STDOUT should be:
179+
"""
180+
Success: Plugin already updated.
181+
"""
182+
183+
# Using version with all rarely makes sense and should probably error and do nothing.
184+
When I try `wp plugin update --version=2.5.4 --all`
185+
Then the return code should be 1
186+
And STDOUT should contain:
187+
"""
188+
Success: Installed 1 of 1 plugins.
189+
"""
190+
And STDERR should be:
191+
"""
192+
Error: Can't find the requested plugin's version 2.5.4 in the WordPress.org plugin repository (HTTP code 404).
193+
"""

features/theme.feature

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,86 @@ Feature: Manage WordPress themes
434434
Theme stargazer details:
435435
"""
436436
And STDERR should be empty
437+
438+
Scenario: Not giving a slug on update should throw an error unless --all given
439+
Given a WP install
440+
And I run `wp theme path`
441+
And save STDOUT as {THEME_DIR}
442+
And an empty {THEME_DIR} directory
443+
444+
# No themes installed. Don't give an error if --all given for BC.
445+
When I run `wp theme update --all`
446+
Then STDOUT should be:
447+
"""
448+
Success: No themes installed.
449+
"""
450+
451+
When I run `wp theme update --version=0.6 --all`
452+
Then STDOUT should be:
453+
"""
454+
Success: No themes installed.
455+
"""
456+
457+
# One theme installed.
458+
Given I run `wp theme install p2 --version=1.4.2`
459+
460+
When I try `wp theme update`
461+
Then the return code should be 1
462+
And STDERR should be:
463+
"""
464+
Error: Please specify one or more themes, or use --all.
465+
"""
466+
And STDOUT should be empty
467+
468+
When I run `wp theme update --all`
469+
Then STDOUT should contain:
470+
"""
471+
Success: Updated
472+
"""
473+
474+
When I run the previous command again
475+
Then STDOUT should be:
476+
"""
477+
Success: Theme already updated.
478+
"""
479+
480+
# Note: if given version then re-installs.
481+
When I run `wp theme update --version=1.4.2 --all`
482+
Then STDOUT should contain:
483+
"""
484+
Success: Installed 1 of 1 themes.
485+
"""
486+
487+
When I run the previous command again
488+
Then STDOUT should contain:
489+
"""
490+
Success: Installed 1 of 1 themes.
491+
"""
492+
493+
# Two themes installed.
494+
Given I run `wp theme install --force twentytwelve --version=1.0`
495+
496+
When I run `wp theme update --all`
497+
Then STDOUT should contain:
498+
"""
499+
Success: Updated
500+
"""
501+
502+
When I run the previous command again
503+
# BUG: Message should be in plural.
504+
Then STDOUT should be:
505+
"""
506+
Success: Theme already updated.
507+
"""
508+
509+
# Using version with all rarely makes sense and should probably error and do nothing.
510+
When I try `wp theme update --version=1.4.2 --all`
511+
Then the return code should be 1
512+
And STDOUT should contain:
513+
"""
514+
Success: Installed 1 of 1 themes.
515+
"""
516+
And STDERR should be:
517+
"""
518+
Error: Can't find the requested theme's version 1.4.2 in the WordPress.org theme repository (HTTP code 404).
519+
"""

src/Plugin_Command.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected function get_all_items() {
250250
}
251251

252252
/**
253-
* Activates a plugin.
253+
* Activates one or more plugins.
254254
*
255255
* ## OPTIONS
256256
*
@@ -279,10 +279,8 @@ public function activate( $args, $assoc_args = array() ) {
279279
$network_wide = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network' );
280280
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
281281

282-
if ( $all ) {
283-
$args = array_map( function( $file ){
284-
return Utils\get_plugin_name( $file );
285-
}, array_keys( $this->get_all_plugins() ) );
282+
if ( ! ( $args = $this->check_optional_args_and_all( $args, $all ) ) ) {
283+
return;
286284
}
287285

288286
$successes = $errors = 0;
@@ -325,7 +323,7 @@ public function activate( $args, $assoc_args = array() ) {
325323
}
326324

327325
/**
328-
* Deactivates a plugin.
326+
* Deactivates one or more plugins.
329327
*
330328
* ## OPTIONS
331329
*
@@ -352,10 +350,8 @@ public function deactivate( $args, $assoc_args = array() ) {
352350
$network_wide = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network' );
353351
$disable_all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' );
354352

355-
if ( $disable_all ) {
356-
$args = array_map( function( $file ){
357-
return Utils\get_plugin_name( $file );
358-
}, array_keys( $this->get_all_plugins() ) );
353+
if ( ! ( $args = $this->check_optional_args_and_all( $args, $disable_all ) ) ) {
354+
return;
359355
}
360356

361357
$successes = $errors = 0;
@@ -610,6 +606,12 @@ protected function install_from_repo( $slug, $assoc_args ) {
610606
* @alias upgrade
611607
*/
612608
public function update( $args, $assoc_args ) {
609+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
610+
611+
if ( ! ( $args = $this->check_optional_args_and_all( $args, $all ) ) ) {
612+
return;
613+
}
614+
613615
if ( isset( $assoc_args['version'] ) ) {
614616
foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
615617
$assoc_args['force'] = 1;
@@ -662,12 +664,12 @@ protected function filter_item_list( $items, $args ) {
662664
}
663665

664666
/**
665-
* Installs a plugin.
667+
* Installs one or more plugins.
666668
*
667669
* ## OPTIONS
668670
*
669671
* <plugin|zip|url>...
670-
* : A plugin slug, the path to a local zip file, or URL to a remote zip file.
672+
* : One or more plugins to install. Accepts a plugin slug, the path to a local zip file, or a URL to a remote zip file.
671673
*
672674
* [--version=<version>]
673675
* : If set, get that particular version from wordpress.org, instead of the
@@ -804,7 +806,7 @@ public function get( $args, $assoc_args ) {
804806
}
805807

806808
/**
807-
* Uninstalls a plugin.
809+
* Uninstalls one or more plugins.
808810
*
809811
* ## OPTIONS
810812
*
@@ -1072,4 +1074,28 @@ private function _delete( $plugin ) {
10721074
private function get_all_plugins() {
10731075
return apply_filters( 'all_plugins', get_plugins() );
10741076
}
1077+
1078+
/**
1079+
* If have optional args ([<plugin>...]) and an all option, then check have something to do.
1080+
*
1081+
* @param array $args Passed-in arguments.
1082+
* @param bool $all All flag.
1083+
* @return array Same as $args if not all, otherwise all slugs.
1084+
*/
1085+
private function check_optional_args_and_all( $args, $all ) {
1086+
if ( $all ) {
1087+
$args = array_map( function( $file ){
1088+
return Utils\get_plugin_name( $file );
1089+
}, array_keys( $this->get_all_plugins() ) );
1090+
}
1091+
1092+
if ( empty( $args ) ) {
1093+
if ( ! $all ) {
1094+
WP_CLI::error( 'Please specify one or more plugins, or use --all.' );
1095+
}
1096+
WP_CLI::success( 'No plugins installed.' ); // Don't error if --all given for BC.
1097+
}
1098+
1099+
return $args;
1100+
}
10751101
}

src/Theme_Command.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ protected function filter_item_list( $items, $args ) {
477477
}
478478

479479
/**
480-
* Installs a theme.
480+
* Installs one or more themes.
481481
*
482482
* ## OPTIONS
483483
*
484484
* <theme|zip|url>...
485-
* : A theme slug, the path to a local zip file, or URL to a remote zip file.
485+
* : One or more themes to install. Accepts a theme slug, the path to a local zip file, or a URL to a remote zip file.
486486
*
487487
* [--version=<version>]
488488
* : If set, get that particular version from wordpress.org, instead of the
@@ -666,6 +666,12 @@ public function get( $args, $assoc_args ) {
666666
* @alias upgrade
667667
*/
668668
public function update( $args, $assoc_args ) {
669+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
670+
671+
if ( ! ( $args = $this->check_optional_args_and_all( $args, $all ) ) ) {
672+
return;
673+
}
674+
669675
if ( isset( $assoc_args['version'] ) ) {
670676
foreach ( $this->fetcher->get_many( $args ) as $theme ) {
671677
$r = delete_theme( $theme->stylesheet );
@@ -711,9 +717,9 @@ public function is_installed( $args, $assoc_args = array() ) {
711717
}
712718

713719
/**
714-
* Deletes a theme.
720+
* Deletes one or more themes.
715721
*
716-
* Removes the theme from the filesystem.
722+
* Removes the theme or themes from the filesystem.
717723
*
718724
* ## OPTIONS
719725
*
@@ -810,4 +816,28 @@ public function delete( $args ) {
810816
public function list_( $_, $assoc_args ) {
811817
parent::_list( $_, $assoc_args );
812818
}
819+
820+
/**
821+
* If have optional args ([<theme>...]) and an all option, then check have something to do.
822+
*
823+
* @param array $args Passed-in arguments.
824+
* @param bool $all All flag.
825+
* @return array Same as $args if not all, otherwise all slugs.
826+
*/
827+
private function check_optional_args_and_all( $args, $all ) {
828+
if ( $all ) {
829+
$args = array_map( function( $item ){
830+
return Utils\get_theme_name( $item );
831+
}, array_keys( $this->get_all_items() ) );
832+
}
833+
834+
if ( empty( $args ) ) {
835+
if ( ! $all ) {
836+
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
837+
}
838+
WP_CLI::success( 'No themes installed.' ); // Don't error if --all given for BC.
839+
}
840+
841+
return $args;
842+
}
813843
}

0 commit comments

Comments
 (0)