@@ -7,56 +7,56 @@ |
||
7 | 7 | */ |
8 | 8 | abstract class ActionScheduler_Lock { |
9 | 9 | |
10 | - /** @var ActionScheduler_Lock */ |
|
11 | - private static $locker = null; |
|
12 | - |
|
13 | - /** @var int */ |
|
14 | - protected static $lock_duration = MINUTE_IN_SECONDS; |
|
15 | - |
|
16 | - /** |
|
17 | - * Check if a lock is set for a given lock type. |
|
18 | - * |
|
19 | - * @param string $lock_type A string to identify different lock types. |
|
20 | - * @return bool |
|
21 | - */ |
|
22 | - public function is_locked( $lock_type ) { |
|
23 | - return ( $this->get_expiration( $lock_type ) >= time() ); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Set a lock. |
|
28 | - * |
|
29 | - * @param string $lock_type A string to identify different lock types. |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - abstract public function set( $lock_type ); |
|
33 | - |
|
34 | - /** |
|
35 | - * If a lock is set, return the timestamp it was set to expiry. |
|
36 | - * |
|
37 | - * @param string $lock_type A string to identify different lock types. |
|
38 | - * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
|
39 | - */ |
|
40 | - abstract public function get_expiration( $lock_type ); |
|
41 | - |
|
42 | - /** |
|
43 | - * Get the amount of time to set for a given lock. 60 seconds by default. |
|
44 | - * |
|
45 | - * @param string $lock_type A string to identify different lock types. |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - protected function get_duration( $lock_type ) { |
|
49 | - return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return ActionScheduler_Lock |
|
54 | - */ |
|
55 | - public static function instance() { |
|
56 | - if ( empty( self::$locker ) ) { |
|
57 | - $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
58 | - self::$locker = new $class(); |
|
59 | - } |
|
60 | - return self::$locker; |
|
61 | - } |
|
10 | + /** @var ActionScheduler_Lock */ |
|
11 | + private static $locker = null; |
|
12 | + |
|
13 | + /** @var int */ |
|
14 | + protected static $lock_duration = MINUTE_IN_SECONDS; |
|
15 | + |
|
16 | + /** |
|
17 | + * Check if a lock is set for a given lock type. |
|
18 | + * |
|
19 | + * @param string $lock_type A string to identify different lock types. |
|
20 | + * @return bool |
|
21 | + */ |
|
22 | + public function is_locked( $lock_type ) { |
|
23 | + return ( $this->get_expiration( $lock_type ) >= time() ); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Set a lock. |
|
28 | + * |
|
29 | + * @param string $lock_type A string to identify different lock types. |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + abstract public function set( $lock_type ); |
|
33 | + |
|
34 | + /** |
|
35 | + * If a lock is set, return the timestamp it was set to expiry. |
|
36 | + * |
|
37 | + * @param string $lock_type A string to identify different lock types. |
|
38 | + * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
|
39 | + */ |
|
40 | + abstract public function get_expiration( $lock_type ); |
|
41 | + |
|
42 | + /** |
|
43 | + * Get the amount of time to set for a given lock. 60 seconds by default. |
|
44 | + * |
|
45 | + * @param string $lock_type A string to identify different lock types. |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + protected function get_duration( $lock_type ) { |
|
49 | + return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return ActionScheduler_Lock |
|
54 | + */ |
|
55 | + public static function instance() { |
|
56 | + if ( empty( self::$locker ) ) { |
|
57 | + $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
58 | + self::$locker = new $class(); |
|
59 | + } |
|
60 | + return self::$locker; |
|
61 | + } |
|
62 | 62 | } |
@@ -19,8 +19,8 @@ discard block |
||
19 | 19 | * @param string $lock_type A string to identify different lock types. |
20 | 20 | * @return bool |
21 | 21 | */ |
22 | - public function is_locked( $lock_type ) { |
|
23 | - return ( $this->get_expiration( $lock_type ) >= time() ); |
|
22 | + public function is_locked($lock_type) { |
|
23 | + return ($this->get_expiration($lock_type) >= time()); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * @param string $lock_type A string to identify different lock types. |
30 | 30 | * @return bool |
31 | 31 | */ |
32 | - abstract public function set( $lock_type ); |
|
32 | + abstract public function set($lock_type); |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * If a lock is set, return the timestamp it was set to expiry. |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param string $lock_type A string to identify different lock types. |
38 | 38 | * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
39 | 39 | */ |
40 | - abstract public function get_expiration( $lock_type ); |
|
40 | + abstract public function get_expiration($lock_type); |
|
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Get the amount of time to set for a given lock. 60 seconds by default. |
@@ -45,16 +45,16 @@ discard block |
||
45 | 45 | * @param string $lock_type A string to identify different lock types. |
46 | 46 | * @return int |
47 | 47 | */ |
48 | - protected function get_duration( $lock_type ) { |
|
49 | - return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
48 | + protected function get_duration($lock_type) { |
|
49 | + return apply_filters('action_scheduler_lock_duration', self::$lock_duration, $lock_type); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * @return ActionScheduler_Lock |
54 | 54 | */ |
55 | 55 | public static function instance() { |
56 | - if ( empty( self::$locker ) ) { |
|
57 | - $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
56 | + if (empty(self::$locker)) { |
|
57 | + $class = apply_filters('action_scheduler_lock_class', 'ActionScheduler_OptionLock'); |
|
58 | 58 | self::$locker = new $class(); |
59 | 59 | } |
60 | 60 | return self::$locker; |
@@ -6,171 +6,171 @@ |
||
6 | 6 | * @codeCoverageIgnore |
7 | 7 | */ |
8 | 8 | abstract class ActionScheduler_Logger { |
9 | - private static $logger = null; |
|
10 | - |
|
11 | - /** |
|
12 | - * @return ActionScheduler_Logger |
|
13 | - */ |
|
14 | - public static function instance() { |
|
15 | - if ( empty( self::$logger ) ) { |
|
16 | - $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
17 | - self::$logger = new $class(); |
|
18 | - } |
|
19 | - return self::$logger; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param string $action_id |
|
24 | - * @param string $message |
|
25 | - * @param DateTime $date |
|
26 | - * |
|
27 | - * @return string The log entry ID |
|
28 | - */ |
|
29 | - abstract public function log( $action_id, $message, DateTime $date = null ); |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string $entry_id |
|
33 | - * |
|
34 | - * @return ActionScheduler_LogEntry |
|
35 | - */ |
|
36 | - abstract public function get_entry( $entry_id ); |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $action_id |
|
40 | - * |
|
41 | - * @return ActionScheduler_LogEntry[] |
|
42 | - */ |
|
43 | - abstract public function get_logs( $action_id ); |
|
44 | - |
|
45 | - /** |
|
46 | - * @codeCoverageIgnore |
|
47 | - */ |
|
48 | - public function init() { |
|
49 | - $this->hook_stored_action(); |
|
50 | - add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | - add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | - add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | - add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | - add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | - add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | - add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | - add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | - add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | - add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | - add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
61 | - } |
|
62 | - |
|
63 | - public function hook_stored_action() { |
|
64 | - add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
65 | - } |
|
66 | - |
|
67 | - public function unhook_stored_action() { |
|
68 | - remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
69 | - } |
|
70 | - |
|
71 | - public function log_stored_action( $action_id ) { |
|
72 | - $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
73 | - } |
|
74 | - |
|
75 | - public function log_canceled_action( $action_id ) { |
|
76 | - $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
77 | - } |
|
78 | - |
|
79 | - public function log_started_action( $action_id, $context = '' ) { |
|
80 | - if ( ! empty( $context ) ) { |
|
81 | - /* translators: %s: context */ |
|
82 | - $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
83 | - } else { |
|
84 | - $message = __( 'action started', 'action-scheduler' ); |
|
85 | - } |
|
86 | - $this->log( $action_id, $message ); |
|
87 | - } |
|
88 | - |
|
89 | - public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | - if ( ! empty( $context ) ) { |
|
91 | - /* translators: %s: context */ |
|
92 | - $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
93 | - } else { |
|
94 | - $message = __( 'action complete', 'action-scheduler' ); |
|
95 | - } |
|
96 | - $this->log( $action_id, $message ); |
|
97 | - } |
|
98 | - |
|
99 | - public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | - if ( ! empty( $context ) ) { |
|
101 | - /* translators: 1: context 2: exception message */ |
|
102 | - $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
103 | - } else { |
|
104 | - /* translators: %s: exception message */ |
|
105 | - $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
106 | - } |
|
107 | - $this->log( $action_id, $message ); |
|
108 | - } |
|
109 | - |
|
110 | - public function log_timed_out_action( $action_id, $timeout ) { |
|
111 | - /* translators: %s: amount of time */ |
|
112 | - $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
113 | - } |
|
114 | - |
|
115 | - public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | - if ( ! empty( $error ) ) { |
|
117 | - /* translators: 1: error message 2: filename 3: line */ |
|
118 | - $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - public function log_reset_action( $action_id ) { |
|
123 | - $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
124 | - } |
|
125 | - |
|
126 | - public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | - if ( ! empty( $context ) ) { |
|
128 | - /* translators: %s: context */ |
|
129 | - $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
130 | - } else { |
|
131 | - $message = __( 'action ignored', 'action-scheduler' ); |
|
132 | - } |
|
133 | - $this->log( $action_id, $message ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $action_id |
|
138 | - * @param Exception|NULL $exception The exception which occured when fetching the action. NULL by default for backward compatibility. |
|
139 | - * |
|
140 | - * @return ActionScheduler_LogEntry[] |
|
141 | - */ |
|
142 | - public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
143 | - |
|
144 | - if ( $exception !== null ) { |
|
145 | - /* translators: %s: exception message */ |
|
146 | - $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
147 | - } else { |
|
148 | - $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
149 | - } |
|
150 | - |
|
151 | - $this->log( $action_id, $log_message ); |
|
152 | - } |
|
153 | - |
|
154 | - public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
155 | - /* translators: %s: exception message */ |
|
156 | - $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Bulk add cancel action log entries. |
|
161 | - * |
|
162 | - * Implemented here for backward compatibility. Should be implemented in parent loggers |
|
163 | - * for more performant bulk logging. |
|
164 | - * |
|
165 | - * @param array $action_ids List of action ID. |
|
166 | - */ |
|
167 | - public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | - if ( empty( $action_ids ) ) { |
|
169 | - return; |
|
170 | - } |
|
171 | - |
|
172 | - foreach ( $action_ids as $action_id ) { |
|
173 | - $this->log_canceled_action( $action_id ); |
|
174 | - } |
|
175 | - } |
|
9 | + private static $logger = null; |
|
10 | + |
|
11 | + /** |
|
12 | + * @return ActionScheduler_Logger |
|
13 | + */ |
|
14 | + public static function instance() { |
|
15 | + if ( empty( self::$logger ) ) { |
|
16 | + $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
17 | + self::$logger = new $class(); |
|
18 | + } |
|
19 | + return self::$logger; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param string $action_id |
|
24 | + * @param string $message |
|
25 | + * @param DateTime $date |
|
26 | + * |
|
27 | + * @return string The log entry ID |
|
28 | + */ |
|
29 | + abstract public function log( $action_id, $message, DateTime $date = null ); |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string $entry_id |
|
33 | + * |
|
34 | + * @return ActionScheduler_LogEntry |
|
35 | + */ |
|
36 | + abstract public function get_entry( $entry_id ); |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $action_id |
|
40 | + * |
|
41 | + * @return ActionScheduler_LogEntry[] |
|
42 | + */ |
|
43 | + abstract public function get_logs( $action_id ); |
|
44 | + |
|
45 | + /** |
|
46 | + * @codeCoverageIgnore |
|
47 | + */ |
|
48 | + public function init() { |
|
49 | + $this->hook_stored_action(); |
|
50 | + add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | + add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | + add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | + add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | + add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | + add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | + add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | + add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | + add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | + add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | + add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
61 | + } |
|
62 | + |
|
63 | + public function hook_stored_action() { |
|
64 | + add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
65 | + } |
|
66 | + |
|
67 | + public function unhook_stored_action() { |
|
68 | + remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
69 | + } |
|
70 | + |
|
71 | + public function log_stored_action( $action_id ) { |
|
72 | + $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
73 | + } |
|
74 | + |
|
75 | + public function log_canceled_action( $action_id ) { |
|
76 | + $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
77 | + } |
|
78 | + |
|
79 | + public function log_started_action( $action_id, $context = '' ) { |
|
80 | + if ( ! empty( $context ) ) { |
|
81 | + /* translators: %s: context */ |
|
82 | + $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
83 | + } else { |
|
84 | + $message = __( 'action started', 'action-scheduler' ); |
|
85 | + } |
|
86 | + $this->log( $action_id, $message ); |
|
87 | + } |
|
88 | + |
|
89 | + public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | + if ( ! empty( $context ) ) { |
|
91 | + /* translators: %s: context */ |
|
92 | + $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
93 | + } else { |
|
94 | + $message = __( 'action complete', 'action-scheduler' ); |
|
95 | + } |
|
96 | + $this->log( $action_id, $message ); |
|
97 | + } |
|
98 | + |
|
99 | + public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | + if ( ! empty( $context ) ) { |
|
101 | + /* translators: 1: context 2: exception message */ |
|
102 | + $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
103 | + } else { |
|
104 | + /* translators: %s: exception message */ |
|
105 | + $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
106 | + } |
|
107 | + $this->log( $action_id, $message ); |
|
108 | + } |
|
109 | + |
|
110 | + public function log_timed_out_action( $action_id, $timeout ) { |
|
111 | + /* translators: %s: amount of time */ |
|
112 | + $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
113 | + } |
|
114 | + |
|
115 | + public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | + if ( ! empty( $error ) ) { |
|
117 | + /* translators: 1: error message 2: filename 3: line */ |
|
118 | + $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + public function log_reset_action( $action_id ) { |
|
123 | + $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
124 | + } |
|
125 | + |
|
126 | + public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | + if ( ! empty( $context ) ) { |
|
128 | + /* translators: %s: context */ |
|
129 | + $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
130 | + } else { |
|
131 | + $message = __( 'action ignored', 'action-scheduler' ); |
|
132 | + } |
|
133 | + $this->log( $action_id, $message ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $action_id |
|
138 | + * @param Exception|NULL $exception The exception which occured when fetching the action. NULL by default for backward compatibility. |
|
139 | + * |
|
140 | + * @return ActionScheduler_LogEntry[] |
|
141 | + */ |
|
142 | + public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
143 | + |
|
144 | + if ( $exception !== null ) { |
|
145 | + /* translators: %s: exception message */ |
|
146 | + $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
147 | + } else { |
|
148 | + $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
149 | + } |
|
150 | + |
|
151 | + $this->log( $action_id, $log_message ); |
|
152 | + } |
|
153 | + |
|
154 | + public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
155 | + /* translators: %s: exception message */ |
|
156 | + $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Bulk add cancel action log entries. |
|
161 | + * |
|
162 | + * Implemented here for backward compatibility. Should be implemented in parent loggers |
|
163 | + * for more performant bulk logging. |
|
164 | + * |
|
165 | + * @param array $action_ids List of action ID. |
|
166 | + */ |
|
167 | + public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | + if ( empty( $action_ids ) ) { |
|
169 | + return; |
|
170 | + } |
|
171 | + |
|
172 | + foreach ( $action_ids as $action_id ) { |
|
173 | + $this->log_canceled_action( $action_id ); |
|
174 | + } |
|
175 | + } |
|
176 | 176 | } |
@@ -12,8 +12,8 @@ discard block |
||
12 | 12 | * @return ActionScheduler_Logger |
13 | 13 | */ |
14 | 14 | public static function instance() { |
15 | - if ( empty( self::$logger ) ) { |
|
16 | - $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
15 | + if (empty(self::$logger)) { |
|
16 | + $class = apply_filters('action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger'); |
|
17 | 17 | self::$logger = new $class(); |
18 | 18 | } |
19 | 19 | return self::$logger; |
@@ -26,111 +26,111 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @return string The log entry ID |
28 | 28 | */ |
29 | - abstract public function log( $action_id, $message, DateTime $date = null ); |
|
29 | + abstract public function log($action_id, $message, DateTime $date = null); |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * @param string $entry_id |
33 | 33 | * |
34 | 34 | * @return ActionScheduler_LogEntry |
35 | 35 | */ |
36 | - abstract public function get_entry( $entry_id ); |
|
36 | + abstract public function get_entry($entry_id); |
|
37 | 37 | |
38 | 38 | /** |
39 | 39 | * @param string $action_id |
40 | 40 | * |
41 | 41 | * @return ActionScheduler_LogEntry[] |
42 | 42 | */ |
43 | - abstract public function get_logs( $action_id ); |
|
43 | + abstract public function get_logs($action_id); |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @codeCoverageIgnore |
47 | 47 | */ |
48 | 48 | public function init() { |
49 | 49 | $this->hook_stored_action(); |
50 | - add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | - add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | - add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | - add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | - add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | - add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | - add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | - add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | - add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | - add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | - add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
50 | + add_action('action_scheduler_canceled_action', array($this, 'log_canceled_action'), 10, 1); |
|
51 | + add_action('action_scheduler_begin_execute', array($this, 'log_started_action'), 10, 2); |
|
52 | + add_action('action_scheduler_after_execute', array($this, 'log_completed_action'), 10, 3); |
|
53 | + add_action('action_scheduler_failed_execution', array($this, 'log_failed_action'), 10, 3); |
|
54 | + add_action('action_scheduler_failed_action', array($this, 'log_timed_out_action'), 10, 2); |
|
55 | + add_action('action_scheduler_unexpected_shutdown', array($this, 'log_unexpected_shutdown'), 10, 2); |
|
56 | + add_action('action_scheduler_reset_action', array($this, 'log_reset_action'), 10, 1); |
|
57 | + add_action('action_scheduler_execution_ignored', array($this, 'log_ignored_action'), 10, 2); |
|
58 | + add_action('action_scheduler_failed_fetch_action', array($this, 'log_failed_fetch_action'), 10, 2); |
|
59 | + add_action('action_scheduler_failed_to_schedule_next_instance', array($this, 'log_failed_schedule_next_instance'), 10, 2); |
|
60 | + add_action('action_scheduler_bulk_cancel_actions', array($this, 'bulk_log_cancel_actions'), 10, 1); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | public function hook_stored_action() { |
64 | - add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
64 | + add_action('action_scheduler_stored_action', array($this, 'log_stored_action')); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | public function unhook_stored_action() { |
68 | - remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
68 | + remove_action('action_scheduler_stored_action', array($this, 'log_stored_action')); |
|
69 | 69 | } |
70 | 70 | |
71 | - public function log_stored_action( $action_id ) { |
|
72 | - $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
71 | + public function log_stored_action($action_id) { |
|
72 | + $this->log($action_id, __('action created', 'action-scheduler')); |
|
73 | 73 | } |
74 | 74 | |
75 | - public function log_canceled_action( $action_id ) { |
|
76 | - $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
75 | + public function log_canceled_action($action_id) { |
|
76 | + $this->log($action_id, __('action canceled', 'action-scheduler')); |
|
77 | 77 | } |
78 | 78 | |
79 | - public function log_started_action( $action_id, $context = '' ) { |
|
80 | - if ( ! empty( $context ) ) { |
|
79 | + public function log_started_action($action_id, $context = '') { |
|
80 | + if ( ! empty($context)) { |
|
81 | 81 | /* translators: %s: context */ |
82 | - $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
82 | + $message = sprintf(__('action started via %s', 'action-scheduler'), $context); |
|
83 | 83 | } else { |
84 | - $message = __( 'action started', 'action-scheduler' ); |
|
84 | + $message = __('action started', 'action-scheduler'); |
|
85 | 85 | } |
86 | - $this->log( $action_id, $message ); |
|
86 | + $this->log($action_id, $message); |
|
87 | 87 | } |
88 | 88 | |
89 | - public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | - if ( ! empty( $context ) ) { |
|
89 | + public function log_completed_action($action_id, $action = null, $context = '') { |
|
90 | + if ( ! empty($context)) { |
|
91 | 91 | /* translators: %s: context */ |
92 | - $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
92 | + $message = sprintf(__('action complete via %s', 'action-scheduler'), $context); |
|
93 | 93 | } else { |
94 | - $message = __( 'action complete', 'action-scheduler' ); |
|
94 | + $message = __('action complete', 'action-scheduler'); |
|
95 | 95 | } |
96 | - $this->log( $action_id, $message ); |
|
96 | + $this->log($action_id, $message); |
|
97 | 97 | } |
98 | 98 | |
99 | - public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | - if ( ! empty( $context ) ) { |
|
99 | + public function log_failed_action($action_id, Exception $exception, $context = '') { |
|
100 | + if ( ! empty($context)) { |
|
101 | 101 | /* translators: 1: context 2: exception message */ |
102 | - $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
102 | + $message = sprintf(__('action failed via %1$s: %2$s', 'action-scheduler'), $context, $exception->getMessage()); |
|
103 | 103 | } else { |
104 | 104 | /* translators: %s: exception message */ |
105 | - $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
105 | + $message = sprintf(__('action failed: %s', 'action-scheduler'), $exception->getMessage()); |
|
106 | 106 | } |
107 | - $this->log( $action_id, $message ); |
|
107 | + $this->log($action_id, $message); |
|
108 | 108 | } |
109 | 109 | |
110 | - public function log_timed_out_action( $action_id, $timeout ) { |
|
110 | + public function log_timed_out_action($action_id, $timeout) { |
|
111 | 111 | /* translators: %s: amount of time */ |
112 | - $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
112 | + $this->log($action_id, sprintf(__('action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler'), $timeout)); |
|
113 | 113 | } |
114 | 114 | |
115 | - public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | - if ( ! empty( $error ) ) { |
|
115 | + public function log_unexpected_shutdown($action_id, $error) { |
|
116 | + if ( ! empty($error)) { |
|
117 | 117 | /* translators: 1: error message 2: filename 3: line */ |
118 | - $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
118 | + $this->log($action_id, sprintf(__('unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler'), $error['message'], $error['file'], $error['line'])); |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - public function log_reset_action( $action_id ) { |
|
123 | - $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
122 | + public function log_reset_action($action_id) { |
|
123 | + $this->log($action_id, __('action reset', 'action-scheduler')); |
|
124 | 124 | } |
125 | 125 | |
126 | - public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | - if ( ! empty( $context ) ) { |
|
126 | + public function log_ignored_action($action_id, $context = '') { |
|
127 | + if ( ! empty($context)) { |
|
128 | 128 | /* translators: %s: context */ |
129 | - $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
129 | + $message = sprintf(__('action ignored via %s', 'action-scheduler'), $context); |
|
130 | 130 | } else { |
131 | - $message = __( 'action ignored', 'action-scheduler' ); |
|
131 | + $message = __('action ignored', 'action-scheduler'); |
|
132 | 132 | } |
133 | - $this->log( $action_id, $message ); |
|
133 | + $this->log($action_id, $message); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -139,21 +139,21 @@ discard block |
||
139 | 139 | * |
140 | 140 | * @return ActionScheduler_LogEntry[] |
141 | 141 | */ |
142 | - public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
142 | + public function log_failed_fetch_action($action_id, Exception $exception = null) { |
|
143 | 143 | |
144 | - if ( $exception !== null ) { |
|
144 | + if ($exception !== null) { |
|
145 | 145 | /* translators: %s: exception message */ |
146 | - $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
146 | + $log_message = sprintf(__('There was a failure fetching this action: %s', 'action-scheduler'), $exception->getMessage()); |
|
147 | 147 | } else { |
148 | - $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
148 | + $log_message = __('There was a failure fetching this action', 'action-scheduler'); |
|
149 | 149 | } |
150 | 150 | |
151 | - $this->log( $action_id, $log_message ); |
|
151 | + $this->log($action_id, $log_message); |
|
152 | 152 | } |
153 | 153 | |
154 | - public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
154 | + public function log_failed_schedule_next_instance($action_id, Exception $exception) { |
|
155 | 155 | /* translators: %s: exception message */ |
156 | - $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
156 | + $this->log($action_id, sprintf(__('There was a failure scheduling the next instance of this action: %s', 'action-scheduler'), $exception->getMessage())); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | /** |
@@ -164,13 +164,13 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @param array $action_ids List of action ID. |
166 | 166 | */ |
167 | - public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | - if ( empty( $action_ids ) ) { |
|
167 | + public function bulk_log_cancel_actions($action_ids) { |
|
168 | + if (empty($action_ids)) { |
|
169 | 169 | return; |
170 | 170 | } |
171 | 171 | |
172 | - foreach ( $action_ids as $action_id ) { |
|
173 | - $this->log_canceled_action( $action_id ); |
|
172 | + foreach ($action_ids as $action_id) { |
|
173 | + $this->log_canceled_action($action_id); |
|
174 | 174 | } |
175 | 175 | } |
176 | 176 | } |
@@ -6,450 +6,450 @@ |
||
6 | 6 | * @codeCoverageIgnore |
7 | 7 | */ |
8 | 8 | abstract class ActionScheduler_Store extends ActionScheduler_Store_Deprecated { |
9 | - const STATUS_COMPLETE = 'complete'; |
|
10 | - const STATUS_PENDING = 'pending'; |
|
11 | - const STATUS_RUNNING = 'in-progress'; |
|
12 | - const STATUS_FAILED = 'failed'; |
|
13 | - const STATUS_CANCELED = 'canceled'; |
|
14 | - const DEFAULT_CLASS = 'ActionScheduler_wpPostStore'; |
|
15 | - |
|
16 | - /** @var ActionScheduler_Store */ |
|
17 | - private static $store = null; |
|
18 | - |
|
19 | - /** @var int */ |
|
20 | - protected static $max_args_length = 191; |
|
21 | - |
|
22 | - /** |
|
23 | - * @param ActionScheduler_Action $action |
|
24 | - * @param DateTime $scheduled_date Optional Date of the first instance |
|
25 | - * to store. Otherwise uses the first date of the action's |
|
26 | - * schedule. |
|
27 | - * |
|
28 | - * @return int The action ID |
|
29 | - */ |
|
30 | - abstract public function save_action( ActionScheduler_Action $action, DateTime $scheduled_date = null ); |
|
31 | - |
|
32 | - /** |
|
33 | - * @param string $action_id |
|
34 | - * |
|
35 | - * @return ActionScheduler_Action |
|
36 | - */ |
|
37 | - abstract public function fetch_action( $action_id ); |
|
38 | - |
|
39 | - /** |
|
40 | - * Find an action. |
|
41 | - * |
|
42 | - * Note: the query ordering changes based on the passed 'status' value. |
|
43 | - * |
|
44 | - * @param string $hook Action hook. |
|
45 | - * @param array $params Parameters of the action to find. |
|
46 | - * |
|
47 | - * @return string|null ID of the next action matching the criteria or NULL if not found. |
|
48 | - */ |
|
49 | - public function find_action( $hook, $params = array() ) { |
|
50 | - $params = wp_parse_args( |
|
51 | - $params, |
|
52 | - array( |
|
53 | - 'args' => null, |
|
54 | - 'status' => self::STATUS_PENDING, |
|
55 | - 'group' => '', |
|
56 | - ) |
|
57 | - ); |
|
58 | - |
|
59 | - // These params are fixed for this method. |
|
60 | - $params['hook'] = $hook; |
|
61 | - $params['orderby'] = 'date'; |
|
62 | - $params['per_page'] = 1; |
|
63 | - |
|
64 | - if ( ! empty( $params['status'] ) ) { |
|
65 | - if ( self::STATUS_PENDING === $params['status'] ) { |
|
66 | - $params['order'] = 'ASC'; // Find the next action that matches. |
|
67 | - } else { |
|
68 | - $params['order'] = 'DESC'; // Find the most recent action that matches. |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - $results = $this->query_actions( $params ); |
|
73 | - |
|
74 | - return empty( $results ) ? null : $results[0]; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Query for action count or list of action IDs. |
|
79 | - * |
|
80 | - * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
81 | - * |
|
82 | - * @param array $query { |
|
83 | - * Query filtering options. |
|
84 | - * |
|
85 | - * @type string $hook The name of the actions. Optional. |
|
86 | - * @type string|array $status The status or statuses of the actions. Optional. |
|
87 | - * @type array $args The args array of the actions. Optional. |
|
88 | - * @type DateTime $date The scheduled date of the action. Used in UTC timezone. Optional. |
|
89 | - * @type string $date_compare Operator for selecting by $date param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='. |
|
90 | - * @type DateTime $modified The last modified date of the action. Used in UTC timezone. Optional. |
|
91 | - * @type string $modified_compare Operator for comparing $modified param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='. |
|
92 | - * @type string $group The group the action belongs to. Optional. |
|
93 | - * @type bool|int $claimed TRUE to find claimed actions, FALSE to find unclaimed actions, an int to find a specific claim ID. Optional. |
|
94 | - * @type int $per_page Number of results to return. Defaults to 5. |
|
95 | - * @type int $offset The query pagination offset. Defaults to 0. |
|
96 | - * @type int $orderby Accepted values are 'hook', 'group', 'modified', 'date' or 'none'. Defaults to 'date'. |
|
97 | - * @type string $order Accepted values are 'ASC' or 'DESC'. Defaults to 'ASC'. |
|
98 | - * } |
|
99 | - * @param string $query_type Whether to select or count the results. Default, select. |
|
100 | - * |
|
101 | - * @return string|array|null The IDs of actions matching the query. Null on failure. |
|
102 | - */ |
|
103 | - abstract public function query_actions( $query = array(), $query_type = 'select' ); |
|
104 | - |
|
105 | - /** |
|
106 | - * Run query to get a single action ID. |
|
107 | - * |
|
108 | - * @since 3.3.0 |
|
109 | - * |
|
110 | - * @see ActionScheduler_Store::query_actions for $query arg usage but 'per_page' and 'offset' can't be used. |
|
111 | - * |
|
112 | - * @param array $query Query parameters. |
|
113 | - * |
|
114 | - * @return int|null |
|
115 | - */ |
|
116 | - public function query_action( $query ) { |
|
117 | - $query['per_page'] = 1; |
|
118 | - $query['offset'] = 0; |
|
119 | - $results = $this->query_actions( $query ); |
|
120 | - |
|
121 | - if ( empty( $results ) ) { |
|
122 | - return null; |
|
123 | - } else { |
|
124 | - return (int) $results[0]; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Get a count of all actions in the store, grouped by status |
|
130 | - * |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - abstract public function action_counts(); |
|
134 | - |
|
135 | - /** |
|
136 | - * Get additional action counts. |
|
137 | - * |
|
138 | - * - add past-due actions |
|
139 | - * |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - public function extra_action_counts() { |
|
143 | - $extra_actions = array(); |
|
144 | - |
|
145 | - $pastdue_action_counts = (int) $this->query_actions( |
|
146 | - array( |
|
147 | - 'status' => self::STATUS_PENDING, |
|
148 | - 'date' => as_get_datetime_object(), |
|
149 | - ), |
|
150 | - 'count' |
|
151 | - ); |
|
152 | - |
|
153 | - if ( $pastdue_action_counts ) { |
|
154 | - $extra_actions['past-due'] = $pastdue_action_counts; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Allows 3rd party code to add extra action counts (used in filters in the list table). |
|
159 | - * |
|
160 | - * @since 3.5.0 |
|
161 | - * @param $extra_actions array Array with format action_count_identifier => action count. |
|
162 | - */ |
|
163 | - return apply_filters( 'action_scheduler_extra_action_counts', $extra_actions ); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $action_id |
|
168 | - */ |
|
169 | - abstract public function cancel_action( $action_id ); |
|
170 | - |
|
171 | - /** |
|
172 | - * @param string $action_id |
|
173 | - */ |
|
174 | - abstract public function delete_action( $action_id ); |
|
175 | - |
|
176 | - /** |
|
177 | - * @param string $action_id |
|
178 | - * |
|
179 | - * @return DateTime The date the action is schedule to run, or the date that it ran. |
|
180 | - */ |
|
181 | - abstract public function get_date( $action_id ); |
|
182 | - |
|
183 | - /** |
|
184 | - * @param int $max_actions |
|
185 | - * @param DateTime $before_date Claim only actions schedule before the given date. Defaults to now. |
|
186 | - * @param array $hooks Claim only actions with a hook or hooks. |
|
187 | - * @param string $group Claim only actions in the given group. |
|
188 | - * |
|
189 | - * @return ActionScheduler_ActionClaim |
|
190 | - */ |
|
191 | - abstract public function stake_claim( $max_actions = 10, DateTime $before_date = null, $hooks = array(), $group = '' ); |
|
192 | - |
|
193 | - /** |
|
194 | - * @return int |
|
195 | - */ |
|
196 | - abstract public function get_claim_count(); |
|
197 | - |
|
198 | - /** |
|
199 | - * @param ActionScheduler_ActionClaim $claim |
|
200 | - */ |
|
201 | - abstract public function release_claim( ActionScheduler_ActionClaim $claim ); |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $action_id |
|
205 | - */ |
|
206 | - abstract public function unclaim_action( $action_id ); |
|
207 | - |
|
208 | - /** |
|
209 | - * @param string $action_id |
|
210 | - */ |
|
211 | - abstract public function mark_failure( $action_id ); |
|
212 | - |
|
213 | - /** |
|
214 | - * @param string $action_id |
|
215 | - */ |
|
216 | - abstract public function log_execution( $action_id ); |
|
217 | - |
|
218 | - /** |
|
219 | - * @param string $action_id |
|
220 | - */ |
|
221 | - abstract public function mark_complete( $action_id ); |
|
222 | - |
|
223 | - /** |
|
224 | - * @param string $action_id |
|
225 | - * |
|
226 | - * @return string |
|
227 | - */ |
|
228 | - abstract public function get_status( $action_id ); |
|
229 | - |
|
230 | - /** |
|
231 | - * @param string $action_id |
|
232 | - * @return mixed |
|
233 | - */ |
|
234 | - abstract public function get_claim_id( $action_id ); |
|
235 | - |
|
236 | - /** |
|
237 | - * @param string $claim_id |
|
238 | - * @return array |
|
239 | - */ |
|
240 | - abstract public function find_actions_by_claim_id( $claim_id ); |
|
241 | - |
|
242 | - /** |
|
243 | - * @param string $comparison_operator |
|
244 | - * @return string |
|
245 | - */ |
|
246 | - protected function validate_sql_comparator( $comparison_operator ) { |
|
247 | - if ( in_array( $comparison_operator, array( '!=', '>', '>=', '<', '<=', '=' ) ) ) { |
|
248 | - return $comparison_operator; |
|
249 | - } |
|
250 | - return '='; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Get the time MySQL formated date/time string for an action's (next) scheduled date. |
|
255 | - * |
|
256 | - * @param ActionScheduler_Action $action |
|
257 | - * @param DateTime $scheduled_date (optional) |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - protected function get_scheduled_date_string( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
261 | - $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
|
262 | - if ( ! $next ) { |
|
263 | - return '0000-00-00 00:00:00'; |
|
264 | - } |
|
265 | - $next->setTimezone( new DateTimeZone( 'UTC' ) ); |
|
266 | - |
|
267 | - return $next->format( 'Y-m-d H:i:s' ); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Get the time MySQL formated date/time string for an action's (next) scheduled date. |
|
272 | - * |
|
273 | - * @param ActionScheduler_Action $action |
|
274 | - * @param DateTime $scheduled_date (optional) |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - protected function get_scheduled_date_string_local( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
278 | - $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
|
279 | - if ( ! $next ) { |
|
280 | - return '0000-00-00 00:00:00'; |
|
281 | - } |
|
282 | - |
|
283 | - ActionScheduler_TimezoneHelper::set_local_timezone( $next ); |
|
284 | - return $next->format( 'Y-m-d H:i:s' ); |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Validate that we could decode action arguments. |
|
289 | - * |
|
290 | - * @param mixed $args The decoded arguments. |
|
291 | - * @param int $action_id The action ID. |
|
292 | - * |
|
293 | - * @throws ActionScheduler_InvalidActionException When the decoded arguments are invalid. |
|
294 | - */ |
|
295 | - protected function validate_args( $args, $action_id ) { |
|
296 | - // Ensure we have an array of args. |
|
297 | - if ( ! is_array( $args ) ) { |
|
298 | - throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id ); |
|
299 | - } |
|
300 | - |
|
301 | - // Validate JSON decoding if possible. |
|
302 | - if ( function_exists( 'json_last_error' ) && JSON_ERROR_NONE !== json_last_error() ) { |
|
303 | - throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id, $args ); |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Validate a ActionScheduler_Schedule object. |
|
309 | - * |
|
310 | - * @param mixed $schedule The unserialized ActionScheduler_Schedule object. |
|
311 | - * @param int $action_id The action ID. |
|
312 | - * |
|
313 | - * @throws ActionScheduler_InvalidActionException When the schedule is invalid. |
|
314 | - */ |
|
315 | - protected function validate_schedule( $schedule, $action_id ) { |
|
316 | - if ( empty( $schedule ) || ! is_a( $schedule, 'ActionScheduler_Schedule' ) ) { |
|
317 | - throw ActionScheduler_InvalidActionException::from_schedule( $action_id, $schedule ); |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * InnoDB indexes have a maximum size of 767 bytes by default, which is only 191 characters with utf8mb4. |
|
323 | - * |
|
324 | - * Previously, AS wasn't concerned about args length, as we used the (unindex) post_content column. However, |
|
325 | - * with custom tables, we use an indexed VARCHAR column instead. |
|
326 | - * |
|
327 | - * @param ActionScheduler_Action $action Action to be validated. |
|
328 | - * @throws InvalidArgumentException When json encoded args is too long. |
|
329 | - */ |
|
330 | - protected function validate_action( ActionScheduler_Action $action ) { |
|
331 | - if ( strlen( json_encode( $action->get_args() ) ) > static::$max_args_length ) { |
|
332 | - throw new InvalidArgumentException( sprintf( __( 'ActionScheduler_Action::$args too long. To ensure the args column can be indexed, action args should not be more than %d characters when encoded as JSON.', 'action-scheduler' ), static::$max_args_length ) ); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Cancel pending actions by hook. |
|
338 | - * |
|
339 | - * @since 3.0.0 |
|
340 | - * |
|
341 | - * @param string $hook Hook name. |
|
342 | - * |
|
343 | - * @return void |
|
344 | - */ |
|
345 | - public function cancel_actions_by_hook( $hook ) { |
|
346 | - $action_ids = true; |
|
347 | - while ( ! empty( $action_ids ) ) { |
|
348 | - $action_ids = $this->query_actions( |
|
349 | - array( |
|
350 | - 'hook' => $hook, |
|
351 | - 'status' => self::STATUS_PENDING, |
|
352 | - 'per_page' => 1000, |
|
353 | - 'orderby' => 'action_id', |
|
354 | - ) |
|
355 | - ); |
|
356 | - |
|
357 | - $this->bulk_cancel_actions( $action_ids ); |
|
358 | - } |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Cancel pending actions by group. |
|
363 | - * |
|
364 | - * @since 3.0.0 |
|
365 | - * |
|
366 | - * @param string $group Group slug. |
|
367 | - * |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - public function cancel_actions_by_group( $group ) { |
|
371 | - $action_ids = true; |
|
372 | - while ( ! empty( $action_ids ) ) { |
|
373 | - $action_ids = $this->query_actions( |
|
374 | - array( |
|
375 | - 'group' => $group, |
|
376 | - 'status' => self::STATUS_PENDING, |
|
377 | - 'per_page' => 1000, |
|
378 | - 'orderby' => 'action_id', |
|
379 | - ) |
|
380 | - ); |
|
381 | - |
|
382 | - $this->bulk_cancel_actions( $action_ids ); |
|
383 | - } |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Cancel a set of action IDs. |
|
388 | - * |
|
389 | - * @since 3.0.0 |
|
390 | - * |
|
391 | - * @param array $action_ids List of action IDs. |
|
392 | - * |
|
393 | - * @return void |
|
394 | - */ |
|
395 | - private function bulk_cancel_actions( $action_ids ) { |
|
396 | - foreach ( $action_ids as $action_id ) { |
|
397 | - $this->cancel_action( $action_id ); |
|
398 | - } |
|
399 | - |
|
400 | - do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * @return array |
|
405 | - */ |
|
406 | - public function get_status_labels() { |
|
407 | - return array( |
|
408 | - self::STATUS_COMPLETE => __( 'Complete', 'action-scheduler' ), |
|
409 | - self::STATUS_PENDING => __( 'Pending', 'action-scheduler' ), |
|
410 | - self::STATUS_RUNNING => __( 'In-progress', 'action-scheduler' ), |
|
411 | - self::STATUS_FAILED => __( 'Failed', 'action-scheduler' ), |
|
412 | - self::STATUS_CANCELED => __( 'Canceled', 'action-scheduler' ), |
|
413 | - ); |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * Check if there are any pending scheduled actions due to run. |
|
418 | - * |
|
419 | - * @param ActionScheduler_Action $action |
|
420 | - * @param DateTime $scheduled_date (optional) |
|
421 | - * @return string |
|
422 | - */ |
|
423 | - public function has_pending_actions_due() { |
|
424 | - $pending_actions = $this->query_actions( |
|
425 | - array( |
|
426 | - 'date' => as_get_datetime_object(), |
|
427 | - 'status' => self::STATUS_PENDING, |
|
428 | - 'orderby' => 'none', |
|
429 | - ) |
|
430 | - ); |
|
431 | - |
|
432 | - return ! empty( $pending_actions ); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Callable initialization function optionally overridden in derived classes. |
|
437 | - */ |
|
438 | - public function init() {} |
|
439 | - |
|
440 | - /** |
|
441 | - * Callable function to mark an action as migrated optionally overridden in derived classes. |
|
442 | - */ |
|
443 | - public function mark_migrated( $action_id ) {} |
|
444 | - |
|
445 | - /** |
|
446 | - * @return ActionScheduler_Store |
|
447 | - */ |
|
448 | - public static function instance() { |
|
449 | - if ( empty( self::$store ) ) { |
|
450 | - $class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS ); |
|
451 | - self::$store = new $class(); |
|
452 | - } |
|
453 | - return self::$store; |
|
454 | - } |
|
9 | + const STATUS_COMPLETE = 'complete'; |
|
10 | + const STATUS_PENDING = 'pending'; |
|
11 | + const STATUS_RUNNING = 'in-progress'; |
|
12 | + const STATUS_FAILED = 'failed'; |
|
13 | + const STATUS_CANCELED = 'canceled'; |
|
14 | + const DEFAULT_CLASS = 'ActionScheduler_wpPostStore'; |
|
15 | + |
|
16 | + /** @var ActionScheduler_Store */ |
|
17 | + private static $store = null; |
|
18 | + |
|
19 | + /** @var int */ |
|
20 | + protected static $max_args_length = 191; |
|
21 | + |
|
22 | + /** |
|
23 | + * @param ActionScheduler_Action $action |
|
24 | + * @param DateTime $scheduled_date Optional Date of the first instance |
|
25 | + * to store. Otherwise uses the first date of the action's |
|
26 | + * schedule. |
|
27 | + * |
|
28 | + * @return int The action ID |
|
29 | + */ |
|
30 | + abstract public function save_action( ActionScheduler_Action $action, DateTime $scheduled_date = null ); |
|
31 | + |
|
32 | + /** |
|
33 | + * @param string $action_id |
|
34 | + * |
|
35 | + * @return ActionScheduler_Action |
|
36 | + */ |
|
37 | + abstract public function fetch_action( $action_id ); |
|
38 | + |
|
39 | + /** |
|
40 | + * Find an action. |
|
41 | + * |
|
42 | + * Note: the query ordering changes based on the passed 'status' value. |
|
43 | + * |
|
44 | + * @param string $hook Action hook. |
|
45 | + * @param array $params Parameters of the action to find. |
|
46 | + * |
|
47 | + * @return string|null ID of the next action matching the criteria or NULL if not found. |
|
48 | + */ |
|
49 | + public function find_action( $hook, $params = array() ) { |
|
50 | + $params = wp_parse_args( |
|
51 | + $params, |
|
52 | + array( |
|
53 | + 'args' => null, |
|
54 | + 'status' => self::STATUS_PENDING, |
|
55 | + 'group' => '', |
|
56 | + ) |
|
57 | + ); |
|
58 | + |
|
59 | + // These params are fixed for this method. |
|
60 | + $params['hook'] = $hook; |
|
61 | + $params['orderby'] = 'date'; |
|
62 | + $params['per_page'] = 1; |
|
63 | + |
|
64 | + if ( ! empty( $params['status'] ) ) { |
|
65 | + if ( self::STATUS_PENDING === $params['status'] ) { |
|
66 | + $params['order'] = 'ASC'; // Find the next action that matches. |
|
67 | + } else { |
|
68 | + $params['order'] = 'DESC'; // Find the most recent action that matches. |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + $results = $this->query_actions( $params ); |
|
73 | + |
|
74 | + return empty( $results ) ? null : $results[0]; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Query for action count or list of action IDs. |
|
79 | + * |
|
80 | + * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
81 | + * |
|
82 | + * @param array $query { |
|
83 | + * Query filtering options. |
|
84 | + * |
|
85 | + * @type string $hook The name of the actions. Optional. |
|
86 | + * @type string|array $status The status or statuses of the actions. Optional. |
|
87 | + * @type array $args The args array of the actions. Optional. |
|
88 | + * @type DateTime $date The scheduled date of the action. Used in UTC timezone. Optional. |
|
89 | + * @type string $date_compare Operator for selecting by $date param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='. |
|
90 | + * @type DateTime $modified The last modified date of the action. Used in UTC timezone. Optional. |
|
91 | + * @type string $modified_compare Operator for comparing $modified param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='. |
|
92 | + * @type string $group The group the action belongs to. Optional. |
|
93 | + * @type bool|int $claimed TRUE to find claimed actions, FALSE to find unclaimed actions, an int to find a specific claim ID. Optional. |
|
94 | + * @type int $per_page Number of results to return. Defaults to 5. |
|
95 | + * @type int $offset The query pagination offset. Defaults to 0. |
|
96 | + * @type int $orderby Accepted values are 'hook', 'group', 'modified', 'date' or 'none'. Defaults to 'date'. |
|
97 | + * @type string $order Accepted values are 'ASC' or 'DESC'. Defaults to 'ASC'. |
|
98 | + * } |
|
99 | + * @param string $query_type Whether to select or count the results. Default, select. |
|
100 | + * |
|
101 | + * @return string|array|null The IDs of actions matching the query. Null on failure. |
|
102 | + */ |
|
103 | + abstract public function query_actions( $query = array(), $query_type = 'select' ); |
|
104 | + |
|
105 | + /** |
|
106 | + * Run query to get a single action ID. |
|
107 | + * |
|
108 | + * @since 3.3.0 |
|
109 | + * |
|
110 | + * @see ActionScheduler_Store::query_actions for $query arg usage but 'per_page' and 'offset' can't be used. |
|
111 | + * |
|
112 | + * @param array $query Query parameters. |
|
113 | + * |
|
114 | + * @return int|null |
|
115 | + */ |
|
116 | + public function query_action( $query ) { |
|
117 | + $query['per_page'] = 1; |
|
118 | + $query['offset'] = 0; |
|
119 | + $results = $this->query_actions( $query ); |
|
120 | + |
|
121 | + if ( empty( $results ) ) { |
|
122 | + return null; |
|
123 | + } else { |
|
124 | + return (int) $results[0]; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Get a count of all actions in the store, grouped by status |
|
130 | + * |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + abstract public function action_counts(); |
|
134 | + |
|
135 | + /** |
|
136 | + * Get additional action counts. |
|
137 | + * |
|
138 | + * - add past-due actions |
|
139 | + * |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + public function extra_action_counts() { |
|
143 | + $extra_actions = array(); |
|
144 | + |
|
145 | + $pastdue_action_counts = (int) $this->query_actions( |
|
146 | + array( |
|
147 | + 'status' => self::STATUS_PENDING, |
|
148 | + 'date' => as_get_datetime_object(), |
|
149 | + ), |
|
150 | + 'count' |
|
151 | + ); |
|
152 | + |
|
153 | + if ( $pastdue_action_counts ) { |
|
154 | + $extra_actions['past-due'] = $pastdue_action_counts; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Allows 3rd party code to add extra action counts (used in filters in the list table). |
|
159 | + * |
|
160 | + * @since 3.5.0 |
|
161 | + * @param $extra_actions array Array with format action_count_identifier => action count. |
|
162 | + */ |
|
163 | + return apply_filters( 'action_scheduler_extra_action_counts', $extra_actions ); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $action_id |
|
168 | + */ |
|
169 | + abstract public function cancel_action( $action_id ); |
|
170 | + |
|
171 | + /** |
|
172 | + * @param string $action_id |
|
173 | + */ |
|
174 | + abstract public function delete_action( $action_id ); |
|
175 | + |
|
176 | + /** |
|
177 | + * @param string $action_id |
|
178 | + * |
|
179 | + * @return DateTime The date the action is schedule to run, or the date that it ran. |
|
180 | + */ |
|
181 | + abstract public function get_date( $action_id ); |
|
182 | + |
|
183 | + /** |
|
184 | + * @param int $max_actions |
|
185 | + * @param DateTime $before_date Claim only actions schedule before the given date. Defaults to now. |
|
186 | + * @param array $hooks Claim only actions with a hook or hooks. |
|
187 | + * @param string $group Claim only actions in the given group. |
|
188 | + * |
|
189 | + * @return ActionScheduler_ActionClaim |
|
190 | + */ |
|
191 | + abstract public function stake_claim( $max_actions = 10, DateTime $before_date = null, $hooks = array(), $group = '' ); |
|
192 | + |
|
193 | + /** |
|
194 | + * @return int |
|
195 | + */ |
|
196 | + abstract public function get_claim_count(); |
|
197 | + |
|
198 | + /** |
|
199 | + * @param ActionScheduler_ActionClaim $claim |
|
200 | + */ |
|
201 | + abstract public function release_claim( ActionScheduler_ActionClaim $claim ); |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $action_id |
|
205 | + */ |
|
206 | + abstract public function unclaim_action( $action_id ); |
|
207 | + |
|
208 | + /** |
|
209 | + * @param string $action_id |
|
210 | + */ |
|
211 | + abstract public function mark_failure( $action_id ); |
|
212 | + |
|
213 | + /** |
|
214 | + * @param string $action_id |
|
215 | + */ |
|
216 | + abstract public function log_execution( $action_id ); |
|
217 | + |
|
218 | + /** |
|
219 | + * @param string $action_id |
|
220 | + */ |
|
221 | + abstract public function mark_complete( $action_id ); |
|
222 | + |
|
223 | + /** |
|
224 | + * @param string $action_id |
|
225 | + * |
|
226 | + * @return string |
|
227 | + */ |
|
228 | + abstract public function get_status( $action_id ); |
|
229 | + |
|
230 | + /** |
|
231 | + * @param string $action_id |
|
232 | + * @return mixed |
|
233 | + */ |
|
234 | + abstract public function get_claim_id( $action_id ); |
|
235 | + |
|
236 | + /** |
|
237 | + * @param string $claim_id |
|
238 | + * @return array |
|
239 | + */ |
|
240 | + abstract public function find_actions_by_claim_id( $claim_id ); |
|
241 | + |
|
242 | + /** |
|
243 | + * @param string $comparison_operator |
|
244 | + * @return string |
|
245 | + */ |
|
246 | + protected function validate_sql_comparator( $comparison_operator ) { |
|
247 | + if ( in_array( $comparison_operator, array( '!=', '>', '>=', '<', '<=', '=' ) ) ) { |
|
248 | + return $comparison_operator; |
|
249 | + } |
|
250 | + return '='; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Get the time MySQL formated date/time string for an action's (next) scheduled date. |
|
255 | + * |
|
256 | + * @param ActionScheduler_Action $action |
|
257 | + * @param DateTime $scheduled_date (optional) |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + protected function get_scheduled_date_string( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
261 | + $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
|
262 | + if ( ! $next ) { |
|
263 | + return '0000-00-00 00:00:00'; |
|
264 | + } |
|
265 | + $next->setTimezone( new DateTimeZone( 'UTC' ) ); |
|
266 | + |
|
267 | + return $next->format( 'Y-m-d H:i:s' ); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Get the time MySQL formated date/time string for an action's (next) scheduled date. |
|
272 | + * |
|
273 | + * @param ActionScheduler_Action $action |
|
274 | + * @param DateTime $scheduled_date (optional) |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + protected function get_scheduled_date_string_local( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
278 | + $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
|
279 | + if ( ! $next ) { |
|
280 | + return '0000-00-00 00:00:00'; |
|
281 | + } |
|
282 | + |
|
283 | + ActionScheduler_TimezoneHelper::set_local_timezone( $next ); |
|
284 | + return $next->format( 'Y-m-d H:i:s' ); |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Validate that we could decode action arguments. |
|
289 | + * |
|
290 | + * @param mixed $args The decoded arguments. |
|
291 | + * @param int $action_id The action ID. |
|
292 | + * |
|
293 | + * @throws ActionScheduler_InvalidActionException When the decoded arguments are invalid. |
|
294 | + */ |
|
295 | + protected function validate_args( $args, $action_id ) { |
|
296 | + // Ensure we have an array of args. |
|
297 | + if ( ! is_array( $args ) ) { |
|
298 | + throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id ); |
|
299 | + } |
|
300 | + |
|
301 | + // Validate JSON decoding if possible. |
|
302 | + if ( function_exists( 'json_last_error' ) && JSON_ERROR_NONE !== json_last_error() ) { |
|
303 | + throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id, $args ); |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Validate a ActionScheduler_Schedule object. |
|
309 | + * |
|
310 | + * @param mixed $schedule The unserialized ActionScheduler_Schedule object. |
|
311 | + * @param int $action_id The action ID. |
|
312 | + * |
|
313 | + * @throws ActionScheduler_InvalidActionException When the schedule is invalid. |
|
314 | + */ |
|
315 | + protected function validate_schedule( $schedule, $action_id ) { |
|
316 | + if ( empty( $schedule ) || ! is_a( $schedule, 'ActionScheduler_Schedule' ) ) { |
|
317 | + throw ActionScheduler_InvalidActionException::from_schedule( $action_id, $schedule ); |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * InnoDB indexes have a maximum size of 767 bytes by default, which is only 191 characters with utf8mb4. |
|
323 | + * |
|
324 | + * Previously, AS wasn't concerned about args length, as we used the (unindex) post_content column. However, |
|
325 | + * with custom tables, we use an indexed VARCHAR column instead. |
|
326 | + * |
|
327 | + * @param ActionScheduler_Action $action Action to be validated. |
|
328 | + * @throws InvalidArgumentException When json encoded args is too long. |
|
329 | + */ |
|
330 | + protected function validate_action( ActionScheduler_Action $action ) { |
|
331 | + if ( strlen( json_encode( $action->get_args() ) ) > static::$max_args_length ) { |
|
332 | + throw new InvalidArgumentException( sprintf( __( 'ActionScheduler_Action::$args too long. To ensure the args column can be indexed, action args should not be more than %d characters when encoded as JSON.', 'action-scheduler' ), static::$max_args_length ) ); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Cancel pending actions by hook. |
|
338 | + * |
|
339 | + * @since 3.0.0 |
|
340 | + * |
|
341 | + * @param string $hook Hook name. |
|
342 | + * |
|
343 | + * @return void |
|
344 | + */ |
|
345 | + public function cancel_actions_by_hook( $hook ) { |
|
346 | + $action_ids = true; |
|
347 | + while ( ! empty( $action_ids ) ) { |
|
348 | + $action_ids = $this->query_actions( |
|
349 | + array( |
|
350 | + 'hook' => $hook, |
|
351 | + 'status' => self::STATUS_PENDING, |
|
352 | + 'per_page' => 1000, |
|
353 | + 'orderby' => 'action_id', |
|
354 | + ) |
|
355 | + ); |
|
356 | + |
|
357 | + $this->bulk_cancel_actions( $action_ids ); |
|
358 | + } |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Cancel pending actions by group. |
|
363 | + * |
|
364 | + * @since 3.0.0 |
|
365 | + * |
|
366 | + * @param string $group Group slug. |
|
367 | + * |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + public function cancel_actions_by_group( $group ) { |
|
371 | + $action_ids = true; |
|
372 | + while ( ! empty( $action_ids ) ) { |
|
373 | + $action_ids = $this->query_actions( |
|
374 | + array( |
|
375 | + 'group' => $group, |
|
376 | + 'status' => self::STATUS_PENDING, |
|
377 | + 'per_page' => 1000, |
|
378 | + 'orderby' => 'action_id', |
|
379 | + ) |
|
380 | + ); |
|
381 | + |
|
382 | + $this->bulk_cancel_actions( $action_ids ); |
|
383 | + } |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Cancel a set of action IDs. |
|
388 | + * |
|
389 | + * @since 3.0.0 |
|
390 | + * |
|
391 | + * @param array $action_ids List of action IDs. |
|
392 | + * |
|
393 | + * @return void |
|
394 | + */ |
|
395 | + private function bulk_cancel_actions( $action_ids ) { |
|
396 | + foreach ( $action_ids as $action_id ) { |
|
397 | + $this->cancel_action( $action_id ); |
|
398 | + } |
|
399 | + |
|
400 | + do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * @return array |
|
405 | + */ |
|
406 | + public function get_status_labels() { |
|
407 | + return array( |
|
408 | + self::STATUS_COMPLETE => __( 'Complete', 'action-scheduler' ), |
|
409 | + self::STATUS_PENDING => __( 'Pending', 'action-scheduler' ), |
|
410 | + self::STATUS_RUNNING => __( 'In-progress', 'action-scheduler' ), |
|
411 | + self::STATUS_FAILED => __( 'Failed', 'action-scheduler' ), |
|
412 | + self::STATUS_CANCELED => __( 'Canceled', 'action-scheduler' ), |
|
413 | + ); |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * Check if there are any pending scheduled actions due to run. |
|
418 | + * |
|
419 | + * @param ActionScheduler_Action $action |
|
420 | + * @param DateTime $scheduled_date (optional) |
|
421 | + * @return string |
|
422 | + */ |
|
423 | + public function has_pending_actions_due() { |
|
424 | + $pending_actions = $this->query_actions( |
|
425 | + array( |
|
426 | + 'date' => as_get_datetime_object(), |
|
427 | + 'status' => self::STATUS_PENDING, |
|
428 | + 'orderby' => 'none', |
|
429 | + ) |
|
430 | + ); |
|
431 | + |
|
432 | + return ! empty( $pending_actions ); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Callable initialization function optionally overridden in derived classes. |
|
437 | + */ |
|
438 | + public function init() {} |
|
439 | + |
|
440 | + /** |
|
441 | + * Callable function to mark an action as migrated optionally overridden in derived classes. |
|
442 | + */ |
|
443 | + public function mark_migrated( $action_id ) {} |
|
444 | + |
|
445 | + /** |
|
446 | + * @return ActionScheduler_Store |
|
447 | + */ |
|
448 | + public static function instance() { |
|
449 | + if ( empty( self::$store ) ) { |
|
450 | + $class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS ); |
|
451 | + self::$store = new $class(); |
|
452 | + } |
|
453 | + return self::$store; |
|
454 | + } |
|
455 | 455 | } |
@@ -27,14 +27,14 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return int The action ID |
29 | 29 | */ |
30 | - abstract public function save_action( ActionScheduler_Action $action, DateTime $scheduled_date = null ); |
|
30 | + abstract public function save_action(ActionScheduler_Action $action, DateTime $scheduled_date = null); |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * @param string $action_id |
34 | 34 | * |
35 | 35 | * @return ActionScheduler_Action |
36 | 36 | */ |
37 | - abstract public function fetch_action( $action_id ); |
|
37 | + abstract public function fetch_action($action_id); |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * Find an action. |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @return string|null ID of the next action matching the criteria or NULL if not found. |
48 | 48 | */ |
49 | - public function find_action( $hook, $params = array() ) { |
|
49 | + public function find_action($hook, $params = array()) { |
|
50 | 50 | $params = wp_parse_args( |
51 | 51 | $params, |
52 | 52 | array( |
@@ -61,17 +61,17 @@ discard block |
||
61 | 61 | $params['orderby'] = 'date'; |
62 | 62 | $params['per_page'] = 1; |
63 | 63 | |
64 | - if ( ! empty( $params['status'] ) ) { |
|
65 | - if ( self::STATUS_PENDING === $params['status'] ) { |
|
64 | + if ( ! empty($params['status'])) { |
|
65 | + if (self::STATUS_PENDING === $params['status']) { |
|
66 | 66 | $params['order'] = 'ASC'; // Find the next action that matches. |
67 | 67 | } else { |
68 | 68 | $params['order'] = 'DESC'; // Find the most recent action that matches. |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | - $results = $this->query_actions( $params ); |
|
72 | + $results = $this->query_actions($params); |
|
73 | 73 | |
74 | - return empty( $results ) ? null : $results[0]; |
|
74 | + return empty($results) ? null : $results[0]; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | * |
101 | 101 | * @return string|array|null The IDs of actions matching the query. Null on failure. |
102 | 102 | */ |
103 | - abstract public function query_actions( $query = array(), $query_type = 'select' ); |
|
103 | + abstract public function query_actions($query = array(), $query_type = 'select'); |
|
104 | 104 | |
105 | 105 | /** |
106 | 106 | * Run query to get a single action ID. |
@@ -113,12 +113,12 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @return int|null |
115 | 115 | */ |
116 | - public function query_action( $query ) { |
|
116 | + public function query_action($query) { |
|
117 | 117 | $query['per_page'] = 1; |
118 | 118 | $query['offset'] = 0; |
119 | - $results = $this->query_actions( $query ); |
|
119 | + $results = $this->query_actions($query); |
|
120 | 120 | |
121 | - if ( empty( $results ) ) { |
|
121 | + if (empty($results)) { |
|
122 | 122 | return null; |
123 | 123 | } else { |
124 | 124 | return (int) $results[0]; |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | 'count' |
151 | 151 | ); |
152 | 152 | |
153 | - if ( $pastdue_action_counts ) { |
|
153 | + if ($pastdue_action_counts) { |
|
154 | 154 | $extra_actions['past-due'] = $pastdue_action_counts; |
155 | 155 | } |
156 | 156 | |
@@ -160,25 +160,25 @@ discard block |
||
160 | 160 | * @since 3.5.0 |
161 | 161 | * @param $extra_actions array Array with format action_count_identifier => action count. |
162 | 162 | */ |
163 | - return apply_filters( 'action_scheduler_extra_action_counts', $extra_actions ); |
|
163 | + return apply_filters('action_scheduler_extra_action_counts', $extra_actions); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
167 | 167 | * @param string $action_id |
168 | 168 | */ |
169 | - abstract public function cancel_action( $action_id ); |
|
169 | + abstract public function cancel_action($action_id); |
|
170 | 170 | |
171 | 171 | /** |
172 | 172 | * @param string $action_id |
173 | 173 | */ |
174 | - abstract public function delete_action( $action_id ); |
|
174 | + abstract public function delete_action($action_id); |
|
175 | 175 | |
176 | 176 | /** |
177 | 177 | * @param string $action_id |
178 | 178 | * |
179 | 179 | * @return DateTime The date the action is schedule to run, or the date that it ran. |
180 | 180 | */ |
181 | - abstract public function get_date( $action_id ); |
|
181 | + abstract public function get_date($action_id); |
|
182 | 182 | |
183 | 183 | /** |
184 | 184 | * @param int $max_actions |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | * |
189 | 189 | * @return ActionScheduler_ActionClaim |
190 | 190 | */ |
191 | - abstract public function stake_claim( $max_actions = 10, DateTime $before_date = null, $hooks = array(), $group = '' ); |
|
191 | + abstract public function stake_claim($max_actions = 10, DateTime $before_date = null, $hooks = array(), $group = ''); |
|
192 | 192 | |
193 | 193 | /** |
194 | 194 | * @return int |
@@ -198,53 +198,53 @@ discard block |
||
198 | 198 | /** |
199 | 199 | * @param ActionScheduler_ActionClaim $claim |
200 | 200 | */ |
201 | - abstract public function release_claim( ActionScheduler_ActionClaim $claim ); |
|
201 | + abstract public function release_claim(ActionScheduler_ActionClaim $claim); |
|
202 | 202 | |
203 | 203 | /** |
204 | 204 | * @param string $action_id |
205 | 205 | */ |
206 | - abstract public function unclaim_action( $action_id ); |
|
206 | + abstract public function unclaim_action($action_id); |
|
207 | 207 | |
208 | 208 | /** |
209 | 209 | * @param string $action_id |
210 | 210 | */ |
211 | - abstract public function mark_failure( $action_id ); |
|
211 | + abstract public function mark_failure($action_id); |
|
212 | 212 | |
213 | 213 | /** |
214 | 214 | * @param string $action_id |
215 | 215 | */ |
216 | - abstract public function log_execution( $action_id ); |
|
216 | + abstract public function log_execution($action_id); |
|
217 | 217 | |
218 | 218 | /** |
219 | 219 | * @param string $action_id |
220 | 220 | */ |
221 | - abstract public function mark_complete( $action_id ); |
|
221 | + abstract public function mark_complete($action_id); |
|
222 | 222 | |
223 | 223 | /** |
224 | 224 | * @param string $action_id |
225 | 225 | * |
226 | 226 | * @return string |
227 | 227 | */ |
228 | - abstract public function get_status( $action_id ); |
|
228 | + abstract public function get_status($action_id); |
|
229 | 229 | |
230 | 230 | /** |
231 | 231 | * @param string $action_id |
232 | 232 | * @return mixed |
233 | 233 | */ |
234 | - abstract public function get_claim_id( $action_id ); |
|
234 | + abstract public function get_claim_id($action_id); |
|
235 | 235 | |
236 | 236 | /** |
237 | 237 | * @param string $claim_id |
238 | 238 | * @return array |
239 | 239 | */ |
240 | - abstract public function find_actions_by_claim_id( $claim_id ); |
|
240 | + abstract public function find_actions_by_claim_id($claim_id); |
|
241 | 241 | |
242 | 242 | /** |
243 | 243 | * @param string $comparison_operator |
244 | 244 | * @return string |
245 | 245 | */ |
246 | - protected function validate_sql_comparator( $comparison_operator ) { |
|
247 | - if ( in_array( $comparison_operator, array( '!=', '>', '>=', '<', '<=', '=' ) ) ) { |
|
246 | + protected function validate_sql_comparator($comparison_operator) { |
|
247 | + if (in_array($comparison_operator, array('!=', '>', '>=', '<', '<=', '='))) { |
|
248 | 248 | return $comparison_operator; |
249 | 249 | } |
250 | 250 | return '='; |
@@ -257,14 +257,14 @@ discard block |
||
257 | 257 | * @param DateTime $scheduled_date (optional) |
258 | 258 | * @return string |
259 | 259 | */ |
260 | - protected function get_scheduled_date_string( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
260 | + protected function get_scheduled_date_string(ActionScheduler_Action $action, DateTime $scheduled_date = null) { |
|
261 | 261 | $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
262 | - if ( ! $next ) { |
|
262 | + if ( ! $next) { |
|
263 | 263 | return '0000-00-00 00:00:00'; |
264 | 264 | } |
265 | - $next->setTimezone( new DateTimeZone( 'UTC' ) ); |
|
265 | + $next->setTimezone(new DateTimeZone('UTC')); |
|
266 | 266 | |
267 | - return $next->format( 'Y-m-d H:i:s' ); |
|
267 | + return $next->format('Y-m-d H:i:s'); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /** |
@@ -274,14 +274,14 @@ discard block |
||
274 | 274 | * @param DateTime $scheduled_date (optional) |
275 | 275 | * @return string |
276 | 276 | */ |
277 | - protected function get_scheduled_date_string_local( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { |
|
277 | + protected function get_scheduled_date_string_local(ActionScheduler_Action $action, DateTime $scheduled_date = null) { |
|
278 | 278 | $next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date; |
279 | - if ( ! $next ) { |
|
279 | + if ( ! $next) { |
|
280 | 280 | return '0000-00-00 00:00:00'; |
281 | 281 | } |
282 | 282 | |
283 | - ActionScheduler_TimezoneHelper::set_local_timezone( $next ); |
|
284 | - return $next->format( 'Y-m-d H:i:s' ); |
|
283 | + ActionScheduler_TimezoneHelper::set_local_timezone($next); |
|
284 | + return $next->format('Y-m-d H:i:s'); |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | /** |
@@ -292,15 +292,15 @@ discard block |
||
292 | 292 | * |
293 | 293 | * @throws ActionScheduler_InvalidActionException When the decoded arguments are invalid. |
294 | 294 | */ |
295 | - protected function validate_args( $args, $action_id ) { |
|
295 | + protected function validate_args($args, $action_id) { |
|
296 | 296 | // Ensure we have an array of args. |
297 | - if ( ! is_array( $args ) ) { |
|
298 | - throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id ); |
|
297 | + if ( ! is_array($args)) { |
|
298 | + throw ActionScheduler_InvalidActionException::from_decoding_args($action_id); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | // Validate JSON decoding if possible. |
302 | - if ( function_exists( 'json_last_error' ) && JSON_ERROR_NONE !== json_last_error() ) { |
|
303 | - throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id, $args ); |
|
302 | + if (function_exists('json_last_error') && JSON_ERROR_NONE !== json_last_error()) { |
|
303 | + throw ActionScheduler_InvalidActionException::from_decoding_args($action_id, $args); |
|
304 | 304 | } |
305 | 305 | } |
306 | 306 | |
@@ -312,9 +312,9 @@ discard block |
||
312 | 312 | * |
313 | 313 | * @throws ActionScheduler_InvalidActionException When the schedule is invalid. |
314 | 314 | */ |
315 | - protected function validate_schedule( $schedule, $action_id ) { |
|
316 | - if ( empty( $schedule ) || ! is_a( $schedule, 'ActionScheduler_Schedule' ) ) { |
|
317 | - throw ActionScheduler_InvalidActionException::from_schedule( $action_id, $schedule ); |
|
315 | + protected function validate_schedule($schedule, $action_id) { |
|
316 | + if (empty($schedule) || ! is_a($schedule, 'ActionScheduler_Schedule')) { |
|
317 | + throw ActionScheduler_InvalidActionException::from_schedule($action_id, $schedule); |
|
318 | 318 | } |
319 | 319 | } |
320 | 320 | |
@@ -327,9 +327,9 @@ discard block |
||
327 | 327 | * @param ActionScheduler_Action $action Action to be validated. |
328 | 328 | * @throws InvalidArgumentException When json encoded args is too long. |
329 | 329 | */ |
330 | - protected function validate_action( ActionScheduler_Action $action ) { |
|
331 | - if ( strlen( json_encode( $action->get_args() ) ) > static::$max_args_length ) { |
|
332 | - throw new InvalidArgumentException( sprintf( __( 'ActionScheduler_Action::$args too long. To ensure the args column can be indexed, action args should not be more than %d characters when encoded as JSON.', 'action-scheduler' ), static::$max_args_length ) ); |
|
330 | + protected function validate_action(ActionScheduler_Action $action) { |
|
331 | + if (strlen(json_encode($action->get_args())) > static::$max_args_length) { |
|
332 | + throw new InvalidArgumentException(sprintf(__('ActionScheduler_Action::$args too long. To ensure the args column can be indexed, action args should not be more than %d characters when encoded as JSON.', 'action-scheduler'), static::$max_args_length)); |
|
333 | 333 | } |
334 | 334 | } |
335 | 335 | |
@@ -342,9 +342,9 @@ discard block |
||
342 | 342 | * |
343 | 343 | * @return void |
344 | 344 | */ |
345 | - public function cancel_actions_by_hook( $hook ) { |
|
345 | + public function cancel_actions_by_hook($hook) { |
|
346 | 346 | $action_ids = true; |
347 | - while ( ! empty( $action_ids ) ) { |
|
347 | + while ( ! empty($action_ids)) { |
|
348 | 348 | $action_ids = $this->query_actions( |
349 | 349 | array( |
350 | 350 | 'hook' => $hook, |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | ) |
355 | 355 | ); |
356 | 356 | |
357 | - $this->bulk_cancel_actions( $action_ids ); |
|
357 | + $this->bulk_cancel_actions($action_ids); |
|
358 | 358 | } |
359 | 359 | } |
360 | 360 | |
@@ -367,9 +367,9 @@ discard block |
||
367 | 367 | * |
368 | 368 | * @return void |
369 | 369 | */ |
370 | - public function cancel_actions_by_group( $group ) { |
|
370 | + public function cancel_actions_by_group($group) { |
|
371 | 371 | $action_ids = true; |
372 | - while ( ! empty( $action_ids ) ) { |
|
372 | + while ( ! empty($action_ids)) { |
|
373 | 373 | $action_ids = $this->query_actions( |
374 | 374 | array( |
375 | 375 | 'group' => $group, |
@@ -379,7 +379,7 @@ discard block |
||
379 | 379 | ) |
380 | 380 | ); |
381 | 381 | |
382 | - $this->bulk_cancel_actions( $action_ids ); |
|
382 | + $this->bulk_cancel_actions($action_ids); |
|
383 | 383 | } |
384 | 384 | } |
385 | 385 | |
@@ -392,12 +392,12 @@ discard block |
||
392 | 392 | * |
393 | 393 | * @return void |
394 | 394 | */ |
395 | - private function bulk_cancel_actions( $action_ids ) { |
|
396 | - foreach ( $action_ids as $action_id ) { |
|
397 | - $this->cancel_action( $action_id ); |
|
395 | + private function bulk_cancel_actions($action_ids) { |
|
396 | + foreach ($action_ids as $action_id) { |
|
397 | + $this->cancel_action($action_id); |
|
398 | 398 | } |
399 | 399 | |
400 | - do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
400 | + do_action('action_scheduler_bulk_cancel_actions', $action_ids); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | /** |
@@ -405,11 +405,11 @@ discard block |
||
405 | 405 | */ |
406 | 406 | public function get_status_labels() { |
407 | 407 | return array( |
408 | - self::STATUS_COMPLETE => __( 'Complete', 'action-scheduler' ), |
|
409 | - self::STATUS_PENDING => __( 'Pending', 'action-scheduler' ), |
|
410 | - self::STATUS_RUNNING => __( 'In-progress', 'action-scheduler' ), |
|
411 | - self::STATUS_FAILED => __( 'Failed', 'action-scheduler' ), |
|
412 | - self::STATUS_CANCELED => __( 'Canceled', 'action-scheduler' ), |
|
408 | + self::STATUS_COMPLETE => __('Complete', 'action-scheduler'), |
|
409 | + self::STATUS_PENDING => __('Pending', 'action-scheduler'), |
|
410 | + self::STATUS_RUNNING => __('In-progress', 'action-scheduler'), |
|
411 | + self::STATUS_FAILED => __('Failed', 'action-scheduler'), |
|
412 | + self::STATUS_CANCELED => __('Canceled', 'action-scheduler'), |
|
413 | 413 | ); |
414 | 414 | } |
415 | 415 | |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | ) |
430 | 430 | ); |
431 | 431 | |
432 | - return ! empty( $pending_actions ); |
|
432 | + return ! empty($pending_actions); |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | /** |
@@ -440,14 +440,14 @@ discard block |
||
440 | 440 | /** |
441 | 441 | * Callable function to mark an action as migrated optionally overridden in derived classes. |
442 | 442 | */ |
443 | - public function mark_migrated( $action_id ) {} |
|
443 | + public function mark_migrated($action_id) {} |
|
444 | 444 | |
445 | 445 | /** |
446 | 446 | * @return ActionScheduler_Store |
447 | 447 | */ |
448 | 448 | public static function instance() { |
449 | - if ( empty( self::$store ) ) { |
|
450 | - $class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS ); |
|
449 | + if (empty(self::$store)) { |
|
450 | + $class = apply_filters('action_scheduler_store_class', self::DEFAULT_CLASS); |
|
451 | 451 | self::$store = new $class(); |
452 | 452 | } |
453 | 453 | return self::$store; |
@@ -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 | } |