| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_Logger |
| 5 |
* @codeCoverageIgnore |
| 6 |
*/ |
| 7 |
abstract class ActionScheduler_Logger { |
| 8 |
private static $logger = NULL; |
| 9 |
|
| 10 |
/** |
| 11 |
* @return ActionScheduler_Logger |
| 12 |
*/ |
| 13 |
public static function instance() { |
| 14 |
if ( empty(self::$logger) ) { |
| 15 |
$class = apply_filters('action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger'); |
| 16 |
self::$logger = new $class(); |
| 17 |
} |
| 18 |
return self::$logger; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @param string $action_id |
| 23 |
* @param string $message |
| 24 |
* @param DateTime $date |
| 25 |
* |
| 26 |
* @return string The log entry ID |
| 27 |
*/ |
| 28 |
abstract public function log( $action_id, $message, DateTime $date = NULL ); |
| 29 |
|
| 30 |
/** |
| 31 |
* @param string $entry_id |
| 32 |
* |
| 33 |
* @return ActionScheduler_LogEntry |
| 34 |
*/ |
| 35 |
abstract public function get_entry( $entry_id ); |
| 36 |
|
| 37 |
/** |
| 38 |
* @param string $action_id |
| 39 |
* |
| 40 |
* @return ActionScheduler_LogEntry[] |
| 41 |
*/ |
| 42 |
abstract public function get_logs( $action_id ); |
| 43 |
|
| 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 ( ! is_null( $exception ) ) { |
| 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 |
} |
| 177 |
|