PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.3
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Background / QueueService.php
surecart / app / src / Background Last commit date
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