2016-11-13 21:15:55 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! class_exists( 'WC_Log_Handler' ) ) {
|
|
|
|
include_once( dirname( dirname( __FILE__ ) ) . '/abstracts/abstract-wc-log-handler.php' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores email recipients.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-11-16 19:19:20 +00:00
|
|
|
private $to = array();
|
2016-11-13 21:15:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the logger.
|
|
|
|
*
|
|
|
|
* @param $args additional args. {
|
|
|
|
* Optional. @see WC_Log_Handler::__construct.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @type string|array $to Optional. Email or emails to recieve log messages. Default site admin.
|
2016-11-13 21:15:55 +00:00
|
|
|
* }
|
|
|
|
*/
|
2016-11-13 22:22:45 +00:00
|
|
|
public function __construct( $args = array() ) {
|
2016-11-13 21:15:55 +00:00
|
|
|
|
|
|
|
$args = wp_parse_args( $args, array(
|
|
|
|
'threshold' => 'critical',
|
|
|
|
'to' => get_option( 'admin_email' ),
|
|
|
|
) );
|
|
|
|
|
|
|
|
parent::__construct( $args );
|
|
|
|
|
|
|
|
if ( is_array( $args['to'] ) ) {
|
|
|
|
foreach ( $args['to'] as $to ) {
|
|
|
|
$this->add_email( $to );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->add_email( $args['to'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-10 21:21:41 +00:00
|
|
|
* @return bool True on success.
|
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-10 21:21:41 +00:00
|
|
|
return wp_mail( $this->to, $subject, $body );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-11-19 16:33:31 +00:00
|
|
|
return __( 'You have recieved the following WooCommerce log message:', 'woocommerce' )
|
|
|
|
. 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-11-16 19:19:20 +00:00
|
|
|
array_push( $this->to, $email );
|
2016-11-13 21:15:55 +00:00
|
|
|
}
|
|
|
|
}
|