Skip to content

Commit 1be4886

Browse files
Merge pull request #321 from wp-cli/add/plugin-exclude-argument
Add optional `--exclude=<name>` argument for multiple plugin commands
2 parents 66712a2 + fc3149d commit 1be4886

File tree

7 files changed

+133
-15
lines changed

7 files changed

+133
-15
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ See the WordPress [Plugin Handbook](https://developer.wordpress.org/plugins/) de
5757
Activates one or more plugins.
5858

5959
~~~
60-
wp plugin activate [<plugin>...] [--all] [--network]
60+
wp plugin activate [<plugin>...] [--all] [--exclude=<name>] [--network]
6161
~~~
6262

6363
**OPTIONS**
@@ -68,6 +68,9 @@ wp plugin activate [<plugin>...] [--all] [--network]
6868
[--all]
6969
If set, all plugins will be activated.
7070

71+
[--exclude=<name>]
72+
Comma separated list of plugin slugs to be excluded from activation.
73+
7174
[--network]
7275
If set, the plugin will be activated for the entire multisite network.
7376

@@ -90,7 +93,7 @@ wp plugin activate [<plugin>...] [--all] [--network]
9093
Deactivates one or more plugins.
9194

9295
~~~
93-
wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--network]
96+
wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--exclude=<name>] [--network]
9497
~~~
9598

9699
**OPTIONS**
@@ -104,6 +107,9 @@ wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--network]
104107
[--all]
105108
If set, all plugins will be deactivated.
106109

110+
[--exclude=<name>]
111+
Comma separated list of plugin names that should be excluded from deactivation.
112+
107113
[--network]
108114
If set, the plugin will be deactivated for the entire multisite network.
109115

@@ -114,14 +120,20 @@ wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--network]
114120
Plugin 'hello' deactivated.
115121
Success: Deactivated 1 of 1 plugins.
116122

123+
# Deactivate all plugins with exclusion
124+
$ wp plugin deactivate --all --exclude=hello,wordpress-seo
125+
Plugin 'contact-form-7' deactivated.
126+
Plugin 'ninja-forms' deactivated.
127+
Success: Deactivated 2 of 2 plugins.
128+
117129

118130

119131
### wp plugin delete
120132

121133
Deletes plugin files without deactivating or uninstalling.
122134

123135
~~~
124-
wp plugin delete [<plugin>...] [--all]
136+
wp plugin delete [<plugin>...] [--all] [--exclude=<name>]
125137
~~~
126138

127139
**OPTIONS**
@@ -132,6 +144,9 @@ wp plugin delete [<plugin>...] [--all]
132144
[--all]
133145
If set, all plugins will be deleted.
134146

147+
[--exclude=<name>]
148+
Comma separated list of plugin slugs to be excluded from deletion.
149+
135150
**EXAMPLES**
136151

137152
# Delete plugin
@@ -144,6 +159,12 @@ wp plugin delete [<plugin>...] [--all]
144159
Deleted 'tinymce-templates' plugin.
145160
Success: Deleted 1 of 1 plugins.
146161

162+
# Delete all plugins excluding specified ones
163+
$ wp plugin delete --all --exclude=hello-dolly,jetpack
164+
Deleted 'akismet' plugin.
165+
Deleted 'tinymce-templates' plugin.
166+
Success: Deleted 2 of 2 plugins.
167+
147168

148169

149170
### wp plugin get
@@ -578,7 +599,7 @@ inactive, then it will be activated.
578599
Uninstalls one or more plugins.
579600

580601
~~~
581-
wp plugin uninstall [<plugin>...] [--deactivate] [--skip-delete] [--all]
602+
wp plugin uninstall [<plugin>...] [--deactivate] [--skip-delete] [--all] [--exclude=<name>]
582603
~~~
583604

584605
**OPTIONS**
@@ -596,12 +617,21 @@ wp plugin uninstall [<plugin>...] [--deactivate] [--skip-delete] [--all]
596617
[--all]
597618
If set, all plugins will be uninstalled.
598619

620+
[--exclude=<name>]
621+
Comma separated list of plugin slugs to be excluded from uninstall.
622+
599623
**EXAMPLES**
600624

601625
$ wp plugin uninstall hello
602626
Uninstalled and deleted 'hello' plugin.
603627
Success: Uninstalled 1 of 1 plugins.
604628

629+
# Uninstall all plugins excluding specified ones
630+
$ wp plugin uninstall --all --exclude=hello-dolly,jetpack
631+
Uninstalled and deleted 'akismet' plugin.
632+
Uninstalled and deleted 'tinymce-templates' plugin.
633+
Success: Uninstalled 2 of 2 plugins.
634+
605635

606636

607637
### wp plugin update

features/plugin-activate.feature

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Feature: Activate WordPress plugins
7878
When I run `wp plugin activate --all`
7979
Then STDOUT should be:
8080
"""
81-
Success: No plugins installed.
81+
Success: No plugins activated.
8282
"""
8383

8484
@require-wp-5.2
@@ -106,3 +106,12 @@ Feature: Activate WordPress plugins
106106
"""
107107
1 out of 1
108108
"""
109+
110+
Scenario: Adding --exclude with plugin activate --all should exclude the plugins specified via --exclude
111+
When I try `wp plugin activate --all --exclude=hello`
112+
Then STDOUT should be:
113+
"""
114+
Plugin 'akismet' activated.
115+
Success: Activated 1 of 1 plugins.
116+
"""
117+
And the return code should be 0

features/plugin-deactivate.feature

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,23 @@ Feature: Deactivate WordPress plugins
7474
When I run `wp plugin deactivate --all`
7575
Then STDOUT should be:
7676
"""
77-
Success: No plugins installed.
77+
Success: No plugins deactivated.
7878
"""
79+
80+
Scenario: Adding --exclude with plugin deactivate --all should exclude the plugins specified via --exclude
81+
When I try `wp plugin deactivate --all --exclude=hello`
82+
Then STDOUT should be:
83+
"""
84+
Plugin 'akismet' deactivated.
85+
Success: Deactivated 1 of 1 plugins.
86+
"""
87+
And the return code should be 0
88+
89+
Scenario: Adding --exclude with plugin deactivate should throw an error unless --all given
90+
When I try `wp plugin deactivate --exclude=hello`
91+
Then the return code should be 1
92+
And STDERR should be:
93+
"""
94+
Error: Please specify one or more plugins, or use --all.
95+
"""
96+
And STDOUT should be empty

features/plugin-delete.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ Feature: Delete WordPress plugins
3939
Warning: The 'edit-flow' plugin could not be found.
4040
"""
4141
And the return code should be 0
42+
43+
Scenario: Excluding a plugin from deletion when using --all switch
44+
When I try `wp plugin delete --all --exclude=akismet,hello`
45+
Then STDOUT should be:
46+
"""
47+
Success: No plugins deleted.
48+
"""
49+
And the return code should be 0

features/plugin-uninstall.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,11 @@ Feature: Uninstall a WordPress plugin
7171
"""
7272
Success: Uninstalled 2 of 2 plugins.
7373
"""
74+
75+
Scenario: Excluding a plugin from uninstallation when using --all switch
76+
When I try `wp plugin uninstall --all --exclude=akismet,hello`
77+
Then STDOUT should be:
78+
"""
79+
Success: No plugins uninstalled.
80+
"""
81+
And the return code should be 0

src/Plugin_Command.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ protected function get_all_items() {
289289
* [--all]
290290
* : If set, all plugins will be activated.
291291
*
292+
* [--exclude=<name>]
293+
* : Comma separated list of plugin slugs to be excluded from activation.
294+
*
292295
* [--network]
293296
* : If set, the plugin will be activated for the entire multisite network.
294297
*
@@ -307,8 +310,9 @@ protected function get_all_items() {
307310
public function activate( $args, $assoc_args = array() ) {
308311
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
309312
$all = Utils\get_flag_value( $assoc_args, 'all', false );
313+
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
310314

311-
$args = $this->check_optional_args_and_all( $args, $all );
315+
$args = $this->check_optional_args_and_all( $args, $all, 'activate', $all_exclude );
312316
if ( ! $args ) {
313317
return;
314318
}
@@ -375,6 +379,9 @@ public function activate( $args, $assoc_args = array() ) {
375379
* [--all]
376380
* : If set, all plugins will be deactivated.
377381
*
382+
* [--exclude=<name>]
383+
* : Comma separated list of plugin slugs that should be excluded from deactivation.
384+
*
378385
* [--network]
379386
* : If set, the plugin will be deactivated for the entire multisite network.
380387
*
@@ -384,12 +391,19 @@ public function activate( $args, $assoc_args = array() ) {
384391
* $ wp plugin deactivate hello
385392
* Plugin 'hello' deactivated.
386393
* Success: Deactivated 1 of 1 plugins.
394+
*
395+
* # Deactivate all plugins with exclusion
396+
* $ wp plugin deactivate --all --exclude=hello,wordpress-seo
397+
* Plugin 'contact-form-7' deactivated.
398+
* Plugin 'ninja-forms' deactivated.
399+
* Success: Deactivated 2 of 2 plugins.
387400
*/
388401
public function deactivate( $args, $assoc_args = array() ) {
389-
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
390-
$disable_all = Utils\get_flag_value( $assoc_args, 'all' );
402+
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
403+
$disable_all = Utils\get_flag_value( $assoc_args, 'all' );
404+
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
391405

392-
$args = $this->check_optional_args_and_all( $args, $disable_all );
406+
$args = $this->check_optional_args_and_all( $args, $disable_all, 'deactivate', $disable_all_exclude );
393407
if ( ! $args ) {
394408
return;
395409
}
@@ -891,18 +905,28 @@ public function get( $args, $assoc_args ) {
891905
* [--all]
892906
* : If set, all plugins will be uninstalled.
893907
*
908+
* [--exclude=<name>]
909+
* : Comma separated list of plugin slugs to be excluded from uninstall.
910+
*
894911
* ## EXAMPLES
895912
*
896913
* $ wp plugin uninstall hello
897914
* Uninstalled and deleted 'hello' plugin.
898915
* Success: Uninstalled 1 of 1 plugins.
916+
*
917+
* # Uninstall all plugins excluding specified ones
918+
* $ wp plugin uninstall --all --exclude=hello-dolly,jetpack
919+
* Uninstalled and deleted 'akismet' plugin.
920+
* Uninstalled and deleted 'tinymce-templates' plugin.
921+
* Success: Uninstalled 2 of 2 plugins.
899922
*/
900923
public function uninstall( $args, $assoc_args = array() ) {
901924

902-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
925+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
926+
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );
903927

904928
// Check if plugin names or --all is passed.
905-
$args = $this->check_optional_args_and_all( $args, $all, 'uninstall' );
929+
$args = $this->check_optional_args_and_all( $args, $all, 'uninstall', $all_exclude );
906930
if ( ! $args ) {
907931
return;
908932
}
@@ -1014,6 +1038,9 @@ public function is_active( $args, $assoc_args = array() ) {
10141038
* [--all]
10151039
* : If set, all plugins will be deleted.
10161040
*
1041+
* [--exclude=<name>]
1042+
* : Comma separated list of plugin slugs to be excluded from deletion.
1043+
*
10171044
* ## EXAMPLES
10181045
*
10191046
* # Delete plugin
@@ -1025,12 +1052,19 @@ public function is_active( $args, $assoc_args = array() ) {
10251052
* $ wp plugin delete $(wp plugin list --status=inactive --field=name)
10261053
* Deleted 'tinymce-templates' plugin.
10271054
* Success: Deleted 1 of 1 plugins.
1055+
*
1056+
* # Delete all plugins excluding specified ones
1057+
* $ wp plugin delete --all --exclude=hello-dolly,jetpack
1058+
* Deleted 'akismet' plugin.
1059+
* Deleted 'tinymce-templates' plugin.
1060+
* Success: Deleted 2 of 2 plugins.
10281061
*/
10291062
public function delete( $args, $assoc_args = array() ) {
1030-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
1063+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
1064+
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );
10311065

10321066
// Check if plugin names or --all is passed.
1033-
$args = $this->check_optional_args_and_all( $args, $all, 'delete' );
1067+
$args = $this->check_optional_args_and_all( $args, $all, 'delete', $all_exclude );
10341068
if ( ! $args ) {
10351069
return;
10361070
}

src/WP_CLI/ParsePluginNameInput.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,27 @@ trait ParsePluginNameInput {
1313
* @param bool $all All flag.
1414
* @param string $verb Optional. Verb to use. Defaults to 'install'.
1515
* @return array Same as $args if not all, otherwise all slugs.
16+
* @param string $exclude Comma separated list of plugin slugs.
1617
* @throws ExitException If neither plugin name nor --all were provided.
1718
*/
18-
protected function check_optional_args_and_all( $args, $all, $verb = 'install' ) {
19+
protected function check_optional_args_and_all( $args, $all, $verb = 'install', $exclude = null ) {
1920
if ( $all ) {
2021
$args = array_map(
2122
'\WP_CLI\Utils\get_plugin_name',
2223
array_keys( $this->get_all_plugins() )
2324
);
2425
}
2526

27+
if ( $all && $exclude ) {
28+
$exclude_list = explode( ',', trim( $exclude, ',' ) );
29+
$args = array_filter(
30+
$args,
31+
static function( $slug ) use ( $exclude_list ) {
32+
return ! in_array( $slug, $exclude_list, true );
33+
}
34+
);
35+
}
36+
2637
if ( empty( $args ) ) {
2738
if ( ! $all ) {
2839
WP_CLI::error( 'Please specify one or more plugins, or use --all.' );

0 commit comments

Comments
 (0)