AsyncRequest.php
1 year ago
AsyncWebhookService.php
1 year ago
BackgroundProcess.php
1 year ago
BackgroundServiceProvider.php
1 year ago
BulkActionService.php
1 year ago
QueueService.php
1 year ago
QueueService.php
278 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Background; |
| 4 | |
| 5 | /** |
| 6 | * SureCart Queue |
| 7 | * |
| 8 | * A job queue using WordPress actions. |
| 9 | */ |
| 10 | class QueueService { |
| 11 | /** |
| 12 | * Enqueue an action to run one time, as soon as possible |
| 13 | * |
| 14 | * @param string $hook The hook to trigger. |
| 15 | * @param array $args Arguments to pass when the hook triggers. |
| 16 | * @param string $group The group to assign this job to. |
| 17 | * @param bool $unique If true, ensure this action is not already scheduled. |
| 18 | * |
| 19 | * @return string $this. |
| 20 | */ |
| 21 | public function async( $hook, $args = array(), $group = '', $unique = false ) { |
| 22 | \as_enqueue_async_action( $hook, $args, $group, $unique ); |
| 23 | return $this; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Schedule an action to run once at some time in the future |
| 28 | * |
| 29 | * @param int $timestamp When the job will run. |
| 30 | * @param string $hook The hook to trigger. |
| 31 | * @param array $args Arguments to pass when the hook triggers. |
| 32 | * @param string $group The group to assign this job to. |
| 33 | * @return string $this. |
| 34 | */ |
| 35 | public function scheduleSingle( $timestamp, $hook, $args = array(), $group = '' ) { |
| 36 | \as_schedule_single_action( $timestamp, $hook, $args, $group ); |
| 37 | return $this; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Schedule a recurring action |
| 42 | * |
| 43 | * @param int $timestamp When the first instance of the job will run. |
| 44 | * @param int $interval_in_seconds How long to wait between runs. |
| 45 | * @param string $hook The hook to trigger. |
| 46 | * @param array $args Arguments to pass when the hook triggers. |
| 47 | * @param string $group The group to assign this job to. |
| 48 | * @return string $this. |
| 49 | */ |
| 50 | public function scheduleRecurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) { |
| 51 | \as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Schedule an action that recurs on a cron-like schedule. |
| 57 | * |
| 58 | * @param int $timestamp The schedule will start on or after this time. |
| 59 | * @param string $cron_schedule A cron-link schedule string. |
| 60 | * @see http://en.wikipedia.org/wiki/Cron |
| 61 | * * * * * * * |
| 62 | * ┬ ┬ ┬ ┬ ┬ ┬ |
| 63 | * | | | | | | |
| 64 | * | | | | | + year [optional] |
| 65 | * | | | | +----- day of week (0 - 7) (Sunday=0 or 7) |
| 66 | * | | | +---------- month (1 - 12) |
| 67 | * | | +--------------- day of month (1 - 31) |
| 68 | * | +-------------------- hour (0 - 23) |
| 69 | * +------------------------- min (0 - 59) |
| 70 | * @param string $hook The hook to trigger. |
| 71 | * @param array $args Arguments to pass when the hook triggers. |
| 72 | * @param string $group The group to assign this job to. |
| 73 | * @return string $this |
| 74 | */ |
| 75 | public function scheduleCron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) { |
| 76 | \as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group ); |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Dequeue the next scheduled instance of an action with a matching hook (and optionally matching args and group). |
| 82 | * |
| 83 | * Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action. |
| 84 | * |
| 85 | * While technically only the next instance of a recurring or cron action is unscheduled by this method, that will also |
| 86 | * prevent all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled |
| 87 | * in a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled |
| 88 | * only after the former action is run. As the next instance is never run, because it's unscheduled by this function, |
| 89 | * then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled |
| 90 | * by this method also. |
| 91 | * |
| 92 | * @param string $hook The hook that the job will trigger. |
| 93 | * @param array $args Args that would have been passed to the job. |
| 94 | * @param string $group The group the job is assigned to (if any). |
| 95 | */ |
| 96 | public function cancel( $hook, $args = array(), $group = '' ) { |
| 97 | \as_unschedule_action( $hook, $args, $group ); |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Dequeue all actions with a matching hook (and optionally matching args and group) so no matching actions are ever run. |
| 103 | * |
| 104 | * @param string $hook The hook that the job will trigger. |
| 105 | * @param array $args Args that would have been passed to the job. |
| 106 | * @param string $group The group the job is assigned to (if any). |
| 107 | */ |
| 108 | public function cancelAll( $hook, $args = array(), $group = '' ) { |
| 109 | \as_unschedule_all_actions( $hook, $args, $group ); |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the date and time for the next scheduled occurrence of an action with a given hook |
| 115 | * (an optionally that matches certain args and group), if any. |
| 116 | * |
| 117 | * @param string $hook The hook that the job will trigger. |
| 118 | * @param array $args Filter to a hook with matching args that will be passed to the job when it runs. |
| 119 | * @param string $group Filter to only actions assigned to a specific group. |
| 120 | * @return DateTime|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook. |
| 121 | */ |
| 122 | public function getNext( $hook, $args = null, $group = '' ) { |
| 123 | return as_next_scheduled_action( $hook, $args, $group ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the date and time for the next scheduled occurrence of an action with a given hook |
| 128 | * (an optionally that matches certain args and group), if any. |
| 129 | * |
| 130 | * @param string $hook The hook that the job will trigger. |
| 131 | * @param array $args Filter to a hook with matching args that will be passed to the job when it runs. |
| 132 | * @param string $group Filter to only actions assigned to a specific group. |
| 133 | * @return DateTime|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook. |
| 134 | */ |
| 135 | public function getNextDate( $hook, $args = null, $group = '' ) { |
| 136 | $next_timestamp = as_next_scheduled_action( $hook, $args, $group ); |
| 137 | |
| 138 | if ( is_numeric( $next_timestamp ) ) { |
| 139 | return new \DateTime( "@{$next_timestamp}", new \DateTimeZone( 'UTC' ) ); |
| 140 | } |
| 141 | |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Check if there is a scheduled action in the queue but more efficiently than as_next_scheduled_action(). |
| 148 | * |
| 149 | * It's recommended to use this function when you need to know whether a specific action is currently scheduled |
| 150 | * (pending or in-progress). |
| 151 | * |
| 152 | * @since 3.3.0 |
| 153 | * |
| 154 | * @param string $hook The hook of the action. |
| 155 | * @param array $args Args that have been passed to the action. Null will matches any args. |
| 156 | * @param string $group The group the job is assigned to. |
| 157 | * |
| 158 | * @return bool True if a matching action is pending or in-progress, false otherwise. |
| 159 | */ |
| 160 | public function isScheduled( $hook, $args = null, $group = '' ) { |
| 161 | return \as_has_scheduled_action( $hook, $args, $group ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Shold we show a migration notice? |
| 166 | * |
| 167 | * @param string $hook The hook that the job will trigger. |
| 168 | * @param array $args Args that have been passed to the action. Null will matches any args. |
| 169 | * @param string $group The group the job is assigned to. |
| 170 | * |
| 171 | * @return bool |
| 172 | */ |
| 173 | public function showNotice( $hook, $args = null, $group = '' ) { |
| 174 | // no scheduled actions (this is quicker for us to check). |
| 175 | $has_action = \as_has_scheduled_action( $hook, $args, $group ); |
| 176 | if ( ! $has_action ) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | $next_action = as_get_scheduled_actions( |
| 181 | [ |
| 182 | 'hook' => $hook, |
| 183 | 'status' => \ActionScheduler_Store::STATUS_PENDING, |
| 184 | 'per_page' => 1, |
| 185 | ], |
| 186 | ); |
| 187 | |
| 188 | if ( empty( $next_action ) || ! is_array( $next_action ) ) { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // find out if any actions get_args() has 'show_notice' => true. |
| 193 | $show_notice = false; |
| 194 | foreach ( $next_action as $action ) { |
| 195 | $args = $action->get_args(); |
| 196 | if ( isset( $args['show_notice'] ) && ! empty( $args['show_notice'] ) ) { |
| 197 | $show_notice = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return $show_notice; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Check if there are any failed actions for a given hook and group. |
| 207 | * |
| 208 | * @param string $hook The hook that the job will trigger. |
| 209 | * @param string $group The group the job is assigned to (if any). |
| 210 | * @return bool True if there are any failed actions, false otherwise. |
| 211 | */ |
| 212 | public function hasFailedActions( $hook, $group = '' ) { |
| 213 | $args = [ |
| 214 | 'hook' => $hook, |
| 215 | 'group' => $group, |
| 216 | 'status' => \ActionScheduler_Store::STATUS_FAILED, |
| 217 | 'per_page' => 1, // We only need to know if at least one exists. |
| 218 | ]; |
| 219 | |
| 220 | $failed_actions = $this->search( $args, 'ids' ); |
| 221 | |
| 222 | return ! empty( $failed_actions ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Find scheduled actions |
| 227 | * |
| 228 | * @param array $args Possible arguments, with their default values: |
| 229 | * 'hook' => '' - the name of the action that will be triggered |
| 230 | * 'args' => null - the args array that will be passed with the action |
| 231 | * '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. |
| 232 | * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '=' |
| 233 | * '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. |
| 234 | * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '=' |
| 235 | * 'group' => '' - the group the action belongs to |
| 236 | * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
| 237 | * 'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID |
| 238 | * 'per_page' => 5 - Number of results to return |
| 239 | * 'offset' => 0 |
| 240 | * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date' |
| 241 | * 'order' => 'ASC'. |
| 242 | * |
| 243 | * @param string $return_format OBJECT, ARRAY_A, or ids. |
| 244 | * @return array |
| 245 | */ |
| 246 | public function search( $args = array(), $return_format = OBJECT ) { |
| 247 | return \as_get_scheduled_actions( $args, $return_format ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Start running the queue immediately. |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public function run() { |
| 256 | $identifier = 'as_async_request_queue_runner'; |
| 257 | |
| 258 | // make an async request to run the queue. |
| 259 | wp_remote_post( |
| 260 | esc_url_raw( |
| 261 | add_query_arg( |
| 262 | [ |
| 263 | 'action' => $identifier, |
| 264 | 'nonce' => wp_create_nonce( $identifier ), |
| 265 | ], |
| 266 | admin_url( 'admin-ajax.php' ) |
| 267 | ) |
| 268 | ), |
| 269 | array( |
| 270 | 'timeout' => 0.01, |
| 271 | 'blocking' => false, |
| 272 | 'cookies' => $_COOKIE, |
| 273 | 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
| 274 | ) |
| 275 | ); |
| 276 | } |
| 277 | } |
| 278 |