Skip to content

Commit 829cdf9

Browse files
Add wp plugin language uninstall --all (#128)
* Remove superfluous comment * Update tests * Move out of loop * Fix var name * Fix formatting * Add `wp plugin uninstall —all` support * Fix tests * Update wording * Fix tests * Undo change * Use correct var * themes -> plugins * Apply suggestions from code review Co-authored-by: Daniel Bachhuber <[email protected]> * Add test hardening --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 4b69d65 commit 829cdf9

File tree

5 files changed

+226
-42
lines changed

5 files changed

+226
-42
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,31 @@ These fields will be displayed by default for each translation:
409409
Uninstalls a given language for a plugin.
410410

411411
~~~
412-
wp language plugin uninstall <plugin> <language>...
412+
wp language plugin uninstall [<plugin>] [--all] <language>... [--format=<format>]
413413
~~~
414414

415415
**OPTIONS**
416416

417-
<plugin>
417+
[<plugin>]
418418
Plugin to uninstall language for.
419419

420+
[--all]
421+
If set, languages for all plugins will be uninstalled.
422+
420423
<language>...
421424
Language code to uninstall.
422425

426+
[--format=<format>]
427+
Render output in a particular format. Used when installing languages for all plugins.
428+
---
429+
default: table
430+
options:
431+
- table
432+
- csv
433+
- json
434+
- summary
435+
---
436+
423437
**EXAMPLES**
424438

425439
$ wp language plugin uninstall hello-dolly ja

features/language-plugin.feature

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,21 @@ Feature: Manage translation files for a WordPress install
127127
And the wp-content/languages/plugins/hello-dolly-cs_CZ.mo file should not exist
128128
And the wp-content/languages/plugins/hello-dolly-de_DE.po file should not exist
129129
And the wp-content/languages/plugins/hello-dolly-de_DE.mo file should not exist
130-
And STDOUT should be:
130+
And STDOUT should contain:
131131
"""
132-
Success: Language uninstalled.
133-
Success: Language uninstalled.
132+
Language 'cs_CZ' for 'hello-dolly' uninstalled.
133+
Language 'de_DE' for 'hello-dolly' uninstalled.
134134
"""
135135

136136
When I try `wp language plugin uninstall hello-dolly fr_FR`
137-
Then STDERR should be:
137+
Then STDERR should contain:
138138
"""
139-
Error: Language not installed.
139+
Warning: Language 'fr_FR' not installed.
140+
"""
141+
And STDERR should contain:
142+
"""
143+
Error: No languages uninstalled (1 failed).
140144
"""
141-
And STDOUT should be empty
142145
And the return code should be 1
143146

144147
When I try `wp language plugin uninstall hello-dolly en_GB`
@@ -149,18 +152,51 @@ Feature: Manage translation files for a WordPress install
149152
And STDOUT should be empty
150153
And the return code should be 0
151154

152-
When I try `wp language plugin install hello-dolly invalid_lang`
155+
When I try `wp language plugin install hello-dolly en_GB de_DE invalid_lang`
153156
Then STDERR should be:
154157
"""
155158
Warning: Language 'invalid_lang' not available.
156159
"""
157-
And STDOUT should be:
160+
And STDOUT should contain:
158161
"""
162+
Language 'de_DE' installed.
159163
Language 'invalid_lang' not installed.
160-
Success: Installed 0 of 1 languages (1 skipped).
164+
Success: Installed 1 of 3 languages (2 skipped).
161165
"""
162166
And the return code should be 0
163167

168+
When I run `wp language plugin uninstall --all de_DE --format=csv`
169+
Then the return code should be 0
170+
And STDOUT should contain:
171+
"""
172+
akismet,de_DE,"not installed"
173+
"""
174+
And STDOUT should contain:
175+
"""
176+
hello,de_DE,"not installed"
177+
"""
178+
And STDOUT should contain:
179+
"""
180+
hello-dolly,de_DE,uninstalled
181+
"""
182+
And STDERR should be empty
183+
184+
When I run `wp language plugin uninstall --all de_DE --format=csv`
185+
Then the return code should be 0
186+
And STDOUT should contain:
187+
"""
188+
akismet,de_DE,"not installed"
189+
"""
190+
And STDOUT should contain:
191+
"""
192+
hello,de_DE,"not installed"
193+
"""
194+
And STDOUT should contain:
195+
"""
196+
hello-dolly,de_DE,"not installed"
197+
"""
198+
And STDERR should be empty
199+
164200
@require-wp-4.0
165201
Scenario: Don't allow active language to be uninstalled
166202
Given a WP install
@@ -206,6 +242,14 @@ Feature: Manage translation files for a WordPress install
206242
"""
207243
And STDOUT should be empty
208244

245+
When I try `wp language plugin uninstall de_DE`
246+
Then the return code should be 1
247+
And STDERR should be:
248+
"""
249+
Error: Please specify one or more plugins, or use --all.
250+
"""
251+
And STDOUT should be empty
252+
209253
Given an empty {PLUGIN_DIR} directory
210254
When I run `wp language plugin list --all`
211255
Then STDOUT should be:
@@ -219,6 +263,12 @@ Feature: Manage translation files for a WordPress install
219263
Success: No plugins installed.
220264
"""
221265

266+
When I run `wp language plugin uninstall de_DE --all`
267+
Then STDOUT should be:
268+
"""
269+
Success: No plugins installed.
270+
"""
271+
222272
@require-wp-4.0
223273
Scenario: Ensure correct language is installed for plugin version
224274
Given a WP install

features/language-theme.feature

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ Feature: Manage translation files for a WordPress install
194194
"""
195195
And STDOUT should be empty
196196

197+
When I try `wp language theme uninstall de_DE`
198+
Then the return code should be 1
199+
And STDERR should be:
200+
"""
201+
Error: Please specify one or more themes, or use --all.
202+
"""
203+
And STDOUT should be empty
204+
197205
Given an empty {THEME_DIR} directory
198206
When I run `wp language theme list --all`
199207
Then STDOUT should be:
@@ -207,6 +215,12 @@ Feature: Manage translation files for a WordPress install
207215
Success: No themes installed.
208216
"""
209217

218+
When I run `wp language theme uninstall de_DE --all`
219+
Then STDOUT should be:
220+
"""
221+
Success: No themes installed.
222+
"""
223+
210224
@require-wp-4.0
211225
Scenario: Ensure correct language is installed for theme version
212226
Given a WP install
@@ -281,7 +295,7 @@ Feature: Manage translation files for a WordPress install
281295
twentyseventeen,de_DE,uninstalled
282296
twentysixteen,de_DE,uninstalled
283297
"""
284-
And STDERR should be empty
298+
And STDERR should be empty
285299

286300
When I run `wp language theme uninstall --all de_DE --format=csv`
287301
Then the return code should be 0
@@ -291,4 +305,4 @@ Feature: Manage translation files for a WordPress install
291305
twentyseventeen,de_DE,"not installed"
292306
twentysixteen,de_DE,"not installed"
293307
"""
294-
And STDERR should be empty
308+
And STDERR should be empty

src/Plugin_Language_Command.php

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,26 @@ private function install_many( $args, $assoc_args ) {
370370
*
371371
* ## OPTIONS
372372
*
373-
* <plugin>
373+
* [<plugin>]
374374
* : Plugin to uninstall language for.
375375
*
376+
* [--all]
377+
* : If set, languages for all plugins will be uninstalled.
378+
*
376379
* <language>...
377380
* : Language code to uninstall.
378381
*
382+
* [--format=<format>]
383+
* : Render output in a particular format. Used when installing languages for all plugins.
384+
* ---
385+
* default: table
386+
* options:
387+
* - table
388+
* - csv
389+
* - json
390+
* - summary
391+
* ---
392+
*
379393
* ## EXAMPLES
380394
*
381395
* $ wp language plugin uninstall hello-dolly ja
@@ -387,7 +401,32 @@ public function uninstall( $args, $assoc_args ) {
387401
/** @var WP_Filesystem_Base $wp_filesystem */
388402
global $wp_filesystem;
389403

390-
$plugin = array_shift( $args );
404+
if ( empty( $assoc_args['format'] ) ) {
405+
$assoc_args['format'] = 'table';
406+
}
407+
408+
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
409+
$logger = new \WP_CLI\Loggers\Quiet();
410+
\WP_CLI::set_logger( $logger );
411+
}
412+
413+
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
414+
415+
if ( ! $all && count( $args ) < 2 ) {
416+
\WP_CLI::error( 'Please specify one or more plugins, or use --all.' );
417+
}
418+
419+
if ( $all ) {
420+
$plugins = array_map( '\WP_CLI\Utils\get_plugin_name', array_keys( $this->get_all_plugins() ) );
421+
422+
if ( empty( $plugins ) ) {
423+
WP_CLI::success( 'No plugins installed.' );
424+
return;
425+
}
426+
} else {
427+
$plugins = array( array_shift( $args ) );
428+
}
429+
391430
$language_codes = (array) $args;
392431
$current_locale = get_locale();
393432

@@ -400,24 +439,87 @@ public function uninstall( $args, $assoc_args ) {
400439

401440
// As of WP 4.0, no API for deleting a language pack
402441
WP_Filesystem();
403-
$available = $this->get_installed_languages( $plugin );
404442

405-
foreach ( $language_codes as $language_code ) {
406-
if ( ! in_array( $language_code, $available, true ) ) {
407-
\WP_CLI::error( 'Language not installed.' );
408-
}
443+
$count = count( $plugins ) * count( $language_codes );
409444

410-
if ( $language_code === $current_locale ) {
411-
\WP_CLI::warning( "The '{$language_code}' language is active." );
412-
exit;
413-
}
445+
$results = array();
414446

415-
if ( $wp_filesystem->delete( "{$dir}/{$plugin}-{$language_code}.po" ) && $wp_filesystem->delete( "{$dir}/{$plugin}-{$language_code}.mo" ) ) {
416-
\WP_CLI::success( 'Language uninstalled.' );
417-
} else {
418-
\WP_CLI::error( "Couldn't uninstall language." );
447+
$successes = 0;
448+
$errors = 0;
449+
$skips = 0;
450+
451+
foreach ( $plugins as $plugin ) {
452+
$available = $this->get_installed_languages( $plugin );
453+
454+
foreach ( $language_codes as $language_code ) {
455+
$result = [
456+
'name' => $plugin,
457+
'locale' => $language_code,
458+
'status' => 'not available',
459+
];
460+
461+
if ( ! in_array( $language_code, $available, true ) ) {
462+
$result['status'] = 'not installed';
463+
\WP_CLI::warning( "Language '{$language_code}' not installed." );
464+
if ( $all ) {
465+
++$skips;
466+
} else {
467+
++$errors;
468+
}
469+
$results[] = (object) $result;
470+
continue;
471+
}
472+
473+
if ( $language_code === $current_locale ) {
474+
\WP_CLI::warning( "The '{$language_code}' language is active." );
475+
exit;
476+
}
477+
478+
$po_file = "{$dir}/{$plugin}-{$language_code}.po";
479+
$mo_file = "{$dir}/{$plugin}-{$language_code}.mo";
480+
481+
$files_to_remove = array( $po_file, $mo_file );
482+
483+
$count_files_removed = 0;
484+
$had_one_file = 0;
485+
foreach ( $files_to_remove as $file ) {
486+
if ( $wp_filesystem->exists( $file ) ) {
487+
$had_one_file = 1;
488+
if ( $wp_filesystem->delete( $file ) ) {
489+
++$count_files_removed;
490+
} else {
491+
\WP_CLI::error( "Couldn't uninstall language: $language_code from plugin $plugin." );
492+
}
493+
}
494+
}
495+
496+
if ( count( $files_to_remove ) === $count_files_removed ) {
497+
$result['status'] = 'uninstalled';
498+
++$successes;
499+
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' uninstalled." );
500+
} elseif ( $count_files_removed ) {
501+
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' partially uninstalled." );
502+
$result['status'] = 'partial uninstall';
503+
++$errors;
504+
} elseif ( $had_one_file ) { /* $count_files_removed == 0 */
505+
\WP_CLI::log( "Couldn't uninstall language '{$language_code}' from plugin {$plugin}." );
506+
$result['status'] = 'failed to uninstall';
507+
++$errors;
508+
} else {
509+
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' already uninstalled." );
510+
$result['status'] = 'already uninstalled';
511+
++$skips;
512+
}
513+
514+
$results[] = (object) $result;
419515
}
420516
}
517+
518+
if ( 'summary' !== $assoc_args['format'] ) {
519+
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'name', 'locale', 'status' ) );
520+
}
521+
522+
\WP_CLI\Utils\report_batch_operation_results( 'language', 'uninstall', $count, $successes, $errors, $skips );
421523
}
422524

423525
/**

0 commit comments

Comments
 (0)