Commit 3ed84d22 authored by Erick Hitter's avatar Erick Hitter
Browse files

Standardize how automatic-execution flag is managed

parent 3cc39c57
......@@ -51,8 +51,7 @@ class Events extends Singleton {
} elseif ( $disabled > time() ) {
$this->run_disabled = $disabled;
} else {
update_option( self::DISABLE_RUN_OPTION, 0 );
$this->run_disabled = 0;
$this->update_run_status( 0 );
}
}
......@@ -495,11 +494,40 @@ class Events extends Singleton {
/**
* Return status of automatic event execution
*
* @return int 0 is run is enabled, 1 if run is disabled indefinitely, otherwise timestamp when execution will resume
* @return int 0 if run is enabled, 1 if run is disabled indefinitely, otherwise timestamp when execution will resume
*/
public function run_disabled() {
return $this->run_disabled;
}
/**
* Set automatic execution status
*
* 0 if run is enabled, 1 if run is disabled indefinitely, otherwise timestamp when execution will resume
*
* @param int $new_status
* @return bool
*/
public function update_run_status( $new_status ) {
$new_status = absint( $new_status );
if ( $new_status > 1 && $new_status < time() ) {
return false;
}
if ( $new_status === $this->run_disabled() ) {
return false;
}
$updated = update_option( self::DISABLE_RUN_OPTION, $new_status );
if ( $updated ) {
$this->run_disabled = $new_status;
return true;
}
return false;
}
}
Events::instance();
......@@ -12,7 +12,7 @@ class Orchestrate extends \WP_CLI_Command {
* @subcommand check-status
*/
public function get_automatic_execution_status( $args, $assoc_args ) {
$status = get_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );
$status = \Automattic\WP\Cron_Control\Events::instance()->run_disabled();
switch ( $status ) {
case 0 :
......@@ -45,18 +45,33 @@ class Orchestrate extends \WP_CLI_Command {
$disable_ts = absint( $disable_ts );
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'enable', false ) ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );
\WP_CLI::success( __( 'Enabled', 'automattic-cron-control' ) );
return;
$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( 0 );
if ( $updated ) {
\WP_CLI::success( __( 'Enabled', 'automattic-cron-control' ) );
return;
}
\WP_CLI::error( __( 'Could not enable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
} elseif ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable', false ) ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 1 );
\WP_CLI::success( __( 'Disabled', 'automattic-cron-control' ) );
return;
$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( 1 );
if ( $updated ) {
\WP_CLI::success( __( 'Disabled', 'automattic-cron-control' ) );
return;
}
\WP_CLI::error( __( 'Could not disable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
} elseif( $disable_ts > 0 ) {
if ( $disable_ts > time() ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, $disable_ts );
\WP_CLI::success( sprintf( __( 'Disabled until %s', 'automattic-cron-control' ), date_i18n( 'Y-m-d H:i:s T', $disable_ts ) ) );
return;
$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( $disable_ts );
if ( $updated ) {
\WP_CLI::success( sprintf( __( 'Disabled until %s', 'automattic-cron-control' ), date_i18n( 'Y-m-d H:i:s T', $disable_ts ) ) );
return;
}
\WP_CLI::error( __( 'Could not disable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
} else {
\WP_CLI::error( __( 'Timestamp is in the past.', '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