woocommerce/includes/log-handlers/class-wc-log-handler-email.php

136 lines
3.8 KiB
PHP
Raw Normal View History

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 {
/**
* 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
*/
private $recipients = array();
2016-11-13 21:15:55 +00:00
/**
* Constructor for log handler.
2016-11-13 21:15:55 +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
*/
public function __construct( $recipients = null, $threshold = 'alert' ) {
if ( null === $recipients ) {
$recipients = get_option( 'admin_email' );
}
2016-11-13 21:15:55 +00:00
if ( is_array( $recipients ) ) {
foreach ( $recipients as $recipient ) {
$this->add_email( $recipient );
2016-11-13 21:15:55 +00:00
}
} else {
$this->add_email( $recipients );
2016-11-13 21:15:55 +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.
*
* @param int $timestamp Log timestamp.
2016-11-13 21:15:55 +00:00
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message Log message.
* @param array $context Optional. Additional information for log handlers.
2016-11-13 21:15:55 +00:00
*
* @return bool True on success.
2016-11-13 21:15:55 +00:00
*/
public function handle( $timestamp, $level, $message, $context ) {
2016-11-13 21:15:55 +00:00
if ( $this->should_handle( $level ) ) {
$subject = $this->get_subject( $timestamp, $level, $message, $context );
$body = $this->get_body( $timestamp, $level, $message, $context );
return wp_mail( $this->recipients, $subject, $body );
2016-11-13 21:15:55 +00:00
}
return true;
}
/**
* Build subject for log email.
*
* @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message Log message.
* @param array $context Optional. Additional information for log handlers.
*
* @return string subject
*/
public function get_subject( $timestamp, $level, $message, $context ) {
$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
}
/**
* Build body for log email.
*
* @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message Log message.
* @param array $context Optional. Additional information for log handlers.
*
* @return string body
*/
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' )
. PHP_EOL . PHP_EOL . $entry;
2016-11-13 21:15:55 +00:00
}
/**
* Adds an email to the list of recipients.
*
* @param string email Email address to add
2016-11-13 21:15:55 +00:00
*/
public function add_email( $email ) {
array_push( $this->recipients, $email );
2016-11-13 21:15:55 +00:00
}
}