2016-11-13 21:15:55 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles log entries by sending an email.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected $threshold;
|
|
|
|
|
2016-11-13 21:15:55 +00:00
|
|
|
/**
|
|
|
|
* Stores email recipients.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-11 11:02:40 +00:00
|
|
|
private $recipients = array();
|
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.
|
|
|
|
* @param string $threshold Optional. Minimum level that should receive log messages.
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set handler severity threshold.
|
|
|
|
*
|
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
|
|
|
*/
|
|
|
|
public function set_threshold( $level ) {
|
|
|
|
$this->threshold = WC_Log_Levels::get_level_severity( $level );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether handler should handle log.
|
|
|
|
*
|
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
|
|
|
* @return bool True if the log should be handled.
|
|
|
|
*/
|
|
|
|
public function should_handle( $level ) {
|
|
|
|
return $this->threshold <= WC_Log_Levels::get_level_severity( $level );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a log entry.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-13 21:15:55 +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.
|
|
|
|
* @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-11-20 13:32:46 +00:00
|
|
|
$subject = $this->get_subject( $timestamp, $level, $message, $context );
|
|
|
|
$body = $this->get_body( $timestamp, $level, $message, $context );
|
2016-12-11 11:02:40 +00:00
|
|
|
return wp_mail( $this->recipients, $subject, $body );
|
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
|
|
|
/**
|
|
|
|
* Build subject for log email.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-13 22:22:45 +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.
|
|
|
|
* @param array $context Optional. Additional information for log handlers.
|
2016-11-13 22:22:45 +00:00
|
|
|
*
|
|
|
|
* @return string subject
|
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
public function get_subject( $timestamp, $level, $message, $context ) {
|
2016-11-19 16:34:27 +00:00
|
|
|
$site_name = get_bloginfo( 'name' );
|
2016-11-19 17:16:09 +00:00
|
|
|
return sprintf( __( '[%1$s] WooCommerce log message from %2$s', 'woocommerce' ), strtoupper( $level ), $site_name );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 22:22:45 +00:00
|
|
|
/**
|
|
|
|
* Build body for log email.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-13 22:22:45 +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.
|
|
|
|
* @param array $context Optional. Additional information for log handlers.
|
2016-11-13 22:22:45 +00:00
|
|
|
*
|
|
|
|
* @return string body
|
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
public function get_body( $timestamp, $level, $message, $context ) {
|
|
|
|
$entry = $this->format_entry( $timestamp, $level, $message, $context );
|
2016-12-11 17:03:48 +00:00
|
|
|
return __( 'You have received the following WooCommerce log message:', 'woocommerce' )
|
2016-11-19 16:33:31 +00:00
|
|
|
. PHP_EOL . PHP_EOL . $entry;
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an email to the list of recipients.
|
2016-11-13 22:22:45 +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
|
|
|
}
|
|
|
|
}
|