wp-mail-smtp
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
ActionScheduler_ActionFactory.php
WP_CLI
2 years ago
abstracts
2 years ago
actions
2 years ago
data-stores
2 years ago
migration
2 years ago
schedules
2 years ago
schema
2 years ago
ActionScheduler_ActionClaim.php
2 years ago
ActionScheduler_ActionFactory.php
2 years ago
ActionScheduler_AdminView.php
2 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
2 years ago
ActionScheduler_Compatibility.php
2 years ago
ActionScheduler_DataController.php
2 years ago
ActionScheduler_DateTime.php
2 years ago
ActionScheduler_Exception.php
2 years ago
ActionScheduler_FatalErrorMonitor.php
2 years ago
ActionScheduler_InvalidActionException.php
2 years ago
ActionScheduler_ListTable.php
2 years ago
ActionScheduler_LogEntry.php
2 years ago
ActionScheduler_NullLogEntry.php
2 years ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
2 years ago
ActionScheduler_QueueRunner.php
2 years ago
ActionScheduler_Versions.php
2 years ago
ActionScheduler_WPCommentCleaner.php
2 years ago
ActionScheduler_wcSystemStatus.php
2 years ago
ActionScheduler_ActionFactory.php
343 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_ActionFactory |
| 5 | */ |
| 6 | class ActionScheduler_ActionFactory { |
| 7 | |
| 8 | /** |
| 9 | * Return stored actions for given params. |
| 10 | * |
| 11 | * @param string $status The action's status in the data store. |
| 12 | * @param string $hook The hook to trigger when this action runs. |
| 13 | * @param array $args Args to pass to callbacks when the hook is triggered. |
| 14 | * @param ActionScheduler_Schedule $schedule The action's schedule. |
| 15 | * @param string $group A group to put the action in. |
| 16 | * @param int $priority The action priority. |
| 17 | * |
| 18 | * @return ActionScheduler_Action An instance of the stored action. |
| 19 | */ |
| 20 | public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) { |
| 21 | // The 6th parameter ($priority) is not formally declared in the method signature to maintain compatibility with |
| 22 | // third-party subclasses created before this param was added. |
| 23 | $priority = func_num_args() >= 6 ? (int) func_get_arg( 5 ) : 10; |
| 24 | |
| 25 | switch ( $status ) { |
| 26 | case ActionScheduler_Store::STATUS_PENDING: |
| 27 | $action_class = 'ActionScheduler_Action'; |
| 28 | break; |
| 29 | case ActionScheduler_Store::STATUS_CANCELED: |
| 30 | $action_class = 'ActionScheduler_CanceledAction'; |
| 31 | if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
| 32 | $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
| 33 | } |
| 34 | break; |
| 35 | default: |
| 36 | $action_class = 'ActionScheduler_FinishedAction'; |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
| 41 | |
| 42 | $action = new $action_class( $hook, $args, $schedule, $group ); |
| 43 | $action->set_priority( $priority ); |
| 44 | |
| 45 | /** |
| 46 | * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
| 47 | * |
| 48 | * @param ActionScheduler_Action $action The instantiated action. |
| 49 | * @param string $hook The instantiated action's hook. |
| 50 | * @param array $args The instantiated action's args. |
| 51 | * @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
| 52 | * @param string $group The instantiated action's group. |
| 53 | * @param int $priority The action priority. |
| 54 | */ |
| 55 | return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group, $priority ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time). |
| 60 | * |
| 61 | * This method creates a new action using the NullSchedule. In practice, this results in an action scheduled to |
| 62 | * execute "now". Therefore, it will generally run as soon as possible but is not prioritized ahead of other actions |
| 63 | * that are already past-due. |
| 64 | * |
| 65 | * @param string $hook The hook to trigger when this action runs. |
| 66 | * @param array $args Args to pass when the hook is triggered. |
| 67 | * @param string $group A group to put the action in. |
| 68 | * |
| 69 | * @return int The ID of the stored action. |
| 70 | */ |
| 71 | public function async( $hook, $args = array(), $group = '' ) { |
| 72 | return $this->async_unique( $hook, $args, $group, false ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Same as async, but also supports $unique param. |
| 77 | * |
| 78 | * @param string $hook The hook to trigger when this action runs. |
| 79 | * @param array $args Args to pass when the hook is triggered. |
| 80 | * @param string $group A group to put the action in. |
| 81 | * @param bool $unique Whether to ensure the action is unique. |
| 82 | * |
| 83 | * @return int The ID of the stored action. |
| 84 | */ |
| 85 | public function async_unique( $hook, $args = array(), $group = '', $unique = true ) { |
| 86 | $schedule = new ActionScheduler_NullSchedule(); |
| 87 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 88 | return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Create single action. |
| 93 | * |
| 94 | * @param string $hook The hook to trigger when this action runs. |
| 95 | * @param array $args Args to pass when the hook is triggered. |
| 96 | * @param int $when Unix timestamp when the action will run. |
| 97 | * @param string $group A group to put the action in. |
| 98 | * |
| 99 | * @return int The ID of the stored action. |
| 100 | */ |
| 101 | public function single( $hook, $args = array(), $when = null, $group = '' ) { |
| 102 | return $this->single_unique( $hook, $args, $when, $group, false ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Create single action only if there is no pending or running action with same name and params. |
| 107 | * |
| 108 | * @param string $hook The hook to trigger when this action runs. |
| 109 | * @param array $args Args to pass when the hook is triggered. |
| 110 | * @param int $when Unix timestamp when the action will run. |
| 111 | * @param string $group A group to put the action in. |
| 112 | * @param bool $unique Whether action scheduled should be unique. |
| 113 | * |
| 114 | * @return int The ID of the stored action. |
| 115 | */ |
| 116 | public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) { |
| 117 | $date = as_get_datetime_object( $when ); |
| 118 | $schedule = new ActionScheduler_SimpleSchedule( $date ); |
| 119 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 120 | return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Create the first instance of an action recurring on a given interval. |
| 125 | * |
| 126 | * @param string $hook The hook to trigger when this action runs. |
| 127 | * @param array $args Args to pass when the hook is triggered. |
| 128 | * @param int $first Unix timestamp for the first run. |
| 129 | * @param int $interval Seconds between runs. |
| 130 | * @param string $group A group to put the action in. |
| 131 | * |
| 132 | * @return int The ID of the stored action. |
| 133 | */ |
| 134 | public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
| 135 | return $this->recurring_unique( $hook, $args, $first, $interval, $group, false ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Create the first instance of an action recurring on a given interval only if there is no pending or running action with same name and params. |
| 140 | * |
| 141 | * @param string $hook The hook to trigger when this action runs. |
| 142 | * @param array $args Args to pass when the hook is triggered. |
| 143 | * @param int $first Unix timestamp for the first run. |
| 144 | * @param int $interval Seconds between runs. |
| 145 | * @param string $group A group to put the action in. |
| 146 | * @param bool $unique Whether action scheduled should be unique. |
| 147 | * |
| 148 | * @return int The ID of the stored action. |
| 149 | */ |
| 150 | public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) { |
| 151 | if ( empty( $interval ) ) { |
| 152 | return $this->single_unique( $hook, $args, $first, $group, $unique ); |
| 153 | } |
| 154 | $date = as_get_datetime_object( $first ); |
| 155 | $schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
| 156 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 157 | return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Create the first instance of an action recurring on a Cron schedule. |
| 162 | * |
| 163 | * @param string $hook The hook to trigger when this action runs. |
| 164 | * @param array $args Args to pass when the hook is triggered. |
| 165 | * @param int $base_timestamp The first instance of the action will be scheduled |
| 166 | * to run at a time calculated after this timestamp matching the cron |
| 167 | * expression. This can be used to delay the first instance of the action. |
| 168 | * @param int $schedule A cron definition string. |
| 169 | * @param string $group A group to put the action in. |
| 170 | * |
| 171 | * @return int The ID of the stored action. |
| 172 | */ |
| 173 | public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
| 174 | return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false ); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Create the first instance of an action recurring on a Cron schedule only if there is no pending or running action with same name and params. |
| 180 | * |
| 181 | * @param string $hook The hook to trigger when this action runs. |
| 182 | * @param array $args Args to pass when the hook is triggered. |
| 183 | * @param int $base_timestamp The first instance of the action will be scheduled |
| 184 | * to run at a time calculated after this timestamp matching the cron |
| 185 | * expression. This can be used to delay the first instance of the action. |
| 186 | * @param int $schedule A cron definition string. |
| 187 | * @param string $group A group to put the action in. |
| 188 | * @param bool $unique Whether action scheduled should be unique. |
| 189 | * |
| 190 | * @return int The ID of the stored action. |
| 191 | **/ |
| 192 | public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) { |
| 193 | if ( empty( $schedule ) ) { |
| 194 | return $this->single_unique( $hook, $args, $base_timestamp, $group, $unique ); |
| 195 | } |
| 196 | $date = as_get_datetime_object( $base_timestamp ); |
| 197 | $cron = CronExpression::factory( $schedule ); |
| 198 | $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
| 199 | $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 200 | return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Create a successive instance of a recurring or cron action. |
| 205 | * |
| 206 | * Importantly, the action will be rescheduled to run based on the current date/time. |
| 207 | * That means when the action is scheduled to run in the past, the next scheduled date |
| 208 | * will be pushed forward. For example, if a recurring action set to run every hour |
| 209 | * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the |
| 210 | * future, which is 1 hour and 5 seconds from when it was last scheduled to run. |
| 211 | * |
| 212 | * Alternatively, if the action is scheduled to run in the future, and is run early, |
| 213 | * likely via manual intervention, then its schedule will change based on the time now. |
| 214 | * For example, if a recurring action set to run every day, and is run 12 hours early, |
| 215 | * it will run again in 24 hours, not 36 hours. |
| 216 | * |
| 217 | * This slippage is less of an issue with Cron actions, as the specific run time can |
| 218 | * be set for them to run, e.g. 1am each day. In those cases, and entire period would |
| 219 | * need to be missed before there was any change is scheduled, e.g. in the case of an |
| 220 | * action scheduled for 1am each day, the action would need to run an entire day late. |
| 221 | * |
| 222 | * @param ActionScheduler_Action $action The existing action. |
| 223 | * |
| 224 | * @return string The ID of the stored action |
| 225 | * @throws InvalidArgumentException If $action is not a recurring action. |
| 226 | */ |
| 227 | public function repeat( $action ) { |
| 228 | $schedule = $action->get_schedule(); |
| 229 | $next = $schedule->get_next( as_get_datetime_object() ); |
| 230 | |
| 231 | if ( is_null( $next ) || ! $schedule->is_recurring() ) { |
| 232 | throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) ); |
| 233 | } |
| 234 | |
| 235 | $schedule_class = get_class( $schedule ); |
| 236 | $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
| 237 | $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
| 238 | $new_action->set_priority( $action->get_priority() ); |
| 239 | return $this->store( $new_action ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Creates a scheduled action. |
| 244 | * |
| 245 | * This general purpose method can be used in place of specific methods such as async(), |
| 246 | * async_unique(), single() or single_unique(), etc. |
| 247 | * |
| 248 | * @internal Not intended for public use, should not be overriden by subclasses. |
| 249 | * @throws Exception May be thrown if invalid options are passed. |
| 250 | * |
| 251 | * @param array $options { |
| 252 | * Describes the action we wish to schedule. |
| 253 | * |
| 254 | * @type string $type Must be one of 'async', 'cron', 'recurring', or 'single'. |
| 255 | * @type string $hook The hook to be executed. |
| 256 | * @type array $arguments Arguments to be passed to the callback. |
| 257 | * @type string $group The action group. |
| 258 | * @type bool $unique If the action should be unique. |
| 259 | * @type int $when Timestamp. Indicates when the action, or first instance of the action in the case |
| 260 | * of recurring or cron actions, becomes due. |
| 261 | * @type int|string $pattern Recurrence pattern. This is either an interval in seconds for recurring actions |
| 262 | * or a cron expression for cron actions. |
| 263 | * @type int $priority Lower values means higher priority. Should be in the range 0-255. |
| 264 | * } |
| 265 | * |
| 266 | * @return int |
| 267 | */ |
| 268 | public function create( array $options = array() ) { |
| 269 | $defaults = array( |
| 270 | 'type' => 'single', |
| 271 | 'hook' => '', |
| 272 | 'arguments' => array(), |
| 273 | 'group' => '', |
| 274 | 'unique' => false, |
| 275 | 'when' => time(), |
| 276 | 'pattern' => null, |
| 277 | 'priority' => 10, |
| 278 | ); |
| 279 | |
| 280 | $options = array_merge( $defaults, $options ); |
| 281 | |
| 282 | // Cron/recurring actions without a pattern are treated as single actions (this gives calling code the ability |
| 283 | // to use functions like as_schedule_recurring_action() to schedule recurring as well as single actions). |
| 284 | if ( ( 'cron' === $options['type'] || 'recurring' === $options['type'] ) && empty( $options['pattern'] ) ) { |
| 285 | $options['type'] = 'single'; |
| 286 | } |
| 287 | |
| 288 | switch ( $options['type'] ) { |
| 289 | case 'async': |
| 290 | $schedule = new ActionScheduler_NullSchedule(); |
| 291 | break; |
| 292 | |
| 293 | case 'cron': |
| 294 | $date = as_get_datetime_object( $options['when'] ); |
| 295 | $cron = CronExpression::factory( $options['pattern'] ); |
| 296 | $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
| 297 | break; |
| 298 | |
| 299 | case 'recurring': |
| 300 | $date = as_get_datetime_object( $options['when'] ); |
| 301 | $schedule = new ActionScheduler_IntervalSchedule( $date, $options['pattern'] ); |
| 302 | break; |
| 303 | |
| 304 | case 'single': |
| 305 | $date = as_get_datetime_object( $options['when'] ); |
| 306 | $schedule = new ActionScheduler_SimpleSchedule( $date ); |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | throw new Exception( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." ); |
| 311 | } |
| 312 | |
| 313 | $action = new ActionScheduler_Action( $options['hook'], $options['arguments'], $schedule, $options['group'] ); |
| 314 | $action->set_priority( $options['priority'] ); |
| 315 | return $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Save action to database. |
| 320 | * |
| 321 | * @param ActionScheduler_Action $action Action object to save. |
| 322 | * |
| 323 | * @return int The ID of the stored action |
| 324 | */ |
| 325 | protected function store( ActionScheduler_Action $action ) { |
| 326 | $store = ActionScheduler_Store::instance(); |
| 327 | return $store->save_action( $action ); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Store action if it's unique. |
| 332 | * |
| 333 | * @param ActionScheduler_Action $action Action object to store. |
| 334 | * |
| 335 | * @return int ID of the created action. Will be 0 if action was not created. |
| 336 | */ |
| 337 | protected function store_unique_action( ActionScheduler_Action $action ) { |
| 338 | $store = ActionScheduler_Store::instance(); |
| 339 | return method_exists( $store, 'save_unique_action' ) ? |
| 340 | $store->save_unique_action( $action ) : $store->save_action( $action ); |
| 341 | } |
| 342 | } |
| 343 |