2016-11-13 21:15:55 +00:00
|
|
|
<?php
|
2018-03-07 13:49:32 +00:00
|
|
|
/**
|
|
|
|
* Class WC_Log_Handler_Email file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Log Handlers
|
|
|
|
*/
|
|
|
|
|
2016-11-13 21:15:55 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2018-03-07 13:49:32 +00:00
|
|
|
exit; // Exit if accessed directly.
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles log entries by sending an email.
|
|
|
|
*
|
2016-12-16 20:02:54 +00:00
|
|
|
* WARNING!
|
|
|
|
* This log handler has known limitations.
|
|
|
|
*
|
|
|
|
* Log messages are aggregated and sent once per request (if necessary). If the site experiences a
|
|
|
|
* problem, the log email may never be sent. This handler should be used with another handler which
|
|
|
|
* stores logs in order to prevent loss.
|
|
|
|
*
|
|
|
|
* It is not recommended to use this handler on a high traffic site. There will be a maximum of 1
|
|
|
|
* email sent per request per handler, but that could still be a dangerous amount of emails under
|
|
|
|
* heavy traffic. Do not confuse this handler with an appropriate monitoring solution!
|
|
|
|
*
|
|
|
|
* If you understand these limitations, feel free to use this handler or borrow parts of the design
|
|
|
|
* to implement your own!
|
|
|
|
*
|
2016-11-13 21:15:55 +00:00
|
|
|
* @class WC_Log_Handler_Email
|
|
|
|
* @version 1.0.0
|
2016-11-20 14:18:06 +00:00
|
|
|
* @package WooCommerce/Classes/Log_Handlers
|
2016-11-13 21:15:55 +00:00
|
|
|
*/
|
|
|
|
class WC_Log_Handler_Email extends WC_Log_Handler {
|
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
/**
|
|
|
|
* Minimum log level this handler will process.
|
|
|
|
*
|
|
|
|
* @var int Integer representation of minimum log level to handle.
|
|
|
|
*/
|
|
|
|
protected $threshold;
|
|
|
|
|
2016-11-13 21:15:55 +00:00
|
|
|
/**
|
|
|
|
* Stores email recipients.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected $recipients = array();
|
2016-11-13 21:15:55 +00:00
|
|
|
|
2016-12-16 20:02:54 +00:00
|
|
|
/**
|
|
|
|
* Stores log messages.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected $logs = array();
|
2016-12-16 20:02:54 +00:00
|
|
|
|
2016-12-21 19:56:53 +00:00
|
|
|
/**
|
|
|
|
* Stores integer representation of maximum logged level.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $max_severity = null;
|
|
|
|
|
2016-11-13 21:15:55 +00:00
|
|
|
/**
|
2016-12-11 11:02:40 +00:00
|
|
|
* Constructor for log handler.
|
2016-11-13 21:15:55 +00:00
|
|
|
*
|
2016-12-11 11:02:40 +00:00
|
|
|
* @param string|array $recipients Optional. Email(s) to receive log messages. Defaults to site admin email.
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param string $threshold Optional. Minimum level that should receive log messages.
|
2018-03-07 13:49:32 +00:00
|
|
|
* Default 'alert'. One of: emergency|alert|critical|error|warning|notice|info|debug.
|
2016-11-13 21:15:55 +00:00
|
|
|
*/
|
2016-12-11 11:02:40 +00:00
|
|
|
public function __construct( $recipients = null, $threshold = 'alert' ) {
|
|
|
|
if ( null === $recipients ) {
|
|
|
|
$recipients = get_option( 'admin_email' );
|
|
|
|
}
|
2016-11-13 21:15:55 +00:00
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
if ( is_array( $recipients ) ) {
|
|
|
|
foreach ( $recipients as $recipient ) {
|
|
|
|
$this->add_email( $recipient );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-11 11:02:40 +00:00
|
|
|
$this->add_email( $recipients );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
$this->set_threshold( $threshold );
|
2016-12-16 20:02:54 +00:00
|
|
|
add_action( 'shutdown', array( $this, 'send_log_email' ) );
|
2016-12-11 11:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set handler severity threshold.
|
|
|
|
*
|
2018-03-07 13:49:32 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2016-12-11 11:02:40 +00:00
|
|
|
*/
|
|
|
|
public function set_threshold( $level ) {
|
|
|
|
$this->threshold = WC_Log_Levels::get_level_severity( $level );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether handler should handle log.
|
|
|
|
*
|
2018-03-07 13:49:32 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2016-12-11 11:02:40 +00:00
|
|
|
* @return bool True if the log should be handled.
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected function should_handle( $level ) {
|
2016-12-11 11:02:40 +00:00
|
|
|
return $this->threshold <= WC_Log_Levels::get_level_severity( $level );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a log entry.
|
|
|
|
*
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2018-03-07 13:49:32 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param string $message Log message.
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param array $context Optional. Additional information for log handlers.
|
2016-11-13 21:15:55 +00:00
|
|
|
*
|
2016-12-15 14:34:48 +00:00
|
|
|
* @return bool False if value was not handled and true if value was handled.
|
2016-11-13 21:15:55 +00:00
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
public function handle( $timestamp, $level, $message, $context ) {
|
2016-11-13 21:15:55 +00:00
|
|
|
|
|
|
|
if ( $this->should_handle( $level ) ) {
|
2016-12-16 20:02:54 +00:00
|
|
|
$this->add_log( $timestamp, $level, $message, $context );
|
|
|
|
return true;
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 14:34:48 +00:00
|
|
|
return false;
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 22:22:45 +00:00
|
|
|
/**
|
2016-12-16 20:02:54 +00:00
|
|
|
* Send log email.
|
2016-11-13 22:22:45 +00:00
|
|
|
*
|
2016-12-16 20:02:54 +00:00
|
|
|
* @return bool True if email is successfully sent otherwise false.
|
|
|
|
*/
|
|
|
|
public function send_log_email() {
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
if ( ! empty( $this->logs ) ) {
|
|
|
|
$subject = $this->get_subject();
|
2018-03-07 13:30:00 +00:00
|
|
|
$body = $this->get_body();
|
|
|
|
$result = wp_mail( $this->recipients, $subject, $body );
|
2016-12-16 20:02:54 +00:00
|
|
|
$this->clear_logs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build subject for log email.
|
2016-11-13 22:22:45 +00:00
|
|
|
*
|
|
|
|
* @return string subject
|
|
|
|
*/
|
2016-12-21 19:56:53 +00:00
|
|
|
protected function get_subject() {
|
2016-11-19 16:34:27 +00:00
|
|
|
$site_name = get_bloginfo( 'name' );
|
2016-12-21 19:56:53 +00:00
|
|
|
$max_level = strtoupper( WC_Log_Levels::get_severity_level( $this->max_severity ) );
|
|
|
|
$log_count = count( $this->logs );
|
|
|
|
|
|
|
|
return sprintf(
|
2018-03-07 13:49:32 +00:00
|
|
|
/* translators: 1: Site name 2: Maximum level 3: Log count */
|
2016-12-21 19:56:53 +00:00
|
|
|
_n(
|
|
|
|
'[%1$s] %2$s: %3$s WooCommerce log message',
|
|
|
|
'[%1$s] %2$s: %3$s WooCommerce log messages',
|
|
|
|
$log_count,
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
$site_name,
|
|
|
|
$max_level,
|
|
|
|
$log_count
|
|
|
|
);
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 22:22:45 +00:00
|
|
|
/**
|
|
|
|
* Build body for log email.
|
|
|
|
*
|
|
|
|
* @return string body
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected function get_body() {
|
2016-12-16 20:02:54 +00:00
|
|
|
$site_name = get_bloginfo( 'name' );
|
2018-03-07 13:30:00 +00:00
|
|
|
$entries = implode( PHP_EOL, $this->logs );
|
2016-12-21 19:56:53 +00:00
|
|
|
$log_count = count( $this->logs );
|
2016-12-22 22:34:24 +00:00
|
|
|
return _n(
|
2018-03-07 13:30:00 +00:00
|
|
|
'You have received the following WooCommerce log message:',
|
|
|
|
'You have received the following WooCommerce log messages:',
|
|
|
|
$log_count,
|
|
|
|
'woocommerce'
|
|
|
|
) . PHP_EOL
|
2016-12-16 20:02:54 +00:00
|
|
|
. PHP_EOL
|
|
|
|
. $entries
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
2018-03-07 13:49:32 +00:00
|
|
|
/* translators: %s: Site name */
|
2016-12-16 20:02:54 +00:00
|
|
|
. sprintf( __( 'Visit %s admin area:', 'woocommerce' ), $site_name )
|
|
|
|
. PHP_EOL
|
|
|
|
. admin_url();
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an email to the list of recipients.
|
2016-11-13 22:22:45 +00:00
|
|
|
*
|
2018-03-07 13:49:32 +00:00
|
|
|
* @param string $email Email address to add.
|
2016-11-13 21:15:55 +00:00
|
|
|
*/
|
|
|
|
public function add_email( $email ) {
|
2016-12-11 11:02:40 +00:00
|
|
|
array_push( $this->recipients, $email );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
2016-12-16 20:02:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add log message.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2018-03-07 13:49:32 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param string $message Log message.
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param array $context Additional information for log handlers.
|
2016-12-16 20:02:54 +00:00
|
|
|
*/
|
|
|
|
protected function add_log( $timestamp, $level, $message, $context ) {
|
|
|
|
$this->logs[] = $this->format_entry( $timestamp, $level, $message, $context );
|
2016-12-21 19:56:53 +00:00
|
|
|
|
|
|
|
$log_severity = WC_Log_Levels::get_level_severity( $level );
|
|
|
|
if ( $this->max_severity < $log_severity ) {
|
|
|
|
$this->max_severity = $log_severity;
|
|
|
|
}
|
2016-12-16 20:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear log messages.
|
|
|
|
*/
|
|
|
|
protected function clear_logs() {
|
|
|
|
$this->logs = array();
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|