| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_Logger |
| 5 |
* |
| 6 |
* @codeCoverageIgnore |
| 7 |
*/ |
| 8 |
abstract class ActionScheduler_Logger { |
| 9 |
|
| 10 |
/** |
| 11 |
* Instance. |
| 12 |
* |
| 13 |
* @var null|self |
| 14 |
*/ |
| 15 |
private static $logger = null; |
| 16 |
|
| 17 |
/** |
| 18 |
* Get instance. |
| 19 |
* |
| 20 |
* @return ActionScheduler_Logger |
| 21 |
*/ |
| 22 |
public static function instance() { |
| 23 |
if ( empty( self::$logger ) ) { |
| 24 |
$class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
| 25 |
self::$logger = new $class(); |
| 26 |
} |
| 27 |
return self::$logger; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Create log entry. |
| 32 |
* |
| 33 |
* @param string $action_id Action ID. |
| 34 |
* @param string $message Log message. |
| 35 |
* @param DateTime|null $date Log date. |
| 36 |
* |
| 37 |
* @return string The log entry ID |
| 38 |
*/ |
| 39 |
abstract public function log( $action_id, $message, ?DateTime $date = null ); |
| 40 |
|
| 41 |
/** |
| 42 |
* Get action's log entry. |
| 43 |
* |
| 44 |
* @param string $entry_id Entry ID. |
| 45 |
* |
| 46 |
* @return ActionScheduler_LogEntry |
| 47 |
*/ |
| 48 |
abstract public function get_entry( $entry_id ); |
| 49 |
|
| 50 |
/** |
| 51 |
* Get action's logs. |
| 52 |
* |
| 53 |
* @param string $action_id Action ID. |
| 54 |
* |
| 55 |
* @return ActionScheduler_LogEntry[] |
| 56 |
*/ |
| 57 |
abstract public function get_logs( $action_id ); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Initialize. |
| 62 |
* |
| 63 |
* @codeCoverageIgnore |
| 64 |
*/ |
| 65 |
public function init() { |
| 66 |
$this->hook_stored_action(); |
| 67 |
add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
| 68 |
add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
| 69 |
add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
| 70 |
add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
| 71 |
add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
| 72 |
add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
| 73 |
add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
| 74 |
add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
| 75 |
add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
| 76 |
add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
| 77 |
add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Register callback for storing action. |
| 82 |
*/ |
| 83 |
public function hook_stored_action() { |
| 84 |
add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Unhook callback for storing action. |
| 89 |
*/ |
| 90 |
public function unhook_stored_action() { |
| 91 |
remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Log action stored. |
| 96 |
* |
| 97 |
* @param int $action_id Action ID. |
| 98 |
*/ |
| 99 |
public function log_stored_action( $action_id ) { |
| 100 |
$this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Log action cancellation. |
| 105 |
* |
| 106 |
* @param int $action_id Action ID. |
| 107 |
*/ |
| 108 |
public function log_canceled_action( $action_id ) { |
| 109 |
$this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Log action start. |
| 114 |
* |
| 115 |
* @param int $action_id Action ID. |
| 116 |
* @param string $context Action execution context. |
| 117 |
*/ |
| 118 |
public function log_started_action( $action_id, $context = '' ) { |
| 119 |
if ( ! empty( $context ) ) { |
| 120 |
/* translators: %s: context */ |
| 121 |
$message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
| 122 |
} else { |
| 123 |
$message = __( 'action started', 'action-scheduler' ); |
| 124 |
} |
| 125 |
$this->log( $action_id, $message ); |
| 126 |
} |
| 127 |
|
| 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 = '' ) { |
| 136 |
if ( ! empty( $context ) ) { |
| 137 |
/* translators: %s: context */ |
| 138 |
$message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
| 139 |
} else { |
| 140 |
$message = __( 'action complete', 'action-scheduler' ); |
| 141 |
} |
| 142 |
$this->log( $action_id, $message ); |
| 143 |
} |
| 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 |
*/ |
| 152 |
public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
| 153 |
if ( ! empty( $context ) ) { |
| 154 |
/* translators: 1: context 2: exception message */ |
| 155 |
$message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
| 156 |
} else { |
| 157 |
/* translators: %s: exception message */ |
| 158 |
$message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
| 159 |
} |
| 160 |
$this->log( $action_id, $message ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Log action timeout. |
| 165 |
* |
| 166 |
* @param int $action_id Action ID. |
| 167 |
* @param string $timeout Timeout. |
| 168 |
*/ |
| 169 |
public function log_timed_out_action( $action_id, $timeout ) { |
| 170 |
/* translators: %s: amount of time */ |
| 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 ) ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Log unexpected shutdown. |
| 176 |
* |
| 177 |
* @param int $action_id Action ID. |
| 178 |
* @param mixed[] $error Error. |
| 179 |
*/ |
| 180 |
public function log_unexpected_shutdown( $action_id, $error ) { |
| 181 |
if ( ! empty( $error ) ) { |
| 182 |
/* translators: 1: error message 2: filename 3: line */ |
| 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'] ) ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Log action reset. |
| 189 |
* |
| 190 |
* @param int $action_id Action ID. |
| 191 |
*/ |
| 192 |
public function log_reset_action( $action_id ) { |
| 193 |
$this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Log ignored action. |
| 198 |
* |
| 199 |
* @param int $action_id Action ID. |
| 200 |
* @param string $context Action execution context. |
| 201 |
*/ |
| 202 |
public function log_ignored_action( $action_id, $context = '' ) { |
| 203 |
if ( ! empty( $context ) ) { |
| 204 |
/* translators: %s: context */ |
| 205 |
$message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
| 206 |
} else { |
| 207 |
$message = __( 'action ignored', 'action-scheduler' ); |
| 208 |
} |
| 209 |
$this->log( $action_id, $message ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Log the failure of fetching the action. |
| 214 |
* |
| 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. |
| 217 |
*/ |
| 218 |
public function log_failed_fetch_action( $action_id, ?Exception $exception = null ) { |
| 219 |
|
| 220 |
if ( ! is_null( $exception ) ) { |
| 221 |
/* translators: %s: exception message */ |
| 222 |
$log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
| 223 |
} else { |
| 224 |
$log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
| 225 |
} |
| 226 |
|
| 227 |
$this->log( $action_id, $log_message ); |
| 228 |
} |
| 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 |
*/ |
| 236 |
public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
| 237 |
/* translators: %s: exception message */ |
| 238 |
$this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Bulk add cancel action log entries. |
| 243 |
* |
| 244 |
* Implemented here for backward compatibility. Should be implemented in parent loggers |
| 245 |
* for more performant bulk logging. |
| 246 |
* |
| 247 |
* @param array $action_ids List of action ID. |
| 248 |
*/ |
| 249 |
public function bulk_log_cancel_actions( $action_ids ) { |
| 250 |
if ( empty( $action_ids ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
foreach ( $action_ids as $action_id ) { |
| 255 |
$this->log_canceled_action( $action_id ); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|