@@ -7,654 +7,654 @@ |
||
7 | 7 | */ |
8 | 8 | class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { |
9 | 9 | |
10 | - /** |
|
11 | - * The package name. |
|
12 | - * |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $package = 'action-scheduler'; |
|
16 | - |
|
17 | - /** |
|
18 | - * Columns to show (name => label). |
|
19 | - * |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $columns = array(); |
|
23 | - |
|
24 | - /** |
|
25 | - * Actions (name => label). |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $row_actions = array(); |
|
30 | - |
|
31 | - /** |
|
32 | - * The active data stores |
|
33 | - * |
|
34 | - * @var ActionScheduler_Store |
|
35 | - */ |
|
36 | - protected $store; |
|
37 | - |
|
38 | - /** |
|
39 | - * A logger to use for getting action logs to display |
|
40 | - * |
|
41 | - * @var ActionScheduler_Logger |
|
42 | - */ |
|
43 | - protected $logger; |
|
44 | - |
|
45 | - /** |
|
46 | - * A ActionScheduler_QueueRunner runner instance (or child class) |
|
47 | - * |
|
48 | - * @var ActionScheduler_QueueRunner |
|
49 | - */ |
|
50 | - protected $runner; |
|
51 | - |
|
52 | - /** |
|
53 | - * Bulk actions. The key of the array is the method name of the implementation: |
|
54 | - * |
|
55 | - * bulk_<key>(array $ids, string $sql_in). |
|
56 | - * |
|
57 | - * See the comments in the parent class for further details |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $bulk_actions = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * Flag variable to render our notifications, if any, once. |
|
65 | - * |
|
66 | - * @var bool |
|
67 | - */ |
|
68 | - protected static $did_notification = false; |
|
69 | - |
|
70 | - /** |
|
71 | - * Array of seconds for common time periods, like week or month, alongside an internationalised string representation, i.e. "Day" or "Days" |
|
72 | - * |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - private static $time_periods; |
|
76 | - |
|
77 | - /** |
|
78 | - * Sets the current data store object into `store->action` and initialises the object. |
|
79 | - * |
|
80 | - * @param ActionScheduler_Store $store |
|
81 | - * @param ActionScheduler_Logger $logger |
|
82 | - * @param ActionScheduler_QueueRunner $runner |
|
83 | - */ |
|
84 | - public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
|
85 | - |
|
86 | - $this->store = $store; |
|
87 | - $this->logger = $logger; |
|
88 | - $this->runner = $runner; |
|
89 | - |
|
90 | - $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
|
91 | - |
|
92 | - $this->bulk_actions = array( |
|
93 | - 'delete' => __( 'Delete', 'action-scheduler' ), |
|
94 | - ); |
|
95 | - |
|
96 | - $this->columns = array( |
|
97 | - 'hook' => __( 'Hook', 'action-scheduler' ), |
|
98 | - 'status' => __( 'Status', 'action-scheduler' ), |
|
99 | - 'args' => __( 'Arguments', 'action-scheduler' ), |
|
100 | - 'group' => __( 'Group', 'action-scheduler' ), |
|
101 | - 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
|
102 | - 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
|
103 | - 'log_entries' => __( 'Log', 'action-scheduler' ), |
|
104 | - ); |
|
105 | - |
|
106 | - $this->sort_by = array( |
|
107 | - 'schedule', |
|
108 | - 'hook', |
|
109 | - 'group', |
|
110 | - ); |
|
111 | - |
|
112 | - $this->search_by = array( |
|
113 | - 'hook', |
|
114 | - 'args', |
|
115 | - 'claim_id', |
|
116 | - ); |
|
117 | - |
|
118 | - $request_status = $this->get_request_status(); |
|
119 | - |
|
120 | - if ( empty( $request_status ) ) { |
|
121 | - $this->sort_by[] = 'status'; |
|
122 | - } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { |
|
123 | - $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
|
124 | - $this->sort_by[] = 'claim_id'; |
|
125 | - } |
|
126 | - |
|
127 | - $this->row_actions = array( |
|
128 | - 'hook' => array( |
|
129 | - 'run' => array( |
|
130 | - 'name' => __( 'Run', 'action-scheduler' ), |
|
131 | - 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
|
132 | - ), |
|
133 | - 'cancel' => array( |
|
134 | - 'name' => __( 'Cancel', 'action-scheduler' ), |
|
135 | - 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
|
136 | - 'class' => 'cancel trash', |
|
137 | - ), |
|
138 | - ), |
|
139 | - ); |
|
140 | - |
|
141 | - self::$time_periods = array( |
|
142 | - array( |
|
143 | - 'seconds' => YEAR_IN_SECONDS, |
|
144 | - /* translators: %s: amount of time */ |
|
145 | - 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
|
146 | - ), |
|
147 | - array( |
|
148 | - 'seconds' => MONTH_IN_SECONDS, |
|
149 | - /* translators: %s: amount of time */ |
|
150 | - 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
|
151 | - ), |
|
152 | - array( |
|
153 | - 'seconds' => WEEK_IN_SECONDS, |
|
154 | - /* translators: %s: amount of time */ |
|
155 | - 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
|
156 | - ), |
|
157 | - array( |
|
158 | - 'seconds' => DAY_IN_SECONDS, |
|
159 | - /* translators: %s: amount of time */ |
|
160 | - 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
|
161 | - ), |
|
162 | - array( |
|
163 | - 'seconds' => HOUR_IN_SECONDS, |
|
164 | - /* translators: %s: amount of time */ |
|
165 | - 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
|
166 | - ), |
|
167 | - array( |
|
168 | - 'seconds' => MINUTE_IN_SECONDS, |
|
169 | - /* translators: %s: amount of time */ |
|
170 | - 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
|
171 | - ), |
|
172 | - array( |
|
173 | - 'seconds' => 1, |
|
174 | - /* translators: %s: amount of time */ |
|
175 | - 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
|
176 | - ), |
|
177 | - ); |
|
178 | - |
|
179 | - parent::__construct( |
|
180 | - array( |
|
181 | - 'singular' => 'action-scheduler', |
|
182 | - 'plural' => 'action-scheduler', |
|
183 | - 'ajax' => false, |
|
184 | - ) |
|
185 | - ); |
|
186 | - |
|
187 | - add_screen_option( |
|
188 | - 'per_page', |
|
189 | - array( |
|
190 | - 'default' => $this->items_per_page, |
|
191 | - ) |
|
192 | - ); |
|
193 | - |
|
194 | - add_filter( 'set_screen_option_' . $this->get_per_page_option_name(), array( $this, 'set_items_per_page_option' ), 10, 3 ); |
|
195 | - set_screen_options(); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Handles setting the items_per_page option for this screen. |
|
200 | - * |
|
201 | - * @param mixed $status Default false (to skip saving the current option). |
|
202 | - * @param string $option Screen option name. |
|
203 | - * @param int $value Screen option value. |
|
204 | - * @return int |
|
205 | - */ |
|
206 | - public function set_items_per_page_option( $status, $option, $value ) { |
|
207 | - return $value; |
|
208 | - } |
|
209 | - /** |
|
210 | - * Convert an interval of seconds into a two part human friendly string. |
|
211 | - * |
|
212 | - * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
213 | - * even if an action is 1 day and 11 hours away, it will display "1 day". This function goes one step |
|
214 | - * further to display two degrees of accuracy. |
|
215 | - * |
|
216 | - * Inspired by the Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
217 | - * |
|
218 | - * @param int $interval A interval in seconds. |
|
219 | - * @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. |
|
220 | - * @return string A human friendly string representation of the interval. |
|
221 | - */ |
|
222 | - private static function human_interval( $interval, $periods_to_include = 2 ) { |
|
223 | - |
|
224 | - if ( $interval <= 0 ) { |
|
225 | - return __( 'Now!', 'action-scheduler' ); |
|
226 | - } |
|
227 | - |
|
228 | - $output = ''; |
|
229 | - |
|
230 | - 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++ ) { |
|
231 | - |
|
232 | - $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
|
233 | - |
|
234 | - if ( $periods_in_interval > 0 ) { |
|
235 | - if ( ! empty( $output ) ) { |
|
236 | - $output .= ' '; |
|
237 | - } |
|
238 | - $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 ); |
|
239 | - $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
|
240 | - $periods_included++; |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - return $output; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. |
|
249 | - * |
|
250 | - * @param ActionScheduler_Action $action |
|
251 | - * |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - protected function get_recurrence( $action ) { |
|
255 | - $schedule = $action->get_schedule(); |
|
256 | - if ( $schedule->is_recurring() ) { |
|
257 | - $recurrence = $schedule->get_recurrence(); |
|
258 | - |
|
259 | - if ( is_numeric( $recurrence ) ) { |
|
260 | - /* translators: %s: time interval */ |
|
261 | - return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
|
262 | - } else { |
|
263 | - return $recurrence; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - return __( 'Non-repeating', 'action-scheduler' ); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Serializes the argument of an action to render it in a human friendly format. |
|
272 | - * |
|
273 | - * @param array $row The array representation of the current row of the table |
|
274 | - * |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public function column_args( array $row ) { |
|
278 | - if ( empty( $row['args'] ) ) { |
|
279 | - return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
|
280 | - } |
|
281 | - |
|
282 | - $row_html = '<ul>'; |
|
283 | - foreach ( $row['args'] as $key => $value ) { |
|
284 | - $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); |
|
285 | - } |
|
286 | - $row_html .= '</ul>'; |
|
287 | - |
|
288 | - return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
293 | - * |
|
294 | - * @param array $row Action array. |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - public function column_log_entries( array $row ) { |
|
298 | - |
|
299 | - $log_entries_html = '<ol>'; |
|
300 | - |
|
301 | - $timezone = new DateTimezone( 'UTC' ); |
|
302 | - |
|
303 | - foreach ( $row['log_entries'] as $log_entry ) { |
|
304 | - $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
|
305 | - } |
|
306 | - |
|
307 | - $log_entries_html .= '</ol>'; |
|
308 | - |
|
309 | - return $log_entries_html; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
314 | - * |
|
315 | - * @param ActionScheduler_LogEntry $log_entry |
|
316 | - * @param DateTimezone $timezone |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
|
320 | - $date = $log_entry->get_date(); |
|
321 | - $date->setTimezone( $timezone ); |
|
322 | - 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() ) ); |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Only display row actions for pending actions. |
|
327 | - * |
|
328 | - * @param array $row Row to render |
|
329 | - * @param string $column_name Current row |
|
330 | - * |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - protected function maybe_render_actions( $row, $column_name ) { |
|
334 | - if ( 'pending' === strtolower( $row['status_name'] ) ) { |
|
335 | - return parent::maybe_render_actions( $row, $column_name ); |
|
336 | - } |
|
337 | - |
|
338 | - return ''; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Renders admin notifications |
|
343 | - * |
|
344 | - * Notifications: |
|
345 | - * 1. When the maximum number of tasks are being executed simultaneously. |
|
346 | - * 2. Notifications when a task is manually executed. |
|
347 | - * 3. Tables are missing. |
|
348 | - */ |
|
349 | - public function display_admin_notices() { |
|
350 | - global $wpdb; |
|
351 | - |
|
352 | - if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
|
353 | - $table_list = array( |
|
354 | - 'actionscheduler_actions', |
|
355 | - 'actionscheduler_logs', |
|
356 | - 'actionscheduler_groups', |
|
357 | - 'actionscheduler_claims', |
|
358 | - ); |
|
359 | - |
|
360 | - $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
361 | - foreach ( $table_list as $table_name ) { |
|
362 | - if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { |
|
363 | - $this->admin_notices[] = array( |
|
364 | - 'class' => 'error', |
|
365 | - 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler' ), |
|
366 | - ); |
|
367 | - $this->recreate_tables(); |
|
368 | - parent::display_admin_notices(); |
|
369 | - |
|
370 | - return; |
|
371 | - } |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - if ( $this->runner->has_maximum_concurrent_batches() ) { |
|
376 | - $claim_count = $this->store->get_claim_count(); |
|
377 | - $this->admin_notices[] = array( |
|
378 | - 'class' => 'updated', |
|
379 | - 'message' => sprintf( |
|
380 | - /* translators: %s: amount of claims */ |
|
381 | - _n( |
|
382 | - 'Maximum simultaneous queues already in progress (%s queue). No additional queues will begin processing until the current queues are complete.', |
|
383 | - 'Maximum simultaneous queues already in progress (%s queues). No additional queues will begin processing until the current queues are complete.', |
|
384 | - $claim_count, |
|
385 | - 'action-scheduler' |
|
386 | - ), |
|
387 | - $claim_count |
|
388 | - ), |
|
389 | - ); |
|
390 | - } elseif ( $this->store->has_pending_actions_due() ) { |
|
391 | - |
|
392 | - $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
|
393 | - |
|
394 | - // No lock set or lock expired |
|
395 | - if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
|
396 | - $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
|
397 | - /* translators: %s: process URL */ |
|
398 | - $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 ) ); |
|
399 | - } else { |
|
400 | - /* translators: %d: seconds */ |
|
401 | - $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
|
402 | - } |
|
403 | - |
|
404 | - $this->admin_notices[] = array( |
|
405 | - 'class' => 'notice notice-info', |
|
406 | - 'message' => $async_request_message, |
|
407 | - ); |
|
408 | - } |
|
409 | - |
|
410 | - $notification = get_transient( 'action_scheduler_admin_notice' ); |
|
411 | - |
|
412 | - if ( is_array( $notification ) ) { |
|
413 | - delete_transient( 'action_scheduler_admin_notice' ); |
|
414 | - |
|
415 | - $action = $this->store->fetch_action( $notification['action_id'] ); |
|
416 | - $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
|
417 | - if ( 1 == $notification['success'] ) { |
|
418 | - $class = 'updated'; |
|
419 | - switch ( $notification['row_action_type'] ) { |
|
420 | - case 'run': |
|
421 | - /* translators: %s: action HTML */ |
|
422 | - $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
|
423 | - break; |
|
424 | - case 'cancel': |
|
425 | - /* translators: %s: action HTML */ |
|
426 | - $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
|
427 | - break; |
|
428 | - default: |
|
429 | - /* translators: %s: action HTML */ |
|
430 | - $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
|
431 | - break; |
|
432 | - } |
|
433 | - } else { |
|
434 | - $class = 'error'; |
|
435 | - /* translators: 1: action HTML 2: action ID 3: error message */ |
|
436 | - $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'] ) ); |
|
437 | - } |
|
438 | - |
|
439 | - $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
|
440 | - |
|
441 | - $this->admin_notices[] = array( |
|
442 | - 'class' => $class, |
|
443 | - 'message' => $action_message_html, |
|
444 | - ); |
|
445 | - } |
|
446 | - |
|
447 | - parent::display_admin_notices(); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Prints the scheduled date in a human friendly format. |
|
452 | - * |
|
453 | - * @param array $row The array representation of the current row of the table |
|
454 | - * |
|
455 | - * @return string |
|
456 | - */ |
|
457 | - public function column_schedule( $row ) { |
|
458 | - return $this->get_schedule_display_string( $row['schedule'] ); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Get the scheduled date in a human friendly format. |
|
463 | - * |
|
464 | - * @param ActionScheduler_Schedule $schedule |
|
465 | - * @return string |
|
466 | - */ |
|
467 | - protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
|
468 | - |
|
469 | - $schedule_display_string = ''; |
|
470 | - |
|
471 | - if ( is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
472 | - return __( 'async', 'action-scheduler' ); |
|
473 | - } |
|
474 | - |
|
475 | - if ( ! $schedule->get_date() ) { |
|
476 | - return '0000-00-00 00:00:00'; |
|
477 | - } |
|
478 | - |
|
479 | - $next_timestamp = $schedule->get_date()->getTimestamp(); |
|
480 | - |
|
481 | - $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
|
482 | - $schedule_display_string .= '<br/>'; |
|
483 | - |
|
484 | - if ( gmdate( 'U' ) > $next_timestamp ) { |
|
485 | - /* translators: %s: date interval */ |
|
486 | - $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
|
487 | - } else { |
|
488 | - /* translators: %s: date interval */ |
|
489 | - $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
|
490 | - } |
|
491 | - |
|
492 | - return $schedule_display_string; |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Bulk delete |
|
497 | - * |
|
498 | - * Deletes actions based on their ID. This is the handler for the bulk delete. It assumes the data |
|
499 | - * properly validated by the callee and it will delete the actions without any extra validation. |
|
500 | - * |
|
501 | - * @param array $ids |
|
502 | - * @param string $ids_sql Inherited and unused |
|
503 | - */ |
|
504 | - protected function bulk_delete( array $ids, $ids_sql ) { |
|
505 | - foreach ( $ids as $id ) { |
|
506 | - $this->store->delete_action( $id ); |
|
507 | - } |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
512 | - * parameters are valid. |
|
513 | - * |
|
514 | - * @param int $action_id |
|
515 | - */ |
|
516 | - protected function row_action_cancel( $action_id ) { |
|
517 | - $this->process_row_action( $action_id, 'cancel' ); |
|
518 | - } |
|
519 | - |
|
520 | - /** |
|
521 | - * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
522 | - * parameters are valid. |
|
523 | - * |
|
524 | - * @param int $action_id |
|
525 | - */ |
|
526 | - protected function row_action_run( $action_id ) { |
|
527 | - $this->process_row_action( $action_id, 'run' ); |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Force the data store schema updates. |
|
532 | - */ |
|
533 | - protected function recreate_tables() { |
|
534 | - if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
|
535 | - $store = $this->store; |
|
536 | - } else { |
|
537 | - $store = new ActionScheduler_HybridStore(); |
|
538 | - } |
|
539 | - add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
|
540 | - |
|
541 | - $store_schema = new ActionScheduler_StoreSchema(); |
|
542 | - $logger_schema = new ActionScheduler_LoggerSchema(); |
|
543 | - $store_schema->register_tables( true ); |
|
544 | - $logger_schema->register_tables( true ); |
|
545 | - |
|
546 | - remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
|
547 | - } |
|
548 | - /** |
|
549 | - * Implements the logic behind processing an action once an action link is clicked on the list table. |
|
550 | - * |
|
551 | - * @param int $action_id |
|
552 | - * @param string $row_action_type The type of action to perform on the action. |
|
553 | - */ |
|
554 | - protected function process_row_action( $action_id, $row_action_type ) { |
|
555 | - try { |
|
556 | - switch ( $row_action_type ) { |
|
557 | - case 'run': |
|
558 | - $this->runner->process_action( $action_id, 'Admin List Table' ); |
|
559 | - break; |
|
560 | - case 'cancel': |
|
561 | - $this->store->cancel_action( $action_id ); |
|
562 | - break; |
|
563 | - } |
|
564 | - $success = 1; |
|
565 | - $error_message = ''; |
|
566 | - } catch ( Exception $e ) { |
|
567 | - $success = 0; |
|
568 | - $error_message = $e->getMessage(); |
|
569 | - } |
|
570 | - |
|
571 | - set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * {@inheritDoc} |
|
576 | - */ |
|
577 | - public function prepare_items() { |
|
578 | - $this->prepare_column_headers(); |
|
579 | - |
|
580 | - $per_page = $this->get_items_per_page( $this->get_per_page_option_name(), $this->items_per_page ); |
|
581 | - |
|
582 | - $query = array( |
|
583 | - 'per_page' => $per_page, |
|
584 | - 'offset' => $this->get_items_offset(), |
|
585 | - 'status' => $this->get_request_status(), |
|
586 | - 'orderby' => $this->get_request_orderby(), |
|
587 | - 'order' => $this->get_request_order(), |
|
588 | - 'search' => $this->get_request_search_query(), |
|
589 | - ); |
|
590 | - |
|
591 | - /** |
|
592 | - * Change query arguments to query for past-due actions. |
|
593 | - * Past-due actions have the 'pending' status and are in the past. |
|
594 | - * This is needed because registering 'past-due' as a status is overkill. |
|
595 | - */ |
|
596 | - if ( 'past-due' === $this->get_request_status() ) { |
|
597 | - $query['status'] = ActionScheduler_Store::STATUS_PENDING; |
|
598 | - $query['date'] = as_get_datetime_object(); |
|
599 | - } |
|
600 | - |
|
601 | - $this->items = array(); |
|
602 | - |
|
603 | - $total_items = $this->store->query_actions( $query, 'count' ); |
|
604 | - |
|
605 | - $status_labels = $this->store->get_status_labels(); |
|
606 | - |
|
607 | - foreach ( $this->store->query_actions( $query ) as $action_id ) { |
|
608 | - try { |
|
609 | - $action = $this->store->fetch_action( $action_id ); |
|
610 | - } catch ( Exception $e ) { |
|
611 | - continue; |
|
612 | - } |
|
613 | - if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
|
614 | - continue; |
|
615 | - } |
|
616 | - $this->items[ $action_id ] = array( |
|
617 | - 'ID' => $action_id, |
|
618 | - 'hook' => $action->get_hook(), |
|
619 | - 'status_name' => $this->store->get_status( $action_id ), |
|
620 | - 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
|
621 | - 'args' => $action->get_args(), |
|
622 | - 'group' => $action->get_group(), |
|
623 | - 'log_entries' => $this->logger->get_logs( $action_id ), |
|
624 | - 'claim_id' => $this->store->get_claim_id( $action_id ), |
|
625 | - 'recurrence' => $this->get_recurrence( $action ), |
|
626 | - 'schedule' => $action->get_schedule(), |
|
627 | - ); |
|
628 | - } |
|
629 | - |
|
630 | - $this->set_pagination_args( |
|
631 | - array( |
|
632 | - 'total_items' => $total_items, |
|
633 | - 'per_page' => $per_page, |
|
634 | - 'total_pages' => ceil( $total_items / $per_page ), |
|
635 | - ) |
|
636 | - ); |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Prints the available statuses so the user can click to filter. |
|
641 | - */ |
|
642 | - protected function display_filter_by_status() { |
|
643 | - $this->status_counts = $this->store->action_counts() + $this->store->extra_action_counts(); |
|
644 | - parent::display_filter_by_status(); |
|
645 | - } |
|
646 | - |
|
647 | - /** |
|
648 | - * Get the text to display in the search box on the list table. |
|
649 | - */ |
|
650 | - protected function get_search_box_button_text() { |
|
651 | - return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
|
652 | - } |
|
653 | - |
|
654 | - /** |
|
655 | - * {@inheritDoc} |
|
656 | - */ |
|
657 | - protected function get_per_page_option_name() { |
|
658 | - return str_replace( '-', '_', $this->screen->id ) . '_per_page'; |
|
659 | - } |
|
10 | + /** |
|
11 | + * The package name. |
|
12 | + * |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $package = 'action-scheduler'; |
|
16 | + |
|
17 | + /** |
|
18 | + * Columns to show (name => label). |
|
19 | + * |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $columns = array(); |
|
23 | + |
|
24 | + /** |
|
25 | + * Actions (name => label). |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $row_actions = array(); |
|
30 | + |
|
31 | + /** |
|
32 | + * The active data stores |
|
33 | + * |
|
34 | + * @var ActionScheduler_Store |
|
35 | + */ |
|
36 | + protected $store; |
|
37 | + |
|
38 | + /** |
|
39 | + * A logger to use for getting action logs to display |
|
40 | + * |
|
41 | + * @var ActionScheduler_Logger |
|
42 | + */ |
|
43 | + protected $logger; |
|
44 | + |
|
45 | + /** |
|
46 | + * A ActionScheduler_QueueRunner runner instance (or child class) |
|
47 | + * |
|
48 | + * @var ActionScheduler_QueueRunner |
|
49 | + */ |
|
50 | + protected $runner; |
|
51 | + |
|
52 | + /** |
|
53 | + * Bulk actions. The key of the array is the method name of the implementation: |
|
54 | + * |
|
55 | + * bulk_<key>(array $ids, string $sql_in). |
|
56 | + * |
|
57 | + * See the comments in the parent class for further details |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $bulk_actions = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * Flag variable to render our notifications, if any, once. |
|
65 | + * |
|
66 | + * @var bool |
|
67 | + */ |
|
68 | + protected static $did_notification = false; |
|
69 | + |
|
70 | + /** |
|
71 | + * Array of seconds for common time periods, like week or month, alongside an internationalised string representation, i.e. "Day" or "Days" |
|
72 | + * |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + private static $time_periods; |
|
76 | + |
|
77 | + /** |
|
78 | + * Sets the current data store object into `store->action` and initialises the object. |
|
79 | + * |
|
80 | + * @param ActionScheduler_Store $store |
|
81 | + * @param ActionScheduler_Logger $logger |
|
82 | + * @param ActionScheduler_QueueRunner $runner |
|
83 | + */ |
|
84 | + public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
|
85 | + |
|
86 | + $this->store = $store; |
|
87 | + $this->logger = $logger; |
|
88 | + $this->runner = $runner; |
|
89 | + |
|
90 | + $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
|
91 | + |
|
92 | + $this->bulk_actions = array( |
|
93 | + 'delete' => __( 'Delete', 'action-scheduler' ), |
|
94 | + ); |
|
95 | + |
|
96 | + $this->columns = array( |
|
97 | + 'hook' => __( 'Hook', 'action-scheduler' ), |
|
98 | + 'status' => __( 'Status', 'action-scheduler' ), |
|
99 | + 'args' => __( 'Arguments', 'action-scheduler' ), |
|
100 | + 'group' => __( 'Group', 'action-scheduler' ), |
|
101 | + 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
|
102 | + 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
|
103 | + 'log_entries' => __( 'Log', 'action-scheduler' ), |
|
104 | + ); |
|
105 | + |
|
106 | + $this->sort_by = array( |
|
107 | + 'schedule', |
|
108 | + 'hook', |
|
109 | + 'group', |
|
110 | + ); |
|
111 | + |
|
112 | + $this->search_by = array( |
|
113 | + 'hook', |
|
114 | + 'args', |
|
115 | + 'claim_id', |
|
116 | + ); |
|
117 | + |
|
118 | + $request_status = $this->get_request_status(); |
|
119 | + |
|
120 | + if ( empty( $request_status ) ) { |
|
121 | + $this->sort_by[] = 'status'; |
|
122 | + } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { |
|
123 | + $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
|
124 | + $this->sort_by[] = 'claim_id'; |
|
125 | + } |
|
126 | + |
|
127 | + $this->row_actions = array( |
|
128 | + 'hook' => array( |
|
129 | + 'run' => array( |
|
130 | + 'name' => __( 'Run', 'action-scheduler' ), |
|
131 | + 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
|
132 | + ), |
|
133 | + 'cancel' => array( |
|
134 | + 'name' => __( 'Cancel', 'action-scheduler' ), |
|
135 | + 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
|
136 | + 'class' => 'cancel trash', |
|
137 | + ), |
|
138 | + ), |
|
139 | + ); |
|
140 | + |
|
141 | + self::$time_periods = array( |
|
142 | + array( |
|
143 | + 'seconds' => YEAR_IN_SECONDS, |
|
144 | + /* translators: %s: amount of time */ |
|
145 | + 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
|
146 | + ), |
|
147 | + array( |
|
148 | + 'seconds' => MONTH_IN_SECONDS, |
|
149 | + /* translators: %s: amount of time */ |
|
150 | + 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
|
151 | + ), |
|
152 | + array( |
|
153 | + 'seconds' => WEEK_IN_SECONDS, |
|
154 | + /* translators: %s: amount of time */ |
|
155 | + 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
|
156 | + ), |
|
157 | + array( |
|
158 | + 'seconds' => DAY_IN_SECONDS, |
|
159 | + /* translators: %s: amount of time */ |
|
160 | + 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
|
161 | + ), |
|
162 | + array( |
|
163 | + 'seconds' => HOUR_IN_SECONDS, |
|
164 | + /* translators: %s: amount of time */ |
|
165 | + 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
|
166 | + ), |
|
167 | + array( |
|
168 | + 'seconds' => MINUTE_IN_SECONDS, |
|
169 | + /* translators: %s: amount of time */ |
|
170 | + 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
|
171 | + ), |
|
172 | + array( |
|
173 | + 'seconds' => 1, |
|
174 | + /* translators: %s: amount of time */ |
|
175 | + 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
|
176 | + ), |
|
177 | + ); |
|
178 | + |
|
179 | + parent::__construct( |
|
180 | + array( |
|
181 | + 'singular' => 'action-scheduler', |
|
182 | + 'plural' => 'action-scheduler', |
|
183 | + 'ajax' => false, |
|
184 | + ) |
|
185 | + ); |
|
186 | + |
|
187 | + add_screen_option( |
|
188 | + 'per_page', |
|
189 | + array( |
|
190 | + 'default' => $this->items_per_page, |
|
191 | + ) |
|
192 | + ); |
|
193 | + |
|
194 | + add_filter( 'set_screen_option_' . $this->get_per_page_option_name(), array( $this, 'set_items_per_page_option' ), 10, 3 ); |
|
195 | + set_screen_options(); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Handles setting the items_per_page option for this screen. |
|
200 | + * |
|
201 | + * @param mixed $status Default false (to skip saving the current option). |
|
202 | + * @param string $option Screen option name. |
|
203 | + * @param int $value Screen option value. |
|
204 | + * @return int |
|
205 | + */ |
|
206 | + public function set_items_per_page_option( $status, $option, $value ) { |
|
207 | + return $value; |
|
208 | + } |
|
209 | + /** |
|
210 | + * Convert an interval of seconds into a two part human friendly string. |
|
211 | + * |
|
212 | + * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
213 | + * even if an action is 1 day and 11 hours away, it will display "1 day". This function goes one step |
|
214 | + * further to display two degrees of accuracy. |
|
215 | + * |
|
216 | + * Inspired by the Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
217 | + * |
|
218 | + * @param int $interval A interval in seconds. |
|
219 | + * @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. |
|
220 | + * @return string A human friendly string representation of the interval. |
|
221 | + */ |
|
222 | + private static function human_interval( $interval, $periods_to_include = 2 ) { |
|
223 | + |
|
224 | + if ( $interval <= 0 ) { |
|
225 | + return __( 'Now!', 'action-scheduler' ); |
|
226 | + } |
|
227 | + |
|
228 | + $output = ''; |
|
229 | + |
|
230 | + 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++ ) { |
|
231 | + |
|
232 | + $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
|
233 | + |
|
234 | + if ( $periods_in_interval > 0 ) { |
|
235 | + if ( ! empty( $output ) ) { |
|
236 | + $output .= ' '; |
|
237 | + } |
|
238 | + $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 ); |
|
239 | + $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
|
240 | + $periods_included++; |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + return $output; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. |
|
249 | + * |
|
250 | + * @param ActionScheduler_Action $action |
|
251 | + * |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + protected function get_recurrence( $action ) { |
|
255 | + $schedule = $action->get_schedule(); |
|
256 | + if ( $schedule->is_recurring() ) { |
|
257 | + $recurrence = $schedule->get_recurrence(); |
|
258 | + |
|
259 | + if ( is_numeric( $recurrence ) ) { |
|
260 | + /* translators: %s: time interval */ |
|
261 | + return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
|
262 | + } else { |
|
263 | + return $recurrence; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + return __( 'Non-repeating', 'action-scheduler' ); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Serializes the argument of an action to render it in a human friendly format. |
|
272 | + * |
|
273 | + * @param array $row The array representation of the current row of the table |
|
274 | + * |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public function column_args( array $row ) { |
|
278 | + if ( empty( $row['args'] ) ) { |
|
279 | + return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
|
280 | + } |
|
281 | + |
|
282 | + $row_html = '<ul>'; |
|
283 | + foreach ( $row['args'] as $key => $value ) { |
|
284 | + $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); |
|
285 | + } |
|
286 | + $row_html .= '</ul>'; |
|
287 | + |
|
288 | + return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
293 | + * |
|
294 | + * @param array $row Action array. |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + public function column_log_entries( array $row ) { |
|
298 | + |
|
299 | + $log_entries_html = '<ol>'; |
|
300 | + |
|
301 | + $timezone = new DateTimezone( 'UTC' ); |
|
302 | + |
|
303 | + foreach ( $row['log_entries'] as $log_entry ) { |
|
304 | + $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
|
305 | + } |
|
306 | + |
|
307 | + $log_entries_html .= '</ol>'; |
|
308 | + |
|
309 | + return $log_entries_html; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
|
314 | + * |
|
315 | + * @param ActionScheduler_LogEntry $log_entry |
|
316 | + * @param DateTimezone $timezone |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
|
320 | + $date = $log_entry->get_date(); |
|
321 | + $date->setTimezone( $timezone ); |
|
322 | + 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() ) ); |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Only display row actions for pending actions. |
|
327 | + * |
|
328 | + * @param array $row Row to render |
|
329 | + * @param string $column_name Current row |
|
330 | + * |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + protected function maybe_render_actions( $row, $column_name ) { |
|
334 | + if ( 'pending' === strtolower( $row['status_name'] ) ) { |
|
335 | + return parent::maybe_render_actions( $row, $column_name ); |
|
336 | + } |
|
337 | + |
|
338 | + return ''; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Renders admin notifications |
|
343 | + * |
|
344 | + * Notifications: |
|
345 | + * 1. When the maximum number of tasks are being executed simultaneously. |
|
346 | + * 2. Notifications when a task is manually executed. |
|
347 | + * 3. Tables are missing. |
|
348 | + */ |
|
349 | + public function display_admin_notices() { |
|
350 | + global $wpdb; |
|
351 | + |
|
352 | + if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
|
353 | + $table_list = array( |
|
354 | + 'actionscheduler_actions', |
|
355 | + 'actionscheduler_logs', |
|
356 | + 'actionscheduler_groups', |
|
357 | + 'actionscheduler_claims', |
|
358 | + ); |
|
359 | + |
|
360 | + $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
361 | + foreach ( $table_list as $table_name ) { |
|
362 | + if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { |
|
363 | + $this->admin_notices[] = array( |
|
364 | + 'class' => 'error', |
|
365 | + 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler' ), |
|
366 | + ); |
|
367 | + $this->recreate_tables(); |
|
368 | + parent::display_admin_notices(); |
|
369 | + |
|
370 | + return; |
|
371 | + } |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + if ( $this->runner->has_maximum_concurrent_batches() ) { |
|
376 | + $claim_count = $this->store->get_claim_count(); |
|
377 | + $this->admin_notices[] = array( |
|
378 | + 'class' => 'updated', |
|
379 | + 'message' => sprintf( |
|
380 | + /* translators: %s: amount of claims */ |
|
381 | + _n( |
|
382 | + 'Maximum simultaneous queues already in progress (%s queue). No additional queues will begin processing until the current queues are complete.', |
|
383 | + 'Maximum simultaneous queues already in progress (%s queues). No additional queues will begin processing until the current queues are complete.', |
|
384 | + $claim_count, |
|
385 | + 'action-scheduler' |
|
386 | + ), |
|
387 | + $claim_count |
|
388 | + ), |
|
389 | + ); |
|
390 | + } elseif ( $this->store->has_pending_actions_due() ) { |
|
391 | + |
|
392 | + $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
|
393 | + |
|
394 | + // No lock set or lock expired |
|
395 | + if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
|
396 | + $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
|
397 | + /* translators: %s: process URL */ |
|
398 | + $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 ) ); |
|
399 | + } else { |
|
400 | + /* translators: %d: seconds */ |
|
401 | + $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
|
402 | + } |
|
403 | + |
|
404 | + $this->admin_notices[] = array( |
|
405 | + 'class' => 'notice notice-info', |
|
406 | + 'message' => $async_request_message, |
|
407 | + ); |
|
408 | + } |
|
409 | + |
|
410 | + $notification = get_transient( 'action_scheduler_admin_notice' ); |
|
411 | + |
|
412 | + if ( is_array( $notification ) ) { |
|
413 | + delete_transient( 'action_scheduler_admin_notice' ); |
|
414 | + |
|
415 | + $action = $this->store->fetch_action( $notification['action_id'] ); |
|
416 | + $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
|
417 | + if ( 1 == $notification['success'] ) { |
|
418 | + $class = 'updated'; |
|
419 | + switch ( $notification['row_action_type'] ) { |
|
420 | + case 'run': |
|
421 | + /* translators: %s: action HTML */ |
|
422 | + $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
|
423 | + break; |
|
424 | + case 'cancel': |
|
425 | + /* translators: %s: action HTML */ |
|
426 | + $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
|
427 | + break; |
|
428 | + default: |
|
429 | + /* translators: %s: action HTML */ |
|
430 | + $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
|
431 | + break; |
|
432 | + } |
|
433 | + } else { |
|
434 | + $class = 'error'; |
|
435 | + /* translators: 1: action HTML 2: action ID 3: error message */ |
|
436 | + $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'] ) ); |
|
437 | + } |
|
438 | + |
|
439 | + $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
|
440 | + |
|
441 | + $this->admin_notices[] = array( |
|
442 | + 'class' => $class, |
|
443 | + 'message' => $action_message_html, |
|
444 | + ); |
|
445 | + } |
|
446 | + |
|
447 | + parent::display_admin_notices(); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Prints the scheduled date in a human friendly format. |
|
452 | + * |
|
453 | + * @param array $row The array representation of the current row of the table |
|
454 | + * |
|
455 | + * @return string |
|
456 | + */ |
|
457 | + public function column_schedule( $row ) { |
|
458 | + return $this->get_schedule_display_string( $row['schedule'] ); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Get the scheduled date in a human friendly format. |
|
463 | + * |
|
464 | + * @param ActionScheduler_Schedule $schedule |
|
465 | + * @return string |
|
466 | + */ |
|
467 | + protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
|
468 | + |
|
469 | + $schedule_display_string = ''; |
|
470 | + |
|
471 | + if ( is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
472 | + return __( 'async', 'action-scheduler' ); |
|
473 | + } |
|
474 | + |
|
475 | + if ( ! $schedule->get_date() ) { |
|
476 | + return '0000-00-00 00:00:00'; |
|
477 | + } |
|
478 | + |
|
479 | + $next_timestamp = $schedule->get_date()->getTimestamp(); |
|
480 | + |
|
481 | + $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
|
482 | + $schedule_display_string .= '<br/>'; |
|
483 | + |
|
484 | + if ( gmdate( 'U' ) > $next_timestamp ) { |
|
485 | + /* translators: %s: date interval */ |
|
486 | + $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
|
487 | + } else { |
|
488 | + /* translators: %s: date interval */ |
|
489 | + $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
|
490 | + } |
|
491 | + |
|
492 | + return $schedule_display_string; |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Bulk delete |
|
497 | + * |
|
498 | + * Deletes actions based on their ID. This is the handler for the bulk delete. It assumes the data |
|
499 | + * properly validated by the callee and it will delete the actions without any extra validation. |
|
500 | + * |
|
501 | + * @param array $ids |
|
502 | + * @param string $ids_sql Inherited and unused |
|
503 | + */ |
|
504 | + protected function bulk_delete( array $ids, $ids_sql ) { |
|
505 | + foreach ( $ids as $id ) { |
|
506 | + $this->store->delete_action( $id ); |
|
507 | + } |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
512 | + * parameters are valid. |
|
513 | + * |
|
514 | + * @param int $action_id |
|
515 | + */ |
|
516 | + protected function row_action_cancel( $action_id ) { |
|
517 | + $this->process_row_action( $action_id, 'cancel' ); |
|
518 | + } |
|
519 | + |
|
520 | + /** |
|
521 | + * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
|
522 | + * parameters are valid. |
|
523 | + * |
|
524 | + * @param int $action_id |
|
525 | + */ |
|
526 | + protected function row_action_run( $action_id ) { |
|
527 | + $this->process_row_action( $action_id, 'run' ); |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Force the data store schema updates. |
|
532 | + */ |
|
533 | + protected function recreate_tables() { |
|
534 | + if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
|
535 | + $store = $this->store; |
|
536 | + } else { |
|
537 | + $store = new ActionScheduler_HybridStore(); |
|
538 | + } |
|
539 | + add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
|
540 | + |
|
541 | + $store_schema = new ActionScheduler_StoreSchema(); |
|
542 | + $logger_schema = new ActionScheduler_LoggerSchema(); |
|
543 | + $store_schema->register_tables( true ); |
|
544 | + $logger_schema->register_tables( true ); |
|
545 | + |
|
546 | + remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
|
547 | + } |
|
548 | + /** |
|
549 | + * Implements the logic behind processing an action once an action link is clicked on the list table. |
|
550 | + * |
|
551 | + * @param int $action_id |
|
552 | + * @param string $row_action_type The type of action to perform on the action. |
|
553 | + */ |
|
554 | + protected function process_row_action( $action_id, $row_action_type ) { |
|
555 | + try { |
|
556 | + switch ( $row_action_type ) { |
|
557 | + case 'run': |
|
558 | + $this->runner->process_action( $action_id, 'Admin List Table' ); |
|
559 | + break; |
|
560 | + case 'cancel': |
|
561 | + $this->store->cancel_action( $action_id ); |
|
562 | + break; |
|
563 | + } |
|
564 | + $success = 1; |
|
565 | + $error_message = ''; |
|
566 | + } catch ( Exception $e ) { |
|
567 | + $success = 0; |
|
568 | + $error_message = $e->getMessage(); |
|
569 | + } |
|
570 | + |
|
571 | + set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * {@inheritDoc} |
|
576 | + */ |
|
577 | + public function prepare_items() { |
|
578 | + $this->prepare_column_headers(); |
|
579 | + |
|
580 | + $per_page = $this->get_items_per_page( $this->get_per_page_option_name(), $this->items_per_page ); |
|
581 | + |
|
582 | + $query = array( |
|
583 | + 'per_page' => $per_page, |
|
584 | + 'offset' => $this->get_items_offset(), |
|
585 | + 'status' => $this->get_request_status(), |
|
586 | + 'orderby' => $this->get_request_orderby(), |
|
587 | + 'order' => $this->get_request_order(), |
|
588 | + 'search' => $this->get_request_search_query(), |
|
589 | + ); |
|
590 | + |
|
591 | + /** |
|
592 | + * Change query arguments to query for past-due actions. |
|
593 | + * Past-due actions have the 'pending' status and are in the past. |
|
594 | + * This is needed because registering 'past-due' as a status is overkill. |
|
595 | + */ |
|
596 | + if ( 'past-due' === $this->get_request_status() ) { |
|
597 | + $query['status'] = ActionScheduler_Store::STATUS_PENDING; |
|
598 | + $query['date'] = as_get_datetime_object(); |
|
599 | + } |
|
600 | + |
|
601 | + $this->items = array(); |
|
602 | + |
|
603 | + $total_items = $this->store->query_actions( $query, 'count' ); |
|
604 | + |
|
605 | + $status_labels = $this->store->get_status_labels(); |
|
606 | + |
|
607 | + foreach ( $this->store->query_actions( $query ) as $action_id ) { |
|
608 | + try { |
|
609 | + $action = $this->store->fetch_action( $action_id ); |
|
610 | + } catch ( Exception $e ) { |
|
611 | + continue; |
|
612 | + } |
|
613 | + if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
|
614 | + continue; |
|
615 | + } |
|
616 | + $this->items[ $action_id ] = array( |
|
617 | + 'ID' => $action_id, |
|
618 | + 'hook' => $action->get_hook(), |
|
619 | + 'status_name' => $this->store->get_status( $action_id ), |
|
620 | + 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
|
621 | + 'args' => $action->get_args(), |
|
622 | + 'group' => $action->get_group(), |
|
623 | + 'log_entries' => $this->logger->get_logs( $action_id ), |
|
624 | + 'claim_id' => $this->store->get_claim_id( $action_id ), |
|
625 | + 'recurrence' => $this->get_recurrence( $action ), |
|
626 | + 'schedule' => $action->get_schedule(), |
|
627 | + ); |
|
628 | + } |
|
629 | + |
|
630 | + $this->set_pagination_args( |
|
631 | + array( |
|
632 | + 'total_items' => $total_items, |
|
633 | + 'per_page' => $per_page, |
|
634 | + 'total_pages' => ceil( $total_items / $per_page ), |
|
635 | + ) |
|
636 | + ); |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Prints the available statuses so the user can click to filter. |
|
641 | + */ |
|
642 | + protected function display_filter_by_status() { |
|
643 | + $this->status_counts = $this->store->action_counts() + $this->store->extra_action_counts(); |
|
644 | + parent::display_filter_by_status(); |
|
645 | + } |
|
646 | + |
|
647 | + /** |
|
648 | + * Get the text to display in the search box on the list table. |
|
649 | + */ |
|
650 | + protected function get_search_box_button_text() { |
|
651 | + return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
|
652 | + } |
|
653 | + |
|
654 | + /** |
|
655 | + * {@inheritDoc} |
|
656 | + */ |
|
657 | + protected function get_per_page_option_name() { |
|
658 | + return str_replace( '-', '_', $this->screen->id ) . '_per_page'; |
|
659 | + } |
|
660 | 660 | } |
@@ -81,26 +81,26 @@ discard block |
||
81 | 81 | * @param ActionScheduler_Logger $logger |
82 | 82 | * @param ActionScheduler_QueueRunner $runner |
83 | 83 | */ |
84 | - public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
|
84 | + public function __construct(ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner) { |
|
85 | 85 | |
86 | 86 | $this->store = $store; |
87 | 87 | $this->logger = $logger; |
88 | 88 | $this->runner = $runner; |
89 | 89 | |
90 | - $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
|
90 | + $this->table_header = __('Scheduled Actions', 'action-scheduler'); |
|
91 | 91 | |
92 | 92 | $this->bulk_actions = array( |
93 | - 'delete' => __( 'Delete', 'action-scheduler' ), |
|
93 | + 'delete' => __('Delete', 'action-scheduler'), |
|
94 | 94 | ); |
95 | 95 | |
96 | 96 | $this->columns = array( |
97 | - 'hook' => __( 'Hook', 'action-scheduler' ), |
|
98 | - 'status' => __( 'Status', 'action-scheduler' ), |
|
99 | - 'args' => __( 'Arguments', 'action-scheduler' ), |
|
100 | - 'group' => __( 'Group', 'action-scheduler' ), |
|
101 | - 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
|
102 | - 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
|
103 | - 'log_entries' => __( 'Log', 'action-scheduler' ), |
|
97 | + 'hook' => __('Hook', 'action-scheduler'), |
|
98 | + 'status' => __('Status', 'action-scheduler'), |
|
99 | + 'args' => __('Arguments', 'action-scheduler'), |
|
100 | + 'group' => __('Group', 'action-scheduler'), |
|
101 | + 'recurrence' => __('Recurrence', 'action-scheduler'), |
|
102 | + 'schedule' => __('Scheduled Date', 'action-scheduler'), |
|
103 | + 'log_entries' => __('Log', 'action-scheduler'), |
|
104 | 104 | ); |
105 | 105 | |
106 | 106 | $this->sort_by = array( |
@@ -117,22 +117,22 @@ discard block |
||
117 | 117 | |
118 | 118 | $request_status = $this->get_request_status(); |
119 | 119 | |
120 | - if ( empty( $request_status ) ) { |
|
120 | + if (empty($request_status)) { |
|
121 | 121 | $this->sort_by[] = 'status'; |
122 | - } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { |
|
123 | - $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
|
122 | + } elseif (in_array($request_status, array('in-progress', 'failed'))) { |
|
123 | + $this->columns += array('claim_id' => __('Claim ID', 'action-scheduler')); |
|
124 | 124 | $this->sort_by[] = 'claim_id'; |
125 | 125 | } |
126 | 126 | |
127 | 127 | $this->row_actions = array( |
128 | 128 | 'hook' => array( |
129 | 129 | 'run' => array( |
130 | - 'name' => __( 'Run', 'action-scheduler' ), |
|
131 | - 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
|
130 | + 'name' => __('Run', 'action-scheduler'), |
|
131 | + 'desc' => __('Process the action now as if it were run as part of a queue', 'action-scheduler'), |
|
132 | 132 | ), |
133 | 133 | 'cancel' => array( |
134 | - 'name' => __( 'Cancel', 'action-scheduler' ), |
|
135 | - 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
|
134 | + 'name' => __('Cancel', 'action-scheduler'), |
|
135 | + 'desc' => __('Cancel the action now to avoid it being run in future', 'action-scheduler'), |
|
136 | 136 | 'class' => 'cancel trash', |
137 | 137 | ), |
138 | 138 | ), |
@@ -142,37 +142,37 @@ discard block |
||
142 | 142 | array( |
143 | 143 | 'seconds' => YEAR_IN_SECONDS, |
144 | 144 | /* translators: %s: amount of time */ |
145 | - 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
|
145 | + 'names' => _n_noop('%s year', '%s years', 'action-scheduler'), |
|
146 | 146 | ), |
147 | 147 | array( |
148 | 148 | 'seconds' => MONTH_IN_SECONDS, |
149 | 149 | /* translators: %s: amount of time */ |
150 | - 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
|
150 | + 'names' => _n_noop('%s month', '%s months', 'action-scheduler'), |
|
151 | 151 | ), |
152 | 152 | array( |
153 | 153 | 'seconds' => WEEK_IN_SECONDS, |
154 | 154 | /* translators: %s: amount of time */ |
155 | - 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
|
155 | + 'names' => _n_noop('%s week', '%s weeks', 'action-scheduler'), |
|
156 | 156 | ), |
157 | 157 | array( |
158 | 158 | 'seconds' => DAY_IN_SECONDS, |
159 | 159 | /* translators: %s: amount of time */ |
160 | - 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
|
160 | + 'names' => _n_noop('%s day', '%s days', 'action-scheduler'), |
|
161 | 161 | ), |
162 | 162 | array( |
163 | 163 | 'seconds' => HOUR_IN_SECONDS, |
164 | 164 | /* translators: %s: amount of time */ |
165 | - 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
|
165 | + 'names' => _n_noop('%s hour', '%s hours', 'action-scheduler'), |
|
166 | 166 | ), |
167 | 167 | array( |
168 | 168 | 'seconds' => MINUTE_IN_SECONDS, |
169 | 169 | /* translators: %s: amount of time */ |
170 | - 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
|
170 | + 'names' => _n_noop('%s minute', '%s minutes', 'action-scheduler'), |
|
171 | 171 | ), |
172 | 172 | array( |
173 | 173 | 'seconds' => 1, |
174 | 174 | /* translators: %s: amount of time */ |
175 | - 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
|
175 | + 'names' => _n_noop('%s second', '%s seconds', 'action-scheduler'), |
|
176 | 176 | ), |
177 | 177 | ); |
178 | 178 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | ) |
192 | 192 | ); |
193 | 193 | |
194 | - add_filter( 'set_screen_option_' . $this->get_per_page_option_name(), array( $this, 'set_items_per_page_option' ), 10, 3 ); |
|
194 | + add_filter('set_screen_option_'.$this->get_per_page_option_name(), array($this, 'set_items_per_page_option'), 10, 3); |
|
195 | 195 | set_screen_options(); |
196 | 196 | } |
197 | 197 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @param int $value Screen option value. |
204 | 204 | * @return int |
205 | 205 | */ |
206 | - public function set_items_per_page_option( $status, $option, $value ) { |
|
206 | + public function set_items_per_page_option($status, $option, $value) { |
|
207 | 207 | return $value; |
208 | 208 | } |
209 | 209 | /** |
@@ -219,24 +219,24 @@ discard block |
||
219 | 219 | * @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. |
220 | 220 | * @return string A human friendly string representation of the interval. |
221 | 221 | */ |
222 | - private static function human_interval( $interval, $periods_to_include = 2 ) { |
|
222 | + private static function human_interval($interval, $periods_to_include = 2) { |
|
223 | 223 | |
224 | - if ( $interval <= 0 ) { |
|
225 | - return __( 'Now!', 'action-scheduler' ); |
|
224 | + if ($interval <= 0) { |
|
225 | + return __('Now!', 'action-scheduler'); |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | $output = ''; |
229 | 229 | |
230 | - 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++ ) { |
|
230 | + 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++) { |
|
231 | 231 | |
232 | - $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
|
232 | + $periods_in_interval = floor($seconds_remaining / self::$time_periods[$time_period_index]['seconds']); |
|
233 | 233 | |
234 | - if ( $periods_in_interval > 0 ) { |
|
235 | - if ( ! empty( $output ) ) { |
|
234 | + if ($periods_in_interval > 0) { |
|
235 | + if ( ! empty($output)) { |
|
236 | 236 | $output .= ' '; |
237 | 237 | } |
238 | - $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 ); |
|
239 | - $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
|
238 | + $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); |
|
239 | + $seconds_remaining -= $periods_in_interval * self::$time_periods[$time_period_index]['seconds']; |
|
240 | 240 | $periods_included++; |
241 | 241 | } |
242 | 242 | } |
@@ -251,20 +251,20 @@ discard block |
||
251 | 251 | * |
252 | 252 | * @return string |
253 | 253 | */ |
254 | - protected function get_recurrence( $action ) { |
|
254 | + protected function get_recurrence($action) { |
|
255 | 255 | $schedule = $action->get_schedule(); |
256 | - if ( $schedule->is_recurring() ) { |
|
256 | + if ($schedule->is_recurring()) { |
|
257 | 257 | $recurrence = $schedule->get_recurrence(); |
258 | 258 | |
259 | - if ( is_numeric( $recurrence ) ) { |
|
259 | + if (is_numeric($recurrence)) { |
|
260 | 260 | /* translators: %s: time interval */ |
261 | - return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
|
261 | + return sprintf(__('Every %s', 'action-scheduler'), self::human_interval($recurrence)); |
|
262 | 262 | } else { |
263 | 263 | return $recurrence; |
264 | 264 | } |
265 | 265 | } |
266 | 266 | |
267 | - return __( 'Non-repeating', 'action-scheduler' ); |
|
267 | + return __('Non-repeating', 'action-scheduler'); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /** |
@@ -274,18 +274,18 @@ discard block |
||
274 | 274 | * |
275 | 275 | * @return string |
276 | 276 | */ |
277 | - public function column_args( array $row ) { |
|
278 | - if ( empty( $row['args'] ) ) { |
|
279 | - return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
|
277 | + public function column_args(array $row) { |
|
278 | + if (empty($row['args'])) { |
|
279 | + return apply_filters('action_scheduler_list_table_column_args', '', $row); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | $row_html = '<ul>'; |
283 | - foreach ( $row['args'] as $key => $value ) { |
|
284 | - $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); |
|
283 | + foreach ($row['args'] as $key => $value) { |
|
284 | + $row_html .= sprintf('<li><code>%s => %s</code></li>', esc_html(var_export($key, true)), esc_html(var_export($value, true))); |
|
285 | 285 | } |
286 | 286 | $row_html .= '</ul>'; |
287 | 287 | |
288 | - return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
|
288 | + return apply_filters('action_scheduler_list_table_column_args', $row_html, $row); |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | /** |
@@ -294,14 +294,14 @@ discard block |
||
294 | 294 | * @param array $row Action array. |
295 | 295 | * @return string |
296 | 296 | */ |
297 | - public function column_log_entries( array $row ) { |
|
297 | + public function column_log_entries(array $row) { |
|
298 | 298 | |
299 | 299 | $log_entries_html = '<ol>'; |
300 | 300 | |
301 | - $timezone = new DateTimezone( 'UTC' ); |
|
301 | + $timezone = new DateTimezone('UTC'); |
|
302 | 302 | |
303 | - foreach ( $row['log_entries'] as $log_entry ) { |
|
304 | - $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
|
303 | + foreach ($row['log_entries'] as $log_entry) { |
|
304 | + $log_entries_html .= $this->get_log_entry_html($log_entry, $timezone); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | $log_entries_html .= '</ol>'; |
@@ -316,10 +316,10 @@ discard block |
||
316 | 316 | * @param DateTimezone $timezone |
317 | 317 | * @return string |
318 | 318 | */ |
319 | - protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
|
319 | + protected function get_log_entry_html(ActionScheduler_LogEntry $log_entry, DateTimezone $timezone) { |
|
320 | 320 | $date = $log_entry->get_date(); |
321 | - $date->setTimezone( $timezone ); |
|
322 | - 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() ) ); |
|
321 | + $date->setTimezone($timezone); |
|
322 | + 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())); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | /** |
@@ -330,9 +330,9 @@ discard block |
||
330 | 330 | * |
331 | 331 | * @return string |
332 | 332 | */ |
333 | - protected function maybe_render_actions( $row, $column_name ) { |
|
334 | - if ( 'pending' === strtolower( $row['status_name'] ) ) { |
|
335 | - return parent::maybe_render_actions( $row, $column_name ); |
|
333 | + protected function maybe_render_actions($row, $column_name) { |
|
334 | + if ('pending' === strtolower($row['status_name'])) { |
|
335 | + return parent::maybe_render_actions($row, $column_name); |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | return ''; |
@@ -349,7 +349,7 @@ discard block |
||
349 | 349 | public function display_admin_notices() { |
350 | 350 | global $wpdb; |
351 | 351 | |
352 | - if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
|
352 | + if ((is_a($this->store, 'ActionScheduler_HybridStore') || is_a($this->store, 'ActionScheduler_DBStore')) && apply_filters('action_scheduler_enable_recreate_data_store', true)) { |
|
353 | 353 | $table_list = array( |
354 | 354 | 'actionscheduler_actions', |
355 | 355 | 'actionscheduler_logs', |
@@ -357,12 +357,12 @@ discard block |
||
357 | 357 | 'actionscheduler_claims', |
358 | 358 | ); |
359 | 359 | |
360 | - $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
361 | - foreach ( $table_list as $table_name ) { |
|
362 | - if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { |
|
360 | + $found_tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
361 | + foreach ($table_list as $table_name) { |
|
362 | + if ( ! in_array($wpdb->prefix.$table_name, $found_tables)) { |
|
363 | 363 | $this->admin_notices[] = array( |
364 | 364 | 'class' => 'error', |
365 | - 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler' ), |
|
365 | + 'message' => __('It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler'), |
|
366 | 366 | ); |
367 | 367 | $this->recreate_tables(); |
368 | 368 | parent::display_admin_notices(); |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | } |
373 | 373 | } |
374 | 374 | |
375 | - if ( $this->runner->has_maximum_concurrent_batches() ) { |
|
375 | + if ($this->runner->has_maximum_concurrent_batches()) { |
|
376 | 376 | $claim_count = $this->store->get_claim_count(); |
377 | 377 | $this->admin_notices[] = array( |
378 | 378 | 'class' => 'updated', |
@@ -387,18 +387,18 @@ discard block |
||
387 | 387 | $claim_count |
388 | 388 | ), |
389 | 389 | ); |
390 | - } elseif ( $this->store->has_pending_actions_due() ) { |
|
390 | + } elseif ($this->store->has_pending_actions_due()) { |
|
391 | 391 | |
392 | - $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
|
392 | + $async_request_lock_expiration = ActionScheduler::lock()->get_expiration('async-request-runner'); |
|
393 | 393 | |
394 | 394 | // No lock set or lock expired |
395 | - if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
|
396 | - $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
|
395 | + if (false === $async_request_lock_expiration || $async_request_lock_expiration < time()) { |
|
396 | + $in_progress_url = add_query_arg('status', 'in-progress', remove_query_arg('status')); |
|
397 | 397 | /* translators: %s: process URL */ |
398 | - $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 ) ); |
|
398 | + $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)); |
|
399 | 399 | } else { |
400 | 400 | /* translators: %d: seconds */ |
401 | - $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
|
401 | + $async_request_message = sprintf(__('The next queue will begin processing in approximately %d seconds.', 'action-scheduler'), $async_request_lock_expiration - time()); |
|
402 | 402 | } |
403 | 403 | |
404 | 404 | $this->admin_notices[] = array( |
@@ -407,36 +407,36 @@ discard block |
||
407 | 407 | ); |
408 | 408 | } |
409 | 409 | |
410 | - $notification = get_transient( 'action_scheduler_admin_notice' ); |
|
410 | + $notification = get_transient('action_scheduler_admin_notice'); |
|
411 | 411 | |
412 | - if ( is_array( $notification ) ) { |
|
413 | - delete_transient( 'action_scheduler_admin_notice' ); |
|
412 | + if (is_array($notification)) { |
|
413 | + delete_transient('action_scheduler_admin_notice'); |
|
414 | 414 | |
415 | - $action = $this->store->fetch_action( $notification['action_id'] ); |
|
416 | - $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
|
417 | - if ( 1 == $notification['success'] ) { |
|
415 | + $action = $this->store->fetch_action($notification['action_id']); |
|
416 | + $action_hook_html = '<strong><code>'.$action->get_hook().'</code></strong>'; |
|
417 | + if (1 == $notification['success']) { |
|
418 | 418 | $class = 'updated'; |
419 | - switch ( $notification['row_action_type'] ) { |
|
419 | + switch ($notification['row_action_type']) { |
|
420 | 420 | case 'run': |
421 | 421 | /* translators: %s: action HTML */ |
422 | - $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
|
422 | + $action_message_html = sprintf(__('Successfully executed action: %s', 'action-scheduler'), $action_hook_html); |
|
423 | 423 | break; |
424 | 424 | case 'cancel': |
425 | 425 | /* translators: %s: action HTML */ |
426 | - $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
|
426 | + $action_message_html = sprintf(__('Successfully canceled action: %s', 'action-scheduler'), $action_hook_html); |
|
427 | 427 | break; |
428 | 428 | default: |
429 | 429 | /* translators: %s: action HTML */ |
430 | - $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
|
430 | + $action_message_html = sprintf(__('Successfully processed change for action: %s', 'action-scheduler'), $action_hook_html); |
|
431 | 431 | break; |
432 | 432 | } |
433 | 433 | } else { |
434 | 434 | $class = 'error'; |
435 | 435 | /* translators: 1: action HTML 2: action ID 3: error message */ |
436 | - $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'] ) ); |
|
436 | + $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'])); |
|
437 | 437 | } |
438 | 438 | |
439 | - $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
|
439 | + $action_message_html = apply_filters('action_scheduler_admin_notice_html', $action_message_html, $action, $notification); |
|
440 | 440 | |
441 | 441 | $this->admin_notices[] = array( |
442 | 442 | 'class' => $class, |
@@ -454,8 +454,8 @@ discard block |
||
454 | 454 | * |
455 | 455 | * @return string |
456 | 456 | */ |
457 | - public function column_schedule( $row ) { |
|
458 | - return $this->get_schedule_display_string( $row['schedule'] ); |
|
457 | + public function column_schedule($row) { |
|
458 | + return $this->get_schedule_display_string($row['schedule']); |
|
459 | 459 | } |
460 | 460 | |
461 | 461 | /** |
@@ -464,29 +464,29 @@ discard block |
||
464 | 464 | * @param ActionScheduler_Schedule $schedule |
465 | 465 | * @return string |
466 | 466 | */ |
467 | - protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
|
467 | + protected function get_schedule_display_string(ActionScheduler_Schedule $schedule) { |
|
468 | 468 | |
469 | 469 | $schedule_display_string = ''; |
470 | 470 | |
471 | - if ( is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
472 | - return __( 'async', 'action-scheduler' ); |
|
471 | + if (is_a($schedule, 'ActionScheduler_NullSchedule')) { |
|
472 | + return __('async', 'action-scheduler'); |
|
473 | 473 | } |
474 | 474 | |
475 | - if ( ! $schedule->get_date() ) { |
|
475 | + if ( ! $schedule->get_date()) { |
|
476 | 476 | return '0000-00-00 00:00:00'; |
477 | 477 | } |
478 | 478 | |
479 | 479 | $next_timestamp = $schedule->get_date()->getTimestamp(); |
480 | 480 | |
481 | - $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
|
481 | + $schedule_display_string .= $schedule->get_date()->format('Y-m-d H:i:s O'); |
|
482 | 482 | $schedule_display_string .= '<br/>'; |
483 | 483 | |
484 | - if ( gmdate( 'U' ) > $next_timestamp ) { |
|
484 | + if (gmdate('U') > $next_timestamp) { |
|
485 | 485 | /* translators: %s: date interval */ |
486 | - $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
|
486 | + $schedule_display_string .= sprintf(__(' (%s ago)', 'action-scheduler'), self::human_interval(gmdate('U') - $next_timestamp)); |
|
487 | 487 | } else { |
488 | 488 | /* translators: %s: date interval */ |
489 | - $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
|
489 | + $schedule_display_string .= sprintf(__(' (%s)', 'action-scheduler'), self::human_interval($next_timestamp - gmdate('U'))); |
|
490 | 490 | } |
491 | 491 | |
492 | 492 | return $schedule_display_string; |
@@ -501,9 +501,9 @@ discard block |
||
501 | 501 | * @param array $ids |
502 | 502 | * @param string $ids_sql Inherited and unused |
503 | 503 | */ |
504 | - protected function bulk_delete( array $ids, $ids_sql ) { |
|
505 | - foreach ( $ids as $id ) { |
|
506 | - $this->store->delete_action( $id ); |
|
504 | + protected function bulk_delete(array $ids, $ids_sql) { |
|
505 | + foreach ($ids as $id) { |
|
506 | + $this->store->delete_action($id); |
|
507 | 507 | } |
508 | 508 | } |
509 | 509 | |
@@ -513,8 +513,8 @@ discard block |
||
513 | 513 | * |
514 | 514 | * @param int $action_id |
515 | 515 | */ |
516 | - protected function row_action_cancel( $action_id ) { |
|
517 | - $this->process_row_action( $action_id, 'cancel' ); |
|
516 | + protected function row_action_cancel($action_id) { |
|
517 | + $this->process_row_action($action_id, 'cancel'); |
|
518 | 518 | } |
519 | 519 | |
520 | 520 | /** |
@@ -523,27 +523,27 @@ discard block |
||
523 | 523 | * |
524 | 524 | * @param int $action_id |
525 | 525 | */ |
526 | - protected function row_action_run( $action_id ) { |
|
527 | - $this->process_row_action( $action_id, 'run' ); |
|
526 | + protected function row_action_run($action_id) { |
|
527 | + $this->process_row_action($action_id, 'run'); |
|
528 | 528 | } |
529 | 529 | |
530 | 530 | /** |
531 | 531 | * Force the data store schema updates. |
532 | 532 | */ |
533 | 533 | protected function recreate_tables() { |
534 | - if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
|
534 | + if (is_a($this->store, 'ActionScheduler_HybridStore')) { |
|
535 | 535 | $store = $this->store; |
536 | 536 | } else { |
537 | 537 | $store = new ActionScheduler_HybridStore(); |
538 | 538 | } |
539 | - add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
|
539 | + add_action('action_scheduler/created_table', array($store, 'set_autoincrement'), 10, 2); |
|
540 | 540 | |
541 | 541 | $store_schema = new ActionScheduler_StoreSchema(); |
542 | 542 | $logger_schema = new ActionScheduler_LoggerSchema(); |
543 | - $store_schema->register_tables( true ); |
|
544 | - $logger_schema->register_tables( true ); |
|
543 | + $store_schema->register_tables(true); |
|
544 | + $logger_schema->register_tables(true); |
|
545 | 545 | |
546 | - remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
|
546 | + remove_action('action_scheduler/created_table', array($store, 'set_autoincrement'), 10); |
|
547 | 547 | } |
548 | 548 | /** |
549 | 549 | * Implements the logic behind processing an action once an action link is clicked on the list table. |
@@ -551,24 +551,24 @@ discard block |
||
551 | 551 | * @param int $action_id |
552 | 552 | * @param string $row_action_type The type of action to perform on the action. |
553 | 553 | */ |
554 | - protected function process_row_action( $action_id, $row_action_type ) { |
|
554 | + protected function process_row_action($action_id, $row_action_type) { |
|
555 | 555 | try { |
556 | - switch ( $row_action_type ) { |
|
556 | + switch ($row_action_type) { |
|
557 | 557 | case 'run': |
558 | - $this->runner->process_action( $action_id, 'Admin List Table' ); |
|
558 | + $this->runner->process_action($action_id, 'Admin List Table'); |
|
559 | 559 | break; |
560 | 560 | case 'cancel': |
561 | - $this->store->cancel_action( $action_id ); |
|
561 | + $this->store->cancel_action($action_id); |
|
562 | 562 | break; |
563 | 563 | } |
564 | 564 | $success = 1; |
565 | 565 | $error_message = ''; |
566 | - } catch ( Exception $e ) { |
|
566 | + } catch (Exception $e) { |
|
567 | 567 | $success = 0; |
568 | 568 | $error_message = $e->getMessage(); |
569 | 569 | } |
570 | 570 | |
571 | - set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
|
571 | + set_transient('action_scheduler_admin_notice', compact('action_id', 'success', 'error_message', 'row_action_type'), 30); |
|
572 | 572 | } |
573 | 573 | |
574 | 574 | /** |
@@ -577,7 +577,7 @@ discard block |
||
577 | 577 | public function prepare_items() { |
578 | 578 | $this->prepare_column_headers(); |
579 | 579 | |
580 | - $per_page = $this->get_items_per_page( $this->get_per_page_option_name(), $this->items_per_page ); |
|
580 | + $per_page = $this->get_items_per_page($this->get_per_page_option_name(), $this->items_per_page); |
|
581 | 581 | |
582 | 582 | $query = array( |
583 | 583 | 'per_page' => $per_page, |
@@ -593,36 +593,36 @@ discard block |
||
593 | 593 | * Past-due actions have the 'pending' status and are in the past. |
594 | 594 | * This is needed because registering 'past-due' as a status is overkill. |
595 | 595 | */ |
596 | - if ( 'past-due' === $this->get_request_status() ) { |
|
596 | + if ('past-due' === $this->get_request_status()) { |
|
597 | 597 | $query['status'] = ActionScheduler_Store::STATUS_PENDING; |
598 | 598 | $query['date'] = as_get_datetime_object(); |
599 | 599 | } |
600 | 600 | |
601 | 601 | $this->items = array(); |
602 | 602 | |
603 | - $total_items = $this->store->query_actions( $query, 'count' ); |
|
603 | + $total_items = $this->store->query_actions($query, 'count'); |
|
604 | 604 | |
605 | 605 | $status_labels = $this->store->get_status_labels(); |
606 | 606 | |
607 | - foreach ( $this->store->query_actions( $query ) as $action_id ) { |
|
607 | + foreach ($this->store->query_actions($query) as $action_id) { |
|
608 | 608 | try { |
609 | - $action = $this->store->fetch_action( $action_id ); |
|
610 | - } catch ( Exception $e ) { |
|
609 | + $action = $this->store->fetch_action($action_id); |
|
610 | + } catch (Exception $e) { |
|
611 | 611 | continue; |
612 | 612 | } |
613 | - if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
|
613 | + if (is_a($action, 'ActionScheduler_NullAction')) { |
|
614 | 614 | continue; |
615 | 615 | } |
616 | - $this->items[ $action_id ] = array( |
|
616 | + $this->items[$action_id] = array( |
|
617 | 617 | 'ID' => $action_id, |
618 | 618 | 'hook' => $action->get_hook(), |
619 | - 'status_name' => $this->store->get_status( $action_id ), |
|
620 | - 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
|
619 | + 'status_name' => $this->store->get_status($action_id), |
|
620 | + 'status' => $status_labels[$this->store->get_status($action_id)], |
|
621 | 621 | 'args' => $action->get_args(), |
622 | 622 | 'group' => $action->get_group(), |
623 | - 'log_entries' => $this->logger->get_logs( $action_id ), |
|
624 | - 'claim_id' => $this->store->get_claim_id( $action_id ), |
|
625 | - 'recurrence' => $this->get_recurrence( $action ), |
|
623 | + 'log_entries' => $this->logger->get_logs($action_id), |
|
624 | + 'claim_id' => $this->store->get_claim_id($action_id), |
|
625 | + 'recurrence' => $this->get_recurrence($action), |
|
626 | 626 | 'schedule' => $action->get_schedule(), |
627 | 627 | ); |
628 | 628 | } |
@@ -631,7 +631,7 @@ discard block |
||
631 | 631 | array( |
632 | 632 | 'total_items' => $total_items, |
633 | 633 | 'per_page' => $per_page, |
634 | - 'total_pages' => ceil( $total_items / $per_page ), |
|
634 | + 'total_pages' => ceil($total_items / $per_page), |
|
635 | 635 | ) |
636 | 636 | ); |
637 | 637 | } |
@@ -648,13 +648,13 @@ discard block |
||
648 | 648 | * Get the text to display in the search box on the list table. |
649 | 649 | */ |
650 | 650 | protected function get_search_box_button_text() { |
651 | - return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
|
651 | + return __('Search hook, args and claim ID', 'action-scheduler'); |
|
652 | 652 | } |
653 | 653 | |
654 | 654 | /** |
655 | 655 | * {@inheritDoc} |
656 | 656 | */ |
657 | 657 | protected function get_per_page_option_name() { |
658 | - return str_replace( '-', '_', $this->screen->id ) . '_per_page'; |
|
658 | + return str_replace('-', '_', $this->screen->id).'_per_page'; |
|
659 | 659 | } |
660 | 660 | } |
@@ -4,59 +4,59 @@ |
||
4 | 4 | * Class ActionScheduler_Versions |
5 | 5 | */ |
6 | 6 | class ActionScheduler_Versions { |
7 | - /** |
|
8 | - * @var ActionScheduler_Versions |
|
9 | - */ |
|
10 | - private static $instance = null; |
|
11 | - |
|
12 | - private $versions = array(); |
|
13 | - |
|
14 | - public function register( $version_string, $initialization_callback ) { |
|
15 | - if ( isset( $this->versions[ $version_string ] ) ) { |
|
16 | - return false; |
|
17 | - } |
|
18 | - $this->versions[ $version_string ] = $initialization_callback; |
|
19 | - return true; |
|
20 | - } |
|
21 | - |
|
22 | - public function get_versions() { |
|
23 | - return $this->versions; |
|
24 | - } |
|
25 | - |
|
26 | - public function latest_version() { |
|
27 | - $keys = array_keys( $this->versions ); |
|
28 | - if ( empty( $keys ) ) { |
|
29 | - return false; |
|
30 | - } |
|
31 | - uasort( $keys, 'version_compare' ); |
|
32 | - return end( $keys ); |
|
33 | - } |
|
34 | - |
|
35 | - public function latest_version_callback() { |
|
36 | - $latest = $this->latest_version(); |
|
37 | - if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { |
|
38 | - return '__return_null'; |
|
39 | - } |
|
40 | - return $this->versions[ $latest ]; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @return ActionScheduler_Versions |
|
45 | - * @codeCoverageIgnore |
|
46 | - */ |
|
47 | - public static function instance() { |
|
48 | - if ( empty( self::$instance ) ) { |
|
49 | - self::$instance = new self(); |
|
50 | - } |
|
51 | - return self::$instance; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @codeCoverageIgnore |
|
56 | - */ |
|
57 | - public static function initialize_latest_version() { |
|
58 | - $self = self::instance(); |
|
59 | - call_user_func( $self->latest_version_callback() ); |
|
60 | - } |
|
7 | + /** |
|
8 | + * @var ActionScheduler_Versions |
|
9 | + */ |
|
10 | + private static $instance = null; |
|
11 | + |
|
12 | + private $versions = array(); |
|
13 | + |
|
14 | + public function register( $version_string, $initialization_callback ) { |
|
15 | + if ( isset( $this->versions[ $version_string ] ) ) { |
|
16 | + return false; |
|
17 | + } |
|
18 | + $this->versions[ $version_string ] = $initialization_callback; |
|
19 | + return true; |
|
20 | + } |
|
21 | + |
|
22 | + public function get_versions() { |
|
23 | + return $this->versions; |
|
24 | + } |
|
25 | + |
|
26 | + public function latest_version() { |
|
27 | + $keys = array_keys( $this->versions ); |
|
28 | + if ( empty( $keys ) ) { |
|
29 | + return false; |
|
30 | + } |
|
31 | + uasort( $keys, 'version_compare' ); |
|
32 | + return end( $keys ); |
|
33 | + } |
|
34 | + |
|
35 | + public function latest_version_callback() { |
|
36 | + $latest = $this->latest_version(); |
|
37 | + if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { |
|
38 | + return '__return_null'; |
|
39 | + } |
|
40 | + return $this->versions[ $latest ]; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @return ActionScheduler_Versions |
|
45 | + * @codeCoverageIgnore |
|
46 | + */ |
|
47 | + public static function instance() { |
|
48 | + if ( empty( self::$instance ) ) { |
|
49 | + self::$instance = new self(); |
|
50 | + } |
|
51 | + return self::$instance; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @codeCoverageIgnore |
|
56 | + */ |
|
57 | + public static function initialize_latest_version() { |
|
58 | + $self = self::instance(); |
|
59 | + call_user_func( $self->latest_version_callback() ); |
|
60 | + } |
|
61 | 61 | } |
62 | 62 |
@@ -11,11 +11,11 @@ discard block |
||
11 | 11 | |
12 | 12 | private $versions = array(); |
13 | 13 | |
14 | - public function register( $version_string, $initialization_callback ) { |
|
15 | - if ( isset( $this->versions[ $version_string ] ) ) { |
|
14 | + public function register($version_string, $initialization_callback) { |
|
15 | + if (isset($this->versions[$version_string])) { |
|
16 | 16 | return false; |
17 | 17 | } |
18 | - $this->versions[ $version_string ] = $initialization_callback; |
|
18 | + $this->versions[$version_string] = $initialization_callback; |
|
19 | 19 | return true; |
20 | 20 | } |
21 | 21 | |
@@ -24,20 +24,20 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | public function latest_version() { |
27 | - $keys = array_keys( $this->versions ); |
|
28 | - if ( empty( $keys ) ) { |
|
27 | + $keys = array_keys($this->versions); |
|
28 | + if (empty($keys)) { |
|
29 | 29 | return false; |
30 | 30 | } |
31 | - uasort( $keys, 'version_compare' ); |
|
32 | - return end( $keys ); |
|
31 | + uasort($keys, 'version_compare'); |
|
32 | + return end($keys); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | public function latest_version_callback() { |
36 | 36 | $latest = $this->latest_version(); |
37 | - if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { |
|
37 | + if (empty($latest) || ! isset($this->versions[$latest])) { |
|
38 | 38 | return '__return_null'; |
39 | 39 | } |
40 | - return $this->versions[ $latest ]; |
|
40 | + return $this->versions[$latest]; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @codeCoverageIgnore |
46 | 46 | */ |
47 | 47 | public static function instance() { |
48 | - if ( empty( self::$instance ) ) { |
|
48 | + if (empty(self::$instance)) { |
|
49 | 49 | self::$instance = new self(); |
50 | 50 | } |
51 | 51 | return self::$instance; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | */ |
57 | 57 | public static function initialize_latest_version() { |
58 | 58 | $self = self::instance(); |
59 | - call_user_func( $self->latest_version_callback() ); |
|
59 | + call_user_func($self->latest_version_callback()); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 |
@@ -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 | } |
@@ -41,10 +41,10 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @throws Exception When this is not run within WP CLI |
43 | 43 | */ |
44 | - public function __construct( $message, $count, $interval = 100 ) { |
|
45 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
44 | + public function __construct($message, $count, $interval = 100) { |
|
45 | + if ( ! (defined('WP_CLI') && WP_CLI)) { |
|
46 | 46 | /* translators: %s php class name */ |
47 | - throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
47 | + throw new \Exception(sprintf(__('The %s class can only be run within WP CLI.', 'action-scheduler'), __CLASS__)); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | $this->total_ticks = 0; |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | * Increment the progress bar ticks. |
58 | 58 | */ |
59 | 59 | public function tick() { |
60 | - if ( null === $this->progress_bar ) { |
|
60 | + if (null === $this->progress_bar) { |
|
61 | 61 | $this->setup_progress_bar(); |
62 | 62 | } |
63 | 63 | |
64 | 64 | $this->progress_bar->tick(); |
65 | 65 | $this->total_ticks++; |
66 | 66 | |
67 | - do_action( 'action_scheduler/progress_tick', $this->total_ticks ); |
|
67 | + do_action('action_scheduler/progress_tick', $this->total_ticks); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * Finish the current progress bar. |
81 | 81 | */ |
82 | 82 | public function finish() { |
83 | - if ( null !== $this->progress_bar ) { |
|
83 | + if (null !== $this->progress_bar) { |
|
84 | 84 | $this->progress_bar->finish(); |
85 | 85 | } |
86 | 86 | |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @param string $message The message to be used when the next progress bar is created. |
94 | 94 | */ |
95 | - public function set_message( $message ) { |
|
95 | + public function set_message($message) { |
|
96 | 96 | $this->message = $message; |
97 | 97 | } |
98 | 98 | |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @param integer $count The total number of ticks expected to complete. |
103 | 103 | */ |
104 | - public function set_count( $count ) { |
|
104 | + public function set_count($count) { |
|
105 | 105 | $this->count = $count; |
106 | 106 | $this->finish(); |
107 | 107 | } |
@@ -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 | } |
@@ -27,13 +27,13 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @throws Exception When this is not run within WP CLI |
29 | 29 | */ |
30 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
31 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
30 | + public function __construct(ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null) { |
|
31 | + if ( ! (defined('WP_CLI') && WP_CLI)) { |
|
32 | 32 | /* translators: %s php class name */ |
33 | - throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
|
33 | + throw new Exception(sprintf(__('The %s class can only be run within WP CLI.', 'action-scheduler'), __CLASS__)); |
|
34 | 34 | } |
35 | 35 | |
36 | - parent::__construct( $store, $monitor, $cleaner ); |
|
36 | + parent::__construct($store, $monitor, $cleaner); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
@@ -49,25 +49,25 @@ discard block |
||
49 | 49 | * @return int The number of actions that will be run. |
50 | 50 | * @throws \WP_CLI\ExitException When there are too many concurrent batches. |
51 | 51 | */ |
52 | - public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) { |
|
52 | + public function setup($batch_size, $hooks = array(), $group = '', $force = false) { |
|
53 | 53 | $this->run_cleanup(); |
54 | 54 | $this->add_hooks(); |
55 | 55 | |
56 | 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' ) ); |
|
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 | 60 | } else { |
61 | - WP_CLI::error( __( 'There are too many concurrent batches.', 'action-scheduler' ) ); |
|
61 | + WP_CLI::error(__('There are too many concurrent batches.', 'action-scheduler')); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
65 | 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 ); |
|
66 | + $this->claim = $this->store->stake_claim($batch_size, null, $hooks, $group); |
|
67 | + $this->monitor->attach($this->claim); |
|
68 | 68 | $this->actions = $this->claim->get_actions(); |
69 | 69 | |
70 | - return count( $this->actions ); |
|
70 | + return count($this->actions); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | * @author Jeremy Pry |
77 | 77 | */ |
78 | 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 ); |
|
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 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -87,10 +87,10 @@ discard block |
||
87 | 87 | * @author Jeremy Pry |
88 | 88 | */ |
89 | 89 | protected function setup_progress_bar() { |
90 | - $count = count( $this->actions ); |
|
90 | + $count = count($this->actions); |
|
91 | 91 | $this->progress_bar = new ProgressBar( |
92 | 92 | /* translators: %d: amount of actions */ |
93 | - sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ), |
|
93 | + sprintf(_n('Running %d action', 'Running %d actions', $count, 'action-scheduler'), number_format_i18n($count)), |
|
94 | 94 | $count |
95 | 95 | ); |
96 | 96 | } |
@@ -104,24 +104,24 @@ discard block |
||
104 | 104 | * |
105 | 105 | * @return int The number of actions processed. |
106 | 106 | */ |
107 | - public function run( $context = 'WP CLI' ) { |
|
108 | - do_action( 'action_scheduler_before_process_queue' ); |
|
107 | + public function run($context = 'WP CLI') { |
|
108 | + do_action('action_scheduler_before_process_queue'); |
|
109 | 109 | $this->setup_progress_bar(); |
110 | - foreach ( $this->actions as $action_id ) { |
|
110 | + foreach ($this->actions as $action_id) { |
|
111 | 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' ) ); |
|
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 | 114 | break; |
115 | 115 | } |
116 | 116 | |
117 | - $this->process_action( $action_id, $context ); |
|
117 | + $this->process_action($action_id, $context); |
|
118 | 118 | $this->progress_bar->tick(); |
119 | 119 | } |
120 | 120 | |
121 | 121 | $completed = $this->progress_bar->current(); |
122 | 122 | $this->progress_bar->finish(); |
123 | - $this->store->release_claim( $this->claim ); |
|
124 | - do_action( 'action_scheduler_after_process_queue' ); |
|
123 | + $this->store->release_claim($this->claim); |
|
124 | + do_action('action_scheduler_after_process_queue'); |
|
125 | 125 | |
126 | 126 | return $completed; |
127 | 127 | } |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | * |
134 | 134 | * @param $action_id |
135 | 135 | */ |
136 | - public function before_execute( $action_id ) { |
|
136 | + public function before_execute($action_id) { |
|
137 | 137 | /* translators: %s refers to the action ID */ |
138 | - WP_CLI::log( sprintf( __( 'Started processing action %s', 'action-scheduler' ), $action_id ) ); |
|
138 | + WP_CLI::log(sprintf(__('Started processing action %s', 'action-scheduler'), $action_id)); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -146,13 +146,13 @@ discard block |
||
146 | 146 | * @param int $action_id |
147 | 147 | * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility. |
148 | 148 | */ |
149 | - public function after_execute( $action_id, $action = null ) { |
|
149 | + public function after_execute($action_id, $action = null) { |
|
150 | 150 | // backward compatibility |
151 | - if ( null === $action ) { |
|
152 | - $action = $this->store->fetch_action( $action_id ); |
|
151 | + if (null === $action) { |
|
152 | + $action = $this->store->fetch_action($action_id); |
|
153 | 153 | } |
154 | 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() ) ); |
|
155 | + WP_CLI::log(sprintf(__('Completed processing action %1$s with hook: %2$s', 'action-scheduler'), $action_id, $action->get_hook())); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | /** |
@@ -164,10 +164,10 @@ discard block |
||
164 | 164 | * @param Exception $exception |
165 | 165 | * @throws \WP_CLI\ExitException With failure message. |
166 | 166 | */ |
167 | - public function action_failed( $action_id, $exception ) { |
|
167 | + public function action_failed($action_id, $exception) { |
|
168 | 168 | WP_CLI::error( |
169 | 169 | /* translators: 1: action ID 2: exception message */ |
170 | - sprintf( __( 'Error processing action %1$s: %2$s', 'action-scheduler' ), $action_id, $exception->getMessage() ), |
|
170 | + sprintf(__('Error processing action %1$s: %2$s', 'action-scheduler'), $action_id, $exception->getMessage()), |
|
171 | 171 | false |
172 | 172 | ); |
173 | 173 | } |
@@ -178,8 +178,8 @@ discard block |
||
178 | 178 | * @param int $sleep_time Amount of seconds to sleep |
179 | 179 | * @deprecated 3.0.0 |
180 | 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' ); |
|
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 | 183 | |
184 | 184 | ActionScheduler_DataController::free_memory(); |
185 | 185 | } |
@@ -189,8 +189,8 @@ discard block |
||
189 | 189 | */ |
190 | 190 | protected function maybe_stop_the_insanity() { |
191 | 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 ) { |
|
192 | + $current_iteration = intval(trim($this->progress_bar->current())); |
|
193 | + if (0 === $current_iteration % 50) { |
|
194 | 194 | $this->stop_the_insanity(); |
195 | 195 | } |
196 | 196 | } |
@@ -19,170 +19,170 @@ |
||
19 | 19 | */ |
20 | 20 | class Migration_Command extends WP_CLI_Command { |
21 | 21 | |
22 | - /** @var int */ |
|
23 | - private $total_processed = 0; |
|
24 | - |
|
25 | - /** |
|
26 | - * Register the command with WP-CLI |
|
27 | - */ |
|
28 | - public function register() { |
|
29 | - if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
|
30 | - return; |
|
31 | - } |
|
32 | - |
|
33 | - WP_CLI::add_command( |
|
34 | - 'action-scheduler migrate', |
|
35 | - array( $this, 'migrate' ), |
|
36 | - array( |
|
37 | - 'shortdesc' => 'Migrates actions to the DB tables store', |
|
38 | - 'synopsis' => array( |
|
39 | - array( |
|
40 | - 'type' => 'assoc', |
|
41 | - 'name' => 'batch-size', |
|
42 | - 'optional' => true, |
|
43 | - 'default' => 100, |
|
44 | - 'description' => 'The number of actions to process in each batch', |
|
45 | - ), |
|
46 | - array( |
|
47 | - 'type' => 'assoc', |
|
48 | - 'name' => 'free-memory-on', |
|
49 | - 'optional' => true, |
|
50 | - 'default' => 50, |
|
51 | - 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', |
|
52 | - ), |
|
53 | - array( |
|
54 | - 'type' => 'assoc', |
|
55 | - 'name' => 'pause', |
|
56 | - 'optional' => true, |
|
57 | - 'default' => 0, |
|
58 | - 'description' => 'The number of seconds to pause when freeing memory', |
|
59 | - ), |
|
60 | - array( |
|
61 | - 'type' => 'flag', |
|
62 | - 'name' => 'dry-run', |
|
63 | - 'optional' => true, |
|
64 | - 'description' => 'Reports on the actions that would have been migrated, but does not change any data', |
|
65 | - ), |
|
66 | - ), |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Process the data migration. |
|
73 | - * |
|
74 | - * @param array $positional_args Required for WP CLI. Not used in migration. |
|
75 | - * @param array $assoc_args Optional arguments. |
|
76 | - * |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public function migrate( $positional_args, $assoc_args ) { |
|
80 | - $this->init_logging(); |
|
81 | - |
|
82 | - $config = $this->get_migration_config( $assoc_args ); |
|
83 | - $runner = new Runner( $config ); |
|
84 | - $runner->init_destination(); |
|
85 | - |
|
86 | - $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; |
|
87 | - $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; |
|
88 | - $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; |
|
89 | - \ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
90 | - \ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
91 | - |
|
92 | - do { |
|
93 | - $actions_processed = $runner->run( $batch_size ); |
|
94 | - $this->total_processed += $actions_processed; |
|
95 | - } while ( $actions_processed > 0 ); |
|
96 | - |
|
97 | - if ( ! $config->get_dry_run() ) { |
|
98 | - // let the scheduler know that there's nothing left to do |
|
99 | - $scheduler = new Scheduler(); |
|
100 | - $scheduler->mark_complete(); |
|
101 | - } |
|
102 | - |
|
103 | - WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Build the config object used to create the Runner |
|
108 | - * |
|
109 | - * @param array $args Optional arguments. |
|
110 | - * |
|
111 | - * @return ActionScheduler\Migration\Config |
|
112 | - */ |
|
113 | - private function get_migration_config( $args ) { |
|
114 | - $args = wp_parse_args( |
|
115 | - $args, |
|
116 | - array( |
|
117 | - 'dry-run' => false, |
|
118 | - ) |
|
119 | - ); |
|
120 | - |
|
121 | - $config = Controller::instance()->get_migration_config_object(); |
|
122 | - $config->set_dry_run( ! empty( $args['dry-run'] ) ); |
|
123 | - |
|
124 | - return $config; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Hook command line logging into migration actions. |
|
129 | - */ |
|
130 | - private function init_logging() { |
|
131 | - add_action( |
|
132 | - 'action_scheduler/migrate_action_dry_run', |
|
133 | - function ( $action_id ) { |
|
134 | - WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
|
135 | - }, |
|
136 | - 10, |
|
137 | - 1 |
|
138 | - ); |
|
139 | - add_action( |
|
140 | - 'action_scheduler/no_action_to_migrate', |
|
141 | - function ( $action_id ) { |
|
142 | - WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
|
143 | - }, |
|
144 | - 10, |
|
145 | - 1 |
|
146 | - ); |
|
147 | - add_action( |
|
148 | - 'action_scheduler/migrate_action_failed', |
|
149 | - function ( $action_id ) { |
|
150 | - WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
|
151 | - }, |
|
152 | - 10, |
|
153 | - 1 |
|
154 | - ); |
|
155 | - add_action( |
|
156 | - 'action_scheduler/migrate_action_incomplete', |
|
157 | - function ( $source_id, $destination_id ) { |
|
158 | - WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
|
159 | - }, |
|
160 | - 10, |
|
161 | - 2 |
|
162 | - ); |
|
163 | - add_action( |
|
164 | - 'action_scheduler/migrated_action', |
|
165 | - function ( $source_id, $destination_id ) { |
|
166 | - WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
|
167 | - }, |
|
168 | - 10, |
|
169 | - 2 |
|
170 | - ); |
|
171 | - add_action( |
|
172 | - 'action_scheduler/migration_batch_starting', |
|
173 | - function ( $batch ) { |
|
174 | - WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); |
|
175 | - }, |
|
176 | - 10, |
|
177 | - 1 |
|
178 | - ); |
|
179 | - add_action( |
|
180 | - 'action_scheduler/migration_batch_complete', |
|
181 | - function ( $batch ) { |
|
182 | - WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
|
183 | - }, |
|
184 | - 10, |
|
185 | - 1 |
|
186 | - ); |
|
187 | - } |
|
22 | + /** @var int */ |
|
23 | + private $total_processed = 0; |
|
24 | + |
|
25 | + /** |
|
26 | + * Register the command with WP-CLI |
|
27 | + */ |
|
28 | + public function register() { |
|
29 | + if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
|
30 | + return; |
|
31 | + } |
|
32 | + |
|
33 | + WP_CLI::add_command( |
|
34 | + 'action-scheduler migrate', |
|
35 | + array( $this, 'migrate' ), |
|
36 | + array( |
|
37 | + 'shortdesc' => 'Migrates actions to the DB tables store', |
|
38 | + 'synopsis' => array( |
|
39 | + array( |
|
40 | + 'type' => 'assoc', |
|
41 | + 'name' => 'batch-size', |
|
42 | + 'optional' => true, |
|
43 | + 'default' => 100, |
|
44 | + 'description' => 'The number of actions to process in each batch', |
|
45 | + ), |
|
46 | + array( |
|
47 | + 'type' => 'assoc', |
|
48 | + 'name' => 'free-memory-on', |
|
49 | + 'optional' => true, |
|
50 | + 'default' => 50, |
|
51 | + 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', |
|
52 | + ), |
|
53 | + array( |
|
54 | + 'type' => 'assoc', |
|
55 | + 'name' => 'pause', |
|
56 | + 'optional' => true, |
|
57 | + 'default' => 0, |
|
58 | + 'description' => 'The number of seconds to pause when freeing memory', |
|
59 | + ), |
|
60 | + array( |
|
61 | + 'type' => 'flag', |
|
62 | + 'name' => 'dry-run', |
|
63 | + 'optional' => true, |
|
64 | + 'description' => 'Reports on the actions that would have been migrated, but does not change any data', |
|
65 | + ), |
|
66 | + ), |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Process the data migration. |
|
73 | + * |
|
74 | + * @param array $positional_args Required for WP CLI. Not used in migration. |
|
75 | + * @param array $assoc_args Optional arguments. |
|
76 | + * |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public function migrate( $positional_args, $assoc_args ) { |
|
80 | + $this->init_logging(); |
|
81 | + |
|
82 | + $config = $this->get_migration_config( $assoc_args ); |
|
83 | + $runner = new Runner( $config ); |
|
84 | + $runner->init_destination(); |
|
85 | + |
|
86 | + $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; |
|
87 | + $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; |
|
88 | + $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; |
|
89 | + \ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
90 | + \ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
91 | + |
|
92 | + do { |
|
93 | + $actions_processed = $runner->run( $batch_size ); |
|
94 | + $this->total_processed += $actions_processed; |
|
95 | + } while ( $actions_processed > 0 ); |
|
96 | + |
|
97 | + if ( ! $config->get_dry_run() ) { |
|
98 | + // let the scheduler know that there's nothing left to do |
|
99 | + $scheduler = new Scheduler(); |
|
100 | + $scheduler->mark_complete(); |
|
101 | + } |
|
102 | + |
|
103 | + WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Build the config object used to create the Runner |
|
108 | + * |
|
109 | + * @param array $args Optional arguments. |
|
110 | + * |
|
111 | + * @return ActionScheduler\Migration\Config |
|
112 | + */ |
|
113 | + private function get_migration_config( $args ) { |
|
114 | + $args = wp_parse_args( |
|
115 | + $args, |
|
116 | + array( |
|
117 | + 'dry-run' => false, |
|
118 | + ) |
|
119 | + ); |
|
120 | + |
|
121 | + $config = Controller::instance()->get_migration_config_object(); |
|
122 | + $config->set_dry_run( ! empty( $args['dry-run'] ) ); |
|
123 | + |
|
124 | + return $config; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Hook command line logging into migration actions. |
|
129 | + */ |
|
130 | + private function init_logging() { |
|
131 | + add_action( |
|
132 | + 'action_scheduler/migrate_action_dry_run', |
|
133 | + function ( $action_id ) { |
|
134 | + WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
|
135 | + }, |
|
136 | + 10, |
|
137 | + 1 |
|
138 | + ); |
|
139 | + add_action( |
|
140 | + 'action_scheduler/no_action_to_migrate', |
|
141 | + function ( $action_id ) { |
|
142 | + WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
|
143 | + }, |
|
144 | + 10, |
|
145 | + 1 |
|
146 | + ); |
|
147 | + add_action( |
|
148 | + 'action_scheduler/migrate_action_failed', |
|
149 | + function ( $action_id ) { |
|
150 | + WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
|
151 | + }, |
|
152 | + 10, |
|
153 | + 1 |
|
154 | + ); |
|
155 | + add_action( |
|
156 | + 'action_scheduler/migrate_action_incomplete', |
|
157 | + function ( $source_id, $destination_id ) { |
|
158 | + WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
|
159 | + }, |
|
160 | + 10, |
|
161 | + 2 |
|
162 | + ); |
|
163 | + add_action( |
|
164 | + 'action_scheduler/migrated_action', |
|
165 | + function ( $source_id, $destination_id ) { |
|
166 | + WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
|
167 | + }, |
|
168 | + 10, |
|
169 | + 2 |
|
170 | + ); |
|
171 | + add_action( |
|
172 | + 'action_scheduler/migration_batch_starting', |
|
173 | + function ( $batch ) { |
|
174 | + WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); |
|
175 | + }, |
|
176 | + 10, |
|
177 | + 1 |
|
178 | + ); |
|
179 | + add_action( |
|
180 | + 'action_scheduler/migration_batch_complete', |
|
181 | + function ( $batch ) { |
|
182 | + WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
|
183 | + }, |
|
184 | + 10, |
|
185 | + 1 |
|
186 | + ); |
|
187 | + } |
|
188 | 188 | } |
@@ -26,13 +26,13 @@ discard block |
||
26 | 26 | * Register the command with WP-CLI |
27 | 27 | */ |
28 | 28 | public function register() { |
29 | - if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
|
29 | + if ( ! defined('WP_CLI') || ! WP_CLI) { |
|
30 | 30 | return; |
31 | 31 | } |
32 | 32 | |
33 | 33 | WP_CLI::add_command( |
34 | 34 | 'action-scheduler migrate', |
35 | - array( $this, 'migrate' ), |
|
35 | + array($this, 'migrate'), |
|
36 | 36 | array( |
37 | 37 | 'shortdesc' => 'Migrates actions to the DB tables store', |
38 | 38 | 'synopsis' => array( |
@@ -76,31 +76,31 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return void |
78 | 78 | */ |
79 | - public function migrate( $positional_args, $assoc_args ) { |
|
79 | + public function migrate($positional_args, $assoc_args) { |
|
80 | 80 | $this->init_logging(); |
81 | 81 | |
82 | - $config = $this->get_migration_config( $assoc_args ); |
|
83 | - $runner = new Runner( $config ); |
|
82 | + $config = $this->get_migration_config($assoc_args); |
|
83 | + $runner = new Runner($config); |
|
84 | 84 | $runner->init_destination(); |
85 | 85 | |
86 | - $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; |
|
87 | - $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; |
|
88 | - $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; |
|
89 | - \ActionScheduler_DataController::set_free_ticks( $free_on ); |
|
90 | - \ActionScheduler_DataController::set_sleep_time( $sleep ); |
|
86 | + $batch_size = isset($assoc_args['batch-size']) ? (int) $assoc_args['batch-size'] : 100; |
|
87 | + $free_on = isset($assoc_args['free-memory-on']) ? (int) $assoc_args['free-memory-on'] : 50; |
|
88 | + $sleep = isset($assoc_args['pause']) ? (int) $assoc_args['pause'] : 0; |
|
89 | + \ActionScheduler_DataController::set_free_ticks($free_on); |
|
90 | + \ActionScheduler_DataController::set_sleep_time($sleep); |
|
91 | 91 | |
92 | 92 | do { |
93 | - $actions_processed = $runner->run( $batch_size ); |
|
93 | + $actions_processed = $runner->run($batch_size); |
|
94 | 94 | $this->total_processed += $actions_processed; |
95 | - } while ( $actions_processed > 0 ); |
|
95 | + } while ($actions_processed > 0); |
|
96 | 96 | |
97 | - if ( ! $config->get_dry_run() ) { |
|
97 | + if ( ! $config->get_dry_run()) { |
|
98 | 98 | // let the scheduler know that there's nothing left to do |
99 | 99 | $scheduler = new Scheduler(); |
100 | 100 | $scheduler->mark_complete(); |
101 | 101 | } |
102 | 102 | |
103 | - WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
|
103 | + WP_CLI::success(sprintf('%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed)); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * |
111 | 111 | * @return ActionScheduler\Migration\Config |
112 | 112 | */ |
113 | - private function get_migration_config( $args ) { |
|
113 | + private function get_migration_config($args) { |
|
114 | 114 | $args = wp_parse_args( |
115 | 115 | $args, |
116 | 116 | array( |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | ); |
120 | 120 | |
121 | 121 | $config = Controller::instance()->get_migration_config_object(); |
122 | - $config->set_dry_run( ! empty( $args['dry-run'] ) ); |
|
122 | + $config->set_dry_run( ! empty($args['dry-run'])); |
|
123 | 123 | |
124 | 124 | return $config; |
125 | 125 | } |
@@ -130,56 +130,56 @@ discard block |
||
130 | 130 | private function init_logging() { |
131 | 131 | add_action( |
132 | 132 | 'action_scheduler/migrate_action_dry_run', |
133 | - function ( $action_id ) { |
|
134 | - WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
|
133 | + function($action_id) { |
|
134 | + WP_CLI::debug(sprintf('Dry-run: migrated action %d', $action_id)); |
|
135 | 135 | }, |
136 | 136 | 10, |
137 | 137 | 1 |
138 | 138 | ); |
139 | 139 | add_action( |
140 | 140 | 'action_scheduler/no_action_to_migrate', |
141 | - function ( $action_id ) { |
|
142 | - WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
|
141 | + function($action_id) { |
|
142 | + WP_CLI::debug(sprintf('No action found to migrate for ID %d', $action_id)); |
|
143 | 143 | }, |
144 | 144 | 10, |
145 | 145 | 1 |
146 | 146 | ); |
147 | 147 | add_action( |
148 | 148 | 'action_scheduler/migrate_action_failed', |
149 | - function ( $action_id ) { |
|
150 | - WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
|
149 | + function($action_id) { |
|
150 | + WP_CLI::warning(sprintf('Failed migrating action with ID %d', $action_id)); |
|
151 | 151 | }, |
152 | 152 | 10, |
153 | 153 | 1 |
154 | 154 | ); |
155 | 155 | add_action( |
156 | 156 | 'action_scheduler/migrate_action_incomplete', |
157 | - function ( $source_id, $destination_id ) { |
|
158 | - WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
|
157 | + function($source_id, $destination_id) { |
|
158 | + WP_CLI::warning(sprintf('Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id)); |
|
159 | 159 | }, |
160 | 160 | 10, |
161 | 161 | 2 |
162 | 162 | ); |
163 | 163 | add_action( |
164 | 164 | 'action_scheduler/migrated_action', |
165 | - function ( $source_id, $destination_id ) { |
|
166 | - WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
|
165 | + function($source_id, $destination_id) { |
|
166 | + WP_CLI::debug(sprintf('Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id)); |
|
167 | 167 | }, |
168 | 168 | 10, |
169 | 169 | 2 |
170 | 170 | ); |
171 | 171 | add_action( |
172 | 172 | 'action_scheduler/migration_batch_starting', |
173 | - function ( $batch ) { |
|
174 | - WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); |
|
173 | + function($batch) { |
|
174 | + WP_CLI::debug('Beginning migration of batch: '.print_r($batch, true)); |
|
175 | 175 | }, |
176 | 176 | 10, |
177 | 177 | 1 |
178 | 178 | ); |
179 | 179 | add_action( |
180 | 180 | 'action_scheduler/migration_batch_complete', |
181 | - function ( $batch ) { |
|
182 | - WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
|
181 | + function($batch) { |
|
182 | + WP_CLI::log(sprintf('Completed migration of %d actions', count($batch))); |
|
183 | 183 | }, |
184 | 184 | 10, |
185 | 185 | 1 |
@@ -10,13 +10,13 @@ |
||
10 | 10 | * @codeCoverageIgnore |
11 | 11 | */ |
12 | 12 | class DryRun_LogMigrator extends LogMigrator { |
13 | - /** |
|
14 | - * Simulate migrating an action log. |
|
15 | - * |
|
16 | - * @param int $source_action_id Source logger object. |
|
17 | - * @param int $destination_action_id Destination logger object. |
|
18 | - */ |
|
19 | - public function migrate( $source_action_id, $destination_action_id ) { |
|
20 | - // no-op |
|
21 | - } |
|
13 | + /** |
|
14 | + * Simulate migrating an action log. |
|
15 | + * |
|
16 | + * @param int $source_action_id Source logger object. |
|
17 | + * @param int $destination_action_id Destination logger object. |
|
18 | + */ |
|
19 | + public function migrate( $source_action_id, $destination_action_id ) { |
|
20 | + // no-op |
|
21 | + } |
|
22 | 22 | } |
@@ -16,7 +16,7 @@ |
||
16 | 16 | * @param int $source_action_id Source logger object. |
17 | 17 | * @param int $destination_action_id Destination logger object. |
18 | 18 | */ |
19 | - public function migrate( $source_action_id, $destination_action_id ) { |
|
19 | + public function migrate($source_action_id, $destination_action_id) { |
|
20 | 20 | // no-op |
21 | 21 | } |
22 | 22 | } |
@@ -14,77 +14,77 @@ |
||
14 | 14 | * @codeCoverageIgnore |
15 | 15 | */ |
16 | 16 | class BatchFetcher { |
17 | - /** var ActionScheduler_Store */ |
|
18 | - private $store; |
|
17 | + /** var ActionScheduler_Store */ |
|
18 | + private $store; |
|
19 | 19 | |
20 | - /** |
|
21 | - * BatchFetcher constructor. |
|
22 | - * |
|
23 | - * @param ActionScheduler_Store $source_store Source store object. |
|
24 | - */ |
|
25 | - public function __construct( Store $source_store ) { |
|
26 | - $this->store = $source_store; |
|
27 | - } |
|
20 | + /** |
|
21 | + * BatchFetcher constructor. |
|
22 | + * |
|
23 | + * @param ActionScheduler_Store $source_store Source store object. |
|
24 | + */ |
|
25 | + public function __construct( Store $source_store ) { |
|
26 | + $this->store = $source_store; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Retrieve a list of actions. |
|
31 | - * |
|
32 | - * @param int $count The number of actions to retrieve |
|
33 | - * |
|
34 | - * @return int[] A list of action IDs |
|
35 | - */ |
|
36 | - public function fetch( $count = 10 ) { |
|
37 | - foreach ( $this->get_query_strategies( $count ) as $query ) { |
|
38 | - $action_ids = $this->store->query_actions( $query ); |
|
39 | - if ( ! empty( $action_ids ) ) { |
|
40 | - return $action_ids; |
|
41 | - } |
|
42 | - } |
|
29 | + /** |
|
30 | + * Retrieve a list of actions. |
|
31 | + * |
|
32 | + * @param int $count The number of actions to retrieve |
|
33 | + * |
|
34 | + * @return int[] A list of action IDs |
|
35 | + */ |
|
36 | + public function fetch( $count = 10 ) { |
|
37 | + foreach ( $this->get_query_strategies( $count ) as $query ) { |
|
38 | + $action_ids = $this->store->query_actions( $query ); |
|
39 | + if ( ! empty( $action_ids ) ) { |
|
40 | + return $action_ids; |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - return array(); |
|
45 | - } |
|
44 | + return array(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Generate a list of prioritized of action search parameters. |
|
49 | - * |
|
50 | - * @param int $count Number of actions to find. |
|
51 | - * |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - private function get_query_strategies( $count ) { |
|
55 | - $now = as_get_datetime_object(); |
|
56 | - $args = array( |
|
57 | - 'date' => $now, |
|
58 | - 'per_page' => $count, |
|
59 | - 'offset' => 0, |
|
60 | - 'orderby' => 'date', |
|
61 | - 'order' => 'ASC', |
|
62 | - ); |
|
47 | + /** |
|
48 | + * Generate a list of prioritized of action search parameters. |
|
49 | + * |
|
50 | + * @param int $count Number of actions to find. |
|
51 | + * |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + private function get_query_strategies( $count ) { |
|
55 | + $now = as_get_datetime_object(); |
|
56 | + $args = array( |
|
57 | + 'date' => $now, |
|
58 | + 'per_page' => $count, |
|
59 | + 'offset' => 0, |
|
60 | + 'orderby' => 'date', |
|
61 | + 'order' => 'ASC', |
|
62 | + ); |
|
63 | 63 | |
64 | - $priorities = array( |
|
65 | - Store::STATUS_PENDING, |
|
66 | - Store::STATUS_FAILED, |
|
67 | - Store::STATUS_CANCELED, |
|
68 | - Store::STATUS_COMPLETE, |
|
69 | - Store::STATUS_RUNNING, |
|
70 | - '', // any other unanticipated status |
|
71 | - ); |
|
64 | + $priorities = array( |
|
65 | + Store::STATUS_PENDING, |
|
66 | + Store::STATUS_FAILED, |
|
67 | + Store::STATUS_CANCELED, |
|
68 | + Store::STATUS_COMPLETE, |
|
69 | + Store::STATUS_RUNNING, |
|
70 | + '', // any other unanticipated status |
|
71 | + ); |
|
72 | 72 | |
73 | - foreach ( $priorities as $status ) { |
|
74 | - yield wp_parse_args( |
|
75 | - array( |
|
76 | - 'status' => $status, |
|
77 | - 'date_compare' => '<=', |
|
78 | - ), |
|
79 | - $args |
|
80 | - ); |
|
81 | - yield wp_parse_args( |
|
82 | - array( |
|
83 | - 'status' => $status, |
|
84 | - 'date_compare' => '>=', |
|
85 | - ), |
|
86 | - $args |
|
87 | - ); |
|
88 | - } |
|
89 | - } |
|
73 | + foreach ( $priorities as $status ) { |
|
74 | + yield wp_parse_args( |
|
75 | + array( |
|
76 | + 'status' => $status, |
|
77 | + 'date_compare' => '<=', |
|
78 | + ), |
|
79 | + $args |
|
80 | + ); |
|
81 | + yield wp_parse_args( |
|
82 | + array( |
|
83 | + 'status' => $status, |
|
84 | + 'date_compare' => '>=', |
|
85 | + ), |
|
86 | + $args |
|
87 | + ); |
|
88 | + } |
|
89 | + } |
|
90 | 90 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param ActionScheduler_Store $source_store Source store object. |
24 | 24 | */ |
25 | - public function __construct( Store $source_store ) { |
|
25 | + public function __construct(Store $source_store) { |
|
26 | 26 | $this->store = $source_store; |
27 | 27 | } |
28 | 28 | |
@@ -33,10 +33,10 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return int[] A list of action IDs |
35 | 35 | */ |
36 | - public function fetch( $count = 10 ) { |
|
37 | - foreach ( $this->get_query_strategies( $count ) as $query ) { |
|
38 | - $action_ids = $this->store->query_actions( $query ); |
|
39 | - if ( ! empty( $action_ids ) ) { |
|
36 | + public function fetch($count = 10) { |
|
37 | + foreach ($this->get_query_strategies($count) as $query) { |
|
38 | + $action_ids = $this->store->query_actions($query); |
|
39 | + if ( ! empty($action_ids)) { |
|
40 | 40 | return $action_ids; |
41 | 41 | } |
42 | 42 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @return array |
53 | 53 | */ |
54 | - private function get_query_strategies( $count ) { |
|
54 | + private function get_query_strategies($count) { |
|
55 | 55 | $now = as_get_datetime_object(); |
56 | 56 | $args = array( |
57 | 57 | 'date' => $now, |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | '', // any other unanticipated status |
71 | 71 | ); |
72 | 72 | |
73 | - foreach ( $priorities as $status ) { |
|
73 | + foreach ($priorities as $status) { |
|
74 | 74 | yield wp_parse_args( |
75 | 75 | array( |
76 | 76 | 'status' => $status, |
@@ -12,97 +12,97 @@ |
||
12 | 12 | * @codeCoverageIgnore |
13 | 13 | */ |
14 | 14 | class ActionMigrator { |
15 | - /** var ActionScheduler_Store */ |
|
16 | - private $source; |
|
17 | - |
|
18 | - /** var ActionScheduler_Store */ |
|
19 | - private $destination; |
|
20 | - |
|
21 | - /** var LogMigrator */ |
|
22 | - private $log_migrator; |
|
23 | - |
|
24 | - /** |
|
25 | - * ActionMigrator constructor. |
|
26 | - * |
|
27 | - * @param ActionScheduler_Store $source_store Source store object. |
|
28 | - * @param ActionScheduler_Store $destination_store Destination store object. |
|
29 | - * @param LogMigrator $log_migrator Log migrator object. |
|
30 | - */ |
|
31 | - public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
32 | - $this->source = $source_store; |
|
33 | - $this->destination = $destination_store; |
|
34 | - $this->log_migrator = $log_migrator; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Migrate an action. |
|
39 | - * |
|
40 | - * @param int $source_action_id Action ID. |
|
41 | - * |
|
42 | - * @return int 0|new action ID |
|
43 | - */ |
|
44 | - public function migrate( $source_action_id ) { |
|
45 | - try { |
|
46 | - $action = $this->source->fetch_action( $source_action_id ); |
|
47 | - $status = $this->source->get_status( $source_action_id ); |
|
48 | - } catch ( \Exception $e ) { |
|
49 | - $action = null; |
|
50 | - $status = ''; |
|
51 | - } |
|
52 | - |
|
53 | - if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
54 | - // null action or empty status means the fetch operation failed or the action didn't exist |
|
55 | - // null schedule means it's missing vital data |
|
56 | - // delete it and move on |
|
57 | - try { |
|
58 | - $this->source->delete_action( $source_action_id ); |
|
59 | - } catch ( \Exception $e ) { |
|
60 | - // nothing to do, it didn't exist in the first place |
|
61 | - } |
|
62 | - do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
63 | - |
|
64 | - return 0; |
|
65 | - } |
|
66 | - |
|
67 | - try { |
|
68 | - |
|
69 | - // Make sure the last attempt date is set correctly for completed and failed actions |
|
70 | - $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
71 | - |
|
72 | - $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | - } catch ( \Exception $e ) { |
|
74 | - do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
75 | - |
|
76 | - return 0; // could not save the action in the new store |
|
77 | - } |
|
78 | - |
|
79 | - try { |
|
80 | - switch ( $status ) { |
|
81 | - case \ActionScheduler_Store::STATUS_FAILED: |
|
82 | - $this->destination->mark_failure( $destination_action_id ); |
|
83 | - break; |
|
84 | - case \ActionScheduler_Store::STATUS_CANCELED: |
|
85 | - $this->destination->cancel_action( $destination_action_id ); |
|
86 | - break; |
|
87 | - } |
|
88 | - |
|
89 | - $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | - $this->source->delete_action( $source_action_id ); |
|
91 | - |
|
92 | - $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | - if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | - throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
95 | - } |
|
96 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
97 | - |
|
98 | - return $destination_action_id; |
|
99 | - } catch ( \Exception $e ) { |
|
100 | - // could not delete from the old store |
|
101 | - $this->source->mark_migrated( $source_action_id ); |
|
102 | - do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
104 | - |
|
105 | - return $destination_action_id; |
|
106 | - } |
|
107 | - } |
|
15 | + /** var ActionScheduler_Store */ |
|
16 | + private $source; |
|
17 | + |
|
18 | + /** var ActionScheduler_Store */ |
|
19 | + private $destination; |
|
20 | + |
|
21 | + /** var LogMigrator */ |
|
22 | + private $log_migrator; |
|
23 | + |
|
24 | + /** |
|
25 | + * ActionMigrator constructor. |
|
26 | + * |
|
27 | + * @param ActionScheduler_Store $source_store Source store object. |
|
28 | + * @param ActionScheduler_Store $destination_store Destination store object. |
|
29 | + * @param LogMigrator $log_migrator Log migrator object. |
|
30 | + */ |
|
31 | + public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
32 | + $this->source = $source_store; |
|
33 | + $this->destination = $destination_store; |
|
34 | + $this->log_migrator = $log_migrator; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Migrate an action. |
|
39 | + * |
|
40 | + * @param int $source_action_id Action ID. |
|
41 | + * |
|
42 | + * @return int 0|new action ID |
|
43 | + */ |
|
44 | + public function migrate( $source_action_id ) { |
|
45 | + try { |
|
46 | + $action = $this->source->fetch_action( $source_action_id ); |
|
47 | + $status = $this->source->get_status( $source_action_id ); |
|
48 | + } catch ( \Exception $e ) { |
|
49 | + $action = null; |
|
50 | + $status = ''; |
|
51 | + } |
|
52 | + |
|
53 | + if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
54 | + // null action or empty status means the fetch operation failed or the action didn't exist |
|
55 | + // null schedule means it's missing vital data |
|
56 | + // delete it and move on |
|
57 | + try { |
|
58 | + $this->source->delete_action( $source_action_id ); |
|
59 | + } catch ( \Exception $e ) { |
|
60 | + // nothing to do, it didn't exist in the first place |
|
61 | + } |
|
62 | + do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
63 | + |
|
64 | + return 0; |
|
65 | + } |
|
66 | + |
|
67 | + try { |
|
68 | + |
|
69 | + // Make sure the last attempt date is set correctly for completed and failed actions |
|
70 | + $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
71 | + |
|
72 | + $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | + } catch ( \Exception $e ) { |
|
74 | + do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
75 | + |
|
76 | + return 0; // could not save the action in the new store |
|
77 | + } |
|
78 | + |
|
79 | + try { |
|
80 | + switch ( $status ) { |
|
81 | + case \ActionScheduler_Store::STATUS_FAILED: |
|
82 | + $this->destination->mark_failure( $destination_action_id ); |
|
83 | + break; |
|
84 | + case \ActionScheduler_Store::STATUS_CANCELED: |
|
85 | + $this->destination->cancel_action( $destination_action_id ); |
|
86 | + break; |
|
87 | + } |
|
88 | + |
|
89 | + $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | + $this->source->delete_action( $source_action_id ); |
|
91 | + |
|
92 | + $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | + if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | + throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
95 | + } |
|
96 | + do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
97 | + |
|
98 | + return $destination_action_id; |
|
99 | + } catch ( \Exception $e ) { |
|
100 | + // could not delete from the old store |
|
101 | + $this->source->mark_migrated( $source_action_id ); |
|
102 | + do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | + do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
104 | + |
|
105 | + return $destination_action_id; |
|
106 | + } |
|
107 | + } |
|
108 | 108 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | * @param ActionScheduler_Store $destination_store Destination store object. |
29 | 29 | * @param LogMigrator $log_migrator Log migrator object. |
30 | 30 | */ |
31 | - public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
31 | + public function __construct(\ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator) { |
|
32 | 32 | $this->source = $source_store; |
33 | 33 | $this->destination = $destination_store; |
34 | 34 | $this->log_migrator = $log_migrator; |
@@ -41,25 +41,25 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return int 0|new action ID |
43 | 43 | */ |
44 | - public function migrate( $source_action_id ) { |
|
44 | + public function migrate($source_action_id) { |
|
45 | 45 | try { |
46 | - $action = $this->source->fetch_action( $source_action_id ); |
|
47 | - $status = $this->source->get_status( $source_action_id ); |
|
48 | - } catch ( \Exception $e ) { |
|
46 | + $action = $this->source->fetch_action($source_action_id); |
|
47 | + $status = $this->source->get_status($source_action_id); |
|
48 | + } catch (\Exception $e) { |
|
49 | 49 | $action = null; |
50 | 50 | $status = ''; |
51 | 51 | } |
52 | 52 | |
53 | - if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
53 | + if ($action === null || empty($status) || ! $action->get_schedule()->get_date()) { |
|
54 | 54 | // null action or empty status means the fetch operation failed or the action didn't exist |
55 | 55 | // null schedule means it's missing vital data |
56 | 56 | // delete it and move on |
57 | 57 | try { |
58 | - $this->source->delete_action( $source_action_id ); |
|
59 | - } catch ( \Exception $e ) { |
|
58 | + $this->source->delete_action($source_action_id); |
|
59 | + } catch (\Exception $e) { |
|
60 | 60 | // nothing to do, it didn't exist in the first place |
61 | 61 | } |
62 | - do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
62 | + do_action('action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination); |
|
63 | 63 | |
64 | 64 | return 0; |
65 | 65 | } |
@@ -67,40 +67,40 @@ discard block |
||
67 | 67 | try { |
68 | 68 | |
69 | 69 | // Make sure the last attempt date is set correctly for completed and failed actions |
70 | - $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
70 | + $last_attempt_date = ($status !== \ActionScheduler_Store::STATUS_PENDING) ? $this->source->get_date($source_action_id) : null; |
|
71 | 71 | |
72 | - $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | - } catch ( \Exception $e ) { |
|
74 | - do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
72 | + $destination_action_id = $this->destination->save_action($action, null, $last_attempt_date); |
|
73 | + } catch (\Exception $e) { |
|
74 | + do_action('action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination); |
|
75 | 75 | |
76 | 76 | return 0; // could not save the action in the new store |
77 | 77 | } |
78 | 78 | |
79 | 79 | try { |
80 | - switch ( $status ) { |
|
80 | + switch ($status) { |
|
81 | 81 | case \ActionScheduler_Store::STATUS_FAILED: |
82 | - $this->destination->mark_failure( $destination_action_id ); |
|
82 | + $this->destination->mark_failure($destination_action_id); |
|
83 | 83 | break; |
84 | 84 | case \ActionScheduler_Store::STATUS_CANCELED: |
85 | - $this->destination->cancel_action( $destination_action_id ); |
|
85 | + $this->destination->cancel_action($destination_action_id); |
|
86 | 86 | break; |
87 | 87 | } |
88 | 88 | |
89 | - $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | - $this->source->delete_action( $source_action_id ); |
|
89 | + $this->log_migrator->migrate($source_action_id, $destination_action_id); |
|
90 | + $this->source->delete_action($source_action_id); |
|
91 | 91 | |
92 | - $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | - if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | - throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
92 | + $test_action = $this->source->fetch_action($source_action_id); |
|
93 | + if ( ! is_a($test_action, 'ActionScheduler_NullAction')) { |
|
94 | + throw new \RuntimeException(sprintf(__('Unable to remove source migrated action %s', 'action-scheduler'), $source_action_id)); |
|
95 | 95 | } |
96 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
96 | + do_action('action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
97 | 97 | |
98 | 98 | return $destination_action_id; |
99 | - } catch ( \Exception $e ) { |
|
99 | + } catch (\Exception $e) { |
|
100 | 100 | // could not delete from the old store |
101 | - $this->source->mark_migrated( $source_action_id ); |
|
102 | - do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
101 | + $this->source->mark_migrated($source_action_id); |
|
102 | + do_action('action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
103 | + do_action('action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
104 | 104 | |
105 | 105 | return $destination_action_id; |
106 | 106 | } |
@@ -12,127 +12,127 @@ |
||
12 | 12 | * @codeCoverageIgnore |
13 | 13 | */ |
14 | 14 | class Runner { |
15 | - /** @var ActionScheduler_Store */ |
|
16 | - private $source_store; |
|
17 | - |
|
18 | - /** @var ActionScheduler_Store */ |
|
19 | - private $destination_store; |
|
20 | - |
|
21 | - /** @var ActionScheduler_Logger */ |
|
22 | - private $source_logger; |
|
23 | - |
|
24 | - /** @var ActionScheduler_Logger */ |
|
25 | - private $destination_logger; |
|
26 | - |
|
27 | - /** @var BatchFetcher */ |
|
28 | - private $batch_fetcher; |
|
29 | - |
|
30 | - /** @var ActionMigrator */ |
|
31 | - private $action_migrator; |
|
32 | - |
|
33 | - /** @var LogMigrator */ |
|
34 | - private $log_migrator; |
|
35 | - |
|
36 | - /** @var ProgressBar */ |
|
37 | - private $progress_bar; |
|
38 | - |
|
39 | - /** |
|
40 | - * Runner constructor. |
|
41 | - * |
|
42 | - * @param Config $config Migration configuration object. |
|
43 | - */ |
|
44 | - public function __construct( Config $config ) { |
|
45 | - $this->source_store = $config->get_source_store(); |
|
46 | - $this->destination_store = $config->get_destination_store(); |
|
47 | - $this->source_logger = $config->get_source_logger(); |
|
48 | - $this->destination_logger = $config->get_destination_logger(); |
|
49 | - |
|
50 | - $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | - if ( $config->get_dry_run() ) { |
|
52 | - $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | - $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
54 | - } else { |
|
55 | - $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | - $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
57 | - } |
|
58 | - |
|
59 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
60 | - $this->progress_bar = $config->get_progress_bar(); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Run migration batch. |
|
66 | - * |
|
67 | - * @param int $batch_size Optional batch size. Default 10. |
|
68 | - * |
|
69 | - * @return int Size of batch processed. |
|
70 | - */ |
|
71 | - public function run( $batch_size = 10 ) { |
|
72 | - $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | - $batch_size = count( $batch ); |
|
74 | - |
|
75 | - if ( ! $batch_size ) { |
|
76 | - return 0; |
|
77 | - } |
|
78 | - |
|
79 | - if ( $this->progress_bar ) { |
|
80 | - /* translators: %d: amount of actions */ |
|
81 | - $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | - $this->progress_bar->set_count( $batch_size ); |
|
83 | - } |
|
84 | - |
|
85 | - $this->migrate_actions( $batch ); |
|
86 | - |
|
87 | - return $batch_size; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Migration a batch of actions. |
|
92 | - * |
|
93 | - * @param array $action_ids List of action IDs to migrate. |
|
94 | - */ |
|
95 | - public function migrate_actions( array $action_ids ) { |
|
96 | - do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
97 | - |
|
98 | - \ActionScheduler::logger()->unhook_stored_action(); |
|
99 | - $this->destination_logger->unhook_stored_action(); |
|
100 | - |
|
101 | - foreach ( $action_ids as $source_action_id ) { |
|
102 | - $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | - if ( $destination_action_id ) { |
|
104 | - $this->destination_logger->log( |
|
105 | - $destination_action_id, |
|
106 | - sprintf( |
|
107 | - /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
|
108 | - __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
109 | - $source_action_id, |
|
110 | - get_class( $this->source_store ), |
|
111 | - $destination_action_id, |
|
112 | - get_class( $this->destination_store ) |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - if ( $this->progress_bar ) { |
|
118 | - $this->progress_bar->tick(); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - if ( $this->progress_bar ) { |
|
123 | - $this->progress_bar->finish(); |
|
124 | - } |
|
125 | - |
|
126 | - \ActionScheduler::logger()->hook_stored_action(); |
|
127 | - |
|
128 | - do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Initialize destination store and logger. |
|
133 | - */ |
|
134 | - public function init_destination() { |
|
135 | - $this->destination_store->init(); |
|
136 | - $this->destination_logger->init(); |
|
137 | - } |
|
15 | + /** @var ActionScheduler_Store */ |
|
16 | + private $source_store; |
|
17 | + |
|
18 | + /** @var ActionScheduler_Store */ |
|
19 | + private $destination_store; |
|
20 | + |
|
21 | + /** @var ActionScheduler_Logger */ |
|
22 | + private $source_logger; |
|
23 | + |
|
24 | + /** @var ActionScheduler_Logger */ |
|
25 | + private $destination_logger; |
|
26 | + |
|
27 | + /** @var BatchFetcher */ |
|
28 | + private $batch_fetcher; |
|
29 | + |
|
30 | + /** @var ActionMigrator */ |
|
31 | + private $action_migrator; |
|
32 | + |
|
33 | + /** @var LogMigrator */ |
|
34 | + private $log_migrator; |
|
35 | + |
|
36 | + /** @var ProgressBar */ |
|
37 | + private $progress_bar; |
|
38 | + |
|
39 | + /** |
|
40 | + * Runner constructor. |
|
41 | + * |
|
42 | + * @param Config $config Migration configuration object. |
|
43 | + */ |
|
44 | + public function __construct( Config $config ) { |
|
45 | + $this->source_store = $config->get_source_store(); |
|
46 | + $this->destination_store = $config->get_destination_store(); |
|
47 | + $this->source_logger = $config->get_source_logger(); |
|
48 | + $this->destination_logger = $config->get_destination_logger(); |
|
49 | + |
|
50 | + $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | + if ( $config->get_dry_run() ) { |
|
52 | + $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | + $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
54 | + } else { |
|
55 | + $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | + $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
57 | + } |
|
58 | + |
|
59 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
60 | + $this->progress_bar = $config->get_progress_bar(); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Run migration batch. |
|
66 | + * |
|
67 | + * @param int $batch_size Optional batch size. Default 10. |
|
68 | + * |
|
69 | + * @return int Size of batch processed. |
|
70 | + */ |
|
71 | + public function run( $batch_size = 10 ) { |
|
72 | + $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | + $batch_size = count( $batch ); |
|
74 | + |
|
75 | + if ( ! $batch_size ) { |
|
76 | + return 0; |
|
77 | + } |
|
78 | + |
|
79 | + if ( $this->progress_bar ) { |
|
80 | + /* translators: %d: amount of actions */ |
|
81 | + $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | + $this->progress_bar->set_count( $batch_size ); |
|
83 | + } |
|
84 | + |
|
85 | + $this->migrate_actions( $batch ); |
|
86 | + |
|
87 | + return $batch_size; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Migration a batch of actions. |
|
92 | + * |
|
93 | + * @param array $action_ids List of action IDs to migrate. |
|
94 | + */ |
|
95 | + public function migrate_actions( array $action_ids ) { |
|
96 | + do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
97 | + |
|
98 | + \ActionScheduler::logger()->unhook_stored_action(); |
|
99 | + $this->destination_logger->unhook_stored_action(); |
|
100 | + |
|
101 | + foreach ( $action_ids as $source_action_id ) { |
|
102 | + $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | + if ( $destination_action_id ) { |
|
104 | + $this->destination_logger->log( |
|
105 | + $destination_action_id, |
|
106 | + sprintf( |
|
107 | + /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
|
108 | + __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
109 | + $source_action_id, |
|
110 | + get_class( $this->source_store ), |
|
111 | + $destination_action_id, |
|
112 | + get_class( $this->destination_store ) |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + if ( $this->progress_bar ) { |
|
118 | + $this->progress_bar->tick(); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + if ( $this->progress_bar ) { |
|
123 | + $this->progress_bar->finish(); |
|
124 | + } |
|
125 | + |
|
126 | + \ActionScheduler::logger()->hook_stored_action(); |
|
127 | + |
|
128 | + do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Initialize destination store and logger. |
|
133 | + */ |
|
134 | + public function init_destination() { |
|
135 | + $this->destination_store->init(); |
|
136 | + $this->destination_logger->init(); |
|
137 | + } |
|
138 | 138 | } |
@@ -41,22 +41,22 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @param Config $config Migration configuration object. |
43 | 43 | */ |
44 | - public function __construct( Config $config ) { |
|
44 | + public function __construct(Config $config) { |
|
45 | 45 | $this->source_store = $config->get_source_store(); |
46 | 46 | $this->destination_store = $config->get_destination_store(); |
47 | 47 | $this->source_logger = $config->get_source_logger(); |
48 | 48 | $this->destination_logger = $config->get_destination_logger(); |
49 | 49 | |
50 | - $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | - if ( $config->get_dry_run() ) { |
|
52 | - $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | - $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
50 | + $this->batch_fetcher = new BatchFetcher($this->source_store); |
|
51 | + if ($config->get_dry_run()) { |
|
52 | + $this->log_migrator = new DryRun_LogMigrator($this->source_logger, $this->destination_logger); |
|
53 | + $this->action_migrator = new DryRun_ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator); |
|
54 | 54 | } else { |
55 | - $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | - $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
55 | + $this->log_migrator = new LogMigrator($this->source_logger, $this->destination_logger); |
|
56 | + $this->action_migrator = new ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator); |
|
57 | 57 | } |
58 | 58 | |
59 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
59 | + if (defined('WP_CLI') && WP_CLI) { |
|
60 | 60 | $this->progress_bar = $config->get_progress_bar(); |
61 | 61 | } |
62 | 62 | } |
@@ -68,21 +68,21 @@ discard block |
||
68 | 68 | * |
69 | 69 | * @return int Size of batch processed. |
70 | 70 | */ |
71 | - public function run( $batch_size = 10 ) { |
|
72 | - $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | - $batch_size = count( $batch ); |
|
71 | + public function run($batch_size = 10) { |
|
72 | + $batch = $this->batch_fetcher->fetch($batch_size); |
|
73 | + $batch_size = count($batch); |
|
74 | 74 | |
75 | - if ( ! $batch_size ) { |
|
75 | + if ( ! $batch_size) { |
|
76 | 76 | return 0; |
77 | 77 | } |
78 | 78 | |
79 | - if ( $this->progress_bar ) { |
|
79 | + if ($this->progress_bar) { |
|
80 | 80 | /* translators: %d: amount of actions */ |
81 | - $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | - $this->progress_bar->set_count( $batch_size ); |
|
81 | + $this->progress_bar->set_message(sprintf(_n('Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler'), number_format_i18n($batch_size))); |
|
82 | + $this->progress_bar->set_count($batch_size); |
|
83 | 83 | } |
84 | 84 | |
85 | - $this->migrate_actions( $batch ); |
|
85 | + $this->migrate_actions($batch); |
|
86 | 86 | |
87 | 87 | return $batch_size; |
88 | 88 | } |
@@ -92,40 +92,40 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @param array $action_ids List of action IDs to migrate. |
94 | 94 | */ |
95 | - public function migrate_actions( array $action_ids ) { |
|
96 | - do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
95 | + public function migrate_actions(array $action_ids) { |
|
96 | + do_action('action_scheduler/migration_batch_starting', $action_ids); |
|
97 | 97 | |
98 | 98 | \ActionScheduler::logger()->unhook_stored_action(); |
99 | 99 | $this->destination_logger->unhook_stored_action(); |
100 | 100 | |
101 | - foreach ( $action_ids as $source_action_id ) { |
|
102 | - $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | - if ( $destination_action_id ) { |
|
101 | + foreach ($action_ids as $source_action_id) { |
|
102 | + $destination_action_id = $this->action_migrator->migrate($source_action_id); |
|
103 | + if ($destination_action_id) { |
|
104 | 104 | $this->destination_logger->log( |
105 | 105 | $destination_action_id, |
106 | 106 | sprintf( |
107 | 107 | /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
108 | - __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
108 | + __('Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler'), |
|
109 | 109 | $source_action_id, |
110 | - get_class( $this->source_store ), |
|
110 | + get_class($this->source_store), |
|
111 | 111 | $destination_action_id, |
112 | - get_class( $this->destination_store ) |
|
112 | + get_class($this->destination_store) |
|
113 | 113 | ) |
114 | 114 | ); |
115 | 115 | } |
116 | 116 | |
117 | - if ( $this->progress_bar ) { |
|
117 | + if ($this->progress_bar) { |
|
118 | 118 | $this->progress_bar->tick(); |
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - if ( $this->progress_bar ) { |
|
122 | + if ($this->progress_bar) { |
|
123 | 123 | $this->progress_bar->finish(); |
124 | 124 | } |
125 | 125 | |
126 | 126 | \ActionScheduler::logger()->hook_stored_action(); |
127 | 127 | |
128 | - do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
128 | + do_action('action_scheduler/migration_batch_complete', $action_ids); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |