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