class-wc-log-handler-db.php
5 years ago
class-wc-log-handler-email.php
5 years ago
class-wc-log-handler-file.php
5 years ago
class-wc-log-handler-email.php
227 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Log_Handler_Email file. |
| 4 | * |
| 5 | * @package WooCommerce\Log Handlers |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Handles log entries by sending an email. |
| 14 | * |
| 15 | * WARNING! |
| 16 | * This log handler has known limitations. |
| 17 | * |
| 18 | * Log messages are aggregated and sent once per request (if necessary). If the site experiences a |
| 19 | * problem, the log email may never be sent. This handler should be used with another handler which |
| 20 | * stores logs in order to prevent loss. |
| 21 | * |
| 22 | * It is not recommended to use this handler on a high traffic site. There will be a maximum of 1 |
| 23 | * email sent per request per handler, but that could still be a dangerous amount of emails under |
| 24 | * heavy traffic. Do not confuse this handler with an appropriate monitoring solution! |
| 25 | * |
| 26 | * If you understand these limitations, feel free to use this handler or borrow parts of the design |
| 27 | * to implement your own! |
| 28 | * |
| 29 | * @class WC_Log_Handler_Email |
| 30 | * @version 1.0.0 |
| 31 | * @package WooCommerce\Classes\Log_Handlers |
| 32 | */ |
| 33 | class WC_Log_Handler_Email extends WC_Log_Handler { |
| 34 | |
| 35 | /** |
| 36 | * Minimum log level this handler will process. |
| 37 | * |
| 38 | * @var int Integer representation of minimum log level to handle. |
| 39 | */ |
| 40 | protected $threshold; |
| 41 | |
| 42 | /** |
| 43 | * Stores email recipients. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $recipients = array(); |
| 48 | |
| 49 | /** |
| 50 | * Stores log messages. |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | protected $logs = array(); |
| 55 | |
| 56 | /** |
| 57 | * Stores integer representation of maximum logged level. |
| 58 | * |
| 59 | * @var int |
| 60 | */ |
| 61 | protected $max_severity = null; |
| 62 | |
| 63 | /** |
| 64 | * Constructor for log handler. |
| 65 | * |
| 66 | * @param string|array $recipients Optional. Email(s) to receive log messages. Defaults to site admin email. |
| 67 | * @param string $threshold Optional. Minimum level that should receive log messages. |
| 68 | * Default 'alert'. One of: emergency|alert|critical|error|warning|notice|info|debug. |
| 69 | */ |
| 70 | public function __construct( $recipients = null, $threshold = 'alert' ) { |
| 71 | if ( null === $recipients ) { |
| 72 | $recipients = get_option( 'admin_email' ); |
| 73 | } |
| 74 | |
| 75 | if ( is_array( $recipients ) ) { |
| 76 | foreach ( $recipients as $recipient ) { |
| 77 | $this->add_email( $recipient ); |
| 78 | } |
| 79 | } else { |
| 80 | $this->add_email( $recipients ); |
| 81 | } |
| 82 | |
| 83 | $this->set_threshold( $threshold ); |
| 84 | add_action( 'shutdown', array( $this, 'send_log_email' ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Set handler severity threshold. |
| 89 | * |
| 90 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 91 | */ |
| 92 | public function set_threshold( $level ) { |
| 93 | $this->threshold = WC_Log_Levels::get_level_severity( $level ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Determine whether handler should handle log. |
| 98 | * |
| 99 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 100 | * @return bool True if the log should be handled. |
| 101 | */ |
| 102 | protected function should_handle( $level ) { |
| 103 | return $this->threshold <= WC_Log_Levels::get_level_severity( $level ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Handle a log entry. |
| 108 | * |
| 109 | * @param int $timestamp Log timestamp. |
| 110 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 111 | * @param string $message Log message. |
| 112 | * @param array $context Optional. Additional information for log handlers. |
| 113 | * |
| 114 | * @return bool False if value was not handled and true if value was handled. |
| 115 | */ |
| 116 | public function handle( $timestamp, $level, $message, $context ) { |
| 117 | |
| 118 | if ( $this->should_handle( $level ) ) { |
| 119 | $this->add_log( $timestamp, $level, $message, $context ); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Send log email. |
| 128 | * |
| 129 | * @return bool True if email is successfully sent otherwise false. |
| 130 | */ |
| 131 | public function send_log_email() { |
| 132 | $result = false; |
| 133 | |
| 134 | if ( ! empty( $this->logs ) ) { |
| 135 | $subject = $this->get_subject(); |
| 136 | $body = $this->get_body(); |
| 137 | $result = wp_mail( $this->recipients, $subject, $body ); |
| 138 | $this->clear_logs(); |
| 139 | } |
| 140 | |
| 141 | return $result; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Build subject for log email. |
| 146 | * |
| 147 | * @return string subject |
| 148 | */ |
| 149 | protected function get_subject() { |
| 150 | $site_name = get_bloginfo( 'name' ); |
| 151 | $max_level = strtoupper( WC_Log_Levels::get_severity_level( $this->max_severity ) ); |
| 152 | $log_count = count( $this->logs ); |
| 153 | |
| 154 | return sprintf( |
| 155 | /* translators: 1: Site name 2: Maximum level 3: Log count */ |
| 156 | _n( |
| 157 | '[%1$s] %2$s: %3$s WooCommerce log message', |
| 158 | '[%1$s] %2$s: %3$s WooCommerce log messages', |
| 159 | $log_count, |
| 160 | 'woocommerce' |
| 161 | ), |
| 162 | $site_name, |
| 163 | $max_level, |
| 164 | $log_count |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Build body for log email. |
| 170 | * |
| 171 | * @return string body |
| 172 | */ |
| 173 | protected function get_body() { |
| 174 | $site_name = get_bloginfo( 'name' ); |
| 175 | $entries = implode( PHP_EOL, $this->logs ); |
| 176 | $log_count = count( $this->logs ); |
| 177 | return _n( |
| 178 | 'You have received the following WooCommerce log message:', |
| 179 | 'You have received the following WooCommerce log messages:', |
| 180 | $log_count, |
| 181 | 'woocommerce' |
| 182 | ) . PHP_EOL |
| 183 | . PHP_EOL |
| 184 | . $entries |
| 185 | . PHP_EOL |
| 186 | . PHP_EOL |
| 187 | /* translators: %s: Site name */ |
| 188 | . sprintf( __( 'Visit %s admin area:', 'woocommerce' ), $site_name ) |
| 189 | . PHP_EOL |
| 190 | . admin_url(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Adds an email to the list of recipients. |
| 195 | * |
| 196 | * @param string $email Email address to add. |
| 197 | */ |
| 198 | public function add_email( $email ) { |
| 199 | array_push( $this->recipients, $email ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Add log message. |
| 204 | * |
| 205 | * @param int $timestamp Log timestamp. |
| 206 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 207 | * @param string $message Log message. |
| 208 | * @param array $context Additional information for log handlers. |
| 209 | */ |
| 210 | protected function add_log( $timestamp, $level, $message, $context ) { |
| 211 | $this->logs[] = $this->format_entry( $timestamp, $level, $message, $context ); |
| 212 | |
| 213 | $log_severity = WC_Log_Levels::get_level_severity( $level ); |
| 214 | if ( $this->max_severity < $log_severity ) { |
| 215 | $this->max_severity = $log_severity; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Clear log messages. |
| 221 | */ |
| 222 | protected function clear_logs() { |
| 223 | $this->logs = array(); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 |