Skip to content

Commit 7f0a040

Browse files
committed
Uninstall locale from all themes
Refs: #122 Add support for '--all' to the Theme uninstall function. Switch the report output to use a table and support for json.
1 parent 1eb823a commit 7f0a040

File tree

2 files changed

+149
-23
lines changed

2 files changed

+149
-23
lines changed

features/language-theme.feature

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,19 @@ Feature: Manage translation files for a WordPress install
122122
And the wp-content/languages/themes/twentyten-de_DE.po file should not exist
123123
And the wp-content/languages/themes/twentyten-de_DE.mo file should not exist
124124
"""
125-
Success: Language uninstalled.
126-
Success: Language uninstalled.
125+
Success: Language 'cs_CZ' for 'twentyten' uninstalled.
126+
Success: Language 'de_DE' for 'twentyten' uninstalled.
127127
"""
128128

129129
When I try `wp language theme uninstall twentyten de_DE`
130-
Then STDERR should be:
130+
Then STDERR should contain:
131131
"""
132-
Error: Language not installed.
132+
Language 'de_DE' not installed.
133+
"""
134+
And STDERR should contain:
135+
"""
136+
Error: No languages uninstalled (1 failed).
133137
"""
134-
And STDOUT should be empty
135138
And the return code should be 1
136139

137140
When I try `wp language theme install twentyten invalid_lang`
@@ -269,3 +272,23 @@ Feature: Manage translation files for a WordPress install
269272
twentysixteen,invalid_lang,"not available"
270273
"""
271274
And STDERR should be empty
275+
276+
When I run `wp language theme uninstall --all de_DE --format=csv`
277+
Then the return code should be 0
278+
And STDOUT should be:
279+
"""
280+
name,locale,status
281+
twentyseventeen,de_DE,uninstalled
282+
twentysixteen,de_DE,uninstalled
283+
"""
284+
And STDERR should be empty
285+
286+
When I run `wp language theme uninstall --all de_DE --format=csv`
287+
Then the return code should be 0
288+
And STDOUT should be:
289+
"""
290+
name,locale,status
291+
twentyseventeen,de_DE,"not installed"
292+
twentysixteen,de_DE,"not installed"
293+
"""
294+
And STDERR should be empty

src/Theme_Language_Command.php

Lines changed: 121 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ private function install_one( $args, $assoc_args ) {
289289
}
290290
}
291291
}
292-
293292
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
294293
}
295294

@@ -376,12 +375,26 @@ private function install_many( $args, $assoc_args ) {
376375
*
377376
* ## OPTIONS
378377
*
379-
* <theme>
378+
* [<theme>]
380379
* : Theme to uninstall language for.
381380
*
381+
* [--all]
382+
* : If set, languages for all themes will be uninstalled.
383+
*
382384
* <language>...
383385
* : Language code to uninstall.
384386
*
387+
* [--format=<format>]
388+
* : Render output in a particular format. Used when installing languages for all themes.
389+
* ---
390+
* default: table
391+
* options:
392+
* - table
393+
* - csv
394+
* - json
395+
* - summary
396+
* ---
397+
*
385398
* ## EXAMPLES
386399
*
387400
* $ wp language theme uninstall twentyten ja
@@ -393,7 +406,33 @@ public function uninstall( $args, $assoc_args ) {
393406
/** @var WP_Filesystem_Base $wp_filesystem */
394407
global $wp_filesystem;
395408

396-
$theme = array_shift( $args );
409+
if ( empty( $assoc_args['format'] ) ) {
410+
$assoc_args['format'] = 'table';
411+
}
412+
413+
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
414+
$logger = new \WP_CLI\Loggers\Quiet();
415+
\WP_CLI::set_logger( $logger );
416+
}
417+
418+
/* process all themes */
419+
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );
420+
421+
if ( ! $all && count( $args ) < 2 ) {
422+
\WP_CLI::error( 'Please specify a theme, or use --all.' );
423+
}
424+
425+
if ( $all ) {
426+
$themes = wp_get_themes();
427+
$process_themes = array();
428+
foreach ( $themes as $theme_path => $theme_details ) {
429+
$theme_name = \WP_CLI\Utils\get_theme_name( $theme_path );
430+
array_push( $process_themes, $theme_name );
431+
}
432+
} else {
433+
$process_themes = array( array_shift( $args ) );
434+
}
435+
397436
$language_codes = (array) $args;
398437
$current_locale = get_locale();
399438

@@ -404,26 +443,90 @@ public function uninstall( $args, $assoc_args ) {
404443
\WP_CLI::error( 'No files found in language directory.' );
405444
}
406445

407-
// As of WP 4.0, no API for deleting a language pack
408-
WP_Filesystem();
409-
$available = $this->get_installed_languages( $theme );
446+
$count = count( $process_themes ) * count( $language_codes );
410447

411-
foreach ( $language_codes as $language_code ) {
412-
if ( ! in_array( $language_code, $available, true ) ) {
413-
WP_CLI::error( 'Language not installed.' );
414-
}
448+
$results = array();
415449

416-
if ( $language_code === $current_locale ) {
417-
WP_CLI::warning( "The '{$language_code}' language is active." );
418-
exit;
419-
}
450+
$successes = 0;
451+
$errors = 0;
452+
$skips = 0;
420453

421-
if ( $wp_filesystem->delete( "{$dir}/{$theme}-{$language_code}.po" ) && $wp_filesystem->delete( "{$dir}/{$theme}-{$language_code}.mo" ) ) {
422-
WP_CLI::success( 'Language uninstalled.' );
423-
} else {
424-
WP_CLI::error( "Couldn't uninstall language." );
454+
foreach ( $process_themes as $theme ) {
455+
// As of WP 4.0, no API for deleting a language pack
456+
WP_Filesystem();
457+
$available_languages = $this->get_installed_languages( $theme );
458+
459+
foreach ( $language_codes as $language_code ) {
460+
$result = [
461+
'name' => $theme,
462+
'locale' => $language_code,
463+
'status' => 'not available',
464+
];
465+
466+
if ( ! in_array( $language_code, $available_languages, true ) ) {
467+
$result['status'] = 'not installed';
468+
\WP_CLI::warning( "Language '{$language_code}' not installed." );
469+
if ( $all ) {
470+
++$skips;
471+
} else {
472+
++$errors;
473+
}
474+
$results[] = (object) $result;
475+
continue;
476+
}
477+
478+
if ( $language_code === $current_locale ) {
479+
\WP_CLI::warning( "The '{$language_code}' language is active." );
480+
exit;
481+
}
482+
483+
$po_file = "{$dir}/{$theme}-{$language_code}.po";
484+
$mo_file = "{$dir}/{$theme}-{$language_code}.mo";
485+
486+
$files_to_remove = array( $po_file, $mo_file );
487+
488+
$count_files_removed = 0;
489+
$had_one_file = 0;
490+
foreach ( $files_to_remove as $file ) {
491+
if ( $wp_filesystem->exists( $file ) ) {
492+
$had_one_file = 1;
493+
if ( $wp_filesystem->delete( $file ) ) {
494+
++$count_files_removed;
495+
} else {
496+
\WP_CLI::error( "Couldn't uninstall language: $language_code from theme $theme." );
497+
}
498+
}
499+
}
500+
501+
if ( count( $files_to_remove ) === $count_files_removed ) {
502+
$result['status'] = 'uninstalled';
503+
++$successes;
504+
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' uninstalled." );
505+
} elseif ( $count_files_removed ) {
506+
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' partially uninstalled." );
507+
$result['status'] = 'partial uninstall';
508+
++$error;
509+
} else { /* $count_files_removed == 0 */
510+
if ( $had_one_file ) {
511+
\WP_CLI::log( "Couldn't uninstall language '{$language_code}' from theme {$theme}." );
512+
$result['status'] = 'failed to uninstall';
513+
++$error;
514+
} else {
515+
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' already uninstalled." );
516+
$result['status'] = 'already uninstalled';
517+
++$skips;
518+
}
519+
}
520+
521+
$results[] = (object) $result;
425522
}
426523
}
524+
525+
if ( 'summary' !== $assoc_args['format'] ) {
526+
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'name', 'locale', 'status' ) );
527+
}
528+
529+
\WP_CLI\Utils\report_batch_operation_results( 'language', 'uninstall', $count, $successes, $errors, $skips );
427530
}
428531

429532
/**

0 commit comments

Comments
 (0)