| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine; |
| 4 |
|
| 5 |
use StoreEngine\Classes\Exceptions\StoreEngineException; |
| 6 |
use DateTimeZone; |
| 7 |
use StoreEngine\Classes\StoreengineDatetime; |
| 8 |
|
| 9 |
class ActionQueue { |
| 10 |
|
| 11 |
protected static ?ActionQueue $instance = null; |
| 12 |
|
| 13 |
public static function get_instance(): ActionQueue { |
| 14 |
if ( ! function_exists( 'as_schedule_single_action' ) ) { |
| 15 |
throw new StoreEngineException( |
| 16 |
/* translators: %s: Missing Plugin Name. */ |
| 17 |
sprintf( esc_html__( '%s plugin is required.', 'storeengine' ), esc_html__( 'Action Scheduler', 'storeengine' ) ), |
| 18 |
'action-scheduler-missing', |
| 19 |
null, |
| 20 |
500 |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
if ( null === self::$instance ) { |
| 25 |
self::$instance = new self(); |
| 26 |
} |
| 27 |
|
| 28 |
return self::$instance; |
| 29 |
} |
| 30 |
|
| 31 |
protected function __construct() { |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Cloning is forbidden. |
| 36 |
*/ |
| 37 |
public function __clone() { |
| 38 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'storeengine' ), '1.0.0' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Unserializing instances of this class is forbidden. |
| 43 |
*/ |
| 44 |
public function __wakeup() { |
| 45 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of this class is forbidden.', 'storeengine' ), '1.0.0' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Enqueue an action to run one time, as soon as possible |
| 50 |
* |
| 51 |
* @param string $hook The hook to trigger. |
| 52 |
* @param array $args Arguments to pass when the hook triggers. |
| 53 |
* @param string $group The group to assign this job to. |
| 54 |
* |
| 55 |
* @return int The action ID. |
| 56 |
*/ |
| 57 |
public function add( string $hook, array $args = [], string $group = '' ): int { |
| 58 |
return $this->schedule_single( time(), $hook, $args, $group ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Schedule an action to run once at some time in the future |
| 63 |
* |
| 64 |
* @param int $timestamp When the job will run. |
| 65 |
* @param string $hook The hook to trigger. |
| 66 |
* @param array $args Arguments to pass when the hook triggers. |
| 67 |
* @param string $group The group to assign this job to. |
| 68 |
* @return int The action ID. |
| 69 |
*/ |
| 70 |
public function schedule_single( $timestamp, $hook, $args = [], $group = '' ): int { |
| 71 |
return (int) as_schedule_single_action( $timestamp, $hook, $args, $group ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Schedule a recurring action |
| 76 |
* |
| 77 |
* @param int $timestamp When the first instance of the job will run. |
| 78 |
* @param int $interval_in_seconds How long to wait between runs. |
| 79 |
* @param string $hook The hook to trigger. |
| 80 |
* @param array $args Arguments to pass when the hook triggers. |
| 81 |
* @param string $group The group to assign this job to. |
| 82 |
* @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters. |
| 83 |
* @return int The action ID. |
| 84 |
*/ |
| 85 |
public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = [], $group = '', $unique = false ): int { |
| 86 |
return (int) as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group, $unique ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Schedule an action that recurs on a cron-like schedule. |
| 91 |
* |
| 92 |
* @param int $timestamp The schedule will start on or after this time. |
| 93 |
* @param string $cron_schedule A cron-link schedule string. |
| 94 |
* @see http://en.wikipedia.org/wiki/Cron |
| 95 |
* * * * * * * |
| 96 |
* ┬ ┬ ┬ ┬ ┬ ┬ |
| 97 |
* | | | | | | |
| 98 |
* | | | | | + year [optional] |
| 99 |
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7) |
| 100 |
* | | | +---------- month (1 - 12) |
| 101 |
* | | +--------------- day of month (1 - 31) |
| 102 |
* | +-------------------- hour (0 - 23) |
| 103 |
* +------------------------- min (0 - 59) |
| 104 |
* @param string $hook The hook to trigger. |
| 105 |
* @param array $args Arguments to pass when the hook triggers. |
| 106 |
* @param string $group The group to assign this job to. |
| 107 |
* @return int The action ID |
| 108 |
*/ |
| 109 |
public function schedule_cron( int $timestamp, string $cron_schedule, string $hook, array $args = [], string $group = '' ): int { |
| 110 |
return (int) as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Dequeue the next scheduled instance of an action with a matching hook (and optionally matching args and group). |
| 115 |
* |
| 116 |
* Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action. |
| 117 |
* |
| 118 |
* While technically only the next instance of a recurring or cron action is unscheduled by this method, that will also |
| 119 |
* prevent all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled |
| 120 |
* in a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled |
| 121 |
* only after the former action is run. As the next instance is never run, because it's unscheduled by this function, |
| 122 |
* then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled |
| 123 |
* by this method also. |
| 124 |
* |
| 125 |
* @param string $hook The hook that the job will trigger. |
| 126 |
* @param array $args Args that would have been passed to the job. |
| 127 |
* @param string $group The group the job is assigned to (if any). |
| 128 |
*/ |
| 129 |
public function cancel( string $hook, array $args = [], string $group = '' ) { |
| 130 |
as_unschedule_action( $hook, $args, $group ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Dequeue all actions with a matching hook (and optionally matching args and group) so no matching actions are ever run. |
| 135 |
* |
| 136 |
* @param string $hook The hook that the job will trigger. |
| 137 |
* @param array $args Args that would have been passed to the job. |
| 138 |
* @param string $group The group the job is assigned to (if any). |
| 139 |
*/ |
| 140 |
public function cancel_all( string $hook, array $args = [], string $group = '' ) { |
| 141 |
as_unschedule_all_actions( $hook, $args, $group ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the date and time for the next scheduled occurrence of an action with a given hook |
| 146 |
* (an optionally that matches certain args and group), if any. |
| 147 |
* |
| 148 |
* @param string $hook The hook that the job will trigger. |
| 149 |
* @param ?array $args Filter to a hook with matching args that will be passed to the job when it runs. |
| 150 |
* @param string $group Filter to only actions assigned to a specific group. |
| 151 |
* |
| 152 |
* @return StoreengineDatetime|null The date and time for the next occurrence, or null if there is no pending or running scheduled action for the given hook. |
| 153 |
*/ |
| 154 |
public function get_next( string $hook, ?array $args = null, string $group = '' ): ?StoreengineDatetime { |
| 155 |
$next_timestamp = as_next_scheduled_action( $hook, $args, $group ); |
| 156 |
|
| 157 |
if ( is_numeric( $next_timestamp ) ) { |
| 158 |
return new StoreengineDatetime( "@{$next_timestamp}", new DateTimeZone( 'UTC' ) ); |
| 159 |
} |
| 160 |
|
| 161 |
// as_next_scheduled_action returns true (not a timestamp) when a matching action is |
| 162 |
// currently in-progress / async-pending. Treat that as "scheduled now" so callers |
| 163 |
// guarding with `if ( ! get_next(...) )` don't enqueue duplicate recurring actions. |
| 164 |
if ( true === $next_timestamp ) { |
| 165 |
return new StoreengineDatetime( '@' . time(), new DateTimeZone( 'UTC' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Check whether a matching action is currently pending or in-progress. |
| 173 |
* |
| 174 |
* Prefer this over get_next() when you only need a yes/no answer — it's cheaper and |
| 175 |
* does not require the buggy boolean/timestamp conversion in get_next(). |
| 176 |
* |
| 177 |
* @param string $hook The hook that the job will trigger. |
| 178 |
* @param ?array $args Filter to a hook with matching args. Null matches any args. |
| 179 |
* @param string $group Filter to only actions assigned to a specific group. |
| 180 |
*/ |
| 181 |
public function has_scheduled( string $hook, ?array $args = null, string $group = '' ): bool { |
| 182 |
return (bool) as_has_scheduled_action( $hook, $args, $group ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Find scheduled actions |
| 187 |
* |
| 188 |
* @param array $args Possible arguments, with their default values: |
| 189 |
* 'hook' => '' - the name of the action that will be triggered |
| 190 |
* 'args' => null - the args array that will be passed with the action |
| 191 |
* '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. |
| 192 |
* 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '=' |
| 193 |
* '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. |
| 194 |
* 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '=' |
| 195 |
* 'group' => '' - the group the action belongs to |
| 196 |
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
| 197 |
* 'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID |
| 198 |
* 'per_page' => 5 - Number of results to return |
| 199 |
* 'offset' => 0 |
| 200 |
* 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date' |
| 201 |
* 'order' => 'ASC'. |
| 202 |
* |
| 203 |
* @param string $return_format OBJECT, ARRAY_A, or ids. |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function search( array $args = [], string $return_format = OBJECT ): array { |
| 207 |
return as_get_scheduled_actions( $args, $return_format ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Test whether the context of execution comes from async action scheduler. |
| 212 |
* |
| 213 |
* @since 1.6.7 |
| 214 |
* @return bool |
| 215 |
*/ |
| 216 |
public static function is_doing_scheduled_actions(): bool { |
| 217 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 218 |
return isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === wp_unslash( $_REQUEST['action'] ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// End of file action-queue.php. |
| 223 |
|