Skip to content

Commit 0c83ae6

Browse files
committed
Plugin_Command: Add delete_pugin and deleted_plugin hooks to delete_plugin(). Remove .10n.php files when removing language files. Do not count failed delete_plugins as successes, remove deleted plugins from plugin update list
1 parent 5710a18 commit 0c83ae6

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/Plugin_Command.php

Lines changed: 44 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 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( 'update_plugins' );
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( 'update_plugins', $current );
1182+
}
1183+
11581184
if ( ! $this->chained_command ) {
11591185
Utils\report_batch_operation_results( 'plugin', 'uninstall', count( $args ), $successes, $errors );
11601186
}
@@ -1474,7 +1500,15 @@ 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+
do_action( 'delete_plugin', $plugin->file );
1511+
14781512
$plugin_dir = dirname( $plugin->file );
14791513
if ( '.' === $plugin_dir ) {
14801514
$plugin_dir = $plugin->file;
@@ -1495,6 +1529,10 @@ private function delete_plugin( $plugin ) {
14951529
$command = 'rm -rf ';
14961530
}
14971531

1498-
return ! WP_CLI::launch( $command . escapeshellarg( $path ), false );
1532+
$result = ! WP_CLI::launch( $command . escapeshellarg( $path ), false );
1533+
1534+
do_action( 'deleted_plugin', $plugin->file, $result );
1535+
1536+
return $result;
14991537
}
15001538
}

0 commit comments

Comments
 (0)