PluginProbe
WANotifier for Forms and Actions / 2.7.13
WANotifier for Forms and Actions v2.7.13
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / libraries / action-scheduler / classes / schedules / ActionScheduler_CronSchedule.php

ActionScheduler_CronSchedule.php in WANotifier for Forms and Actions 2.7.13, at libraries/action-scheduler/classes/schedules/ActionScheduler_CronSchedule.php

103 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class ActionScheduler_CronSchedule
5 */
6 class ActionScheduler_CronSchedule extends ActionScheduler_Abstract_RecurringSchedule implements ActionScheduler_Schedule {
7
8 /**
9 * Deprecated property @see $this->__wakeup() for details.
10 **/
11 private $start_timestamp = NULL;
12
13 /**
14 * Deprecated property @see $this->__wakeup() for details.
15 **/
16 private $cron = NULL;
17
18 /**
19 * Wrapper for parent constructor to accept a cron expression string and map it to a CronExpression for this
20 * objects $recurrence property.
21 *
22 * @param DateTime $start The date & time to run the action at or after. If $start aligns with the CronSchedule passed via $recurrence, it will be used. If it does not align, the first matching date after it will be used.
23 * @param CronExpression|string $recurrence The CronExpression used to calculate the schedule's next instance.
24 * @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance.
25 */
26 public function __construct( DateTime $start, $recurrence, DateTime $first = null ) {
27 if ( ! is_a( $recurrence, 'CronExpression' ) ) {
28 $recurrence = CronExpression::factory( $recurrence );
29 }
30
31 // For backward compatibility, we need to make sure the date is set to the first matching cron date, not whatever date is passed in. Importantly, by passing true as the 3rd param, if $start matches the cron expression, then it will be used. This was previously handled in the now deprecated next() method.
32 $date = $recurrence->getNextRunDate( $start, 0, true );
33
34 // parent::__construct() will set this to $date by default, but that may be different to $start now.
35 $first = empty( $first ) ? $start : $first;
36
37 parent::__construct( $date, $recurrence, $first );
38 }
39
40 /**
41 * Calculate when an instance of this schedule would start based on a given
42 * date & time using its the CronExpression.
43 *
44 * @param DateTime $after
45 * @return DateTime
46 */
47 protected function calculate_next( DateTime $after ) {
48 return $this->recurrence->getNextRunDate( $after, 0, false );
49 }
50
51 /**
52 * @return string
53 */
54 public function get_recurrence() {
55 return strval( $this->recurrence );
56 }
57
58 /**
59 * Serialize cron schedules with data required prior to AS 3.0.0
60 *
61 * Prior to Action Scheduler 3.0.0, reccuring schedules used different property names to
62 * refer to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp
63 * was the same as ActionScheduler_SimpleSchedule::timestamp. Action Scheduler 3.0.0
64 * aligned properties and property names for better inheritance. To guard against the
65 * possibility of infinite loops if downgrading to Action Scheduler < 3.0.0, we need to
66 * also store the data with the old property names so if it's unserialized in AS < 3.0,
67 * the schedule doesn't end up with a null recurrence.
68 *
69 * @return array
70 */
71 public function __sleep() {
72
73 $sleep_params = parent::__sleep();
74
75 $this->start_timestamp = $this->scheduled_timestamp;
76 $this->cron = $this->recurrence;
77
78 return array_merge( $sleep_params, array(
79 'start_timestamp',
80 'cron'
81 ) );
82 }
83
84 /**
85 * Unserialize cron schedules serialized/stored prior to AS 3.0.0
86 *
87 * For more background, @see ActionScheduler_Abstract_RecurringSchedule::__wakeup().
88 */
89 public function __wakeup() {
90 if ( is_null( $this->scheduled_timestamp ) && ! is_null( $this->start_timestamp ) ) {
91 $this->scheduled_timestamp = $this->start_timestamp;
92 unset( $this->start_timestamp );
93 }
94
95 if ( is_null( $this->recurrence ) && ! is_null( $this->cron ) ) {
96 $this->recurrence = $this->cron;
97 unset( $this->cron );
98 }
99 parent::__wakeup();
100 }
101 }
102
103