From 726ff432bfd67e0176e615dedc297315a4752c23 Mon Sep 17 00:00:00 2001 From: Anthony Grullon Date: Wed, 9 Nov 2022 16:27:38 -0700 Subject: [PATCH 1/6] Delete plugin translation files --- src/Plugin_Command.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 9fe6eaac2..34a862821 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -951,8 +951,37 @@ public function uninstall( $args, $assoc_args = array() ) { $this->deactivate( array( $plugin->name ) ); $this->chained_command = false; } + + if ( is_uninstallable_plugin( $plugin->file ) ) { + uninstall_plugin( $plugin->file ); - 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." ); From 8201768b3bc365df076ed5c03110bc38e43b5cbd Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 06:20:49 -0700 Subject: [PATCH 2/6] Always call `uninstall_plugin()` to restore existing behavior --- src/Plugin_Command.php | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 5fda2871c..f044680ca 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -951,34 +951,32 @@ public function uninstall( $args, $assoc_args = array() ) { $this->deactivate( array( $plugin->name ) ); $this->chained_command = false; } - - if ( is_uninstallable_plugin( $plugin->file ) ) { - uninstall_plugin( $plugin->file ); - $plugin_translations = wp_get_installed_translations( 'plugins' ); + uninstall_plugin( $plugin->file ); - $plugin_slug = dirname( $plugin->file ); + $plugin_translations = wp_get_installed_translations( 'plugins' ); - if ( 'hello.php' === $plugin->file ) { - $plugin_slug = 'hello-dolly'; - } + $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 ]; + // 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(); + 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' ); + 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 ); - } + $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 ); } } } From add89080557b2e2deba7222cac2c1a71b68c6be2 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 06:21:06 -0700 Subject: [PATCH 3/6] Add tests for language pack cleanup behavior --- features/plugin-uninstall.feature | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/features/plugin-uninstall.feature b/features/plugin-uninstall.feature index e267507b7..af6480d35 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 + + @requires-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 From 869f9148d27862a0c7495adb62a2220fa48855be Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 06:24:31 -0700 Subject: [PATCH 4/6] Fix PHPCS issues --- src/Plugin_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index f044680ca..5c83e3043 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -967,7 +967,7 @@ public function uninstall( $args, $assoc_args = array() ) { $translations = $plugin_translations[ $plugin_slug ]; global $wp_filesystem; - require_once ( ABSPATH . '/wp-admin/includes/file.php' ); + require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); foreach ( $translations as $translation => $data ) { From 4333a934f9df75fd5207edea93f75bf8095ed12b Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 06:37:24 -0700 Subject: [PATCH 5/6] Add `wp-cli/language-command` as a dev dependency --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 39ec0786f..a9f7fe18a 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" }, From 7cdd9ecc6d4fbded2718ae03382c61884a6b47f5 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 06:50:12 -0700 Subject: [PATCH 6/6] Fix the `@require` tag --- features/plugin-uninstall.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/plugin-uninstall.feature b/features/plugin-uninstall.feature index af6480d35..998d5a1ab 100644 --- a/features/plugin-uninstall.feature +++ b/features/plugin-uninstall.feature @@ -92,7 +92,7 @@ Feature: Uninstall a WordPress plugin """ And the return code should be 0 - @requires-wp-5.2 + @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`