From aa052e6c6d60754da1480f6d6c280788ad664635 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Tue, 6 Aug 2019 14:16:49 +1000 Subject: [PATCH] Maintain property names for serialized schedules Prior to #333, recurring schedules used different property names to refer to equivalent data. For example, IntervalSchedule::start_timestamp was the same as SimpleSchedule::timestamp. PR #333 aligned properties and property names for better inheritance. To guard against the possibility of infinite loops if downgrading to Action Scheduler < 3.0.0, we need to also store the data with the old property names so if it's unserialized in AS < 3.0, the schedule doesn't end up with a 0 recurrence. Fixes #340. --- .../ActionScheduler_CronSchedule.php | 31 +++++++++++++++++-- .../ActionScheduler_IntervalSchedule.php | 30 ++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/classes/schedules/ActionScheduler_CronSchedule.php b/classes/schedules/ActionScheduler_CronSchedule.php index d46b50ea0..3fa3ddef4 100644 --- a/classes/schedules/ActionScheduler_CronSchedule.php +++ b/classes/schedules/ActionScheduler_CronSchedule.php @@ -55,19 +55,46 @@ public function get_recurrence() { return strval( $this->recurrence ); } + /** + * Serialize cron schedules with data required prior to AS 3.0.0 + * + * Prior to Action Scheduler 3.0.0, reccuring schedules used different property names to + * refer to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp + * was the same as ActionScheduler_SimpleSchedule::timestamp. Action Scheduler 3.0.0 + * aligned properties and property names for better inheritance. To guard against the + * possibility of infinite loops if downgrading to Action Scheduler < 3.0.0, we need to + * also store the data with the old property names so if it's unserialized in AS < 3.0, + * the schedule doesn't end up with a null recurrence. + * + * @return array + */ + public function __sleep() { + + $sleep_params = parent::__sleep(); + + $this->start_timestamp = $sleep_params['scheduled_timestamp']; + $this->cron = $sleep_params['recurrence']; + + return array_merge( $sleep_params, array( + 'start_timestamp', + 'cron' + ) ); + } + /** * Unserialize cron schedules serialized/stored prior to AS 3.0.0 * * For more background, @see ActionScheduler_Abstract_RecurringSchedule::__wakeup(). */ public function __wakeup() { - if ( ! is_null( $this->start_timestamp ) ) { + if ( is_null( $this->scheduled_timestamp ) && ! is_null( $this->start_timestamp ) ) { $this->scheduled_timestamp = $this->start_timestamp; unset( $this->start_timestamp ); } - if ( ! is_null( $this->cron ) ) { + if ( is_null( $this->recurrence ) && ! is_null( $this->cron ) ) { $this->recurrence = $this->cron; + unset( $this->cron ); } parent::__wakeup(); } diff --git a/classes/schedules/ActionScheduler_IntervalSchedule.php b/classes/schedules/ActionScheduler_IntervalSchedule.php index 5ec5a5180..93325e573 100644 --- a/classes/schedules/ActionScheduler_IntervalSchedule.php +++ b/classes/schedules/ActionScheduler_IntervalSchedule.php @@ -35,18 +35,44 @@ public function interval_in_seconds() { return (int) $this->get_recurrence(); } + /** + * Serialize interval schedules with data required prior to AS 3.0.0 + * + * Prior to Action Scheduler 3.0.0, reccuring schedules used different property names to + * refer to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp + * was the same as ActionScheduler_SimpleSchedule::timestamp. Action Scheduler 3.0.0 + * aligned properties and property names for better inheritance. To guard against the + * possibility of infinite loops if downgrading to Action Scheduler < 3.0.0, we need to + * also store the data with the old property names so if it's unserialized in AS < 3.0, + * the schedule doesn't end up with a null/false/0 recurrence. + * + * @return array + */ + public function __sleep() { + + $sleep_params = parent::__sleep(); + + $this->start_timestamp = $sleep_params['scheduled_timestamp']; + $this->interval_in_seconds = $sleep_params['recurrence']; + + return array_merge( $sleep_params, array( + 'start_timestamp', + 'interval_in_seconds' + ) ); + } + /** * Unserialize interval schedules serialized/stored prior to AS 3.0.0 * * For more background, @see ActionScheduler_Abstract_RecurringSchedule::__wakeup(). */ public function __wakeup() { - if ( ! is_null( $this->start_timestamp ) ) { + if ( is_null( $this->scheduled_timestamp ) && ! is_null( $this->start_timestamp ) ) { $this->scheduled_timestamp = $this->start_timestamp; unset( $this->start_timestamp ); } - if ( ! is_null( $this->interval_in_seconds ) ) { + if ( is_null( $this->recurrence ) && ! is_null( $this->interval_in_seconds ) ) { $this->recurrence = $this->interval_in_seconds; unset( $this->interval_in_seconds ); }