Skip to content

Commit

Permalink
Merge pull request #124 from atoomic/uninstall-all
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Oct 18, 2023
2 parents ce08eda + 7f0a040 commit 9b5abfa
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 23 deletions.
33 changes: 28 additions & 5 deletions features/language-theme.feature
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@ Feature: Manage translation files for a WordPress install
And the wp-content/languages/themes/twentyten-de_DE.po file should not exist
And the wp-content/languages/themes/twentyten-de_DE.mo file should not exist
"""
Success: Language uninstalled.
Success: Language uninstalled.
Success: Language 'cs_CZ' for 'twentyten' uninstalled.
Success: Language 'de_DE' for 'twentyten' uninstalled.
"""

When I try `wp language theme uninstall twentyten de_DE`
Then STDERR should be:
Then STDERR should contain:
"""
Error: Language not installed.
Language 'de_DE' 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 theme install twentyten invalid_lang`
Expand Down Expand Up @@ -269,3 +272,23 @@ Feature: Manage translation files for a WordPress install
twentysixteen,invalid_lang,"not available"
"""
And STDERR should be empty

When I run `wp language theme uninstall --all de_DE --format=csv`
Then the return code should be 0
And STDOUT should be:
"""
name,locale,status
twentyseventeen,de_DE,uninstalled
twentysixteen,de_DE,uninstalled
"""
And STDERR should be empty

When I run `wp language theme uninstall --all de_DE --format=csv`
Then the return code should be 0
And STDOUT should be:
"""
name,locale,status
twentyseventeen,de_DE,"not installed"
twentysixteen,de_DE,"not installed"
"""
And STDERR should be empty
139 changes: 121 additions & 18 deletions src/Theme_Language_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ private function install_one( $args, $assoc_args ) {
}
}
}

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

Expand Down Expand Up @@ -376,12 +375,26 @@ private function install_many( $args, $assoc_args ) {
*
* ## OPTIONS
*
* <theme>
* [<theme>]
* : Theme to uninstall language for.
*
* [--all]
* : If set, languages for all themes will be uninstalled.
*
* <language>...
* : Language code to uninstall.
*
* [--format=<format>]
* : Render output in a particular format. Used when installing languages for all themes.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* $ wp language theme uninstall twentyten ja
Expand All @@ -393,7 +406,33 @@ public function uninstall( $args, $assoc_args ) {
/** @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;

$theme = 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 );
}

/* process all themes */
$all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false );

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

if ( $all ) {
$themes = wp_get_themes();
$process_themes = array();
foreach ( $themes as $theme_path => $theme_details ) {
$theme_name = \WP_CLI\Utils\get_theme_name( $theme_path );
array_push( $process_themes, $theme_name );
}
} else {
$process_themes = array( array_shift( $args ) );
}

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

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

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

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

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

if ( $wp_filesystem->delete( "{$dir}/{$theme}-{$language_code}.po" ) && $wp_filesystem->delete( "{$dir}/{$theme}-{$language_code}.mo" ) ) {
WP_CLI::success( 'Language uninstalled.' );
} else {
WP_CLI::error( "Couldn't uninstall language." );
foreach ( $process_themes as $theme ) {
// As of WP 4.0, no API for deleting a language pack
WP_Filesystem();
$available_languages = $this->get_installed_languages( $theme );

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

if ( ! in_array( $language_code, $available_languages, 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}/{$theme}-{$language_code}.po";
$mo_file = "{$dir}/{$theme}-{$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 theme $theme." );
}
}
}

if ( count( $files_to_remove ) === $count_files_removed ) {
$result['status'] = 'uninstalled';
++$successes;
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' uninstalled." );
} elseif ( $count_files_removed ) {
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' partially uninstalled." );
$result['status'] = 'partial uninstall';
++$error;
} else { /* $count_files_removed == 0 */

Check failure on line 509 in src/Theme_Language_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

If control structure block found as the only statement within an "else" block. Use elseif instead.
if ( $had_one_file ) {
\WP_CLI::log( "Couldn't uninstall language '{$language_code}' from theme {$theme}." );
$result['status'] = 'failed to uninstall';
++$error;
} else {
\WP_CLI::log( "Language '{$language_code}' for '{$theme}' 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

0 comments on commit 9b5abfa

Please sign in to comment.