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

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