Commit 116760c1 authored by Erick Hitter's avatar Erick Hitter
Browse files

Don't run unnecessary delete queries

parent 65b9184e
......@@ -538,10 +538,22 @@ class Events_Store extends Singleton {
/**
* Remove entries for non-recurring events that have been run
*/
public function purge_completed_events() {
public function purge_completed_events( $count_first = true ) {
global $wpdb;
$wpdb->delete( $this->get_table_name(), array( 'status' => self::STATUS_COMPLETED, ) );
// Skip count if already performed
if ( $count_first ) {
$srtm = $wpdb->srtm;
$wpdb->srtm = true;
$count = $this->count_events_by_status( self::STATUS_COMPLETED );
$wpdb->srtm = $srtm;
} else {
$count = 1;
}
if ( $count > 0 ) {
$wpdb->delete( $this->get_table_name(), array( 'status' => self::STATUS_COMPLETED, ) );
}
}
/**
......
......@@ -91,9 +91,7 @@ class Events extends \WP_CLI_Command {
// Remove all completed events
if ( isset( $assoc_args['completed'] ) ) {
\WP_CLI::confirm( sprintf( __( 'Remove all completed events?', 'automattic-cron-control' ) ) );
\Automattic\WP\Cron_Control\Events_Store::instance()->purge_completed_events();
\WP_CLI::success( __( 'Entries removed', 'automattic-cron-control' ) );
$this->delete_completed_events( $args, $assoc_args );
return;
}
......@@ -583,6 +581,19 @@ class Events extends \WP_CLI_Command {
return;
}
/**
* Delete all completed events
*/
private function delete_completed_events( $args, $assoc_args ) {
$count = \Automattic\WP\Cron_Control\count_events_by_status( \Automattic\WP\Cron_Control\Events_Store::STATUS_COMPLETED );
\WP_CLI::confirm( sprintf( _n( 'Found one completed event to remove. Continue?', 'Found %s completed events to remove. Continue?', $count, 'automattic-cron-control' ), number_format_i18n( $count ) ) );
\Automattic\WP\Cron_Control\Events_Store::instance()->purge_completed_events( false );
\WP_CLI::success( __( 'Entries removed', 'automattic-cron-control' ) );
}
}
\WP_CLI::add_command( 'cron-control events', 'Automattic\WP\Cron_Control\CLI\Events' );
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment