| 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|null $schedule The action's schedule. |
| 15 |
* @param string $group A group to put the action in. |
| 16 |
* phpcs:ignore Squiz.Commenting.FunctionComment.ExtraParamComment |
| 17 |
* @param int $priority The action priority. |
| 18 |
* |
| 19 |
* @return ActionScheduler_Action An instance of the stored action. |
| 20 |
*/ |
| 21 |
public function get_stored_action( $status, $hook, array $args = array(), ?ActionScheduler_Schedule $schedule = null, $group = '' ) { |
| 22 |
// The 6th parameter ($priority) is not formally declared in the method signature to maintain compatibility with |
| 23 |
// third-party subclasses created before this param was added. |
| 24 |
$priority = func_num_args() >= 6 ? (int) func_get_arg( 5 ) : 10; |
| 25 |
|
| 26 |
switch ( $status ) { |
| 27 |
case ActionScheduler_Store::STATUS_PENDING: |
| 28 |
$action_class = 'ActionScheduler_Action'; |
| 29 |
break; |
| 30 |
case ActionScheduler_Store::STATUS_CANCELED: |
| 31 |
$action_class = 'ActionScheduler_CanceledAction'; |
| 32 |
if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
| 33 |
$schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
| 34 |
} |
| 35 |
break; |
| 36 |
default: |
| 37 |
$action_class = 'ActionScheduler_FinishedAction'; |
| 38 |
break; |
| 39 |
} |
| 40 |
|
| 41 |
$action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
| 42 |
|
| 43 |
$action = new $action_class( $hook, $args, $schedule, $group ); |
| 44 |
$action->set_priority( $priority ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
| 48 |
* |
| 49 |
* @param ActionScheduler_Action $action The instantiated action. |
| 50 |
* @param string $hook The instantiated action's hook. |
| 51 |
* @param array $args The instantiated action's args. |
| 52 |
* @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
| 53 |
* @param string $group The instantiated action's group. |
| 54 |
* @param int $priority The action priority. |
| 55 |
*/ |
| 56 |
return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group, $priority ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Enqueue an action to run one time, as soon as possible (rather a specific scheduled time). |
| 61 |
* |
| 62 |
* This method creates a new action using the NullSchedule. In practice, this results in an action scheduled to |
| 63 |
* execute "now". Therefore, it will generally run as soon as possible but is not prioritized ahead of other actions |
| 64 |
* that are already past-due. |
| 65 |
* |
| 66 |
* @param string $hook The hook to trigger when this action runs. |
| 67 |
* @param array $args Args to pass when the hook is triggered. |
| 68 |
* @param string $group A group to put the action in. |
| 69 |
* |
| 70 |
* @return int The ID of the stored action. |
| 71 |
*/ |
| 72 |
public function async( $hook, $args = array(), $group = '' ) { |
| 73 |
return $this->async_unique( $hook, $args, $group, false ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Same as async, but also supports $unique param. |
| 78 |
* |
| 79 |
* @param string $hook The hook to trigger when this action runs. |
| 80 |
* @param array $args Args to pass when the hook is triggered. |
| 81 |
* @param string $group A group to put the action in. |
| 82 |
* @param bool $unique Whether to ensure the action is unique. |
| 83 |
* |
| 84 |
* @return int The ID of the stored action. |
| 85 |
*/ |
| 86 |
public function async_unique( $hook, $args = array(), $group = '', $unique = true ) { |
| 87 |
$schedule = new ActionScheduler_NullSchedule(); |
| 88 |
$action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 89 |
return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Create single action. |
| 94 |
* |
| 95 |
* @param string $hook The hook to trigger when this action runs. |
| 96 |
* @param array $args Args to pass when the hook is triggered. |
| 97 |
* @param int $when Unix timestamp when the action will run. |
| 98 |
* @param string $group A group to put the action in. |
| 99 |
* |
| 100 |
* @return int The ID of the stored action. |
| 101 |
*/ |
| 102 |
public function single( $hook, $args = array(), $when = null, $group = '' ) { |
| 103 |
return $this->single_unique( $hook, $args, $when, $group, false ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Create single action only if there is no pending or running action with same name and params. |
| 108 |
* |
| 109 |
* @param string $hook The hook to trigger when this action runs. |
| 110 |
* @param array $args Args to pass when the hook is triggered. |
| 111 |
* @param int $when Unix timestamp when the action will run. |
| 112 |
* @param string $group A group to put the action in. |
| 113 |
* @param bool $unique Whether action scheduled should be unique. |
| 114 |
* |
| 115 |
* @return int The ID of the stored action. |
| 116 |
*/ |
| 117 |
public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) { |
| 118 |
$date = as_get_datetime_object( $when ); |
| 119 |
$schedule = new ActionScheduler_SimpleSchedule( $date ); |
| 120 |
$action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 121 |
return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Create the first instance of an action recurring on a given interval. |
| 126 |
* |
| 127 |
* @param string $hook The hook to trigger when this action runs. |
| 128 |
* @param array $args Args to pass when the hook is triggered. |
| 129 |
* @param int $first Unix timestamp for the first run. |
| 130 |
* @param int $interval Seconds between runs. |
| 131 |
* @param string $group A group to put the action in. |
| 132 |
* |
| 133 |
* @return int The ID of the stored action. |
| 134 |
*/ |
| 135 |
public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
| 136 |
return $this->recurring_unique( $hook, $args, $first, $interval, $group, false ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* 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. |
| 141 |
* |
| 142 |
* @param string $hook The hook to trigger when this action runs. |
| 143 |
* @param array $args Args to pass when the hook is triggered. |
| 144 |
* @param int $first Unix timestamp for the first run. |
| 145 |
* @param int $interval Seconds between runs. |
| 146 |
* @param string $group A group to put the action in. |
| 147 |
* @param bool $unique Whether action scheduled should be unique. |
| 148 |
* |
| 149 |
* @return int The ID of the stored action. |
| 150 |
*/ |
| 151 |
public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) { |
| 152 |
if ( empty( $interval ) ) { |
| 153 |
return $this->single_unique( $hook, $args, $first, $group, $unique ); |
| 154 |
} |
| 155 |
$date = as_get_datetime_object( $first ); |
| 156 |
$schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
| 157 |
$action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 158 |
return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Create the first instance of an action recurring on a Cron schedule. |
| 163 |
* |
| 164 |
* @param string $hook The hook to trigger when this action runs. |
| 165 |
* @param array $args Args to pass when the hook is triggered. |
| 166 |
* @param int $base_timestamp The first instance of the action will be scheduled |
| 167 |
* to run at a time calculated after this timestamp matching the cron |
| 168 |
* expression. This can be used to delay the first instance of the action. |
| 169 |
* @param int $schedule A cron definition string. |
| 170 |
* @param string $group A group to put the action in. |
| 171 |
* |
| 172 |
* @return int The ID of the stored action. |
| 173 |
*/ |
| 174 |
public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
| 175 |
return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false ); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* 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. |
| 181 |
* |
| 182 |
* @param string $hook The hook to trigger when this action runs. |
| 183 |
* @param array $args Args to pass when the hook is triggered. |
| 184 |
* @param int $base_timestamp The first instance of the action will be scheduled |
| 185 |
* to run at a time calculated after this timestamp matching the cron |
| 186 |
* expression. This can be used to delay the first instance of the action. |
| 187 |
* @param int $schedule A cron definition string. |
| 188 |
* @param string $group A group to put the action in. |
| 189 |
* @param bool $unique Whether action scheduled should be unique. |
| 190 |
* |
| 191 |
* @return int The ID of the stored action. |
| 192 |
**/ |
| 193 |
public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) { |
| 194 |
if ( empty( $schedule ) ) { |
| 195 |
return $this->single_unique( $hook, $args, $base_timestamp, $group, $unique ); |
| 196 |
} |
| 197 |
$date = as_get_datetime_object( $base_timestamp ); |
| 198 |
$cron = CronExpression::factory( $schedule ); |
| 199 |
$schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
| 200 |
$action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
| 201 |
return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Create a successive instance of a recurring or cron action. |
| 206 |
* |
| 207 |
* Importantly, the action will be rescheduled to run based on the current date/time. |
| 208 |
* That means when the action is scheduled to run in the past, the next scheduled date |
| 209 |
* will be pushed forward. For example, if a recurring action set to run every hour |
| 210 |
* was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the |
| 211 |
* future, which is 1 hour and 5 seconds from when it was last scheduled to run. |
| 212 |
* |
| 213 |
* Alternatively, if the action is scheduled to run in the future, and is run early, |
| 214 |
* likely via manual intervention, then its schedule will change based on the time now. |
| 215 |
* For example, if a recurring action set to run every day, and is run 12 hours early, |
| 216 |
* it will run again in 24 hours, not 36 hours. |
| 217 |
* |
| 218 |
* This slippage is less of an issue with Cron actions, as the specific run time can |
| 219 |
* be set for them to run, e.g. 1am each day. In those cases, and entire period would |
| 220 |
* need to be missed before there was any change is scheduled, e.g. in the case of an |
| 221 |
* action scheduled for 1am each day, the action would need to run an entire day late. |
| 222 |
* |
| 223 |
* @param ActionScheduler_Action $action The existing action. |
| 224 |
* |
| 225 |
* @return string The ID of the stored action |
| 226 |
* @throws InvalidArgumentException If $action is not a recurring action. |
| 227 |
*/ |
| 228 |
public function repeat( $action ) { |
| 229 |
$schedule = $action->get_schedule(); |
| 230 |
$next = $schedule->get_next( as_get_datetime_object() ); |
| 231 |
|
| 232 |
if ( is_null( $next ) || ! $schedule->is_recurring() ) { |
| 233 |
throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) ); |
| 234 |
} |
| 235 |
|
| 236 |
$schedule_class = get_class( $schedule ); |
| 237 |
$new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
| 238 |
$new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
| 239 |
$new_action->set_priority( $action->get_priority() ); |
| 240 |
return $this->store( $new_action ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Creates a scheduled action. |
| 245 |
* |
| 246 |
* This general purpose method can be used in place of specific methods such as async(), |
| 247 |
* async_unique(), single() or single_unique(), etc. |
| 248 |
* |
| 249 |
* @internal Not intended for public use, should not be overridden by subclasses. |
| 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 The action ID. Zero if there was an error scheduling the action. |
| 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 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 311 |
error_log( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." ); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
$action = new ActionScheduler_Action( $options['hook'], $options['arguments'], $schedule, $options['group'] ); |
| 316 |
$action->set_priority( $options['priority'] ); |
| 317 |
|
| 318 |
$action_id = 0; |
| 319 |
try { |
| 320 |
$action_id = $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 321 |
} catch ( Exception $e ) { |
| 322 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 323 |
error_log( |
| 324 |
sprintf( |
| 325 |
/* translators: %1$s is the name of the hook to be enqueued, %2$s is the exception message. */ |
| 326 |
__( 'Caught exception while enqueuing action "%1$s": %2$s', 'action-scheduler' ), |
| 327 |
$options['hook'], |
| 328 |
$e->getMessage() |
| 329 |
) |
| 330 |
); |
| 331 |
} |
| 332 |
return $action_id; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Save action to database. |
| 337 |
* |
| 338 |
* @param ActionScheduler_Action $action Action object to save. |
| 339 |
* |
| 340 |
* @return int The ID of the stored action |
| 341 |
*/ |
| 342 |
protected function store( ActionScheduler_Action $action ) { |
| 343 |
$store = ActionScheduler_Store::instance(); |
| 344 |
return $store->save_action( $action ); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Store action if it's unique. |
| 349 |
* |
| 350 |
* @param ActionScheduler_Action $action Action object to store. |
| 351 |
* |
| 352 |
* @return int ID of the created action. Will be 0 if action was not created. |
| 353 |
*/ |
| 354 |
protected function store_unique_action( ActionScheduler_Action $action ) { |
| 355 |
$store = ActionScheduler_Store::instance(); |
| 356 |
if ( method_exists( $store, 'save_unique_action' ) ) { |
| 357 |
return $store->save_unique_action( $action ); |
| 358 |
} else { |
| 359 |
/** |
| 360 |
* Fallback to non-unique action if the store doesn't support unique actions. |
| 361 |
* We try to save the action as unique, accepting that there might be a race condition. |
| 362 |
* This is likely still better than giving up on unique actions entirely. |
| 363 |
*/ |
| 364 |
$existing_action_id = (int) $store->find_action( |
| 365 |
$action->get_hook(), |
| 366 |
array( |
| 367 |
'args' => $action->get_args(), |
| 368 |
'status' => ActionScheduler_Store::STATUS_PENDING, |
| 369 |
'group' => $action->get_group(), |
| 370 |
) |
| 371 |
); |
| 372 |
if ( $existing_action_id > 0 ) { |
| 373 |
return 0; |
| 374 |
} |
| 375 |
return $store->save_action( $action ); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|