WC_Log_Handler_Email

Handles log entries by sending an email.

Defined (1)

The class is defined in the following location(s).

/includes/log-handlers/class-wc-log-handler-email.php  
  1. class WC_Log_Handler_Email extends WC_Log_Handler { 
  2.  
  3. /** 
  4. * Minimum log level this handler will process. 
  5. * @var int Integer representation of minimum log level to handle. 
  6. */ 
  7. protected $threshold; 
  8.  
  9. /** 
  10. * Stores email recipients. 
  11. * @var array 
  12. */ 
  13. protected $recipients = array(); 
  14.  
  15. /** 
  16. * Stores log messages. 
  17. * @var array 
  18. */ 
  19. protected $logs = array(); 
  20.  
  21. /** 
  22. * Stores integer representation of maximum logged level. 
  23. * @var int 
  24. */ 
  25. protected $max_severity = null; 
  26.  
  27. /** 
  28. * Constructor for log handler. 
  29. * @param string|array $recipients Optional. Email(s) to receive log messages. Defaults to site admin email. 
  30. * @param string $threshold Optional. Minimum level that should receive log messages. 
  31. * Default 'alert'. One of: emergency|alert|critical|error|warning|notice|info|debug 
  32. */ 
  33. public function __construct( $recipients = null, $threshold = 'alert' ) { 
  34. if ( null === $recipients ) { 
  35. $recipients = get_option( 'admin_email' ); 
  36.  
  37. if ( is_array( $recipients ) ) { 
  38. foreach ( $recipients as $recipient ) { 
  39. $this->add_email( $recipient ); 
  40. } else { 
  41. $this->add_email( $recipients ); 
  42.  
  43. $this->set_threshold( $threshold ); 
  44. add_action( 'shutdown', array( $this, 'send_log_email' ) ); 
  45.  
  46. /** 
  47. * Set handler severity threshold. 
  48. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  49. */ 
  50. public function set_threshold( $level ) { 
  51. $this->threshold = WC_Log_Levels::get_level_severity( $level ); 
  52.  
  53. /** 
  54. * Determine whether handler should handle log. 
  55. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  56. * @return bool True if the log should be handled. 
  57. */ 
  58. protected function should_handle( $level ) { 
  59. return $this->threshold <= WC_Log_Levels::get_level_severity( $level ); 
  60.  
  61. /** 
  62. * Handle a log entry. 
  63. * @param int $timestamp Log timestamp. 
  64. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  65. * @param string $message Log message. 
  66. * @param array $context Optional. Additional information for log handlers. 
  67. * @return bool False if value was not handled and true if value was handled. 
  68. */ 
  69. public function handle( $timestamp, $level, $message, $context ) { 
  70.  
  71. if ( $this->should_handle( $level ) ) { 
  72. $this->add_log( $timestamp, $level, $message, $context ); 
  73. return true; 
  74.  
  75. return false; 
  76.  
  77. /** 
  78. * Send log email. 
  79. * @return bool True if email is successfully sent otherwise false. 
  80. */ 
  81. public function send_log_email() { 
  82. $result = false; 
  83.  
  84. if ( ! empty( $this->logs ) ) { 
  85. $subject = $this->get_subject(); 
  86. $body = $this->get_body(); 
  87. $result = wp_mail( $this->recipients, $subject, $body ); 
  88. $this->clear_logs(); 
  89.  
  90. return $result; 
  91.  
  92. /** 
  93. * Build subject for log email. 
  94. * @return string subject 
  95. */ 
  96. protected function get_subject() { 
  97. $site_name = get_bloginfo( 'name' ); 
  98. $max_level = strtoupper( WC_Log_Levels::get_severity_level( $this->max_severity ) ); 
  99. $log_count = count( $this->logs ); 
  100.  
  101. return sprintf( 
  102. _n( 
  103. '[%1$s] %2$s: %3$s WooCommerce log message',  
  104. '[%1$s] %2$s: %3$s WooCommerce log messages',  
  105. $log_count,  
  106. 'woocommerce' 
  107. ),  
  108. $site_name,  
  109. $max_level,  
  110. $log_count 
  111. ); 
  112.  
  113. /** 
  114. * Build body for log email. 
  115. * @return string body 
  116. */ 
  117. protected function get_body() { 
  118. $site_name = get_bloginfo( 'name' ); 
  119. $entries = implode( PHP_EOL, $this->logs ); 
  120. $log_count = count( $this->logs ); 
  121. return _n( 
  122. 'You have received the following WooCommerce log message:',  
  123. 'You have received the following WooCommerce log messages:',  
  124. $log_count,  
  125. 'woocommerce' 
  126. . PHP_EOL 
  127. . PHP_EOL 
  128. . $entries 
  129. . PHP_EOL 
  130. . PHP_EOL 
  131. . sprintf( __( 'Visit %s admin area:', 'woocommerce' ), $site_name ) 
  132. . PHP_EOL 
  133. . admin_url(); 
  134.  
  135. /** 
  136. * Adds an email to the list of recipients. 
  137. * @param string email Email address to add 
  138. */ 
  139. public function add_email( $email ) { 
  140. array_push( $this->recipients, $email ); 
  141.  
  142. /** 
  143. * Add log message. 
  144. */ 
  145. protected function add_log( $timestamp, $level, $message, $context ) { 
  146. $this->logs[] = $this->format_entry( $timestamp, $level, $message, $context ); 
  147.  
  148. $log_severity = WC_Log_Levels::get_level_severity( $level ); 
  149. if ( $this->max_severity < $log_severity ) { 
  150. $this->max_severity = $log_severity; 
  151.  
  152. /** 
  153. * Clear log messages. 
  154. */ 
  155. protected function clear_logs() { 
  156. $this->logs = array(); 
  157.