2018-07-26 02:16:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Action Queue
|
|
|
|
*
|
|
|
|
* @version 3.5.0
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Interface
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC Action Queue
|
|
|
|
*
|
|
|
|
* A job queue using WordPress actions.
|
|
|
|
*
|
|
|
|
* @version 3.5.0
|
|
|
|
*/
|
|
|
|
class WC_Action_Queue implements WC_Queue_Interface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue an action to run one time, as soon as possible
|
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param string $hook The hook to trigger.
|
|
|
|
* @param array $args Arguments to pass when the hook triggers.
|
|
|
|
* @param string $group The group to assign this job to.
|
|
|
|
* @return string The action ID.
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
|
|
|
public function add( $hook, $args = array(), $group = '' ) {
|
|
|
|
return $this->schedule_single( time(), $hook, $args, $group );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule an action to run once at some time in the future
|
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param int $timestamp When the job will run.
|
|
|
|
* @param string $hook The hook to trigger.
|
|
|
|
* @param array $args Arguments to pass when the hook triggers.
|
|
|
|
* @param string $group The group to assign this job to.
|
|
|
|
* @return string The action ID.
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
|
|
|
public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
|
2018-09-20 05:39:56 +00:00
|
|
|
return as_schedule_single_action( $timestamp, $hook, $args, $group );
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a recurring action
|
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param int $timestamp When the first instance of the job will run.
|
|
|
|
* @param int $interval_in_seconds How long to wait between runs.
|
|
|
|
* @param string $hook The hook to trigger.
|
|
|
|
* @param array $args Arguments to pass when the hook triggers.
|
|
|
|
* @param string $group The group to assign this job to.
|
|
|
|
* @return string The action ID.
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
|
|
|
public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
|
2018-09-20 05:39:56 +00:00
|
|
|
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule an action that recurs on a cron-like schedule.
|
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param int $timestamp The schedule will start on or after this time.
|
|
|
|
* @param string $cron_schedule A cron-link schedule string.
|
2018-07-26 02:16:02 +00:00
|
|
|
* @see http://en.wikipedia.org/wiki/Cron
|
|
|
|
* * * * * * *
|
|
|
|
* ┬ ┬ ┬ ┬ ┬ ┬
|
|
|
|
* | | | | | |
|
|
|
|
* | | | | | + year [optional]
|
|
|
|
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
|
|
|
|
* | | | +---------- month (1 - 12)
|
|
|
|
* | | +--------------- day of month (1 - 31)
|
|
|
|
* | +-------------------- hour (0 - 23)
|
|
|
|
* +------------------------- min (0 - 59)
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param string $hook The hook to trigger.
|
|
|
|
* @param array $args Arguments to pass when the hook triggers.
|
|
|
|
* @param string $group The group to assign this job to.
|
2018-07-26 02:16:02 +00:00
|
|
|
* @return string The action ID
|
|
|
|
*/
|
|
|
|
public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
|
2018-09-20 05:39:56 +00:00
|
|
|
return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group );
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-20 05:42:24 +00:00
|
|
|
* Dequeue the next scheduled instance of an action with a matching hook (and optionally matching args and group).
|
2018-07-26 02:16:02 +00:00
|
|
|
*
|
2018-09-20 05:42:24 +00:00
|
|
|
* Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action.
|
2018-07-26 02:16:02 +00:00
|
|
|
*
|
2018-09-20 05:42:24 +00:00
|
|
|
* While technically only the next instance of a recurring or cron action is unscheduled by this method, that will also
|
|
|
|
* prevent all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled
|
|
|
|
* in a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled
|
|
|
|
* only after the former action is run. As the next instance is never run, because it's unscheduled by this function,
|
|
|
|
* then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled
|
|
|
|
* by this method also.
|
2018-07-26 02:16:02 +00:00
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param string $hook The hook that the job will trigger.
|
|
|
|
* @param array $args Args that would have been passed to the job.
|
2018-09-24 01:19:45 +00:00
|
|
|
* @param string $group The group the job is assigned to (if any).
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
|
|
|
public function cancel( $hook, $args = array(), $group = '' ) {
|
2018-09-20 05:39:56 +00:00
|
|
|
as_unschedule_action( $hook, $args, $group );
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 05:42:24 +00:00
|
|
|
/**
|
|
|
|
* Dequeue all actions with a matching hook (and optionally matching args and group) so no matching actions are ever run.
|
|
|
|
*
|
|
|
|
* @param string $hook The hook that the job will trigger.
|
|
|
|
* @param array $args Args that would have been passed to the job.
|
2018-09-24 01:19:45 +00:00
|
|
|
* @param string $group The group the job is assigned to (if any).
|
2018-09-20 05:42:24 +00:00
|
|
|
*/
|
|
|
|
public function cancel_all( $hook, $args = array(), $group = '' ) {
|
|
|
|
as_unschedule_all_actions( $hook, $args, $group );
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:16:02 +00:00
|
|
|
/**
|
2018-07-31 02:08:31 +00:00
|
|
|
* Get the date and time for the next scheduled occurence of an action with a given hook
|
|
|
|
* (an optionally that matches certain args and group), if any.
|
|
|
|
*
|
2018-09-24 01:19:45 +00:00
|
|
|
* @param string $hook The hook that the job will trigger.
|
|
|
|
* @param array $args Filter to a hook with matching args that will be passed to the job when it runs.
|
|
|
|
* @param string $group Filter to only actions assigned to a specific group.
|
2018-08-01 18:11:08 +00:00
|
|
|
* @return WC_DateTime|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook.
|
2018-07-26 02:16:02 +00:00
|
|
|
*/
|
2018-07-31 02:08:31 +00:00
|
|
|
public function get_next( $hook, $args = null, $group = '' ) {
|
|
|
|
|
2018-09-20 05:39:56 +00:00
|
|
|
$next_timestamp = as_next_scheduled_action( $hook, $args, $group );
|
2018-07-31 02:08:31 +00:00
|
|
|
|
2020-10-03 02:07:13 +00:00
|
|
|
if ( is_numeric( $next_timestamp ) ) {
|
2018-12-03 03:06:00 +00:00
|
|
|
return new WC_DateTime( "@{$next_timestamp}", new DateTimeZone( 'UTC' ) );
|
2018-07-31 02:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find scheduled actions
|
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param array $args Possible arguments, with their default values:
|
2018-07-26 02:16:02 +00:00
|
|
|
* 'hook' => '' - the name of the action that will be triggered
|
|
|
|
* 'args' => null - the args array that will be passed with the action
|
|
|
|
* 'date' => null - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
|
|
|
|
* 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
|
|
|
|
* 'modified' => null - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
|
|
|
|
* 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
|
|
|
|
* 'group' => '' - the group the action belongs to
|
|
|
|
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
|
|
|
|
* 'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
|
|
|
|
* 'per_page' => 5 - Number of results to return
|
|
|
|
* 'offset' => 0
|
|
|
|
* 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
|
2018-08-01 18:11:08 +00:00
|
|
|
* 'order' => 'ASC'.
|
2018-07-26 02:16:02 +00:00
|
|
|
*
|
2018-08-01 18:11:08 +00:00
|
|
|
* @param string $return_format OBJECT, ARRAY_A, or ids.
|
2018-07-26 02:16:02 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function search( $args = array(), $return_format = OBJECT ) {
|
2018-09-20 05:39:56 +00:00
|
|
|
return as_get_scheduled_actions( $args, $return_format );
|
2018-07-26 02:16:02 +00:00
|
|
|
}
|
|
|
|
}
|