Skip to content

Commit

Permalink
Delete plugin translation files when plugin is uninstalled (#339)
Browse files Browse the repository at this point in the history
* Delete plugin translation files

* Always call `uninstall_plugin()` to restore existing behavior

* Add tests for language pack cleanup behavior

* Fix PHPCS issues

* Add `wp-cli/language-command` as a dev dependency

* Fix the `@require` tag

Co-authored-by: Daniel Bachhuber <[email protected]>
  • Loading branch information
agrullon95 and danielbachhuber authored Nov 10, 2022
1 parent 82e3dd3 commit a07cad7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require-dev": {
"wp-cli/cache-command": "^2.0",
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/language-command": "^2.0",
"wp-cli/scaffold-command": "^1.2 || ^2",
"wp-cli/wp-cli-tests": "^3.1"
},
Expand Down
23 changes: 23 additions & 0 deletions features/plugin-uninstall.feature
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ Feature: Uninstall a WordPress plugin
Success:
"""
And the return code should be 0

@require-wp-5.2
Scenario: Uninstalling a plugin should remove its language pack too
Given a WP install
And I run `wp plugin install wordpress-importer`
And I run `wp core language install fr_FR`
And I run `wp site switch-language fr_FR`

When I run `wp language plugin install wordpress-importer fr_FR`
Then STDOUT should contain:
"""
Success:
"""
And the wp-content/languages/plugins/wordpress-importer-fr_FR.mo file should exist
And the wp-content/languages/plugins/wordpress-importer-fr_FR.po file should exist

When I run `wp plugin uninstall wordpress-importer`
Then STDOUT should contain:
"""
Success:
"""
And the wp-content/languages/plugins/wordpress-importer-fr_FR.mo file should not exist
And the wp-content/languages/plugins/wordpress-importer-fr_FR.po file should not exist
27 changes: 27 additions & 0 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,33 @@ public function uninstall( $args, $assoc_args = array() ) {

uninstall_plugin( $plugin->file );

$plugin_translations = wp_get_installed_translations( 'plugins' );

$plugin_slug = dirname( $plugin->file );

if ( 'hello.php' === $plugin->file ) {
$plugin_slug = 'hello-dolly';
}

// Remove language files, silently.
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
$translations = $plugin_translations[ $plugin_slug ];

global $wp_filesystem;
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();

foreach ( $translations as $translation => $data ) {
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );

$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
if ( $json_translation_files ) {
array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
}
}
}

if ( ! Utils\get_flag_value( $assoc_args, 'skip-delete' ) && $this->delete_plugin( $plugin ) ) {
WP_CLI::log( "Uninstalled and deleted '$plugin->name' plugin." );
} else {
Expand Down

0 comments on commit a07cad7

Please sign in to comment.