← All changes
|
vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler_Logger.php
+96
-14
1.2.0
→
trunk
View file →
| @@ -1,19 +1,28 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Class ActionScheduler_Logger |
| 5 | + * | |
| 5 | 6 | * @codeCoverageIgnore |
| 6 | 7 | */ |
| 7 | 8 | abstract class ActionScheduler_Logger { |
| 8 | - private static $logger = NULL; | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | + * Instance. | |
| 12 | + * | |
| 13 | + * @var null|self | |
| 14 | + */ | |
| 15 | + private static $logger = null; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Get instance. | |
| 19 | + * | |
| 11 | 20 | * @return ActionScheduler_Logger |
| 12 | 21 | */ |
| 13 | 22 | public static function instance() { |
| 14 | - if ( empty(self::$logger) ) { | |
| 15 | - $class = apply_filters('action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger'); | |
| 23 | + if ( empty( self::$logger ) ) { | |
| 24 | + $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); | |
| 16 | 25 | self::$logger = new $class(); |
| 17 | 26 | } |
| 18 | 27 | return self::$logger; |
| 19 | 28 | } |
| @@ -18,26 +27,32 @@ | ||
| 18 | 27 | return self::$logger; |
| 19 | 28 | } |
| 20 | 29 | |
| 21 | 30 | /** |
| 22 | - * @param string $action_id | |
| 23 | - * @param string $message | |
| 24 | - * @param DateTime $date | |
| 31 | + * Create log entry. | |
| 25 | 32 | * |
| 33 | + * @param string $action_id Action ID. | |
| 34 | + * @param string $message Log message. | |
| 35 | + * @param DateTime|null $date Log date. | |
| 36 | + * | |
| 26 | 37 | * @return string The log entry ID |
| 27 | 38 | */ |
| 28 | - abstract public function log( $action_id, $message, DateTime $date = NULL ); | |
| 39 | + abstract public function log( $action_id, $message, ?DateTime $date = null ); | |
| 29 | 40 | |
| 30 | 41 | /** |
| 31 | - * @param string $entry_id | |
| 42 | + * Get action's log entry. | |
| 32 | 43 | * |
| 44 | + * @param string $entry_id Entry ID. | |
| 45 | + * | |
| 33 | 46 | * @return ActionScheduler_LogEntry |
| 34 | 47 | */ |
| 35 | 48 | abstract public function get_entry( $entry_id ); |
| 36 | 49 | |
| 37 | 50 | /** |
| 38 | - * @param string $action_id | |
| 51 | + * Get action's logs. | |
| 39 | 52 | * |
| 53 | + * @param string $action_id Action ID. | |
| 54 | + * | |
| 40 | 55 | * @return ActionScheduler_LogEntry[] |
| 41 | 56 | */ |
| 42 | 57 | abstract public function get_logs( $action_id ); |
| 43 | 58 | |
| @@ -42,8 +57,10 @@ | ||
| 42 | 57 | abstract public function get_logs( $action_id ); |
| 43 | 58 | |
| 44 | 59 | |
| 45 | 60 | /** |
| 61 | + * Initialize. | |
| 62 | + * | |
| 46 | 63 | * @codeCoverageIgnore |
| 47 | 64 | */ |
| 48 | 65 | public function init() { |
| 49 | 66 | $this->hook_stored_action(); |
| @@ -59,24 +76,46 @@ | ||
| 59 | 76 | add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
| 60 | 77 | add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
| 61 | 78 | } |
| 62 | 79 | |
| 80 | + /** | |
| 81 | + * Register callback for storing action. | |
| 82 | + */ | |
| 63 | 83 | public function hook_stored_action() { |
| 64 | 84 | add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
| 65 | 85 | } |
| 66 | 86 | |
| 87 | + /** | |
| 88 | + * Unhook callback for storing action. | |
| 89 | + */ | |
| 67 | 90 | public function unhook_stored_action() { |
| 68 | 91 | remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
| 69 | 92 | } |
| 70 | 93 | |
| 94 | + /** | |
| 95 | + * Log action stored. | |
| 96 | + * | |
| 97 | + * @param int $action_id Action ID. | |
| 98 | + */ | |
| 71 | 99 | public function log_stored_action( $action_id ) { |
| 72 | 100 | $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
| 73 | 101 | } |
| 74 | 102 | |
| 103 | + /** | |
| 104 | + * Log action cancellation. | |
| 105 | + * | |
| 106 | + * @param int $action_id Action ID. | |
| 107 | + */ | |
| 75 | 108 | public function log_canceled_action( $action_id ) { |
| 76 | 109 | $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
| 77 | 110 | } |
| 78 | 111 | |
| 112 | + /** | |
| 113 | + * Log action start. | |
| 114 | + * | |
| 115 | + * @param int $action_id Action ID. | |
| 116 | + * @param string $context Action execution context. | |
| 117 | + */ | |
| 79 | 118 | public function log_started_action( $action_id, $context = '' ) { |
| 80 | 119 | if ( ! empty( $context ) ) { |
| 81 | 120 | /* translators: %s: context */ |
| 82 | 121 | $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
| @@ -85,9 +124,16 @@ | ||
| 85 | 124 | } |
| 86 | 125 | $this->log( $action_id, $message ); |
| 87 | 126 | } |
| 88 | 127 | |
| 89 | - public function log_completed_action( $action_id, $action = NULL, $context = '' ) { | |
| 128 | + /** | |
| 129 | + * Log action completion. | |
| 130 | + * | |
| 131 | + * @param int $action_id Action ID. | |
| 132 | + * @param null|ActionScheduler_Action $action Action. | |
| 133 | + * @param string $context Action execution context. | |
| 134 | + */ | |
| 135 | + public function log_completed_action( $action_id, $action = null, $context = '' ) { | |
| 90 | 136 | if ( ! empty( $context ) ) { |
| 91 | 137 | /* translators: %s: context */ |
| 92 | 138 | $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
| 93 | 139 | } else { |
| @@ -95,8 +141,15 @@ | ||
| 95 | 141 | } |
| 96 | 142 | $this->log( $action_id, $message ); |
| 97 | 143 | } |
| 98 | 144 | |
| 145 | + /** | |
| 146 | + * Log action failure. | |
| 147 | + * | |
| 148 | + * @param int $action_id Action ID. | |
| 149 | + * @param Exception $exception Exception. | |
| 150 | + * @param string $context Action execution context. | |
| 151 | + */ | |
| 99 | 152 | public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
| 100 | 153 | if ( ! empty( $context ) ) { |
| 101 | 154 | /* translators: 1: context 2: exception message */ |
| 102 | 155 | $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
| @@ -106,13 +159,25 @@ | ||
| 106 | 159 | } |
| 107 | 160 | $this->log( $action_id, $message ); |
| 108 | 161 | } |
| 109 | 162 | |
| 163 | + /** | |
| 164 | + * Log action timeout. | |
| 165 | + * | |
| 166 | + * @param int $action_id Action ID. | |
| 167 | + * @param string $timeout Timeout. | |
| 168 | + */ | |
| 110 | 169 | public function log_timed_out_action( $action_id, $timeout ) { |
| 111 | 170 | /* translators: %s: amount of time */ |
| 112 | 171 | $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 | 172 | } |
| 114 | 173 | |
| 174 | + /** | |
| 175 | + * Log unexpected shutdown. | |
| 176 | + * | |
| 177 | + * @param int $action_id Action ID. | |
| 178 | + * @param mixed[] $error Error. | |
| 179 | + */ | |
| 115 | 180 | public function log_unexpected_shutdown( $action_id, $error ) { |
| 116 | 181 | if ( ! empty( $error ) ) { |
| 117 | 182 | /* translators: 1: error message 2: filename 3: line */ |
| 118 | 183 | $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,12 +183,23 @@ | ||
| 118 | 183 | $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 | 184 | } |
| 120 | 185 | } |
| 121 | 186 | |
| 187 | + /** | |
| 188 | + * Log action reset. | |
| 189 | + * | |
| 190 | + * @param int $action_id Action ID. | |
| 191 | + */ | |
| 122 | 192 | public function log_reset_action( $action_id ) { |
| 123 | 193 | $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
| 124 | 194 | } |
| 125 | 195 | |
| 196 | + /** | |
| 197 | + * Log ignored action. | |
| 198 | + * | |
| 199 | + * @param int $action_id Action ID. | |
| 200 | + * @param string $context Action execution context. | |
| 201 | + */ | |
| 126 | 202 | public function log_ignored_action( $action_id, $context = '' ) { |
| 127 | 203 | if ( ! empty( $context ) ) { |
| 128 | 204 | /* translators: %s: context */ |
| 129 | 205 | $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
| @@ -133,14 +209,14 @@ | ||
| 133 | 209 | $this->log( $action_id, $message ); |
| 134 | 210 | } |
| 135 | 211 | |
| 136 | 212 | /** |
| 137 | - * @param string $action_id | |
| 138 | - * @param Exception|NULL $exception The exception which occured when fetching the action. NULL by default for backward compatibility. | |
| 213 | + * Log the failure of fetching the action. | |
| 139 | 214 | * |
| 140 | - * @return ActionScheduler_LogEntry[] | |
| 215 | + * @param string $action_id Action ID. | |
| 216 | + * @param null|Exception $exception The exception which occurred when fetching the action. NULL by default for backward compatibility. | |
| 141 | 217 | */ |
| 142 | - public function log_failed_fetch_action( $action_id, Exception $exception = NULL ) { | |
| 218 | + public function log_failed_fetch_action( $action_id, ?Exception $exception = null ) { | |
| 143 | 219 | |
| 144 | 220 | if ( ! is_null( $exception ) ) { |
| 145 | 221 | /* translators: %s: exception message */ |
| 146 | 222 | $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
| @@ -150,8 +226,14 @@ | ||
| 150 | 226 | |
| 151 | 227 | $this->log( $action_id, $log_message ); |
| 152 | 228 | } |
| 153 | 229 | |
| 230 | + /** | |
| 231 | + * Log the failure of scheduling the action's next instance. | |
| 232 | + * | |
| 233 | + * @param int $action_id Action ID. | |
| 234 | + * @param Exception $exception Exception object. | |
| 235 | + */ | |
| 154 | 236 | public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
| 155 | 237 | /* translators: %s: exception message */ |
| 156 | 238 | $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
| 157 | 239 | } |