@@ -6,606 +6,606 @@ |
||
6 | 6 | */ |
7 | 7 | class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { |
8 | 8 | |
9 | - /** |
|
10 | - * The package name. |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $package = 'action-scheduler'; |
|
15 | - |
|
16 | - /** |
|
17 | - * Columns to show (name => label). |
|
18 | - * |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $columns = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * Actions (name => label). |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $row_actions = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * The active data stores |
|
32 | - * |
|
33 | - * @var ActionScheduler_Store |
|
34 | - */ |
|
35 | - protected $store; |
|
36 | - |
|
37 | - /** |
|
38 | - * A logger to use for getting action logs to display |
|
39 | - * |
|
40 | - * @var ActionScheduler_Logger |
|
41 | - */ |
|
42 | - protected $logger; |
|
43 | - |
|
44 | - /** |
|
45 | - * A ActionScheduler_QueueRunner runner instance (or child class) |
|
46 | - * |
|
47 | - * @var ActionScheduler_QueueRunner |
|
48 | - */ |
|
49 | - protected $runner; |
|
50 | - |
|
51 | - /** |
|
52 | - * Bulk actions. The key of the array is the method name of the implementation: |
|
53 | - * |
|
54 | - * bulk_<key>(array $ids, string $sql_in). |
|
55 | - * |
|
56 | - * See the comments in the parent class for further details |
|
57 | - * |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $bulk_actions = array(); |
|
61 | - |
|
62 | - /** |
|
63 | - * Flag variable to render our notifications, if any, once. |
|
64 | - * |
|
65 | - * @var bool |
|
66 | - */ |
|
67 | - protected static $did_notification = false; |
|
68 | - |
|
69 | - /** |
|
70 | - * Array of seconds for common time periods, like week or month, alongside an internationalised string representation, i.e. "Day" or "Days" |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - private static $time_periods; |
|
75 | - |
|
76 | - /** |
|
77 | - * Sets the current data store object into `store->action` and initialises the object. |
|
78 | - * |
|
79 | - * @param ActionScheduler_Store $store |
|
80 | - * @param ActionScheduler_Logger $logger |
|
81 | - * @param ActionScheduler_QueueRunner $runner |
|
82 | - */ |
|
83 | - public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
|
84 | - |
|
85 | - $this->store = $store; |
|
86 | - $this->logger = $logger; |
|
87 | - $this->runner = $runner; |
|
88 | - |
|
89 | - $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
|
90 | - |
|
91 | - $this->bulk_actions = array( |
|
92 | - 'delete' => __( 'Delete', 'action-scheduler' ), |
|
93 | - ); |
|
94 | - |
|
95 | - $this->columns = array( |
|
96 | - 'hook' => __( 'Hook', 'action-scheduler' ), |
|
97 | - 'status' => __( 'Status', 'action-scheduler' ), |
|
98 | - 'args' => __( 'Arguments', 'action-scheduler' ), |
|
99 | - 'group' => __( 'Group', 'action-scheduler' ), |
|
100 | - 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
|
101 | - 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
|
102 | - 'log_entries' => __( 'Log', 'action-scheduler' ), |
|
103 | - ); |
|
104 | - |
|
105 | - $this->sort_by = array( |
|
106 | - 'schedule', |
|
107 | - 'hook', |
|
108 | - 'group', |
|
109 | - ); |
|
110 | - |
|
111 | - $this->search_by = array( |
|
112 | - 'hook', |
|
113 | - 'args', |
|
114 | - 'claim_id', |
|
115 | - ); |
|
116 | - |
|
117 | - $request_status = $this->get_request_status(); |
|
118 | - |
|
119 | - if ( empty( $request_status ) ) { |
|
120 | - $this->sort_by[] = 'status'; |
|
121 | - } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { |
|
122 | - $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
|
123 | - $this->sort_by[] = 'claim_id'; |
|
124 | - } |
|
125 | - |
|
126 | - $this->row_actions = array( |
|
127 | - 'hook' => array( |
|
128 | - 'run' => array( |
|
129 | - 'name' => __( 'Run', 'action-scheduler' ), |
|
130 | - 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
|
131 | - ), |
|
132 | - 'cancel' => array( |
|
133 | - 'name' => __( 'Cancel', 'action-scheduler' ), |
|
134 | - 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
|
135 | - 'class' => 'cancel trash', |
|
136 | - ), |
|
137 | - ), |
|
138 | - ); |
|
139 | - |
|
140 | - self::$time_periods = array( |
|
141 | - array( |
|
142 | - 'seconds' => YEAR_IN_SECONDS, |
|
143 | - /* translators: %s: amount of time */ |
|
144 | - 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
|
145 | - ), |
|
146 | - array( |
|
147 | - 'seconds' => MONTH_IN_SECONDS, |
|
148 | - /* translators: %s: amount of time */ |
|
149 | - 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
|
150 | - ), |
|
151 | - array( |
|
152 | - 'seconds' => WEEK_IN_SECONDS, |
|
153 | - /* translators: %s: amount of time */ |
|
154 | - 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
|
155 | - ), |
|
156 | - array( |
|
157 | - 'seconds' => DAY_IN_SECONDS, |
|
158 | - /* translators: %s: amount of time */ |
|
159 | - 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
|
160 | - ), |
|
161 | - array( |
|
162 | - 'seconds' => HOUR_IN_SECONDS, |
|
163 | - /* translators: %s: amount of time */ |
|
164 | - 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
|
165 | - ), |
|
166 | - array( |
|
167 | - 'seconds' => MINUTE_IN_SECONDS, |
|
168 | - /* translators: %s: amount of time */ |
|
169 | - 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
|
170 | - ), |
|
171 | - array( |
|
172 | - 'seconds' => 1, |
|
173 | - /* translators: %s: amount of time */ |
|
174 | - 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
|
175 | - ), |
|
176 | - ); |
|
177 | - |
|
178 | - parent::__construct( array( |
|
179 | - 'singular' => 'action-scheduler', |
|
180 | - 'plural' => 'action-scheduler', |
|
181 | - 'ajax' => false, |
|
182 | - ) ); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Convert an interval of seconds into a two part human friendly string. |
|
187 | - * |
|
188 | - * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
189 | - * even if an action is 1 day and 11 hours away, it will display "1 day". This function goes one step |
|
190 | - * further to display two degrees of accuracy. |
|
191 | - * |
|
192 | - * Inspired by the Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
193 | - * |
|
194 | - * @param int $interval A interval in seconds. |
|
195 | - * @param int $periods_to_include Depth of time periods to include, e.g. for an interval of 70, and $periods_to_include of 2, both minutes and seconds would be included. With a value of 1, only minutes would be included. |
|
196 | - * @return string A human friendly string representation of the interval. |
|
197 | - */ |
|
198 | - private static function human_interval( $interval, $periods_to_include = 2 ) { |
|
199 | - |
|
200 | - if ( $interval <= 0 ) { |
|
201 | - return __( 'Now!', 'action-scheduler' ); |
|
202 | - } |
|
203 | - |
|
204 | - $output = ''; |
|
205 | - |
|
206 | - for ( $time_period_index = 0, $periods_included = 0, $seconds_remaining = $interval; $time_period_index < count( self::$time_periods ) && $seconds_remaining > 0 && $periods_included < $periods_to_include; $time_period_index++ ) { |
|
207 | - |
|
208 | - $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
|
209 | - |
|
210 | - if ( $periods_in_interval > 0 ) { |
|
211 | - if ( ! empty( $output ) ) { |
|
212 | - $output .= ' '; |
|
213 | - } |
|
214 | - $output .= sprintf( _n( self::$time_periods[ $time_period_index ]['names'][0], self::$time_periods[ $time_period_index ]['names'][1], $periods_in_interval, 'action-scheduler' ), $periods_in_interval ); |
|
215 | - $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
|
216 | - $periods_included++; |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - return $output; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. |
|
225 | - * |
|
226 | - * @param ActionScheduler_Action $action |
|
227 | - * |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - protected function get_recurrence( $action ) { |
|
231 | - $schedule = $action->get_schedule(); |
|
232 | - if ( $schedule->is_recurring() ) { |
|
233 | - $recurrence = $schedule->get_recurrence(); |
|
234 | - |
|
235 | - if ( is_numeric( $recurrence ) ) { |
|
236 | - /* translators: %s: time interval */ |
|
237 | - return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
|
238 | - } else { |
|
239 | - return $recurrence; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - return __( 'Non-repeating', 'action-scheduler' ); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Serializes the argument of an action to render it in a human friendly format. |
|
248 | - * |
|
249 | - * @param array $row The array representation of the current row of the table |
|
250 | - * |
|
251 | - * @return string |
|
252 | - */ |
|
253 | - public function column_args( array $row ) { |
|
254 | - if ( empty( $row['args'] ) ) { |
|
255 | - return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
|
256 | - } |
|
257 | - |
|
258 | - $row_html = '<ul>'; |
|
259 | - foreach ( $row['args'] as $key => $value ) { |
|
260 | - $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); |
|
261 | - } |
|
262 | - $row_html .= '</ul>'; |
|
263 | - |
|
264 | - return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
269 | - * |
|
270 | - * @param array $row Action array. |
|
271 | - * @return string |
|
272 | - */ |
|
273 | - public function column_log_entries( array $row ) { |
|
274 | - |
|
275 | - $log_entries_html = '<ol>'; |
|
276 | - |
|
277 | - $timezone = new DateTimezone( 'UTC' ); |
|
278 | - |
|
279 | - foreach ( $row['log_entries'] as $log_entry ) { |
|
280 | - $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
|
281 | - } |
|
282 | - |
|
283 | - $log_entries_html .= '</ol>'; |
|
284 | - |
|
285 | - return $log_entries_html; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
290 | - * |
|
291 | - * @param ActionScheduler_LogEntry $log_entry |
|
292 | - * @param DateTimezone $timezone |
|
293 | - * @return string |
|
294 | - */ |
|
295 | - protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
|
296 | - $date = $log_entry->get_date(); |
|
297 | - $date->setTimezone( $timezone ); |
|
298 | - return sprintf( '<li><strong>%s</strong><br/>%s</li>', esc_html( $date->format( 'Y-m-d H:i:s O' ) ), esc_html( $log_entry->get_message() ) ); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Only display row actions for pending actions. |
|
303 | - * |
|
304 | - * @param array $row Row to render |
|
305 | - * @param string $column_name Current row |
|
306 | - * |
|
307 | - * @return string |
|
308 | - */ |
|
309 | - protected function maybe_render_actions( $row, $column_name ) { |
|
310 | - if ( 'pending' === strtolower( $row['status'] ) ) { |
|
311 | - return parent::maybe_render_actions( $row, $column_name ); |
|
312 | - } |
|
313 | - |
|
314 | - return ''; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Renders admin notifications |
|
319 | - * |
|
320 | - * Notifications: |
|
321 | - * 1. When the maximum number of tasks are being executed simultaneously. |
|
322 | - * 2. Notifications when a task is manually executed. |
|
323 | - * 3. Tables are missing. |
|
324 | - */ |
|
325 | - public function display_admin_notices() { |
|
326 | - global $wpdb; |
|
327 | - |
|
328 | - if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
|
329 | - $table_list = array( |
|
330 | - 'actionscheduler_actions', |
|
331 | - 'actionscheduler_logs', |
|
332 | - 'actionscheduler_groups', |
|
333 | - 'actionscheduler_claims', |
|
334 | - ); |
|
335 | - |
|
336 | - $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
337 | - foreach ( $table_list as $table_name ) { |
|
338 | - if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { |
|
339 | - $this->admin_notices[] = array( |
|
340 | - 'class' => 'error', |
|
341 | - 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).' , 'action-scheduler' ), |
|
342 | - ); |
|
343 | - $this->recreate_tables(); |
|
344 | - parent::display_admin_notices(); |
|
345 | - |
|
346 | - return; |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - if ( $this->runner->has_maximum_concurrent_batches() ) { |
|
352 | - $claim_count = $this->store->get_claim_count(); |
|
353 | - $this->admin_notices[] = array( |
|
354 | - 'class' => 'updated', |
|
355 | - 'message' => sprintf( |
|
356 | - /* translators: %s: amount of claims */ |
|
357 | - _n( |
|
358 | - 'Maximum simultaneous queues already in progress (%s queue). No additional queues will begin processing until the current queues are complete.', |
|
359 | - 'Maximum simultaneous queues already in progress (%s queues). No additional queues will begin processing until the current queues are complete.', |
|
360 | - $claim_count, |
|
361 | - 'action-scheduler' |
|
362 | - ), |
|
363 | - $claim_count |
|
364 | - ), |
|
365 | - ); |
|
366 | - } elseif ( $this->store->has_pending_actions_due() ) { |
|
367 | - |
|
368 | - $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
|
369 | - |
|
370 | - // No lock set or lock expired |
|
371 | - if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
|
372 | - $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
|
373 | - /* translators: %s: process URL */ |
|
374 | - $async_request_message = sprintf( __( 'A new queue has begun processing. <a href="%s">View actions in-progress »</a>', 'action-scheduler' ), esc_url( $in_progress_url ) ); |
|
375 | - } else { |
|
376 | - /* translators: %d: seconds */ |
|
377 | - $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
|
378 | - } |
|
379 | - |
|
380 | - $this->admin_notices[] = array( |
|
381 | - 'class' => 'notice notice-info', |
|
382 | - 'message' => $async_request_message, |
|
383 | - ); |
|
384 | - } |
|
385 | - |
|
386 | - $notification = get_transient( 'action_scheduler_admin_notice' ); |
|
387 | - |
|
388 | - if ( is_array( $notification ) ) { |
|
389 | - delete_transient( 'action_scheduler_admin_notice' ); |
|
390 | - |
|
391 | - $action = $this->store->fetch_action( $notification['action_id'] ); |
|
392 | - $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
|
393 | - if ( 1 == $notification['success'] ) { |
|
394 | - $class = 'updated'; |
|
395 | - switch ( $notification['row_action_type'] ) { |
|
396 | - case 'run' : |
|
397 | - /* translators: %s: action HTML */ |
|
398 | - $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
|
399 | - break; |
|
400 | - case 'cancel' : |
|
401 | - /* translators: %s: action HTML */ |
|
402 | - $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
|
403 | - break; |
|
404 | - default : |
|
405 | - /* translators: %s: action HTML */ |
|
406 | - $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
|
407 | - break; |
|
408 | - } |
|
409 | - } else { |
|
410 | - $class = 'error'; |
|
411 | - /* translators: 1: action HTML 2: action ID 3: error message */ |
|
412 | - $action_message_html = sprintf( __( 'Could not process change for action: "%1$s" (ID: %2$d). Error: %3$s', 'action-scheduler' ), $action_hook_html, esc_html( $notification['action_id'] ), esc_html( $notification['error_message'] ) ); |
|
413 | - } |
|
414 | - |
|
415 | - $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
|
416 | - |
|
417 | - $this->admin_notices[] = array( |
|
418 | - 'class' => $class, |
|
419 | - 'message' => $action_message_html, |
|
420 | - ); |
|
421 | - } |
|
422 | - |
|
423 | - parent::display_admin_notices(); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Prints the scheduled date in a human friendly format. |
|
428 | - * |
|
429 | - * @param array $row The array representation of the current row of the table |
|
430 | - * |
|
431 | - * @return string |
|
432 | - */ |
|
433 | - public function column_schedule( $row ) { |
|
434 | - return $this->get_schedule_display_string( $row['schedule'] ); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Get the scheduled date in a human friendly format. |
|
439 | - * |
|
440 | - * @param ActionScheduler_Schedule $schedule |
|
441 | - * @return string |
|
442 | - */ |
|
443 | - protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
|
444 | - |
|
445 | - $schedule_display_string = ''; |
|
446 | - |
|
447 | - if ( ! $schedule->get_date() ) { |
|
448 | - return '0000-00-00 00:00:00'; |
|
449 | - } |
|
450 | - |
|
451 | - $next_timestamp = $schedule->get_date()->getTimestamp(); |
|
452 | - |
|
453 | - $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
|
454 | - $schedule_display_string .= '<br/>'; |
|
455 | - |
|
456 | - if ( gmdate( 'U' ) > $next_timestamp ) { |
|
457 | - /* translators: %s: date interval */ |
|
458 | - $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
|
459 | - } else { |
|
460 | - /* translators: %s: date interval */ |
|
461 | - $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
|
462 | - } |
|
463 | - |
|
464 | - return $schedule_display_string; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Bulk delete |
|
469 | - * |
|
470 | - * Deletes actions based on their ID. This is the handler for the bulk delete. It assumes the data |
|
471 | - * properly validated by the callee and it will delete the actions without any extra validation. |
|
472 | - * |
|
473 | - * @param array $ids |
|
474 | - * @param string $ids_sql Inherited and unused |
|
475 | - */ |
|
476 | - protected function bulk_delete( array $ids, $ids_sql ) { |
|
477 | - foreach ( $ids as $id ) { |
|
478 | - $this->store->delete_action( $id ); |
|
479 | - } |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
484 | - * parameters are valid. |
|
485 | - * |
|
486 | - * @param int $action_id |
|
487 | - */ |
|
488 | - protected function row_action_cancel( $action_id ) { |
|
489 | - $this->process_row_action( $action_id, 'cancel' ); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
494 | - * parameters are valid. |
|
495 | - * |
|
496 | - * @param int $action_id |
|
497 | - */ |
|
498 | - protected function row_action_run( $action_id ) { |
|
499 | - $this->process_row_action( $action_id, 'run' ); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Force the data store schema updates. |
|
504 | - */ |
|
505 | - protected function recreate_tables() { |
|
506 | - if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
|
507 | - $store = $this->store; |
|
508 | - } else { |
|
509 | - $store = new ActionScheduler_HybridStore(); |
|
510 | - } |
|
511 | - add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
|
512 | - |
|
513 | - $store_schema = new ActionScheduler_StoreSchema(); |
|
514 | - $logger_schema = new ActionScheduler_LoggerSchema(); |
|
515 | - $store_schema->register_tables( true ); |
|
516 | - $logger_schema->register_tables( true ); |
|
517 | - |
|
518 | - remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
|
519 | - } |
|
520 | - /** |
|
521 | - * Implements the logic behind processing an action once an action link is clicked on the list table. |
|
522 | - * |
|
523 | - * @param int $action_id |
|
524 | - * @param string $row_action_type The type of action to perform on the action. |
|
525 | - */ |
|
526 | - protected function process_row_action( $action_id, $row_action_type ) { |
|
527 | - try { |
|
528 | - switch ( $row_action_type ) { |
|
529 | - case 'run' : |
|
530 | - $this->runner->process_action( $action_id, 'Admin List Table' ); |
|
531 | - break; |
|
532 | - case 'cancel' : |
|
533 | - $this->store->cancel_action( $action_id ); |
|
534 | - break; |
|
535 | - } |
|
536 | - $success = 1; |
|
537 | - $error_message = ''; |
|
538 | - } catch ( Exception $e ) { |
|
539 | - $success = 0; |
|
540 | - $error_message = $e->getMessage(); |
|
541 | - } |
|
542 | - |
|
543 | - set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * {@inheritDoc} |
|
548 | - */ |
|
549 | - public function prepare_items() { |
|
550 | - $this->prepare_column_headers(); |
|
551 | - |
|
552 | - $per_page = $this->get_items_per_page( $this->package . '_items_per_page', $this->items_per_page ); |
|
553 | - $query = array( |
|
554 | - 'per_page' => $per_page, |
|
555 | - 'offset' => $this->get_items_offset(), |
|
556 | - 'status' => $this->get_request_status(), |
|
557 | - 'orderby' => $this->get_request_orderby(), |
|
558 | - 'order' => $this->get_request_order(), |
|
559 | - 'search' => $this->get_request_search_query(), |
|
560 | - ); |
|
561 | - |
|
562 | - $this->items = array(); |
|
563 | - |
|
564 | - $total_items = $this->store->query_actions( $query, 'count' ); |
|
565 | - |
|
566 | - $status_labels = $this->store->get_status_labels(); |
|
567 | - |
|
568 | - foreach ( $this->store->query_actions( $query ) as $action_id ) { |
|
569 | - try { |
|
570 | - $action = $this->store->fetch_action( $action_id ); |
|
571 | - } catch ( Exception $e ) { |
|
572 | - continue; |
|
573 | - } |
|
574 | - if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
|
575 | - continue; |
|
576 | - } |
|
577 | - $this->items[ $action_id ] = array( |
|
578 | - 'ID' => $action_id, |
|
579 | - 'hook' => $action->get_hook(), |
|
580 | - 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
|
581 | - 'args' => $action->get_args(), |
|
582 | - 'group' => $action->get_group(), |
|
583 | - 'log_entries' => $this->logger->get_logs( $action_id ), |
|
584 | - 'claim_id' => $this->store->get_claim_id( $action_id ), |
|
585 | - 'recurrence' => $this->get_recurrence( $action ), |
|
586 | - 'schedule' => $action->get_schedule(), |
|
587 | - ); |
|
588 | - } |
|
589 | - |
|
590 | - $this->set_pagination_args( array( |
|
591 | - 'total_items' => $total_items, |
|
592 | - 'per_page' => $per_page, |
|
593 | - 'total_pages' => ceil( $total_items / $per_page ), |
|
594 | - ) ); |
|
595 | - } |
|
596 | - |
|
597 | - /** |
|
598 | - * Prints the available statuses so the user can click to filter. |
|
599 | - */ |
|
600 | - protected function display_filter_by_status() { |
|
601 | - $this->status_counts = $this->store->action_counts(); |
|
602 | - parent::display_filter_by_status(); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Get the text to display in the search box on the list table. |
|
607 | - */ |
|
608 | - protected function get_search_box_button_text() { |
|
609 | - return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
|
610 | - } |
|
9 | + /** |
|
10 | + * The package name. |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $package = 'action-scheduler'; |
|
15 | + |
|
16 | + /** |
|
17 | + * Columns to show (name => label). |
|
18 | + * |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $columns = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * Actions (name => label). |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $row_actions = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * The active data stores |
|
32 | + * |
|
33 | + * @var ActionScheduler_Store |
|
34 | + */ |
|
35 | + protected $store; |
|
36 | + |
|
37 | + /** |
|
38 | + * A logger to use for getting action logs to display |
|
39 | + * |
|
40 | + * @var ActionScheduler_Logger |
|
41 | + */ |
|
42 | + protected $logger; |
|
43 | + |
|
44 | + /** |
|
45 | + * A ActionScheduler_QueueRunner runner instance (or child class) |
|
46 | + * |
|
47 | + * @var ActionScheduler_QueueRunner |
|
48 | + */ |
|
49 | + protected $runner; |
|
50 | + |
|
51 | + /** |
|
52 | + * Bulk actions. The key of the array is the method name of the implementation: |
|
53 | + * |
|
54 | + * bulk_<key>(array $ids, string $sql_in). |
|
55 | + * |
|
56 | + * See the comments in the parent class for further details |
|
57 | + * |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $bulk_actions = array(); |
|
61 | + |
|
62 | + /** |
|
63 | + * Flag variable to render our notifications, if any, once. |
|
64 | + * |
|
65 | + * @var bool |
|
66 | + */ |
|
67 | + protected static $did_notification = false; |
|
68 | + |
|
69 | + /** |
|
70 | + * Array of seconds for common time periods, like week or month, alongside an internationalised string representation, i.e. "Day" or "Days" |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + private static $time_periods; |
|
75 | + |
|
76 | + /** |
|
77 | + * Sets the current data store object into `store->action` and initialises the object. |
|
78 | + * |
|
79 | + * @param ActionScheduler_Store $store |
|
80 | + * @param ActionScheduler_Logger $logger |
|
81 | + * @param ActionScheduler_QueueRunner $runner |
|
82 | + */ |
|
83 | + public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
|
84 | + |
|
85 | + $this->store = $store; |
|
86 | + $this->logger = $logger; |
|
87 | + $this->runner = $runner; |
|
88 | + |
|
89 | + $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
|
90 | + |
|
91 | + $this->bulk_actions = array( |
|
92 | + 'delete' => __( 'Delete', 'action-scheduler' ), |
|
93 | + ); |
|
94 | + |
|
95 | + $this->columns = array( |
|
96 | + 'hook' => __( 'Hook', 'action-scheduler' ), |
|
97 | + 'status' => __( 'Status', 'action-scheduler' ), |
|
98 | + 'args' => __( 'Arguments', 'action-scheduler' ), |
|
99 | + 'group' => __( 'Group', 'action-scheduler' ), |
|
100 | + 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
|
101 | + 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
|
102 | + 'log_entries' => __( 'Log', 'action-scheduler' ), |
|
103 | + ); |
|
104 | + |
|
105 | + $this->sort_by = array( |
|
106 | + 'schedule', |
|
107 | + 'hook', |
|
108 | + 'group', |
|
109 | + ); |
|
110 | + |
|
111 | + $this->search_by = array( |
|
112 | + 'hook', |
|
113 | + 'args', |
|
114 | + 'claim_id', |
|
115 | + ); |
|
116 | + |
|
117 | + $request_status = $this->get_request_status(); |
|
118 | + |
|
119 | + if ( empty( $request_status ) ) { |
|
120 | + $this->sort_by[] = 'status'; |
|
121 | + } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { |
|
122 | + $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
|
123 | + $this->sort_by[] = 'claim_id'; |
|
124 | + } |
|
125 | + |
|
126 | + $this->row_actions = array( |
|
127 | + 'hook' => array( |
|
128 | + 'run' => array( |
|
129 | + 'name' => __( 'Run', 'action-scheduler' ), |
|
130 | + 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
|
131 | + ), |
|
132 | + 'cancel' => array( |
|
133 | + 'name' => __( 'Cancel', 'action-scheduler' ), |
|
134 | + 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
|
135 | + 'class' => 'cancel trash', |
|
136 | + ), |
|
137 | + ), |
|
138 | + ); |
|
139 | + |
|
140 | + self::$time_periods = array( |
|
141 | + array( |
|
142 | + 'seconds' => YEAR_IN_SECONDS, |
|
143 | + /* translators: %s: amount of time */ |
|
144 | + 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
|
145 | + ), |
|
146 | + array( |
|
147 | + 'seconds' => MONTH_IN_SECONDS, |
|
148 | + /* translators: %s: amount of time */ |
|
149 | + 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
|
150 | + ), |
|
151 | + array( |
|
152 | + 'seconds' => WEEK_IN_SECONDS, |
|
153 | + /* translators: %s: amount of time */ |
|
154 | + 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
|
155 | + ), |
|
156 | + array( |
|
157 | + 'seconds' => DAY_IN_SECONDS, |
|
158 | + /* translators: %s: amount of time */ |
|
159 | + 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
|
160 | + ), |
|
161 | + array( |
|
162 | + 'seconds' => HOUR_IN_SECONDS, |
|
163 | + /* translators: %s: amount of time */ |
|
164 | + 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
|
165 | + ), |
|
166 | + array( |
|
167 | + 'seconds' => MINUTE_IN_SECONDS, |
|
168 | + /* translators: %s: amount of time */ |
|
169 | + 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
|
170 | + ), |
|
171 | + array( |
|
172 | + 'seconds' => 1, |
|
173 | + /* translators: %s: amount of time */ |
|
174 | + 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
|
175 | + ), |
|
176 | + ); |
|
177 | + |
|
178 | + parent::__construct( array( |
|
179 | + 'singular' => 'action-scheduler', |
|
180 | + 'plural' => 'action-scheduler', |
|
181 | + 'ajax' => false, |
|
182 | + ) ); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Convert an interval of seconds into a two part human friendly string. |
|
187 | + * |
|
188 | + * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
189 | + * even if an action is 1 day and 11 hours away, it will display "1 day". This function goes one step |
|
190 | + * further to display two degrees of accuracy. |
|
191 | + * |
|
192 | + * Inspired by the Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
193 | + * |
|
194 | + * @param int $interval A interval in seconds. |
|
195 | + * @param int $periods_to_include Depth of time periods to include, e.g. for an interval of 70, and $periods_to_include of 2, both minutes and seconds would be included. With a value of 1, only minutes would be included. |
|
196 | + * @return string A human friendly string representation of the interval. |
|
197 | + */ |
|
198 | + private static function human_interval( $interval, $periods_to_include = 2 ) { |
|
199 | + |
|
200 | + if ( $interval <= 0 ) { |
|
201 | + return __( 'Now!', 'action-scheduler' ); |
|
202 | + } |
|
203 | + |
|
204 | + $output = ''; |
|
205 | + |
|
206 | + for ( $time_period_index = 0, $periods_included = 0, $seconds_remaining = $interval; $time_period_index < count( self::$time_periods ) && $seconds_remaining > 0 && $periods_included < $periods_to_include; $time_period_index++ ) { |
|
207 | + |
|
208 | + $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
|
209 | + |
|
210 | + if ( $periods_in_interval > 0 ) { |
|
211 | + if ( ! empty( $output ) ) { |
|
212 | + $output .= ' '; |
|
213 | + } |
|
214 | + $output .= sprintf( _n( self::$time_periods[ $time_period_index ]['names'][0], self::$time_periods[ $time_period_index ]['names'][1], $periods_in_interval, 'action-scheduler' ), $periods_in_interval ); |
|
215 | + $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
|
216 | + $periods_included++; |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + return $output; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. |
|
225 | + * |
|
226 | + * @param ActionScheduler_Action $action |
|
227 | + * |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + protected function get_recurrence( $action ) { |
|
231 | + $schedule = $action->get_schedule(); |
|
232 | + if ( $schedule->is_recurring() ) { |
|
233 | + $recurrence = $schedule->get_recurrence(); |
|
234 | + |
|
235 | + if ( is_numeric( $recurrence ) ) { |
|
236 | + /* translators: %s: time interval */ |
|
237 | + return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
|
238 | + } else { |
|
239 | + return $recurrence; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + return __( 'Non-repeating', 'action-scheduler' ); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Serializes the argument of an action to render it in a human friendly format. |
|
248 | + * |
|
249 | + * @param array $row The array representation of the current row of the table |
|
250 | + * |
|
251 | + * @return string |
|
252 | + */ |
|
253 | + public function column_args( array $row ) { |
|
254 | + if ( empty( $row['args'] ) ) { |
|
255 | + return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
|
256 | + } |
|
257 | + |
|
258 | + $row_html = '<ul>'; |
|
259 | + foreach ( $row['args'] as $key => $value ) { |
|
260 | + $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); |
|
261 | + } |
|
262 | + $row_html .= '</ul>'; |
|
263 | + |
|
264 | + return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
269 | + * |
|
270 | + * @param array $row Action array. |
|
271 | + * @return string |
|
272 | + */ |
|
273 | + public function column_log_entries( array $row ) { |
|
274 | + |
|
275 | + $log_entries_html = '<ol>'; |
|
276 | + |
|
277 | + $timezone = new DateTimezone( 'UTC' ); |
|
278 | + |
|
279 | + foreach ( $row['log_entries'] as $log_entry ) { |
|
280 | + $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
|
281 | + } |
|
282 | + |
|
283 | + $log_entries_html .= '</ol>'; |
|
284 | + |
|
285 | + return $log_entries_html; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
290 | + * |
|
291 | + * @param ActionScheduler_LogEntry $log_entry |
|
292 | + * @param DateTimezone $timezone |
|
293 | + * @return string |
|
294 | + */ |
|
295 | + protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
|
296 | + $date = $log_entry->get_date(); |
|
297 | + $date->setTimezone( $timezone ); |
|
298 | + return sprintf( '<li><strong>%s</strong><br/>%s</li>', esc_html( $date->format( 'Y-m-d H:i:s O' ) ), esc_html( $log_entry->get_message() ) ); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Only display row actions for pending actions. |
|
303 | + * |
|
304 | + * @param array $row Row to render |
|
305 | + * @param string $column_name Current row |
|
306 | + * |
|
307 | + * @return string |
|
308 | + */ |
|
309 | + protected function maybe_render_actions( $row, $column_name ) { |
|
310 | + if ( 'pending' === strtolower( $row['status'] ) ) { |
|
311 | + return parent::maybe_render_actions( $row, $column_name ); |
|
312 | + } |
|
313 | + |
|
314 | + return ''; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Renders admin notifications |
|
319 | + * |
|
320 | + * Notifications: |
|
321 | + * 1. When the maximum number of tasks are being executed simultaneously. |
|
322 | + * 2. Notifications when a task is manually executed. |
|
323 | + * 3. Tables are missing. |
|
324 | + */ |
|
325 | + public function display_admin_notices() { |
|
326 | + global $wpdb; |
|
327 | + |
|
328 | + if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
|
329 | + $table_list = array( |
|
330 | + 'actionscheduler_actions', |
|
331 | + 'actionscheduler_logs', |
|
332 | + 'actionscheduler_groups', |
|
333 | + 'actionscheduler_claims', |
|
334 | + ); |
|
335 | + |
|
336 | + $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
337 | + foreach ( $table_list as $table_name ) { |
|
338 | + if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { |
|
339 | + $this->admin_notices[] = array( |
|
340 | + 'class' => 'error', |
|
341 | + 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).' , 'action-scheduler' ), |
|
342 | + ); |
|
343 | + $this->recreate_tables(); |
|
344 | + parent::display_admin_notices(); |
|
345 | + |
|
346 | + return; |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + if ( $this->runner->has_maximum_concurrent_batches() ) { |
|
352 | + $claim_count = $this->store->get_claim_count(); |
|
353 | + $this->admin_notices[] = array( |
|
354 | + 'class' => 'updated', |
|
355 | + 'message' => sprintf( |
|
356 | + /* translators: %s: amount of claims */ |
|
357 | + _n( |
|
358 | + 'Maximum simultaneous queues already in progress (%s queue). No additional queues will begin processing until the current queues are complete.', |
|
359 | + 'Maximum simultaneous queues already in progress (%s queues). No additional queues will begin processing until the current queues are complete.', |
|
360 | + $claim_count, |
|
361 | + 'action-scheduler' |
|
362 | + ), |
|
363 | + $claim_count |
|
364 | + ), |
|
365 | + ); |
|
366 | + } elseif ( $this->store->has_pending_actions_due() ) { |
|
367 | + |
|
368 | + $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
|
369 | + |
|
370 | + // No lock set or lock expired |
|
371 | + if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
|
372 | + $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
|
373 | + /* translators: %s: process URL */ |
|
374 | + $async_request_message = sprintf( __( 'A new queue has begun processing. <a href="%s">View actions in-progress »</a>', 'action-scheduler' ), esc_url( $in_progress_url ) ); |
|
375 | + } else { |
|
376 | + /* translators: %d: seconds */ |
|
377 | + $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
|
378 | + } |
|
379 | + |
|
380 | + $this->admin_notices[] = array( |
|
381 | + 'class' => 'notice notice-info', |
|
382 | + 'message' => $async_request_message, |
|
383 | + ); |
|
384 | + } |
|
385 | + |
|
386 | + $notification = get_transient( 'action_scheduler_admin_notice' ); |
|
387 | + |
|
388 | + if ( is_array( $notification ) ) { |
|
389 | + delete_transient( 'action_scheduler_admin_notice' ); |
|
390 | + |
|
391 | + $action = $this->store->fetch_action( $notification['action_id'] ); |
|
392 | + $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
|
393 | + if ( 1 == $notification['success'] ) { |
|
394 | + $class = 'updated'; |
|
395 | + switch ( $notification['row_action_type'] ) { |
|
396 | + case 'run' : |
|
397 | + /* translators: %s: action HTML */ |
|
398 | + $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
|
399 | + break; |
|
400 | + case 'cancel' : |
|
401 | + /* translators: %s: action HTML */ |
|
402 | + $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
|
403 | + break; |
|
404 | + default : |
|
405 | + /* translators: %s: action HTML */ |
|
406 | + $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
|
407 | + break; |
|
408 | + } |
|
409 | + } else { |
|
410 | + $class = 'error'; |
|
411 | + /* translators: 1: action HTML 2: action ID 3: error message */ |
|
412 | + $action_message_html = sprintf( __( 'Could not process change for action: "%1$s" (ID: %2$d). Error: %3$s', 'action-scheduler' ), $action_hook_html, esc_html( $notification['action_id'] ), esc_html( $notification['error_message'] ) ); |
|
413 | + } |
|
414 | + |
|
415 | + $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
|
416 | + |
|
417 | + $this->admin_notices[] = array( |
|
418 | + 'class' => $class, |
|
419 | + 'message' => $action_message_html, |
|
420 | + ); |
|
421 | + } |
|
422 | + |
|
423 | + parent::display_admin_notices(); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Prints the scheduled date in a human friendly format. |
|
428 | + * |
|
429 | + * @param array $row The array representation of the current row of the table |
|
430 | + * |
|
431 | + * @return string |
|
432 | + */ |
|
433 | + public function column_schedule( $row ) { |
|
434 | + return $this->get_schedule_display_string( $row['schedule'] ); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Get the scheduled date in a human friendly format. |
|
439 | + * |
|
440 | + * @param ActionScheduler_Schedule $schedule |
|
441 | + * @return string |
|
442 | + */ |
|
443 | + protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
|
444 | + |
|
445 | + $schedule_display_string = ''; |
|
446 | + |
|
447 | + if ( ! $schedule->get_date() ) { |
|
448 | + return '0000-00-00 00:00:00'; |
|
449 | + } |
|
450 | + |
|
451 | + $next_timestamp = $schedule->get_date()->getTimestamp(); |
|
452 | + |
|
453 | + $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
|
454 | + $schedule_display_string .= '<br/>'; |
|
455 | + |
|
456 | + if ( gmdate( 'U' ) > $next_timestamp ) { |
|
457 | + /* translators: %s: date interval */ |
|
458 | + $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
|
459 | + } else { |
|
460 | + /* translators: %s: date interval */ |
|
461 | + $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
|
462 | + } |
|
463 | + |
|
464 | + return $schedule_display_string; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Bulk delete |
|
469 | + * |
|
470 | + * Deletes actions based on their ID. This is the handler for the bulk delete. It assumes the data |
|
471 | + * properly validated by the callee and it will delete the actions without any extra validation. |
|
472 | + * |
|
473 | + * @param array $ids |
|
474 | + * @param string $ids_sql Inherited and unused |
|
475 | + */ |
|
476 | + protected function bulk_delete( array $ids, $ids_sql ) { |
|
477 | + foreach ( $ids as $id ) { |
|
478 | + $this->store->delete_action( $id ); |
|
479 | + } |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
484 | + * parameters are valid. |
|
485 | + * |
|
486 | + * @param int $action_id |
|
487 | + */ |
|
488 | + protected function row_action_cancel( $action_id ) { |
|
489 | + $this->process_row_action( $action_id, 'cancel' ); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
494 | + * parameters are valid. |
|
495 | + * |
|
496 | + * @param int $action_id |
|
497 | + */ |
|
498 | + protected function row_action_run( $action_id ) { |
|
499 | + $this->process_row_action( $action_id, 'run' ); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Force the data store schema updates. |
|
504 | + */ |
|
505 | + protected function recreate_tables() { |
|
506 | + if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
|
507 | + $store = $this->store; |
|
508 | + } else { |
|
509 | + $store = new ActionScheduler_HybridStore(); |
|
510 | + } |
|
511 | + add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
|
512 | + |
|
513 | + $store_schema = new ActionScheduler_StoreSchema(); |
|
514 | + $logger_schema = new ActionScheduler_LoggerSchema(); |
|
515 | + $store_schema->register_tables( true ); |
|
516 | + $logger_schema->register_tables( true ); |
|
517 | + |
|
518 | + remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
|
519 | + } |
|
520 | + /** |
|
521 | + * Implements the logic behind processing an action once an action link is clicked on the list table. |
|
522 | + * |
|
523 | + * @param int $action_id |
|
524 | + * @param string $row_action_type The type of action to perform on the action. |
|
525 | + */ |
|
526 | + protected function process_row_action( $action_id, $row_action_type ) { |
|
527 | + try { |
|
528 | + switch ( $row_action_type ) { |
|
529 | + case 'run' : |
|
530 | + $this->runner->process_action( $action_id, 'Admin List Table' ); |
|
531 | + break; |
|
532 | + case 'cancel' : |
|
533 | + $this->store->cancel_action( $action_id ); |
|
534 | + break; |
|
535 | + } |
|
536 | + $success = 1; |
|
537 | + $error_message = ''; |
|
538 | + } catch ( Exception $e ) { |
|
539 | + $success = 0; |
|
540 | + $error_message = $e->getMessage(); |
|
541 | + } |
|
542 | + |
|
543 | + set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * {@inheritDoc} |
|
548 | + */ |
|
549 | + public function prepare_items() { |
|
550 | + $this->prepare_column_headers(); |
|
551 | + |
|
552 | + $per_page = $this->get_items_per_page( $this->package . '_items_per_page', $this->items_per_page ); |
|
553 | + $query = array( |
|
554 | + 'per_page' => $per_page, |
|
555 | + 'offset' => $this->get_items_offset(), |
|
556 | + 'status' => $this->get_request_status(), |
|
557 | + 'orderby' => $this->get_request_orderby(), |
|
558 | + 'order' => $this->get_request_order(), |
|
559 | + 'search' => $this->get_request_search_query(), |
|
560 | + ); |
|
561 | + |
|
562 | + $this->items = array(); |
|
563 | + |
|
564 | + $total_items = $this->store->query_actions( $query, 'count' ); |
|
565 | + |
|
566 | + $status_labels = $this->store->get_status_labels(); |
|
567 | + |
|
568 | + foreach ( $this->store->query_actions( $query ) as $action_id ) { |
|
569 | + try { |
|
570 | + $action = $this->store->fetch_action( $action_id ); |
|
571 | + } catch ( Exception $e ) { |
|
572 | + continue; |
|
573 | + } |
|
574 | + if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
|
575 | + continue; |
|
576 | + } |
|
577 | + $this->items[ $action_id ] = array( |
|
578 | + 'ID' => $action_id, |
|
579 | + 'hook' => $action->get_hook(), |
|
580 | + 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
|
581 | + 'args' => $action->get_args(), |
|
582 | + 'group' => $action->get_group(), |
|
583 | + 'log_entries' => $this->logger->get_logs( $action_id ), |
|
584 | + 'claim_id' => $this->store->get_claim_id( $action_id ), |
|
585 | + 'recurrence' => $this->get_recurrence( $action ), |
|
586 | + 'schedule' => $action->get_schedule(), |
|
587 | + ); |
|
588 | + } |
|
589 | + |
|
590 | + $this->set_pagination_args( array( |
|
591 | + 'total_items' => $total_items, |
|
592 | + 'per_page' => $per_page, |
|
593 | + 'total_pages' => ceil( $total_items / $per_page ), |
|
594 | + ) ); |
|
595 | + } |
|
596 | + |
|
597 | + /** |
|
598 | + * Prints the available statuses so the user can click to filter. |
|
599 | + */ |
|
600 | + protected function display_filter_by_status() { |
|
601 | + $this->status_counts = $this->store->action_counts(); |
|
602 | + parent::display_filter_by_status(); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Get the text to display in the search box on the list table. |
|
607 | + */ |
|
608 | + protected function get_search_box_button_text() { |
|
609 | + return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
|
610 | + } |
|
611 | 611 | } |
@@ -17,103 +17,103 @@ |
||
17 | 17 | */ |
18 | 18 | class ProgressBar { |
19 | 19 | |
20 | - /** @var integer */ |
|
21 | - protected $total_ticks; |
|
22 | - |
|
23 | - /** @var integer */ |
|
24 | - protected $count; |
|
25 | - |
|
26 | - /** @var integer */ |
|
27 | - protected $interval; |
|
28 | - |
|
29 | - /** @var string */ |
|
30 | - protected $message; |
|
31 | - |
|
32 | - /** @var \cli\progress\Bar */ |
|
33 | - protected $progress_bar; |
|
34 | - |
|
35 | - /** |
|
36 | - * ProgressBar constructor. |
|
37 | - * |
|
38 | - * @param string $message Text to display before the progress bar. |
|
39 | - * @param integer $count Total number of ticks to be performed. |
|
40 | - * @param integer $interval Optional. The interval in milliseconds between updates. Default 100. |
|
41 | - * |
|
42 | - * @throws Exception When this is not run within WP CLI |
|
43 | - */ |
|
44 | - public function __construct( $message, $count, $interval = 100 ) { |
|
45 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
46 | - /* translators: %s php class name */ |
|
47 | - throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
48 | - } |
|
49 | - |
|
50 | - $this->total_ticks = 0; |
|
51 | - $this->message = $message; |
|
52 | - $this->count = $count; |
|
53 | - $this->interval = $interval; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Increment the progress bar ticks. |
|
58 | - */ |
|
59 | - public function tick() { |
|
60 | - if ( null === $this->progress_bar ) { |
|
61 | - $this->setup_progress_bar(); |
|
62 | - } |
|
63 | - |
|
64 | - $this->progress_bar->tick(); |
|
65 | - $this->total_ticks++; |
|
66 | - |
|
67 | - do_action( 'action_scheduler/progress_tick', $this->total_ticks ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get the progress bar tick count. |
|
72 | - * |
|
73 | - * @return int |
|
74 | - */ |
|
75 | - public function current() { |
|
76 | - return $this->progress_bar ? $this->progress_bar->current() : 0; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Finish the current progress bar. |
|
81 | - */ |
|
82 | - public function finish() { |
|
83 | - if ( null !== $this->progress_bar ) { |
|
84 | - $this->progress_bar->finish(); |
|
85 | - } |
|
86 | - |
|
87 | - $this->progress_bar = null; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Set the message used when creating the progress bar. |
|
92 | - * |
|
93 | - * @param string $message The message to be used when the next progress bar is created. |
|
94 | - */ |
|
95 | - public function set_message( $message ) { |
|
96 | - $this->message = $message; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Set the count for a new progress bar. |
|
101 | - * |
|
102 | - * @param integer $count The total number of ticks expected to complete. |
|
103 | - */ |
|
104 | - public function set_count( $count ) { |
|
105 | - $this->count = $count; |
|
106 | - $this->finish(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Set up the progress bar. |
|
111 | - */ |
|
112 | - protected function setup_progress_bar() { |
|
113 | - $this->progress_bar = \WP_CLI\Utils\make_progress_bar( |
|
114 | - $this->message, |
|
115 | - $this->count, |
|
116 | - $this->interval |
|
117 | - ); |
|
118 | - } |
|
20 | + /** @var integer */ |
|
21 | + protected $total_ticks; |
|
22 | + |
|
23 | + /** @var integer */ |
|
24 | + protected $count; |
|
25 | + |
|
26 | + /** @var integer */ |
|
27 | + protected $interval; |
|
28 | + |
|
29 | + /** @var string */ |
|
30 | + protected $message; |
|
31 | + |
|
32 | + /** @var \cli\progress\Bar */ |
|
33 | + protected $progress_bar; |
|
34 | + |
|
35 | + /** |
|
36 | + * ProgressBar constructor. |
|
37 | + * |
|
38 | + * @param string $message Text to display before the progress bar. |
|
39 | + * @param integer $count Total number of ticks to be performed. |
|
40 | + * @param integer $interval Optional. The interval in milliseconds between updates. Default 100. |
|
41 | + * |
|
42 | + * @throws Exception When this is not run within WP CLI |
|
43 | + */ |
|
44 | + public function __construct( $message, $count, $interval = 100 ) { |
|
45 | + if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
46 | + /* translators: %s php class name */ |
|
47 | + throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
48 | + } |
|
49 | + |
|
50 | + $this->total_ticks = 0; |
|
51 | + $this->message = $message; |
|
52 | + $this->count = $count; |
|
53 | + $this->interval = $interval; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Increment the progress bar ticks. |
|
58 | + */ |
|
59 | + public function tick() { |
|
60 | + if ( null === $this->progress_bar ) { |
|
61 | + $this->setup_progress_bar(); |
|
62 | + } |
|
63 | + |
|
64 | + $this->progress_bar->tick(); |
|
65 | + $this->total_ticks++; |
|
66 | + |
|
67 | + do_action( 'action_scheduler/progress_tick', $this->total_ticks ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get the progress bar tick count. |
|
72 | + * |
|
73 | + * @return int |
|
74 | + */ |
|
75 | + public function current() { |
|
76 | + return $this->progress_bar ? $this->progress_bar->current() : 0; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Finish the current progress bar. |
|
81 | + */ |
|
82 | + public function finish() { |
|
83 | + if ( null !== $this->progress_bar ) { |
|
84 | + $this->progress_bar->finish(); |
|
85 | + } |
|
86 | + |
|
87 | + $this->progress_bar = null; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Set the message used when creating the progress bar. |
|
92 | + * |
|
93 | + * @param string $message The message to be used when the next progress bar is created. |
|
94 | + */ |
|
95 | + public function set_message( $message ) { |
|
96 | + $this->message = $message; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Set the count for a new progress bar. |
|
101 | + * |
|
102 | + * @param integer $count The total number of ticks expected to complete. |
|
103 | + */ |
|
104 | + public function set_count( $count ) { |
|
105 | + $this->count = $count; |
|
106 | + $this->finish(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Set up the progress bar. |
|
111 | + */ |
|
112 | + protected function setup_progress_bar() { |
|
113 | + $this->progress_bar = \WP_CLI\Utils\make_progress_bar( |
|
114 | + $this->message, |
|
115 | + $this->count, |
|
116 | + $this->interval |
|
117 | + ); |
|
118 | + } |
|
119 | 119 | } |
@@ -9,189 +9,189 @@ |
||
9 | 9 | */ |
10 | 10 | class ActionScheduler_WPCLI_QueueRunner extends ActionScheduler_Abstract_QueueRunner { |
11 | 11 | |
12 | - /** @var array */ |
|
13 | - protected $actions; |
|
14 | - |
|
15 | - /** @var ActionScheduler_ActionClaim */ |
|
16 | - protected $claim; |
|
17 | - |
|
18 | - /** @var \cli\progress\Bar */ |
|
19 | - protected $progress_bar; |
|
20 | - |
|
21 | - /** |
|
22 | - * ActionScheduler_WPCLI_QueueRunner constructor. |
|
23 | - * |
|
24 | - * @param ActionScheduler_Store $store |
|
25 | - * @param ActionScheduler_FatalErrorMonitor $monitor |
|
26 | - * @param ActionScheduler_QueueCleaner $cleaner |
|
27 | - * |
|
28 | - * @throws Exception When this is not run within WP CLI |
|
29 | - */ |
|
30 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
31 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
32 | - /* translators: %s php class name */ |
|
33 | - throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
34 | - } |
|
35 | - |
|
36 | - parent::__construct( $store, $monitor, $cleaner ); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Set up the Queue before processing. |
|
41 | - * |
|
42 | - * @author Jeremy Pry |
|
43 | - * |
|
44 | - * @param int $batch_size The batch size to process. |
|
45 | - * @param array $hooks The hooks being used to filter the actions claimed in this batch. |
|
46 | - * @param string $group The group of actions to claim with this batch. |
|
47 | - * @param bool $force Whether to force running even with too many concurrent processes. |
|
48 | - * |
|
49 | - * @return int The number of actions that will be run. |
|
50 | - * @throws \WP_CLI\ExitException When there are too many concurrent batches. |
|
51 | - */ |
|
52 | - public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) { |
|
53 | - $this->run_cleanup(); |
|
54 | - $this->add_hooks(); |
|
55 | - |
|
56 | - // Check to make sure there aren't too many concurrent processes running. |
|
57 | - if ( $this->has_maximum_concurrent_batches() ) { |
|
58 | - if ( $force ) { |
|
59 | - WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'action-scheduler' ) ); |
|
60 | - } else { |
|
61 | - WP_CLI::error( __( 'There are too many concurrent batches.', 'action-scheduler' ) ); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - // Stake a claim and store it. |
|
66 | - $this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group ); |
|
67 | - $this->monitor->attach( $this->claim ); |
|
68 | - $this->actions = $this->claim->get_actions(); |
|
69 | - |
|
70 | - return count( $this->actions ); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Add our hooks to the appropriate actions. |
|
75 | - * |
|
76 | - * @author Jeremy Pry |
|
77 | - */ |
|
78 | - protected function add_hooks() { |
|
79 | - add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) ); |
|
80 | - add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 ); |
|
81 | - add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 ); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Set up the WP CLI progress bar. |
|
86 | - * |
|
87 | - * @author Jeremy Pry |
|
88 | - */ |
|
89 | - protected function setup_progress_bar() { |
|
90 | - $count = count( $this->actions ); |
|
91 | - $this->progress_bar = new ProgressBar( |
|
92 | - /* translators: %d: amount of actions */ |
|
93 | - sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ), |
|
94 | - $count |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Process actions in the queue. |
|
100 | - * |
|
101 | - * @author Jeremy Pry |
|
102 | - * |
|
103 | - * @param string $context Optional runner context. Default 'WP CLI'. |
|
104 | - * |
|
105 | - * @return int The number of actions processed. |
|
106 | - */ |
|
107 | - public function run( $context = 'WP CLI' ) { |
|
108 | - do_action( 'action_scheduler_before_process_queue' ); |
|
109 | - $this->setup_progress_bar(); |
|
110 | - foreach ( $this->actions as $action_id ) { |
|
111 | - // Error if we lost the claim. |
|
112 | - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) { |
|
113 | - WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'action-scheduler' ) ); |
|
114 | - break; |
|
115 | - } |
|
116 | - |
|
117 | - $this->process_action( $action_id, $context ); |
|
118 | - $this->progress_bar->tick(); |
|
119 | - } |
|
120 | - |
|
121 | - $completed = $this->progress_bar->current(); |
|
122 | - $this->progress_bar->finish(); |
|
123 | - $this->store->release_claim( $this->claim ); |
|
124 | - do_action( 'action_scheduler_after_process_queue' ); |
|
125 | - |
|
126 | - return $completed; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Handle WP CLI message when the action is starting. |
|
131 | - * |
|
132 | - * @author Jeremy Pry |
|
133 | - * |
|
134 | - * @param $action_id |
|
135 | - */ |
|
136 | - public function before_execute( $action_id ) { |
|
137 | - /* translators: %s refers to the action ID */ |
|
138 | - WP_CLI::log( sprintf( __( 'Started processing action %s', 'action-scheduler' ), $action_id ) ); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Handle WP CLI message when the action has completed. |
|
143 | - * |
|
144 | - * @author Jeremy Pry |
|
145 | - * |
|
146 | - * @param int $action_id |
|
147 | - * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility. |
|
148 | - */ |
|
149 | - public function after_execute( $action_id, $action = null ) { |
|
150 | - // backward compatibility |
|
151 | - if ( null === $action ) { |
|
152 | - $action = $this->store->fetch_action( $action_id ); |
|
153 | - } |
|
154 | - /* translators: 1: action ID 2: hook name */ |
|
155 | - WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'action-scheduler' ), $action_id, $action->get_hook() ) ); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Handle WP CLI message when the action has failed. |
|
160 | - * |
|
161 | - * @author Jeremy Pry |
|
162 | - * |
|
163 | - * @param int $action_id |
|
164 | - * @param Exception $exception |
|
165 | - * @throws \WP_CLI\ExitException With failure message. |
|
166 | - */ |
|
167 | - public function action_failed( $action_id, $exception ) { |
|
168 | - WP_CLI::error( |
|
169 | - /* translators: 1: action ID 2: exception message */ |
|
170 | - sprintf( __( 'Error processing action %1$s: %2$s', 'action-scheduler' ), $action_id, $exception->getMessage() ), |
|
171 | - false |
|
172 | - ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Sleep and help avoid hitting memory limit |
|
177 | - * |
|
178 | - * @param int $sleep_time Amount of seconds to sleep |
|
179 | - * @deprecated 3.0.0 |
|
180 | - */ |
|
181 | - protected function stop_the_insanity( $sleep_time = 0 ) { |
|
182 | - _deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' ); |
|
183 | - |
|
184 | - ActionScheduler_DataController::free_memory(); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Maybe trigger the stop_the_insanity() method to free up memory. |
|
189 | - */ |
|
190 | - protected function maybe_stop_the_insanity() { |
|
191 | - // The value returned by progress_bar->current() might be padded. Remove padding, and convert to int. |
|
192 | - $current_iteration = intval( trim( $this->progress_bar->current() ) ); |
|
193 | - if ( 0 === $current_iteration % 50 ) { |
|
194 | - $this->stop_the_insanity(); |
|
195 | - } |
|
196 | - } |
|
12 | + /** @var array */ |
|
13 | + protected $actions; |
|
14 | + |
|
15 | + /** @var ActionScheduler_ActionClaim */ |
|
16 | + protected $claim; |
|
17 | + |
|
18 | + /** @var \cli\progress\Bar */ |
|
19 | + protected $progress_bar; |
|
20 | + |
|
21 | + /** |
|
22 | + * ActionScheduler_WPCLI_QueueRunner constructor. |
|
23 | + * |
|
24 | + * @param ActionScheduler_Store $store |
|
25 | + * @param ActionScheduler_FatalErrorMonitor $monitor |
|
26 | + * @param ActionScheduler_QueueCleaner $cleaner |
|
27 | + * |
|
28 | + * @throws Exception When this is not run within WP CLI |
|
29 | + */ |
|
30 | + public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
31 | + if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
32 | + /* translators: %s php class name */ |
|
33 | + throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
34 | + } |
|
35 | + |
|
36 | + parent::__construct( $store, $monitor, $cleaner ); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Set up the Queue before processing. |
|
41 | + * |
|
42 | + * @author Jeremy Pry |
|
43 | + * |
|
44 | + * @param int $batch_size The batch size to process. |
|
45 | + * @param array $hooks The hooks being used to filter the actions claimed in this batch. |
|
46 | + * @param string $group The group of actions to claim with this batch. |
|
47 | + * @param bool $force Whether to force running even with too many concurrent processes. |
|
48 | + * |
|
49 | + * @return int The number of actions that will be run. |
|
50 | + * @throws \WP_CLI\ExitException When there are too many concurrent batches. |
|
51 | + */ |
|
52 | + public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) { |
|
53 | + $this->run_cleanup(); |
|
54 | + $this->add_hooks(); |
|
55 | + |
|
56 | + // Check to make sure there aren't too many concurrent processes running. |
|
57 | + if ( $this->has_maximum_concurrent_batches() ) { |
|
58 | + if ( $force ) { |
|
59 | + WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'action-scheduler' ) ); |
|
60 | + } else { |
|
61 | + WP_CLI::error( __( 'There are too many concurrent batches.', 'action-scheduler' ) ); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + // Stake a claim and store it. |
|
66 | + $this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group ); |
|
67 | + $this->monitor->attach( $this->claim ); |
|
68 | + $this->actions = $this->claim->get_actions(); |
|
69 | + |
|
70 | + return count( $this->actions ); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Add our hooks to the appropriate actions. |
|
75 | + * |
|
76 | + * @author Jeremy Pry |
|
77 | + */ |
|
78 | + protected function add_hooks() { |
|
79 | + add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) ); |
|
80 | + add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 ); |
|
81 | + add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 ); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Set up the WP CLI progress bar. |
|
86 | + * |
|
87 | + * @author Jeremy Pry |
|
88 | + */ |
|
89 | + protected function setup_progress_bar() { |
|
90 | + $count = count( $this->actions ); |
|
91 | + $this->progress_bar = new ProgressBar( |
|
92 | + /* translators: %d: amount of actions */ |
|
93 | + sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ), |
|
94 | + $count |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Process actions in the queue. |
|
100 | + * |
|
101 | + * @author Jeremy Pry |
|
102 | + * |
|
103 | + * @param string $context Optional runner context. Default 'WP CLI'. |
|
104 | + * |
|
105 | + * @return int The number of actions processed. |
|
106 | + */ |
|
107 | + public function run( $context = 'WP CLI' ) { |
|
108 | + do_action( 'action_scheduler_before_process_queue' ); |
|
109 | + $this->setup_progress_bar(); |
|
110 | + foreach ( $this->actions as $action_id ) { |
|
111 | + // Error if we lost the claim. |
|
112 | + if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) { |
|
113 | + WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'action-scheduler' ) ); |
|
114 | + break; |
|
115 | + } |
|
116 | + |
|
117 | + $this->process_action( $action_id, $context ); |
|
118 | + $this->progress_bar->tick(); |
|
119 | + } |
|
120 | + |
|
121 | + $completed = $this->progress_bar->current(); |
|
122 | + $this->progress_bar->finish(); |
|
123 | + $this->store->release_claim( $this->claim ); |
|
124 | + do_action( 'action_scheduler_after_process_queue' ); |
|
125 | + |
|
126 | + return $completed; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Handle WP CLI message when the action is starting. |
|
131 | + * |
|
132 | + * @author Jeremy Pry |
|
133 | + * |
|
134 | + * @param $action_id |
|
135 | + */ |
|
136 | + public function before_execute( $action_id ) { |
|
137 | + /* translators: %s refers to the action ID */ |
|
138 | + WP_CLI::log( sprintf( __( 'Started processing action %s', 'action-scheduler' ), $action_id ) ); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Handle WP CLI message when the action has completed. |
|
143 | + * |
|
144 | + * @author Jeremy Pry |
|
145 | + * |
|
146 | + * @param int $action_id |
|
147 | + * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility. |
|
148 | + */ |
|
149 | + public function after_execute( $action_id, $action = null ) { |
|
150 | + // backward compatibility |
|
151 | + if ( null === $action ) { |
|
152 | + $action = $this->store->fetch_action( $action_id ); |
|
153 | + } |
|
154 | + /* translators: 1: action ID 2: hook name */ |
|
155 | + WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'action-scheduler' ), $action_id, $action->get_hook() ) ); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Handle WP CLI message when the action has failed. |
|
160 | + * |
|
161 | + * @author Jeremy Pry |
|
162 | + * |
|
163 | + * @param int $action_id |
|
164 | + * @param Exception $exception |
|
165 | + * @throws \WP_CLI\ExitException With failure message. |
|
166 | + */ |
|
167 | + public function action_failed( $action_id, $exception ) { |
|
168 | + WP_CLI::error( |
|
169 | + /* translators: 1: action ID 2: exception message */ |
|
170 | + sprintf( __( 'Error processing action %1$s: %2$s', 'action-scheduler' ), $action_id, $exception->getMessage() ), |
|
171 | + false |
|
172 | + ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Sleep and help avoid hitting memory limit |
|
177 | + * |
|
178 | + * @param int $sleep_time Amount of seconds to sleep |
|
179 | + * @deprecated 3.0.0 |
|
180 | + */ |
|
181 | + protected function stop_the_insanity( $sleep_time = 0 ) { |
|
182 | + _deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' ); |
|
183 | + |
|
184 | + ActionScheduler_DataController::free_memory(); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Maybe trigger the stop_the_insanity() method to free up memory. |
|
189 | + */ |
|
190 | + protected function maybe_stop_the_insanity() { |
|
191 | + // The value returned by progress_bar->current() might be padded. Remove padding, and convert to int. |
|
192 | + $current_iteration = intval( trim( $this->progress_bar->current() ) ); |
|
193 | + if ( 0 === $current_iteration % 50 ) { |
|
194 | + $this->stop_the_insanity(); |
|
195 | + } |
|
196 | + } |
|
197 | 197 | } |
@@ -21,128 +21,128 @@ |
||
21 | 21 | */ |
22 | 22 | class Migration_Command extends WP_CLI_Command { |
23 | 23 | |
24 | - /** @var int */ |
|
25 | - private $total_processed = 0; |
|
26 | - |
|
27 | - /** |
|
28 | - * Register the command with WP-CLI |
|
29 | - */ |
|
30 | - public function register() { |
|
31 | - if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
|
32 | - return; |
|
33 | - } |
|
34 | - |
|
35 | - WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [ |
|
36 | - 'shortdesc' => 'Migrates actions to the DB tables store', |
|
37 | - 'synopsis' => [ |
|
38 | - [ |
|
39 | - 'type' => 'assoc', |
|
40 | - 'name' => 'batch-size', |
|
41 | - 'optional' => true, |
|
42 | - 'default' => 100, |
|
43 | - 'description' => 'The number of actions to process in each batch', |
|
44 | - ], |
|
45 | - [ |
|
46 | - 'type' => 'assoc', |
|
47 | - 'name' => 'free-memory-on', |
|
48 | - 'optional' => true, |
|
49 | - 'default' => 50, |
|
50 | - 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', |
|
51 | - ], |
|
52 | - [ |
|
53 | - 'type' => 'assoc', |
|
54 | - 'name' => 'pause', |
|
55 | - 'optional' => true, |
|
56 | - 'default' => 0, |
|
57 | - 'description' => 'The number of seconds to pause when freeing memory', |
|
58 | - ], |
|
59 | - [ |
|
60 | - 'type' => 'flag', |
|
61 | - 'name' => 'dry-run', |
|
62 | - 'optional' => true, |
|
63 | - 'description' => 'Reports on the actions that would have been migrated, but does not change any data', |
|
64 | - ], |
|
65 | - ], |
|
66 | - ] ); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Process the data migration. |
|
71 | - * |
|
72 | - * @param array $positional_args Required for WP CLI. Not used in migration. |
|
73 | - * @param array $assoc_args Optional arguments. |
|
74 | - * |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - public function migrate( $positional_args, $assoc_args ) { |
|
78 | - $this->init_logging(); |
|
79 | - |
|
80 | - $config = $this->get_migration_config( $assoc_args ); |
|
81 | - $runner = new Runner( $config ); |
|
82 | - $runner->init_destination(); |
|
83 | - |
|
84 | - $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100; |
|
85 | - $free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50; |
|
86 | - $sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0; |
|
87 | - \ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
88 | - \ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
89 | - |
|
90 | - do { |
|
91 | - $actions_processed = $runner->run( $batch_size ); |
|
92 | - $this->total_processed += $actions_processed; |
|
93 | - } while ( $actions_processed > 0 ); |
|
94 | - |
|
95 | - if ( ! $config->get_dry_run() ) { |
|
96 | - // let the scheduler know that there's nothing left to do |
|
97 | - $scheduler = new Scheduler(); |
|
98 | - $scheduler->mark_complete(); |
|
99 | - } |
|
100 | - |
|
101 | - WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Build the config object used to create the Runner |
|
106 | - * |
|
107 | - * @param array $args Optional arguments. |
|
108 | - * |
|
109 | - * @return ActionScheduler\Migration\Config |
|
110 | - */ |
|
111 | - private function get_migration_config( $args ) { |
|
112 | - $args = wp_parse_args( $args, [ |
|
113 | - 'dry-run' => false, |
|
114 | - ] ); |
|
115 | - |
|
116 | - $config = Controller::instance()->get_migration_config_object(); |
|
117 | - $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) ); |
|
118 | - |
|
119 | - return $config; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Hook command line logging into migration actions. |
|
124 | - */ |
|
125 | - private function init_logging() { |
|
126 | - add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) { |
|
127 | - WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
|
128 | - }, 10, 1 ); |
|
129 | - add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) { |
|
130 | - WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
|
131 | - }, 10, 1 ); |
|
132 | - add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) { |
|
133 | - WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
|
134 | - }, 10, 1 ); |
|
135 | - add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) { |
|
136 | - WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
|
137 | - }, 10, 2 ); |
|
138 | - add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) { |
|
139 | - WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
|
140 | - }, 10, 2 ); |
|
141 | - add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) { |
|
142 | - WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); |
|
143 | - }, 10, 1 ); |
|
144 | - add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) { |
|
145 | - WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
|
146 | - }, 10, 1 ); |
|
147 | - } |
|
24 | + /** @var int */ |
|
25 | + private $total_processed = 0; |
|
26 | + |
|
27 | + /** |
|
28 | + * Register the command with WP-CLI |
|
29 | + */ |
|
30 | + public function register() { |
|
31 | + if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
|
32 | + return; |
|
33 | + } |
|
34 | + |
|
35 | + WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [ |
|
36 | + 'shortdesc' => 'Migrates actions to the DB tables store', |
|
37 | + 'synopsis' => [ |
|
38 | + [ |
|
39 | + 'type' => 'assoc', |
|
40 | + 'name' => 'batch-size', |
|
41 | + 'optional' => true, |
|
42 | + 'default' => 100, |
|
43 | + 'description' => 'The number of actions to process in each batch', |
|
44 | + ], |
|
45 | + [ |
|
46 | + 'type' => 'assoc', |
|
47 | + 'name' => 'free-memory-on', |
|
48 | + 'optional' => true, |
|
49 | + 'default' => 50, |
|
50 | + 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', |
|
51 | + ], |
|
52 | + [ |
|
53 | + 'type' => 'assoc', |
|
54 | + 'name' => 'pause', |
|
55 | + 'optional' => true, |
|
56 | + 'default' => 0, |
|
57 | + 'description' => 'The number of seconds to pause when freeing memory', |
|
58 | + ], |
|
59 | + [ |
|
60 | + 'type' => 'flag', |
|
61 | + 'name' => 'dry-run', |
|
62 | + 'optional' => true, |
|
63 | + 'description' => 'Reports on the actions that would have been migrated, but does not change any data', |
|
64 | + ], |
|
65 | + ], |
|
66 | + ] ); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Process the data migration. |
|
71 | + * |
|
72 | + * @param array $positional_args Required for WP CLI. Not used in migration. |
|
73 | + * @param array $assoc_args Optional arguments. |
|
74 | + * |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + public function migrate( $positional_args, $assoc_args ) { |
|
78 | + $this->init_logging(); |
|
79 | + |
|
80 | + $config = $this->get_migration_config( $assoc_args ); |
|
81 | + $runner = new Runner( $config ); |
|
82 | + $runner->init_destination(); |
|
83 | + |
|
84 | + $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100; |
|
85 | + $free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50; |
|
86 | + $sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0; |
|
87 | + \ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
88 | + \ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
89 | + |
|
90 | + do { |
|
91 | + $actions_processed = $runner->run( $batch_size ); |
|
92 | + $this->total_processed += $actions_processed; |
|
93 | + } while ( $actions_processed > 0 ); |
|
94 | + |
|
95 | + if ( ! $config->get_dry_run() ) { |
|
96 | + // let the scheduler know that there's nothing left to do |
|
97 | + $scheduler = new Scheduler(); |
|
98 | + $scheduler->mark_complete(); |
|
99 | + } |
|
100 | + |
|
101 | + WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Build the config object used to create the Runner |
|
106 | + * |
|
107 | + * @param array $args Optional arguments. |
|
108 | + * |
|
109 | + * @return ActionScheduler\Migration\Config |
|
110 | + */ |
|
111 | + private function get_migration_config( $args ) { |
|
112 | + $args = wp_parse_args( $args, [ |
|
113 | + 'dry-run' => false, |
|
114 | + ] ); |
|
115 | + |
|
116 | + $config = Controller::instance()->get_migration_config_object(); |
|
117 | + $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) ); |
|
118 | + |
|
119 | + return $config; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Hook command line logging into migration actions. |
|
124 | + */ |
|
125 | + private function init_logging() { |
|
126 | + add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) { |
|
127 | + WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
|
128 | + }, 10, 1 ); |
|
129 | + add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) { |
|
130 | + WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
|
131 | + }, 10, 1 ); |
|
132 | + add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) { |
|
133 | + WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
|
134 | + }, 10, 1 ); |
|
135 | + add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) { |
|
136 | + WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
|
137 | + }, 10, 2 ); |
|
138 | + add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) { |
|
139 | + WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
|
140 | + }, 10, 2 ); |
|
141 | + add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) { |
|
142 | + WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); |
|
143 | + }, 10, 1 ); |
|
144 | + add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) { |
|
145 | + WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
|
146 | + }, 10, 1 ); |
|
147 | + } |
|
148 | 148 | } |
@@ -5,154 +5,154 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command { |
7 | 7 | |
8 | - /** |
|
9 | - * Run the Action Scheduler |
|
10 | - * |
|
11 | - * ## OPTIONS |
|
12 | - * |
|
13 | - * [--batch-size=<size>] |
|
14 | - * : The maximum number of actions to run. Defaults to 100. |
|
15 | - * |
|
16 | - * [--batches=<size>] |
|
17 | - * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete. |
|
18 | - * |
|
19 | - * [--cleanup-batch-size=<size>] |
|
20 | - * : The maximum number of actions to clean up. Defaults to the value of --batch-size. |
|
21 | - * |
|
22 | - * [--hooks=<hooks>] |
|
23 | - * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three` |
|
24 | - * |
|
25 | - * [--group=<group>] |
|
26 | - * : Only run actions from the specified group. Omitting this option runs actions from all groups. |
|
27 | - * |
|
28 | - * [--free-memory-on=<count>] |
|
29 | - * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50. |
|
30 | - * |
|
31 | - * [--pause=<seconds>] |
|
32 | - * : The number of seconds to pause when freeing memory. Default no pause. |
|
33 | - * |
|
34 | - * [--force] |
|
35 | - * : Whether to force execution despite the maximum number of concurrent processes being exceeded. |
|
36 | - * |
|
37 | - * @param array $args Positional arguments. |
|
38 | - * @param array $assoc_args Keyed arguments. |
|
39 | - * @throws \WP_CLI\ExitException When an error occurs. |
|
40 | - * |
|
41 | - * @subcommand run |
|
42 | - */ |
|
43 | - public function run( $args, $assoc_args ) { |
|
44 | - // Handle passed arguments. |
|
45 | - $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) ); |
|
46 | - $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) ); |
|
47 | - $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) ); |
|
48 | - $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) ); |
|
49 | - $hooks = array_filter( array_map( 'trim', $hooks ) ); |
|
50 | - $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' ); |
|
51 | - $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', '' ); |
|
52 | - $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', '' ); |
|
53 | - $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); |
|
8 | + /** |
|
9 | + * Run the Action Scheduler |
|
10 | + * |
|
11 | + * ## OPTIONS |
|
12 | + * |
|
13 | + * [--batch-size=<size>] |
|
14 | + * : The maximum number of actions to run. Defaults to 100. |
|
15 | + * |
|
16 | + * [--batches=<size>] |
|
17 | + * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete. |
|
18 | + * |
|
19 | + * [--cleanup-batch-size=<size>] |
|
20 | + * : The maximum number of actions to clean up. Defaults to the value of --batch-size. |
|
21 | + * |
|
22 | + * [--hooks=<hooks>] |
|
23 | + * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three` |
|
24 | + * |
|
25 | + * [--group=<group>] |
|
26 | + * : Only run actions from the specified group. Omitting this option runs actions from all groups. |
|
27 | + * |
|
28 | + * [--free-memory-on=<count>] |
|
29 | + * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50. |
|
30 | + * |
|
31 | + * [--pause=<seconds>] |
|
32 | + * : The number of seconds to pause when freeing memory. Default no pause. |
|
33 | + * |
|
34 | + * [--force] |
|
35 | + * : Whether to force execution despite the maximum number of concurrent processes being exceeded. |
|
36 | + * |
|
37 | + * @param array $args Positional arguments. |
|
38 | + * @param array $assoc_args Keyed arguments. |
|
39 | + * @throws \WP_CLI\ExitException When an error occurs. |
|
40 | + * |
|
41 | + * @subcommand run |
|
42 | + */ |
|
43 | + public function run( $args, $assoc_args ) { |
|
44 | + // Handle passed arguments. |
|
45 | + $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) ); |
|
46 | + $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) ); |
|
47 | + $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) ); |
|
48 | + $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) ); |
|
49 | + $hooks = array_filter( array_map( 'trim', $hooks ) ); |
|
50 | + $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' ); |
|
51 | + $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', '' ); |
|
52 | + $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', '' ); |
|
53 | + $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); |
|
54 | 54 | |
55 | - ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
56 | - ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
55 | + ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
56 | + ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
57 | 57 | |
58 | - $batches_completed = 0; |
|
59 | - $actions_completed = 0; |
|
60 | - $unlimited = $batches === 0; |
|
58 | + $batches_completed = 0; |
|
59 | + $actions_completed = 0; |
|
60 | + $unlimited = $batches === 0; |
|
61 | 61 | |
62 | - try { |
|
63 | - // Custom queue cleaner instance. |
|
64 | - $cleaner = new ActionScheduler_QueueCleaner( null, $clean ); |
|
62 | + try { |
|
63 | + // Custom queue cleaner instance. |
|
64 | + $cleaner = new ActionScheduler_QueueCleaner( null, $clean ); |
|
65 | 65 | |
66 | - // Get the queue runner instance |
|
67 | - $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner ); |
|
66 | + // Get the queue runner instance |
|
67 | + $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner ); |
|
68 | 68 | |
69 | - // Determine how many tasks will be run in the first batch. |
|
70 | - $total = $runner->setup( $batch, $hooks, $group, $force ); |
|
69 | + // Determine how many tasks will be run in the first batch. |
|
70 | + $total = $runner->setup( $batch, $hooks, $group, $force ); |
|
71 | 71 | |
72 | - // Run actions for as long as possible. |
|
73 | - while ( $total > 0 ) { |
|
74 | - $this->print_total_actions( $total ); |
|
75 | - $actions_completed += $runner->run(); |
|
76 | - $batches_completed++; |
|
72 | + // Run actions for as long as possible. |
|
73 | + while ( $total > 0 ) { |
|
74 | + $this->print_total_actions( $total ); |
|
75 | + $actions_completed += $runner->run(); |
|
76 | + $batches_completed++; |
|
77 | 77 | |
78 | - // Maybe set up tasks for the next batch. |
|
79 | - $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0; |
|
80 | - } |
|
81 | - } catch ( Exception $e ) { |
|
82 | - $this->print_error( $e ); |
|
83 | - } |
|
78 | + // Maybe set up tasks for the next batch. |
|
79 | + $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0; |
|
80 | + } |
|
81 | + } catch ( Exception $e ) { |
|
82 | + $this->print_error( $e ); |
|
83 | + } |
|
84 | 84 | |
85 | - $this->print_total_batches( $batches_completed ); |
|
86 | - $this->print_success( $actions_completed ); |
|
87 | - } |
|
85 | + $this->print_total_batches( $batches_completed ); |
|
86 | + $this->print_success( $actions_completed ); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Print WP CLI message about how many actions are about to be processed. |
|
91 | - * |
|
92 | - * @author Jeremy Pry |
|
93 | - * |
|
94 | - * @param int $total |
|
95 | - */ |
|
96 | - protected function print_total_actions( $total ) { |
|
97 | - WP_CLI::log( |
|
98 | - sprintf( |
|
99 | - /* translators: %d refers to how many scheduled taks were found to run */ |
|
100 | - _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ), |
|
101 | - number_format_i18n( $total ) |
|
102 | - ) |
|
103 | - ); |
|
104 | - } |
|
89 | + /** |
|
90 | + * Print WP CLI message about how many actions are about to be processed. |
|
91 | + * |
|
92 | + * @author Jeremy Pry |
|
93 | + * |
|
94 | + * @param int $total |
|
95 | + */ |
|
96 | + protected function print_total_actions( $total ) { |
|
97 | + WP_CLI::log( |
|
98 | + sprintf( |
|
99 | + /* translators: %d refers to how many scheduled taks were found to run */ |
|
100 | + _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ), |
|
101 | + number_format_i18n( $total ) |
|
102 | + ) |
|
103 | + ); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Print WP CLI message about how many batches of actions were processed. |
|
108 | - * |
|
109 | - * @author Jeremy Pry |
|
110 | - * |
|
111 | - * @param int $batches_completed |
|
112 | - */ |
|
113 | - protected function print_total_batches( $batches_completed ) { |
|
114 | - WP_CLI::log( |
|
115 | - sprintf( |
|
116 | - /* translators: %d refers to the total number of batches executed */ |
|
117 | - _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ), |
|
118 | - number_format_i18n( $batches_completed ) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
106 | + /** |
|
107 | + * Print WP CLI message about how many batches of actions were processed. |
|
108 | + * |
|
109 | + * @author Jeremy Pry |
|
110 | + * |
|
111 | + * @param int $batches_completed |
|
112 | + */ |
|
113 | + protected function print_total_batches( $batches_completed ) { |
|
114 | + WP_CLI::log( |
|
115 | + sprintf( |
|
116 | + /* translators: %d refers to the total number of batches executed */ |
|
117 | + _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ), |
|
118 | + number_format_i18n( $batches_completed ) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Convert an exception into a WP CLI error. |
|
125 | - * |
|
126 | - * @author Jeremy Pry |
|
127 | - * |
|
128 | - * @param Exception $e The error object. |
|
129 | - * |
|
130 | - * @throws \WP_CLI\ExitException |
|
131 | - */ |
|
132 | - protected function print_error( Exception $e ) { |
|
133 | - WP_CLI::error( |
|
134 | - sprintf( |
|
135 | - /* translators: %s refers to the exception error message */ |
|
136 | - __( 'There was an error running the action scheduler: %s', 'action-scheduler' ), |
|
137 | - $e->getMessage() |
|
138 | - ) |
|
139 | - ); |
|
140 | - } |
|
123 | + /** |
|
124 | + * Convert an exception into a WP CLI error. |
|
125 | + * |
|
126 | + * @author Jeremy Pry |
|
127 | + * |
|
128 | + * @param Exception $e The error object. |
|
129 | + * |
|
130 | + * @throws \WP_CLI\ExitException |
|
131 | + */ |
|
132 | + protected function print_error( Exception $e ) { |
|
133 | + WP_CLI::error( |
|
134 | + sprintf( |
|
135 | + /* translators: %s refers to the exception error message */ |
|
136 | + __( 'There was an error running the action scheduler: %s', 'action-scheduler' ), |
|
137 | + $e->getMessage() |
|
138 | + ) |
|
139 | + ); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Print a success message with the number of completed actions. |
|
144 | - * |
|
145 | - * @author Jeremy Pry |
|
146 | - * |
|
147 | - * @param int $actions_completed |
|
148 | - */ |
|
149 | - protected function print_success( $actions_completed ) { |
|
150 | - WP_CLI::success( |
|
151 | - sprintf( |
|
152 | - /* translators: %d refers to the total number of taskes completed */ |
|
153 | - _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ), |
|
154 | - number_format_i18n( $actions_completed ) |
|
155 | - ) |
|
156 | - ); |
|
157 | - } |
|
142 | + /** |
|
143 | + * Print a success message with the number of completed actions. |
|
144 | + * |
|
145 | + * @author Jeremy Pry |
|
146 | + * |
|
147 | + * @param int $actions_completed |
|
148 | + */ |
|
149 | + protected function print_success( $actions_completed ) { |
|
150 | + WP_CLI::success( |
|
151 | + sprintf( |
|
152 | + /* translators: %d refers to the total number of taskes completed */ |
|
153 | + _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ), |
|
154 | + number_format_i18n( $actions_completed ) |
|
155 | + ) |
|
156 | + ); |
|
157 | + } |
|
158 | 158 | } |
@@ -7,70 +7,70 @@ |
||
7 | 7 | */ |
8 | 8 | class ActionScheduler_DateTime extends DateTime { |
9 | 9 | |
10 | - /** |
|
11 | - * UTC offset. |
|
12 | - * |
|
13 | - * Only used when a timezone is not set. When a timezone string is |
|
14 | - * used, this will be set to 0. |
|
15 | - * |
|
16 | - * @var int |
|
17 | - */ |
|
18 | - protected $utcOffset = 0; |
|
10 | + /** |
|
11 | + * UTC offset. |
|
12 | + * |
|
13 | + * Only used when a timezone is not set. When a timezone string is |
|
14 | + * used, this will be set to 0. |
|
15 | + * |
|
16 | + * @var int |
|
17 | + */ |
|
18 | + protected $utcOffset = 0; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Get the unix timestamp of the current object. |
|
22 | - * |
|
23 | - * Missing in PHP 5.2 so just here so it can be supported consistently. |
|
24 | - * |
|
25 | - * @return int |
|
26 | - */ |
|
27 | - public function getTimestamp() { |
|
28 | - return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' ); |
|
29 | - } |
|
20 | + /** |
|
21 | + * Get the unix timestamp of the current object. |
|
22 | + * |
|
23 | + * Missing in PHP 5.2 so just here so it can be supported consistently. |
|
24 | + * |
|
25 | + * @return int |
|
26 | + */ |
|
27 | + public function getTimestamp() { |
|
28 | + return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' ); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Set the UTC offset. |
|
33 | - * |
|
34 | - * This represents a fixed offset instead of a timezone setting. |
|
35 | - * |
|
36 | - * @param $offset |
|
37 | - */ |
|
38 | - public function setUtcOffset( $offset ) { |
|
39 | - $this->utcOffset = intval( $offset ); |
|
40 | - } |
|
31 | + /** |
|
32 | + * Set the UTC offset. |
|
33 | + * |
|
34 | + * This represents a fixed offset instead of a timezone setting. |
|
35 | + * |
|
36 | + * @param $offset |
|
37 | + */ |
|
38 | + public function setUtcOffset( $offset ) { |
|
39 | + $this->utcOffset = intval( $offset ); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Returns the timezone offset. |
|
44 | - * |
|
45 | - * @return int |
|
46 | - * @link http://php.net/manual/en/datetime.getoffset.php |
|
47 | - */ |
|
48 | - public function getOffset() { |
|
49 | - return $this->utcOffset ? $this->utcOffset : parent::getOffset(); |
|
50 | - } |
|
42 | + /** |
|
43 | + * Returns the timezone offset. |
|
44 | + * |
|
45 | + * @return int |
|
46 | + * @link http://php.net/manual/en/datetime.getoffset.php |
|
47 | + */ |
|
48 | + public function getOffset() { |
|
49 | + return $this->utcOffset ? $this->utcOffset : parent::getOffset(); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Set the TimeZone associated with the DateTime |
|
54 | - * |
|
55 | - * @param DateTimeZone $timezone |
|
56 | - * |
|
57 | - * @return static |
|
58 | - * @link http://php.net/manual/en/datetime.settimezone.php |
|
59 | - */ |
|
60 | - public function setTimezone( $timezone ) { |
|
61 | - $this->utcOffset = 0; |
|
62 | - parent::setTimezone( $timezone ); |
|
52 | + /** |
|
53 | + * Set the TimeZone associated with the DateTime |
|
54 | + * |
|
55 | + * @param DateTimeZone $timezone |
|
56 | + * |
|
57 | + * @return static |
|
58 | + * @link http://php.net/manual/en/datetime.settimezone.php |
|
59 | + */ |
|
60 | + public function setTimezone( $timezone ) { |
|
61 | + $this->utcOffset = 0; |
|
62 | + parent::setTimezone( $timezone ); |
|
63 | 63 | |
64 | - return $this; |
|
65 | - } |
|
64 | + return $this; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Get the timestamp with the WordPress timezone offset added or subtracted. |
|
69 | - * |
|
70 | - * @since 3.0.0 |
|
71 | - * @return int |
|
72 | - */ |
|
73 | - public function getOffsetTimestamp() { |
|
74 | - return $this->getTimestamp() + $this->getOffset(); |
|
75 | - } |
|
67 | + /** |
|
68 | + * Get the timestamp with the WordPress timezone offset added or subtracted. |
|
69 | + * |
|
70 | + * @since 3.0.0 |
|
71 | + * @return int |
|
72 | + */ |
|
73 | + public function getOffsetTimestamp() { |
|
74 | + return $this->getTimestamp() + $this->getOffset(); |
|
75 | + } |
|
76 | 76 | } |
@@ -5,63 +5,63 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_LogEntry { |
7 | 7 | |
8 | - /** |
|
9 | - * @var int $action_id |
|
10 | - */ |
|
11 | - protected $action_id = ''; |
|
8 | + /** |
|
9 | + * @var int $action_id |
|
10 | + */ |
|
11 | + protected $action_id = ''; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var string $message |
|
15 | - */ |
|
16 | - protected $message = ''; |
|
13 | + /** |
|
14 | + * @var string $message |
|
15 | + */ |
|
16 | + protected $message = ''; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @var Datetime $date |
|
20 | - */ |
|
21 | - protected $date; |
|
18 | + /** |
|
19 | + * @var Datetime $date |
|
20 | + */ |
|
21 | + protected $date; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor |
|
25 | - * |
|
26 | - * @param mixed $action_id Action ID |
|
27 | - * @param string $message Message |
|
28 | - * @param Datetime $date Datetime object with the time when this log entry was created. If this parameter is |
|
29 | - * not provided a new Datetime object (with current time) will be created. |
|
30 | - */ |
|
31 | - public function __construct( $action_id, $message, $date = null ) { |
|
23 | + /** |
|
24 | + * Constructor |
|
25 | + * |
|
26 | + * @param mixed $action_id Action ID |
|
27 | + * @param string $message Message |
|
28 | + * @param Datetime $date Datetime object with the time when this log entry was created. If this parameter is |
|
29 | + * not provided a new Datetime object (with current time) will be created. |
|
30 | + */ |
|
31 | + public function __construct( $action_id, $message, $date = null ) { |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | * ActionScheduler_wpCommentLogger::get_entry() previously passed a 3rd param of $comment->comment_type |
35 | 35 | * to ActionScheduler_LogEntry::__construct(), goodness knows why, and the Follow-up Emails plugin |
36 | 36 | * hard-codes loading its own version of ActionScheduler_wpCommentLogger with that out-dated method, |
37 | 37 | * goodness knows why, so we need to guard against that here instead of using a DateTime type declaration |
38 | 38 | * for the constructor's 3rd param of $date and causing a fatal error with older versions of FUE. |
39 | 39 | */ |
40 | - if ( null !== $date && ! is_a( $date, 'DateTime' ) ) { |
|
41 | - _doing_it_wrong( __METHOD__, 'The third parameter must be a valid DateTime instance, or null.', '2.0.0' ); |
|
42 | - $date = null; |
|
43 | - } |
|
40 | + if ( null !== $date && ! is_a( $date, 'DateTime' ) ) { |
|
41 | + _doing_it_wrong( __METHOD__, 'The third parameter must be a valid DateTime instance, or null.', '2.0.0' ); |
|
42 | + $date = null; |
|
43 | + } |
|
44 | 44 | |
45 | - $this->action_id = $action_id; |
|
46 | - $this->message = $message; |
|
47 | - $this->date = $date ? $date : new Datetime; |
|
48 | - } |
|
45 | + $this->action_id = $action_id; |
|
46 | + $this->message = $message; |
|
47 | + $this->date = $date ? $date : new Datetime; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns the date when this log entry was created |
|
52 | - * |
|
53 | - * @return Datetime |
|
54 | - */ |
|
55 | - public function get_date() { |
|
56 | - return $this->date; |
|
57 | - } |
|
50 | + /** |
|
51 | + * Returns the date when this log entry was created |
|
52 | + * |
|
53 | + * @return Datetime |
|
54 | + */ |
|
55 | + public function get_date() { |
|
56 | + return $this->date; |
|
57 | + } |
|
58 | 58 | |
59 | - public function get_action_id() { |
|
60 | - return $this->action_id; |
|
61 | - } |
|
59 | + public function get_action_id() { |
|
60 | + return $this->action_id; |
|
61 | + } |
|
62 | 62 | |
63 | - public function get_message() { |
|
64 | - return $this->message; |
|
65 | - } |
|
63 | + public function get_message() { |
|
64 | + return $this->message; |
|
65 | + } |
|
66 | 66 | } |
67 | 67 |
@@ -14,174 +14,174 @@ |
||
14 | 14 | * @since 3.0.0 |
15 | 15 | */ |
16 | 16 | class ActionScheduler_DataController { |
17 | - /** Action data store class name. */ |
|
18 | - const DATASTORE_CLASS = 'ActionScheduler_DBStore'; |
|
19 | - |
|
20 | - /** Logger data store class name. */ |
|
21 | - const LOGGER_CLASS = 'ActionScheduler_DBLogger'; |
|
22 | - |
|
23 | - /** Migration status option name. */ |
|
24 | - const STATUS_FLAG = 'action_scheduler_migration_status'; |
|
25 | - |
|
26 | - /** Migration status option value. */ |
|
27 | - const STATUS_COMPLETE = 'complete'; |
|
28 | - |
|
29 | - /** Migration minimum required PHP version. */ |
|
30 | - const MIN_PHP_VERSION = '5.5'; |
|
31 | - |
|
32 | - /** @var ActionScheduler_DataController */ |
|
33 | - private static $instance; |
|
34 | - |
|
35 | - /** @var int */ |
|
36 | - private static $sleep_time = 0; |
|
37 | - |
|
38 | - /** @var int */ |
|
39 | - private static $free_ticks = 50; |
|
40 | - |
|
41 | - /** |
|
42 | - * Get a flag indicating whether the migration environment dependencies are met. |
|
43 | - * |
|
44 | - * @return bool |
|
45 | - */ |
|
46 | - public static function dependencies_met() { |
|
47 | - $php_support = version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' ); |
|
48 | - return $php_support && apply_filters( 'action_scheduler_migration_dependencies_met', true ); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Get a flag indicating whether the migration is complete. |
|
53 | - * |
|
54 | - * @return bool Whether the flag has been set marking the migration as complete |
|
55 | - */ |
|
56 | - public static function is_migration_complete() { |
|
57 | - return get_option( self::STATUS_FLAG ) === self::STATUS_COMPLETE; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Mark the migration as complete. |
|
62 | - */ |
|
63 | - public static function mark_migration_complete() { |
|
64 | - update_option( self::STATUS_FLAG, self::STATUS_COMPLETE ); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Unmark migration when a plugin is de-activated. Will not work in case of silent activation, for example in an update. |
|
69 | - * We do this to mitigate the bug of lost actions which happens if there was an AS 2.x to AS 3.x migration in the past, but that plugin is now |
|
70 | - * deactivated and the site was running on AS 2.x again. |
|
71 | - */ |
|
72 | - public static function mark_migration_incomplete() { |
|
73 | - delete_option( self::STATUS_FLAG ); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Set the action store class name. |
|
78 | - * |
|
79 | - * @param string $class Classname of the store class. |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public static function set_store_class( $class ) { |
|
84 | - return self::DATASTORE_CLASS; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Set the action logger class name. |
|
89 | - * |
|
90 | - * @param string $class Classname of the logger class. |
|
91 | - * |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public static function set_logger_class( $class ) { |
|
95 | - return self::LOGGER_CLASS; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Set the sleep time in seconds. |
|
100 | - * |
|
101 | - * @param integer $sleep_time The number of seconds to pause before resuming operation. |
|
102 | - */ |
|
103 | - public static function set_sleep_time( $sleep_time ) { |
|
104 | - self::$sleep_time = $sleep_time; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Set the tick count required for freeing memory. |
|
109 | - * |
|
110 | - * @param integer $free_ticks The number of ticks to free memory on. |
|
111 | - */ |
|
112 | - public static function set_free_ticks( $free_ticks ) { |
|
113 | - self::$free_ticks = $free_ticks; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Free memory if conditions are met. |
|
118 | - * |
|
119 | - * @param int $ticks Current tick count. |
|
120 | - */ |
|
121 | - public static function maybe_free_memory( $ticks ) { |
|
122 | - if ( self::$free_ticks && 0 === $ticks % self::$free_ticks ) { |
|
123 | - self::free_memory(); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Reduce memory footprint by clearing the database query and object caches. |
|
129 | - */ |
|
130 | - public static function free_memory() { |
|
131 | - if ( 0 < self::$sleep_time ) { |
|
132 | - /* translators: %d: amount of time */ |
|
133 | - \WP_CLI::warning( sprintf( _n( 'Stopped the insanity for %d second', 'Stopped the insanity for %d seconds', self::$sleep_time, 'action-scheduler' ), self::$sleep_time ) ); |
|
134 | - sleep( self::$sleep_time ); |
|
135 | - } |
|
136 | - |
|
137 | - \WP_CLI::warning( __( 'Attempting to reduce used memory...', 'action-scheduler' ) ); |
|
138 | - |
|
139 | - /** |
|
140 | - * @var $wpdb \wpdb |
|
141 | - * @var $wp_object_cache \WP_Object_Cache |
|
142 | - */ |
|
143 | - global $wpdb, $wp_object_cache; |
|
144 | - |
|
145 | - $wpdb->queries = array(); |
|
146 | - |
|
147 | - if ( ! is_a( $wp_object_cache, 'WP_Object_Cache' ) ) { |
|
148 | - return; |
|
149 | - } |
|
150 | - |
|
151 | - $wp_object_cache->group_ops = array(); |
|
152 | - $wp_object_cache->stats = array(); |
|
153 | - $wp_object_cache->memcache_debug = array(); |
|
154 | - $wp_object_cache->cache = array(); |
|
155 | - |
|
156 | - if ( is_callable( array( $wp_object_cache, '__remoteset' ) ) ) { |
|
157 | - call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Connect to table datastores if migration is complete. |
|
163 | - * Otherwise, proceed with the migration if the dependencies have been met. |
|
164 | - */ |
|
165 | - public static function init() { |
|
166 | - if ( self::is_migration_complete() ) { |
|
167 | - add_filter( 'action_scheduler_store_class', array( 'ActionScheduler_DataController', 'set_store_class' ), 100 ); |
|
168 | - add_filter( 'action_scheduler_logger_class', array( 'ActionScheduler_DataController', 'set_logger_class' ), 100 ); |
|
169 | - add_action( 'deactivate_plugin', array( 'ActionScheduler_DataController', 'mark_migration_incomplete' ) ); |
|
170 | - } elseif ( self::dependencies_met() ) { |
|
171 | - Controller::init(); |
|
172 | - } |
|
173 | - |
|
174 | - add_action( 'action_scheduler/progress_tick', array( 'ActionScheduler_DataController', 'maybe_free_memory' ) ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Singleton factory. |
|
179 | - */ |
|
180 | - public static function instance() { |
|
181 | - if ( ! isset( self::$instance ) ) { |
|
182 | - self::$instance = new static(); |
|
183 | - } |
|
184 | - |
|
185 | - return self::$instance; |
|
186 | - } |
|
17 | + /** Action data store class name. */ |
|
18 | + const DATASTORE_CLASS = 'ActionScheduler_DBStore'; |
|
19 | + |
|
20 | + /** Logger data store class name. */ |
|
21 | + const LOGGER_CLASS = 'ActionScheduler_DBLogger'; |
|
22 | + |
|
23 | + /** Migration status option name. */ |
|
24 | + const STATUS_FLAG = 'action_scheduler_migration_status'; |
|
25 | + |
|
26 | + /** Migration status option value. */ |
|
27 | + const STATUS_COMPLETE = 'complete'; |
|
28 | + |
|
29 | + /** Migration minimum required PHP version. */ |
|
30 | + const MIN_PHP_VERSION = '5.5'; |
|
31 | + |
|
32 | + /** @var ActionScheduler_DataController */ |
|
33 | + private static $instance; |
|
34 | + |
|
35 | + /** @var int */ |
|
36 | + private static $sleep_time = 0; |
|
37 | + |
|
38 | + /** @var int */ |
|
39 | + private static $free_ticks = 50; |
|
40 | + |
|
41 | + /** |
|
42 | + * Get a flag indicating whether the migration environment dependencies are met. |
|
43 | + * |
|
44 | + * @return bool |
|
45 | + */ |
|
46 | + public static function dependencies_met() { |
|
47 | + $php_support = version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' ); |
|
48 | + return $php_support && apply_filters( 'action_scheduler_migration_dependencies_met', true ); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Get a flag indicating whether the migration is complete. |
|
53 | + * |
|
54 | + * @return bool Whether the flag has been set marking the migration as complete |
|
55 | + */ |
|
56 | + public static function is_migration_complete() { |
|
57 | + return get_option( self::STATUS_FLAG ) === self::STATUS_COMPLETE; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Mark the migration as complete. |
|
62 | + */ |
|
63 | + public static function mark_migration_complete() { |
|
64 | + update_option( self::STATUS_FLAG, self::STATUS_COMPLETE ); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Unmark migration when a plugin is de-activated. Will not work in case of silent activation, for example in an update. |
|
69 | + * We do this to mitigate the bug of lost actions which happens if there was an AS 2.x to AS 3.x migration in the past, but that plugin is now |
|
70 | + * deactivated and the site was running on AS 2.x again. |
|
71 | + */ |
|
72 | + public static function mark_migration_incomplete() { |
|
73 | + delete_option( self::STATUS_FLAG ); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Set the action store class name. |
|
78 | + * |
|
79 | + * @param string $class Classname of the store class. |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public static function set_store_class( $class ) { |
|
84 | + return self::DATASTORE_CLASS; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Set the action logger class name. |
|
89 | + * |
|
90 | + * @param string $class Classname of the logger class. |
|
91 | + * |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public static function set_logger_class( $class ) { |
|
95 | + return self::LOGGER_CLASS; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Set the sleep time in seconds. |
|
100 | + * |
|
101 | + * @param integer $sleep_time The number of seconds to pause before resuming operation. |
|
102 | + */ |
|
103 | + public static function set_sleep_time( $sleep_time ) { |
|
104 | + self::$sleep_time = $sleep_time; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Set the tick count required for freeing memory. |
|
109 | + * |
|
110 | + * @param integer $free_ticks The number of ticks to free memory on. |
|
111 | + */ |
|
112 | + public static function set_free_ticks( $free_ticks ) { |
|
113 | + self::$free_ticks = $free_ticks; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Free memory if conditions are met. |
|
118 | + * |
|
119 | + * @param int $ticks Current tick count. |
|
120 | + */ |
|
121 | + public static function maybe_free_memory( $ticks ) { |
|
122 | + if ( self::$free_ticks && 0 === $ticks % self::$free_ticks ) { |
|
123 | + self::free_memory(); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Reduce memory footprint by clearing the database query and object caches. |
|
129 | + */ |
|
130 | + public static function free_memory() { |
|
131 | + if ( 0 < self::$sleep_time ) { |
|
132 | + /* translators: %d: amount of time */ |
|
133 | + \WP_CLI::warning( sprintf( _n( 'Stopped the insanity for %d second', 'Stopped the insanity for %d seconds', self::$sleep_time, 'action-scheduler' ), self::$sleep_time ) ); |
|
134 | + sleep( self::$sleep_time ); |
|
135 | + } |
|
136 | + |
|
137 | + \WP_CLI::warning( __( 'Attempting to reduce used memory...', 'action-scheduler' ) ); |
|
138 | + |
|
139 | + /** |
|
140 | + * @var $wpdb \wpdb |
|
141 | + * @var $wp_object_cache \WP_Object_Cache |
|
142 | + */ |
|
143 | + global $wpdb, $wp_object_cache; |
|
144 | + |
|
145 | + $wpdb->queries = array(); |
|
146 | + |
|
147 | + if ( ! is_a( $wp_object_cache, 'WP_Object_Cache' ) ) { |
|
148 | + return; |
|
149 | + } |
|
150 | + |
|
151 | + $wp_object_cache->group_ops = array(); |
|
152 | + $wp_object_cache->stats = array(); |
|
153 | + $wp_object_cache->memcache_debug = array(); |
|
154 | + $wp_object_cache->cache = array(); |
|
155 | + |
|
156 | + if ( is_callable( array( $wp_object_cache, '__remoteset' ) ) ) { |
|
157 | + call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Connect to table datastores if migration is complete. |
|
163 | + * Otherwise, proceed with the migration if the dependencies have been met. |
|
164 | + */ |
|
165 | + public static function init() { |
|
166 | + if ( self::is_migration_complete() ) { |
|
167 | + add_filter( 'action_scheduler_store_class', array( 'ActionScheduler_DataController', 'set_store_class' ), 100 ); |
|
168 | + add_filter( 'action_scheduler_logger_class', array( 'ActionScheduler_DataController', 'set_logger_class' ), 100 ); |
|
169 | + add_action( 'deactivate_plugin', array( 'ActionScheduler_DataController', 'mark_migration_incomplete' ) ); |
|
170 | + } elseif ( self::dependencies_met() ) { |
|
171 | + Controller::init(); |
|
172 | + } |
|
173 | + |
|
174 | + add_action( 'action_scheduler/progress_tick', array( 'ActionScheduler_DataController', 'maybe_free_memory' ) ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Singleton factory. |
|
179 | + */ |
|
180 | + public static function instance() { |
|
181 | + if ( ! isset( self::$instance ) ) { |
|
182 | + self::$instance = new static(); |
|
183 | + } |
|
184 | + |
|
185 | + return self::$instance; |
|
186 | + } |
|
187 | 187 | } |
@@ -9,39 +9,39 @@ |
||
9 | 9 | */ |
10 | 10 | class ActionScheduler_InvalidActionException extends \InvalidArgumentException implements ActionScheduler_Exception { |
11 | 11 | |
12 | - /** |
|
13 | - * Create a new exception when the action's schedule cannot be fetched. |
|
14 | - * |
|
15 | - * @param string $action_id The action ID with bad args. |
|
16 | - * @return static |
|
17 | - */ |
|
18 | - public static function from_schedule( $action_id, $schedule ) { |
|
19 | - $message = sprintf( |
|
20 | - /* translators: 1: action ID 2: schedule */ |
|
21 | - __( 'Action [%1$s] has an invalid schedule: %2$s', 'action-scheduler' ), |
|
22 | - $action_id, |
|
23 | - var_export( $schedule, true ) |
|
24 | - ); |
|
12 | + /** |
|
13 | + * Create a new exception when the action's schedule cannot be fetched. |
|
14 | + * |
|
15 | + * @param string $action_id The action ID with bad args. |
|
16 | + * @return static |
|
17 | + */ |
|
18 | + public static function from_schedule( $action_id, $schedule ) { |
|
19 | + $message = sprintf( |
|
20 | + /* translators: 1: action ID 2: schedule */ |
|
21 | + __( 'Action [%1$s] has an invalid schedule: %2$s', 'action-scheduler' ), |
|
22 | + $action_id, |
|
23 | + var_export( $schedule, true ) |
|
24 | + ); |
|
25 | 25 | |
26 | - return new static( $message ); |
|
27 | - } |
|
26 | + return new static( $message ); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Create a new exception when the action's args cannot be decoded to an array. |
|
31 | - * |
|
32 | - * @author Jeremy Pry |
|
33 | - * |
|
34 | - * @param string $action_id The action ID with bad args. |
|
35 | - * @return static |
|
36 | - */ |
|
37 | - public static function from_decoding_args( $action_id, $args = array() ) { |
|
38 | - $message = sprintf( |
|
39 | - /* translators: 1: action ID 2: arguments */ |
|
40 | - __( 'Action [%1$s] has invalid arguments. It cannot be JSON decoded to an array. $args = %2$s', 'action-scheduler' ), |
|
41 | - $action_id, |
|
42 | - var_export( $args, true ) |
|
43 | - ); |
|
29 | + /** |
|
30 | + * Create a new exception when the action's args cannot be decoded to an array. |
|
31 | + * |
|
32 | + * @author Jeremy Pry |
|
33 | + * |
|
34 | + * @param string $action_id The action ID with bad args. |
|
35 | + * @return static |
|
36 | + */ |
|
37 | + public static function from_decoding_args( $action_id, $args = array() ) { |
|
38 | + $message = sprintf( |
|
39 | + /* translators: 1: action ID 2: arguments */ |
|
40 | + __( 'Action [%1$s] has invalid arguments. It cannot be JSON decoded to an array. $args = %2$s', 'action-scheduler' ), |
|
41 | + $action_id, |
|
42 | + var_export( $args, true ) |
|
43 | + ); |
|
44 | 44 | |
45 | - return new static( $message ); |
|
46 | - } |
|
45 | + return new static( $message ); |
|
46 | + } |
|
47 | 47 | } |