Add WC_Action_Queue

As a wrapper for Action Scheduler. This implements the WC_Queue_Interface
so it can be used as WC core's job queue. Implementing WC_Queue_Interface
also means this class has more generic and appropriate job queue nomenclature
than Action Scheduler itself. For exsample, it provides a method for
non-delayed jobs, i.e. WC_Queue_Interface::add(), unlike Action Scheduler,
which requires scheduling a job for now(), which while fairly simple, is not
intuitive or immediately obvious to new comers.

This makes it both more clear and convenient to use Action Scheduler
as a simple job queue instead of just a queue for delayed jobs.
This commit is contained in:
Brent Shepherd 2018-07-26 12:16:02 +10:00
parent 8e4466c9c8
commit 0611ff7665
2 changed files with 140 additions and 0 deletions

View File

@ -334,6 +334,7 @@ final class WooCommerce {
include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
include_once WC_ABSPATH . 'includes/class-wc-logger.php';
include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
/**
* Data stores - used to store and retrieve CRUD object data from the database.

View File

@ -0,0 +1,139 @@
<?php
/**
* Action Queue
*
* @version 3.5.0
* @package WooCommerce/Interface
*/
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
*
* @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
*/
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
*
* @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
*/
public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
return wc_schedule_single_action( $timestamp, $hook, $args, $group );
}
/**
* Schedule a recurring action
*
* @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
*/
public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
return wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
}
/**
* Schedule an action that recurs on a cron-like schedule.
*
* @param int $timestamp The schedule will start on or after this time
* @param string $cron_schedule A cron-link schedule string
* @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)
* @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
*/
public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
return wc_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group );
}
/**
* Dequeue all actions with a matching hook (and optionally matching args and group) so they are not run.
*
* Any recurring actions with a matching hook will also be cancelled, not just the next scheduled action.
*
* Technically, one action in a recurring or Cron action is scheduled at any one point in time. The next
* in the sequence is scheduled after the previous one is run, so only the next scheduled action needs to
* be cancelled/dequeued to stop the sequence.
*
* @param string $hook The hook that the job will trigger
* @param array $args Args that would have been passed to the job
* @param string $group
*/
public function cancel( $hook, $args = array(), $group = '' ) {
wc_unschedule_action( $hook, $args, $group );
}
/**
* @param string $hook
* @param array $args
* @param string $group
*
* @return int|bool The timestamp for the next occurrence, or false if nothing was found
*/
public function get_next( $hook, $args = NULL, $group = '' ) {
return wc_next_scheduled_action( $hook, $args, $group );
}
/**
* Find scheduled actions
*
* @param array $args Possible arguments, with their default values:
* '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'
* 'order' => 'ASC'
*
* @param string $return_format OBJECT, ARRAY_A, or ids
*
* @return array
*/
public function search( $args = array(), $return_format = OBJECT ) {
return wc_get_scheduled_actions( $args, $return_format );
}
}