Skip to content

Commit 0ef2e34

Browse files
authored
Merge pull request #438 from dkoston/dkoston/delete_plugins
2 parents 5710a18 + 22beda4 commit 0ef2e34

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

features/plugin-uninstall.feature

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,47 @@ Feature: Uninstall a WordPress plugin
33
Background:
44
Given a WP install
55

6-
Scenario: Uninstall an installed plugin
6+
Scenario: Uninstall an installed plugin should uninstall, delete files
77
When I run `wp plugin uninstall akismet`
88
Then STDOUT should be:
99
"""
1010
Uninstalled and deleted 'akismet' plugin.
1111
Success: Uninstalled 1 of 1 plugins.
1212
"""
1313
And the return code should be 0
14+
And STDERR should be empty
15+
And the wp-content/plugins/akismet directory should not exist
16+
17+
Scenario: Uninstall an installed plugin but do not delete its files
18+
When I run `wp plugin uninstall akismet --skip-delete`
19+
Then STDOUT should be:
20+
"""
21+
Ran uninstall procedure for 'akismet' plugin without deleting.
22+
Success: Uninstalled 1 of 1 plugins.
23+
"""
24+
And the return code should be 0
25+
And STDERR should be empty
26+
And the wp-content/plugins/akismet directory should exist
27+
28+
Scenario: Uninstall a plugin that is not in a folder and has custom name
29+
When I run `wp plugin uninstall hello`
30+
Then STDOUT should be:
31+
"""
32+
Uninstalled and deleted 'hello' plugin.
33+
Success: Uninstalled 1 of 1 plugins.
34+
"""
35+
And the return code should be 0
36+
And STDERR should be empty
37+
And the wp-content/plugins/hello.php file should not exist
38+
39+
Scenario: Missing required inputs
40+
When I try `wp plugin uninstall`
41+
Then STDERR should be:
42+
"""
43+
Error: Please specify one or more plugins, or use --all.
44+
"""
45+
And the return code should be 1
46+
And STDOUT should be empty
1447

1548
Scenario: Attempting to uninstall a plugin that's activated
1649
When I run `wp plugin activate akismet`
@@ -25,6 +58,21 @@ Feature: Uninstall a WordPress plugin
2558
And STDOUT should be empty
2659
And the return code should be 1
2760

61+
Scenario: Attempting to uninstall a plugin that's activated (using --deactivate)
62+
When I run `wp plugin activate akismet`
63+
Then STDOUT should not be empty
64+
65+
When I try `wp plugin uninstall akismet --deactivate`
66+
Then STDOUT should be:
67+
"""
68+
Deactivating 'akismet'...
69+
Plugin 'akismet' deactivated.
70+
Uninstalled and deleted 'akismet' plugin.
71+
Success: Uninstalled 1 of 1 plugins.
72+
"""
73+
And STDERR should be empty
74+
And the return code should be 0
75+
2876
Scenario: Attempting to uninstall a plugin that doesn't exist
2977
When I try `wp plugin uninstall debug-bar`
3078
Then STDERR should be:
@@ -43,12 +91,14 @@ Feature: Uninstall a WordPress plugin
4391
Success: Uninstalled 2 of 2 plugins.
4492
"""
4593
And the return code should be 0
94+
And STDERR should be empty
4695

4796
When I run the previous command again
4897
Then STDOUT should be:
4998
"""
5099
Success: No plugins uninstalled.
51100
"""
101+
And STDERR should be empty
52102

53103
Scenario: Uninstall all installed plugins when one or more activated
54104
When I run `wp plugin activate --all`
@@ -65,12 +115,14 @@ Feature: Uninstall a WordPress plugin
65115
Error: No plugins uninstalled.
66116
"""
67117
And the return code should be 1
118+
And STDOUT should be empty
68119

69120
When I run `wp plugin uninstall --deactivate --all`
70121
Then STDOUT should contain:
71122
"""
72123
Success: Uninstalled 2 of 2 plugins.
73124
"""
125+
And STDERR should be empty
74126

75127
Scenario: Excluding a plugin from uninstallation when using --all switch
76128
When I try `wp plugin uninstall --all --exclude=akismet,hello`
@@ -79,6 +131,7 @@ Feature: Uninstall a WordPress plugin
79131
Success: No plugins uninstalled.
80132
"""
81133
And the return code should be 0
134+
And STDERR should be empty
82135

83136
Scenario: Excluding a missing plugin should not throw an error
84137
Given a WP install
@@ -91,7 +144,7 @@ Feature: Uninstall a WordPress plugin
91144
And the return code should be 0
92145

93146
@require-wp-5.2
94-
Scenario: Uninstalling a plugin should remove its language pack too
147+
Scenario: Uninstalling a plugin should remove its language pack
95148
Given a WP install
96149
And I run `wp plugin install wordpress-importer`
97150
And I run `wp core language install fr_FR`
@@ -104,6 +157,7 @@ Feature: Uninstall a WordPress plugin
104157
"""
105158
And the wp-content/languages/plugins/wordpress-importer-fr_FR.mo file should exist
106159
And the wp-content/languages/plugins/wordpress-importer-fr_FR.po file should exist
160+
And the wp-content/languages/plugins/wordpress-importer-fr_FR.l10n.php file should exist
107161

108162
When I run `wp plugin uninstall wordpress-importer`
109163
Then STDOUT should contain:
@@ -112,3 +166,29 @@ Feature: Uninstall a WordPress plugin
112166
"""
113167
And the wp-content/languages/plugins/wordpress-importer-fr_FR.mo file should not exist
114168
And the wp-content/languages/plugins/wordpress-importer-fr_FR.po file should not exist
169+
And the wp-content/languages/plugins/wordpress-importer-fr_FR.l10n.php file should not exist
170+
And STDERR should be empty
171+
172+
@require-wp-5.2
173+
Scenario: Uninstalling a plugin should remove its update info
174+
Given a WP install
175+
And I run `wp plugin install wordpress-importer --version=0.6`
176+
And I run `wp plugin status wordpress-importer`
177+
178+
And I run `wp transient get --network update_plugins`
179+
Then STDOUT should contain:
180+
"""
181+
wordpress-importer
182+
"""
183+
184+
When I run `wp plugin uninstall wordpress-importer`
185+
Then STDOUT should contain:
186+
"""
187+
Success:
188+
"""
189+
190+
When I run `wp transient get --network update_plugins`
191+
Then STDOUT should not contain:
192+
"""
193+
wordpress-importer
194+
"""

src/Plugin_Command.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,12 @@ public function uninstall( $args, $assoc_args = array() ) {
10981098
return;
10991099
}
11001100

1101-
$successes = 0;
1102-
$errors = 0;
1103-
$plugins = $this->fetcher->get_many( $args );
1101+
$successes = 0;
1102+
$errors = 0;
1103+
$delete_errors = array();
1104+
$deleted_plugin_files = array();
1105+
1106+
$plugins = $this->fetcher->get_many( $args );
11041107
if ( count( $plugins ) < count( $args ) ) {
11051108
$errors = count( $args ) - count( $plugins );
11061109
}
@@ -1140,6 +1143,7 @@ public function uninstall( $args, $assoc_args = array() ) {
11401143
foreach ( $translations as $translation => $data ) {
11411144
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
11421145
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
1146+
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.l10n.php' );
11431147

11441148
$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
11451149
if ( $json_translation_files ) {
@@ -1148,13 +1152,35 @@ public function uninstall( $args, $assoc_args = array() ) {
11481152
}
11491153
}
11501154

1151-
if ( ! Utils\get_flag_value( $assoc_args, 'skip-delete' ) && $this->delete_plugin( $plugin ) ) {
1152-
WP_CLI::log( "Uninstalled and deleted '$plugin->name' plugin." );
1155+
if ( ! Utils\get_flag_value( $assoc_args, 'skip-delete' ) ) {
1156+
if ( $this->delete_plugin( $plugin ) ) {
1157+
$deleted_plugin_files[] = $plugin->file;
1158+
WP_CLI::log( "Uninstalled and deleted '$plugin->name' plugin." );
1159+
} else {
1160+
$delete_errors[] = $plugin->file;
1161+
WP_CLI::log( "Ran uninstall procedure for '$plugin->name' plugin. Deletion of plugin files failed" );
1162+
++$errors;
1163+
continue;
1164+
}
11531165
} else {
11541166
WP_CLI::log( "Ran uninstall procedure for '$plugin->name' plugin without deleting." );
11551167
}
11561168
++$successes;
11571169
}
1170+
1171+
// Remove deleted plugins from the plugin updates list.
1172+
$current = get_site_transient( $this->upgrade_transient );
1173+
if ( $current ) {
1174+
// Don't remove the plugins that weren't deleted.
1175+
$deleted = array_diff( $deleted_plugin_files, $delete_errors );
1176+
1177+
foreach ( $deleted as $plugin_file ) {
1178+
unset( $current->response[ $plugin_file ] );
1179+
}
1180+
1181+
set_site_transient( $this->upgrade_transient, $current );
1182+
}
1183+
11581184
if ( ! $this->chained_command ) {
11591185
Utils\report_batch_operation_results( 'plugin', 'uninstall', count( $args ), $successes, $errors );
11601186
}
@@ -1474,7 +1500,16 @@ private function get_details( $file ) {
14741500
return $plugin_folder[ $plugin_file ];
14751501
}
14761502

1503+
/**
1504+
* Performs deletion of plugin files
1505+
*
1506+
* @param $plugin - Plugin fetcher object (name, file)
1507+
* @return bool - If plugin was deleted
1508+
*/
14771509
private function delete_plugin( $plugin ) {
1510+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1511+
do_action( 'delete_plugin', $plugin->file );
1512+
14781513
$plugin_dir = dirname( $plugin->file );
14791514
if ( '.' === $plugin_dir ) {
14801515
$plugin_dir = $plugin->file;
@@ -1495,6 +1530,11 @@ private function delete_plugin( $plugin ) {
14951530
$command = 'rm -rf ';
14961531
}
14971532

1498-
return ! WP_CLI::launch( $command . escapeshellarg( $path ), false );
1533+
$result = ! WP_CLI::launch( $command . escapeshellarg( $path ), false );
1534+
1535+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1536+
do_action( 'deleted_plugin', $plugin->file, $result );
1537+
1538+
return $result;
14991539
}
15001540
}

0 commit comments

Comments
 (0)