Commit 0946c8fd authored by Erick Hitter's avatar Erick Hitter
Browse files

Block event creation during certain bulk operations

In certain CLI circumstances, it's counterproductive to create new events, so we shouldn't. For now, limited to deleting events.

Fixes #83
parent 5df3342c
......@@ -20,6 +20,8 @@ class Cron_Options_CPT extends Singleton {
private $option_before_unscheduling = null;
private $job_creation_suspended = false;
/**
* Register hooks
*/
......@@ -286,6 +288,11 @@ class Cron_Options_CPT extends Singleton {
* Also doesn't call `wp_insert_post()` because this function is needed before post types and capabilities are ready.
*/
public function create_or_update_job( $timestamp, $action, $args, $update_id = null ) {
// Don't create new jobs when manipulating jobs via the plugin's CLI commands
if ( $this->job_creation_suspended ) {
return;
}
global $wpdb;
// Build minimum information needed to create a post
......@@ -436,6 +443,22 @@ class Cron_Options_CPT extends Singleton {
private function event_title( $timestamp, $action, $instance ) {
return sprintf( '%s | %s | %s', $timestamp, $action, $instance );
}
/**
* Prevent CPT from creating new entries
*
* Should be used sparingly, and followed by a call to resume_event_creation(), during bulk operations
*/
public function suspend_event_creation() {
$this->job_creation_suspended = true;
}
/**
* Stop discarding events, once again storing them in the CPT
*/
public function resume_event_creation() {
$this->job_creation_suspended = false;
}
}
Cron_Options_CPT::instance();
......@@ -363,7 +363,9 @@ class Events extends \WP_CLI_Command {
\WP_CLI::confirm( sprintf( __( 'Are you sure you want to delete this event?', 'automattic-cron-control' ) ) );
// Try to delete the item and provide some relevant output
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
$trashed = wp_delete_post( $event_post->ID, true );
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
if ( false === $trashed ) {
\WP_CLI::error( sprintf( __( 'Failed to delete event %d', 'automattic-cron-control' ), $jid ) );
......@@ -474,6 +476,9 @@ class Events extends \WP_CLI_Command {
$events_deleted = array();
$events_deleted_count = $events_failed_delete = 0;
// Don't create new events while deleting events
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
foreach ( $events_to_delete as $event_to_delete ) {
$deleted = wp_delete_post( $event_to_delete['ID'], true );
......@@ -498,6 +503,9 @@ class Events extends \WP_CLI_Command {
\Automattic\WP\Cron_Control\_flush_internal_caches();
}
// New events can be created now that removal is complete
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
// List the removed items
\WP_CLI::line( "\n" . __( 'RESULTS:', 'automattic-cron-control' ) );
......
......@@ -25,6 +25,9 @@ class One_Time_Fixers extends \WP_CLI_Command {
// Provide some idea of what's going on
\WP_CLI::line( __( 'CRON CONTROL', 'automattic-cron-control' ) . "\n" );
// Don't create new events while deleting events
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = %s;", 'a8c_cron_ctrl_event' ) );
if ( is_numeric( $count ) ) {
......@@ -102,6 +105,9 @@ class One_Time_Fixers extends \WP_CLI_Command {
\WP_CLI::line( "\n" . sprintf( __( 'Cleared the %s cache', 'automattic-cron-control' ), 'Cron Control' ) );
}
// Let event creation resume
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
// Fin
\WP_CLI::success( __( 'All done.', 'automattic-cron-control' ) );
}
......
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