From 8ac737dd084120f3971bcda78cf45fadf33a0f62 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 16 Mar 2019 00:18:48 -0400 Subject: [PATCH] init prospress/action-scheduler#256 --- .../ActionScheduler_Abstract_QueueRunner.php | 1 + classes/ActionScheduler_IntervalSchedule.php | 28 ++++++++++++++++--- classes/ActionScheduler_ListTable.php | 6 ++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/classes/ActionScheduler_Abstract_QueueRunner.php b/classes/ActionScheduler_Abstract_QueueRunner.php index e34fb8752..54528c85f 100644 --- a/classes/ActionScheduler_Abstract_QueueRunner.php +++ b/classes/ActionScheduler_Abstract_QueueRunner.php @@ -79,6 +79,7 @@ protected function schedule_next_instance( ActionScheduler_Action $action ) { $next = $schedule->next( as_get_datetime_object() ); if ( ! is_null( $next ) && $schedule->is_recurring() ) { + $schedule->set_next( $next ); $this->store->save_action( $action, $next ); } } diff --git a/classes/ActionScheduler_IntervalSchedule.php b/classes/ActionScheduler_IntervalSchedule.php index 604ad61a2..f84529c20 100644 --- a/classes/ActionScheduler_IntervalSchedule.php +++ b/classes/ActionScheduler_IntervalSchedule.php @@ -5,12 +5,15 @@ */ class ActionScheduler_IntervalSchedule implements ActionScheduler_Schedule { /** @var DateTime */ + private $first = NULL; private $start = NULL; + private $first_timestamp = 0; private $start_timestamp = 0; private $interval_in_seconds = 0; - public function __construct( DateTime $start, $interval ) { - $this->start = $start; + public function __construct( DateTime $first, $interval ) { + $this->first = $first; + $this->start = $first; $this->interval_in_seconds = (int)$interval; } @@ -21,11 +24,25 @@ public function __construct( DateTime $start, $interval ) { */ public function next( DateTime $after = NULL ) { $after = empty($after) ? as_get_datetime_object('@0') : clone $after; - if ( $after > $this->start ) { + if ( $after > $this->first ) { $after->modify('+'.$this->interval_in_seconds.' seconds'); return $after; } - return clone $this->start; + return clone $this->first; + } + + /** + * @return DateTime + */ + public function get_start() { + return $this->start; + } + + /** + * @param DateTime $next + */ + public function set_next( DateTime $next ) { + $this->start = $next; } /** @@ -47,14 +64,17 @@ public function interval_in_seconds() { * @return array */ public function __sleep() { + $this->first_timestamp = $this->first->getTimestamp(); $this->start_timestamp = $this->start->getTimestamp(); return array( + 'first_timestamp', 'start_timestamp', 'interval_in_seconds' ); } public function __wakeup() { + $this->first = as_get_datetime_object($this->first_timestamp); $this->start = as_get_datetime_object($this->start_timestamp); } } diff --git a/classes/ActionScheduler_ListTable.php b/classes/ActionScheduler_ListTable.php index 95f97d6b4..88c2fc827 100644 --- a/classes/ActionScheduler_ListTable.php +++ b/classes/ActionScheduler_ListTable.php @@ -379,13 +379,13 @@ protected function get_schedule_display_string( ActionScheduler_Schedule $schedu $schedule_display_string = ''; - if ( ! $schedule->next() ) { + if ( ! $schedule->get_start() ) { return $schedule_display_string; } - $next_timestamp = $schedule->next()->getTimestamp(); + $next_timestamp = $schedule->get_start()->getTimestamp(); - $schedule_display_string .= $schedule->next()->format( 'Y-m-d H:i:s O' ); + $schedule_display_string .= $schedule->get_start()->format( 'Y-m-d H:i:s O' ); $schedule_display_string .= '
'; if ( gmdate( 'U' ) > $next_timestamp ) {