Skip to content

Commit

Permalink
Plugin_Command: Add delete_pugin and deleted_plugin hooks to delete_p…
Browse files Browse the repository at this point in the history
…lugin(). Remove .10n.php files when removing language files. Do not count failed delete_plugins as successes, remove deleted plugins from plugin update list
  • Loading branch information
dkoston committed Dec 10, 2024
1 parent 5710a18 commit 0c83ae6
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,9 +1098,12 @@ public function uninstall( $args, $assoc_args = array() ) {
return;
}

$successes = 0;
$errors = 0;
$plugins = $this->fetcher->get_many( $args );
$successes = 0;
$errors = 0;
$delete_errors = array();
$deleted_plugin_files = array();

$plugins = $this->fetcher->get_many( $args );
if ( count( $plugins ) < count( $args ) ) {
$errors = count( $args ) - count( $plugins );
}
Expand Down Expand Up @@ -1140,6 +1143,7 @@ public function uninstall( $args, $assoc_args = array() ) {
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' );
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.l10n.php' );

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

if ( ! Utils\get_flag_value( $assoc_args, 'skip-delete' ) && $this->delete_plugin( $plugin ) ) {
WP_CLI::log( "Uninstalled and deleted '$plugin->name' plugin." );
if ( ! Utils\get_flag_value( $assoc_args, 'skip-delete' ) ) {
if ( $this->delete_plugin( $plugin ) ) {
$deleted_plugin_files[] = $plugin->file;
WP_CLI::log( "Uninstalled and deleted '$plugin->name' plugin." );
} else {
$delete_errors[] = $plugin->file;
WP_CLI::log( "Ran uninstall procedure for '$plugin->name' plugin. Deletion failed" );
++$errors;
continue;
}
} else {
WP_CLI::log( "Ran uninstall procedure for '$plugin->name' plugin without deleting." );
}
++$successes;
}

// Remove deleted plugins from the plugin updates list.
$current = get_site_transient( 'update_plugins' );
if ( $current ) {
// Don't remove the plugins that weren't deleted.
$deleted = array_diff( $deleted_plugin_files, $delete_errors );

foreach ( $deleted as $plugin_file ) {
unset( $current->response[ $plugin_file ] );
}

set_site_transient( 'update_plugins', $current );
}

if ( ! $this->chained_command ) {
Utils\report_batch_operation_results( 'plugin', 'uninstall', count( $args ), $successes, $errors );
}
Expand Down Expand Up @@ -1474,7 +1500,15 @@ private function get_details( $file ) {
return $plugin_folder[ $plugin_file ];
}

/**
* Performs deletion of plugin files
*
* @param $plugin - Plugin fetcher object (name, file)
* @return bool - If plugin was deleted
*/
private function delete_plugin( $plugin ) {
do_action( 'delete_plugin', $plugin->file );

Check failure on line 1510 in src/Plugin_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "delete_plugin".

$plugin_dir = dirname( $plugin->file );
if ( '.' === $plugin_dir ) {
$plugin_dir = $plugin->file;
Expand All @@ -1495,6 +1529,10 @@ private function delete_plugin( $plugin ) {
$command = 'rm -rf ';
}

return ! WP_CLI::launch( $command . escapeshellarg( $path ), false );
$result = ! WP_CLI::launch( $command . escapeshellarg( $path ), false );

do_action( 'deleted_plugin', $plugin->file, $result );

Check failure on line 1534 in src/Plugin_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "deleted_plugin".

return $result;
}
}

0 comments on commit 0c83ae6

Please sign in to comment.