From 98fbf5326e07cce23cd8fab540ccd687d716ee87 Mon Sep 17 00:00:00 2001 From: Anthony Grullon Date: Thu, 10 Nov 2022 09:17:49 -0700 Subject: [PATCH] Delete plugin translation files when plugin is uninstalled (#339) * 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 --- composer.json | 1 + features/plugin-uninstall.feature | 23 +++++++++++++++++++++++ src/Plugin_Command.php | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/composer.json b/composer.json index 39ec0786..a9f7fe18 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/features/plugin-uninstall.feature b/features/plugin-uninstall.feature index e267507b..998d5a1a 100644 --- a/features/plugin-uninstall.feature +++ b/features/plugin-uninstall.feature @@ -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 diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 40829701..5c83e304 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -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 {