WP_CLI
6 years ago
abstracts
4 years ago
actions
4 years ago
data-stores
4 years ago
migration
4 years ago
schedules
6 years ago
schema
4 years ago
ActionScheduler_ActionClaim.php
6 years ago
ActionScheduler_ActionFactory.php
6 years ago
ActionScheduler_AdminView.php
6 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
6 years ago
ActionScheduler_Compatibility.php
6 years ago
ActionScheduler_DataController.php
6 years ago
ActionScheduler_DateTime.php
6 years ago
ActionScheduler_Exception.php
6 years ago
ActionScheduler_FatalErrorMonitor.php
6 years ago
ActionScheduler_InvalidActionException.php
6 years ago
ActionScheduler_ListTable.php
6 years ago
ActionScheduler_LogEntry.php
6 years ago
ActionScheduler_NullLogEntry.php
6 years ago
ActionScheduler_OptionLock.php
6 years ago
ActionScheduler_QueueCleaner.php
4 years ago
ActionScheduler_QueueRunner.php
6 years ago
ActionScheduler_Versions.php
6 years ago
ActionScheduler_WPCommentCleaner.php
6 years ago
ActionScheduler_wcSystemStatus.php
4 years ago
ActionScheduler_ActionFactory.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_ActionFactory |
| 5 | */ |
| 6 | class ActionScheduler_ActionFactory { |
| 7 | |
| 8 | /** |
| 9 | * @param string $status The action's status in the data store |
| 10 | * @param string $hook The hook to trigger when this action runs |
| 11 | * @param array $args Args to pass to callbacks when the hook is triggered |
| 12 | * @param ActionScheduler_Schedule $schedule The action's schedule |
| 13 | * @param string $group A group to put the action in |
| 14 | * |
| 15 | * @return ActionScheduler_Action An instance of the stored action |
| 16 | */ |
| 17 | public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) { |
| 18 | |
| 19 | switch ( $status ) { |
| 20 | case ActionScheduler_Store::STATUS_PENDING : |
| 21 | $action_class = 'ActionScheduler_Action'; |
| 22 | break; |
| 23 | case ActionScheduler_Store::STATUS_CANCELED : |
| 24 | $action_class = 'ActionScheduler_CanceledAction'; |
| 25 | if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
| 26 | $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
| 27 | } |
| 28 | break; |
| 29 | default : |
| 30 | $action_class = 'ActionScheduler_FinishedAction'; |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
| 35 | |
| 36 | $action = new $action_class( $hook, $args, $schedule, $group ); |
| 37 | |
| 38 | /** |
| 39 | * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
| 40 | * |
| 41 | * @param ActionScheduler_Action $action The instantiated action. |
| 42 | * @param string $hook The instantiated action's hook. |
| 43 | * @param array $args The instantiated action's args. |
| 44 | * @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
| 45 | * @param string $group The instantiated action's group. |
| 46 | */ |
| 47 | return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time). |
| 52 | * |
| 53 | * This method creates a new action with the NULLSchedule. This schedule maps to a MySQL datetime string of |
| 54 | * 0000-00-00 00:00:00. This is done to create a psuedo "async action" type that is fully backward compatible. |
| 55 | * Existing queries to claim actions claim by date, meaning actions scheduled for 0000-00-00 00:00:00 will |
| 56 | * always be claimed prior to actions scheduled for a specific date. This makes sure that any async action is |
| 57 | * given priority in queue processing. This has the added advantage of making sure async actions can be |
| 58 | * claimed by both the existing WP Cron and WP CLI runners, as well as a new async request runner. |
| 59 | * |
| 60 | * @param string $hook The hook to trigger when this action runs |
| 61 | * @param array $args Args to pass when the hook is triggered |
| 62 | * @param string $group A group to put the action in |
| 63 | * |
| 64 | * @return int The ID of the stored action |
| 65 | */ |
| 66 | public function async( $hook, $args = array(), $group = '' ) { |
| 67 | $schedule = new ActionScheduler_NullSchedule(); |
| 68 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 69 | return $this->store( $action ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param string $hook The hook to trigger when this action runs |
| 74 | * @param array $args Args to pass when the hook is triggered |
| 75 | * @param int $when Unix timestamp when the action will run |
| 76 | * @param string $group A group to put the action in |
| 77 | * |
| 78 | * @return int The ID of the stored action |
| 79 | */ |
| 80 | public function single( $hook, $args = array(), $when = null, $group = '' ) { |
| 81 | $date = as_get_datetime_object( $when ); |
| 82 | $schedule = new ActionScheduler_SimpleSchedule( $date ); |
| 83 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 84 | return $this->store( $action ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Create the first instance of an action recurring on a given interval. |
| 89 | * |
| 90 | * @param string $hook The hook to trigger when this action runs |
| 91 | * @param array $args Args to pass when the hook is triggered |
| 92 | * @param int $first Unix timestamp for the first run |
| 93 | * @param int $interval Seconds between runs |
| 94 | * @param string $group A group to put the action in |
| 95 | * |
| 96 | * @return int The ID of the stored action |
| 97 | */ |
| 98 | public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
| 99 | if ( empty($interval) ) { |
| 100 | return $this->single( $hook, $args, $first, $group ); |
| 101 | } |
| 102 | $date = as_get_datetime_object( $first ); |
| 103 | $schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
| 104 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 105 | return $this->store( $action ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Create the first instance of an action recurring on a Cron schedule. |
| 110 | * |
| 111 | * @param string $hook The hook to trigger when this action runs |
| 112 | * @param array $args Args to pass when the hook is triggered |
| 113 | * @param int $base_timestamp The first instance of the action will be scheduled |
| 114 | * to run at a time calculated after this timestamp matching the cron |
| 115 | * expression. This can be used to delay the first instance of the action. |
| 116 | * @param int $schedule A cron definition string |
| 117 | * @param string $group A group to put the action in |
| 118 | * |
| 119 | * @return int The ID of the stored action |
| 120 | */ |
| 121 | public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
| 122 | if ( empty($schedule) ) { |
| 123 | return $this->single( $hook, $args, $base_timestamp, $group ); |
| 124 | } |
| 125 | $date = as_get_datetime_object( $base_timestamp ); |
| 126 | $cron = CronExpression::factory( $schedule ); |
| 127 | $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
| 128 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 129 | return $this->store( $action ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Create a successive instance of a recurring or cron action. |
| 134 | * |
| 135 | * Importantly, the action will be rescheduled to run based on the current date/time. |
| 136 | * That means when the action is scheduled to run in the past, the next scheduled date |
| 137 | * will be pushed forward. For example, if a recurring action set to run every hour |
| 138 | * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the |
| 139 | * future, which is 1 hour and 5 seconds from when it was last scheduled to run. |
| 140 | * |
| 141 | * Alternatively, if the action is scheduled to run in the future, and is run early, |
| 142 | * likely via manual intervention, then its schedule will change based on the time now. |
| 143 | * For example, if a recurring action set to run every day, and is run 12 hours early, |
| 144 | * it will run again in 24 hours, not 36 hours. |
| 145 | * |
| 146 | * This slippage is less of an issue with Cron actions, as the specific run time can |
| 147 | * be set for them to run, e.g. 1am each day. In those cases, and entire period would |
| 148 | * need to be missed before there was any change is scheduled, e.g. in the case of an |
| 149 | * action scheduled for 1am each day, the action would need to run an entire day late. |
| 150 | * |
| 151 | * @param ActionScheduler_Action $action The existing action. |
| 152 | * |
| 153 | * @return string The ID of the stored action |
| 154 | * @throws InvalidArgumentException If $action is not a recurring action. |
| 155 | */ |
| 156 | public function repeat( $action ) { |
| 157 | $schedule = $action->get_schedule(); |
| 158 | $next = $schedule->get_next( as_get_datetime_object() ); |
| 159 | |
| 160 | if ( is_null( $next ) || ! $schedule->is_recurring() ) { |
| 161 | throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'woocommerce' ) ); |
| 162 | } |
| 163 | |
| 164 | $schedule_class = get_class( $schedule ); |
| 165 | $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
| 166 | $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
| 167 | return $this->store( $new_action ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @param ActionScheduler_Action $action |
| 172 | * |
| 173 | * @return int The ID of the stored action |
| 174 | */ |
| 175 | protected function store( ActionScheduler_Action $action ) { |
| 176 | $store = ActionScheduler_Store::instance(); |
| 177 | return $store->save_action( $action ); |
| 178 | } |
| 179 | } |
| 180 |