Skip to content

Commit

Permalink
Add wp plugin language uninstall --all (#128)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
swissspidy and danielbachhuber authored Oct 18, 2023
1 parent 4b69d65 commit 829cdf9
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 42 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,31 @@ These fields will be displayed by default for each translation:
Uninstalls a given language for a plugin.

~~~
wp language plugin uninstall <plugin> <language>...
wp language plugin uninstall [<plugin>] [--all] <language>... [--format=<format>]
~~~

**OPTIONS**

<plugin>
[<plugin>]
Plugin to uninstall language for.

[--all]
If set, languages for all plugins will be uninstalled.

<language>...
Language code to uninstall.

[--format=<format>]
Render output in a particular format. Used when installing languages for all plugins.
---
default: table
options:
- table
- csv
- json
- summary
---

**EXAMPLES**

$ wp language plugin uninstall hello-dolly ja
Expand Down
68 changes: 59 additions & 9 deletions features/language-plugin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,21 @@ Feature: Manage translation files for a WordPress install
And the wp-content/languages/plugins/hello-dolly-cs_CZ.mo file should not exist
And the wp-content/languages/plugins/hello-dolly-de_DE.po file should not exist
And the wp-content/languages/plugins/hello-dolly-de_DE.mo file should not exist
And STDOUT should be:
And STDOUT should contain:
"""
Success: Language uninstalled.
Success: Language uninstalled.
Language 'cs_CZ' for 'hello-dolly' uninstalled.
Language 'de_DE' for 'hello-dolly' uninstalled.
"""

When I try `wp language plugin uninstall hello-dolly fr_FR`
Then STDERR should be:
Then STDERR should contain:
"""
Error: Language not installed.
Warning: Language 'fr_FR' not installed.
"""
And STDERR should contain:
"""
Error: No languages uninstalled (1 failed).
"""
And STDOUT should be empty
And the return code should be 1

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

When I try `wp language plugin install hello-dolly invalid_lang`
When I try `wp language plugin install hello-dolly en_GB de_DE invalid_lang`
Then STDERR should be:
"""
Warning: Language 'invalid_lang' not available.
"""
And STDOUT should be:
And STDOUT should contain:
"""
Language 'de_DE' installed.
Language 'invalid_lang' not installed.
Success: Installed 0 of 1 languages (1 skipped).
Success: Installed 1 of 3 languages (2 skipped).
"""
And the return code should be 0

When I run `wp language plugin uninstall --all de_DE --format=csv`
Then the return code should be 0
And STDOUT should contain:
"""
akismet,de_DE,"not installed"
"""
And STDOUT should contain:
"""
hello,de_DE,"not installed"
"""
And STDOUT should contain:
"""
hello-dolly,de_DE,uninstalled
"""
And STDERR should be empty

When I run `wp language plugin uninstall --all de_DE --format=csv`
Then the return code should be 0
And STDOUT should contain:
"""
akismet,de_DE,"not installed"
"""
And STDOUT should contain:
"""
hello,de_DE,"not installed"
"""
And STDOUT should contain:
"""
hello-dolly,de_DE,"not installed"
"""
And STDERR should be empty

@require-wp-4.0
Scenario: Don't allow active language to be uninstalled
Given a WP install
Expand Down Expand Up @@ -206,6 +242,14 @@ Feature: Manage translation files for a WordPress install
"""
And STDOUT should be empty

When I try `wp language plugin uninstall de_DE`
Then the return code should be 1
And STDERR should be:
"""
Error: Please specify one or more plugins, or use --all.
"""
And STDOUT should be empty

Given an empty {PLUGIN_DIR} directory
When I run `wp language plugin list --all`
Then STDOUT should be:
Expand All @@ -219,6 +263,12 @@ Feature: Manage translation files for a WordPress install
Success: No plugins installed.
"""

When I run `wp language plugin uninstall de_DE --all`
Then STDOUT should be:
"""
Success: No plugins installed.
"""

@require-wp-4.0
Scenario: Ensure correct language is installed for plugin version
Given a WP install
Expand Down
18 changes: 16 additions & 2 deletions features/language-theme.feature
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ Feature: Manage translation files for a WordPress install
"""
And STDOUT should be empty

When I try `wp language theme uninstall de_DE`
Then the return code should be 1
And STDERR should be:
"""
Error: Please specify one or more themes, or use --all.
"""
And STDOUT should be empty

Given an empty {THEME_DIR} directory
When I run `wp language theme list --all`
Then STDOUT should be:
Expand All @@ -207,6 +215,12 @@ Feature: Manage translation files for a WordPress install
Success: No themes installed.
"""

When I run `wp language theme uninstall de_DE --all`
Then STDOUT should be:
"""
Success: No themes installed.
"""

@require-wp-4.0
Scenario: Ensure correct language is installed for theme version
Given a WP install
Expand Down Expand Up @@ -281,7 +295,7 @@ Feature: Manage translation files for a WordPress install
twentyseventeen,de_DE,uninstalled
twentysixteen,de_DE,uninstalled
"""
And STDERR should be empty
And STDERR should be empty

When I run `wp language theme uninstall --all de_DE --format=csv`
Then the return code should be 0
Expand All @@ -291,4 +305,4 @@ Feature: Manage translation files for a WordPress install
twentyseventeen,de_DE,"not installed"
twentysixteen,de_DE,"not installed"
"""
And STDERR should be empty
And STDERR should be empty
132 changes: 117 additions & 15 deletions src/Plugin_Language_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,26 @@ private function install_many( $args, $assoc_args ) {
*
* ## OPTIONS
*
* <plugin>
* [<plugin>]
* : Plugin to uninstall language for.
*
* [--all]
* : If set, languages for all plugins will be uninstalled.
*
* <language>...
* : Language code to uninstall.
*
* [--format=<format>]
* : Render output in a particular format. Used when installing languages for all plugins.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* $ wp language plugin uninstall hello-dolly ja
Expand All @@ -387,7 +401,32 @@ public function uninstall( $args, $assoc_args ) {
/** @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;

$plugin = array_shift( $args );
if ( empty( $assoc_args['format'] ) ) {
$assoc_args['format'] = 'table';
}

if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
$logger = new \WP_CLI\Loggers\Quiet();
\WP_CLI::set_logger( $logger );
}

$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );

if ( ! $all && count( $args ) < 2 ) {
\WP_CLI::error( 'Please specify one or more plugins, or use --all.' );
}

if ( $all ) {
$plugins = array_map( '\WP_CLI\Utils\get_plugin_name', array_keys( $this->get_all_plugins() ) );

if ( empty( $plugins ) ) {
WP_CLI::success( 'No plugins installed.' );
return;
}
} else {
$plugins = array( array_shift( $args ) );
}

$language_codes = (array) $args;
$current_locale = get_locale();

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

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

foreach ( $language_codes as $language_code ) {
if ( ! in_array( $language_code, $available, true ) ) {
\WP_CLI::error( 'Language not installed.' );
}
$count = count( $plugins ) * count( $language_codes );

if ( $language_code === $current_locale ) {
\WP_CLI::warning( "The '{$language_code}' language is active." );
exit;
}
$results = array();

if ( $wp_filesystem->delete( "{$dir}/{$plugin}-{$language_code}.po" ) && $wp_filesystem->delete( "{$dir}/{$plugin}-{$language_code}.mo" ) ) {
\WP_CLI::success( 'Language uninstalled.' );
} else {
\WP_CLI::error( "Couldn't uninstall language." );
$successes = 0;
$errors = 0;
$skips = 0;

foreach ( $plugins as $plugin ) {
$available = $this->get_installed_languages( $plugin );

foreach ( $language_codes as $language_code ) {
$result = [
'name' => $plugin,
'locale' => $language_code,
'status' => 'not available',
];

if ( ! in_array( $language_code, $available, true ) ) {
$result['status'] = 'not installed';
\WP_CLI::warning( "Language '{$language_code}' not installed." );
if ( $all ) {
++$skips;
} else {
++$errors;
}
$results[] = (object) $result;
continue;
}

if ( $language_code === $current_locale ) {
\WP_CLI::warning( "The '{$language_code}' language is active." );
exit;
}

$po_file = "{$dir}/{$plugin}-{$language_code}.po";
$mo_file = "{$dir}/{$plugin}-{$language_code}.mo";

$files_to_remove = array( $po_file, $mo_file );

$count_files_removed = 0;
$had_one_file = 0;
foreach ( $files_to_remove as $file ) {
if ( $wp_filesystem->exists( $file ) ) {
$had_one_file = 1;
if ( $wp_filesystem->delete( $file ) ) {
++$count_files_removed;
} else {
\WP_CLI::error( "Couldn't uninstall language: $language_code from plugin $plugin." );
}
}
}

if ( count( $files_to_remove ) === $count_files_removed ) {
$result['status'] = 'uninstalled';
++$successes;
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' uninstalled." );
} elseif ( $count_files_removed ) {
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' partially uninstalled." );
$result['status'] = 'partial uninstall';
++$errors;
} elseif ( $had_one_file ) { /* $count_files_removed == 0 */
\WP_CLI::log( "Couldn't uninstall language '{$language_code}' from plugin {$plugin}." );
$result['status'] = 'failed to uninstall';
++$errors;
} else {
\WP_CLI::log( "Language '{$language_code}' for '{$plugin}' already uninstalled." );
$result['status'] = 'already uninstalled';
++$skips;
}

$results[] = (object) $result;
}
}

if ( 'summary' !== $assoc_args['format'] ) {
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'name', 'locale', 'status' ) );
}

\WP_CLI\Utils\report_batch_operation_results( 'language', 'uninstall', $count, $successes, $errors, $skips );
}

/**
Expand Down
Loading

0 comments on commit 829cdf9

Please sign in to comment.