| 1 |
<?php |
| 2 |
|
| 3 |
namespace DebugLogViewer\Admin\Services; |
| 4 |
|
| 5 |
use DebugLogViewer\Admin\Controllers\AlertController; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
class EmailService { |
| 12 |
|
| 13 |
public $emails; |
| 14 |
public $subject; |
| 15 |
public $body; |
| 16 |
public $notificationController; |
| 17 |
|
| 18 |
public function __construct( ?AlertController $notificationController = null ) { |
| 19 |
$this->notificationController = $notificationController ?? new AlertController(); |
| 20 |
} |
| 21 |
|
| 22 |
public function setBody( $body ) { |
| 23 |
$this->body = $body; |
| 24 |
} |
| 25 |
public function getBody() { |
| 26 |
return $this->body; |
| 27 |
} |
| 28 |
|
| 29 |
public function setEmails( $emails ) { |
| 30 |
$this->emails = $emails; |
| 31 |
} |
| 32 |
public function getEmails() { |
| 33 |
return $this->emails; |
| 34 |
} |
| 35 |
|
| 36 |
public function setSubject( $subject ) { |
| 37 |
$this->subject = $subject; |
| 38 |
} |
| 39 |
public function getSubject() { |
| 40 |
return $this->subject; |
| 41 |
} |
| 42 |
|
| 43 |
public function prepare( $template, $params ) { |
| 44 |
$data = file_get_contents(realpath(__DIR__) . '/../templates/email/' . $template); |
| 45 |
$data = str_replace('{{dbg_lv_summary}}', $this->renderTable($params['entries']), $data); |
| 46 |
$data = str_replace('{{dbg_lv_website}}', $params['website'], $data); |
| 47 |
$data = str_replace('{{dbg_lv_generated_date}}', date_i18n('jS \of F Y \a\t h:i:s A'), $data); |
| 48 |
|
| 49 |
$this->setBody($data); |
| 50 |
} |
| 51 |
|
| 52 |
private function renderTable( $errors ) { |
| 53 |
$body = ''; |
| 54 |
$td_style_odd = ' style="border-bottom: 1px solid #e8eef8; color: #4c4c4c; padding: 10px 12px; vertical-align: top;"'; |
| 55 |
$td_style_even = ' style="background-color: #f7f9ff; border-bottom: 1px solid #e8eef8; color: #4c4c4c; padding: 10px 12px; vertical-align: top;"'; |
| 56 |
|
| 57 |
$index = 1; |
| 58 |
foreach ($this->notificationController->getLevelsToReport() as $level) { |
| 59 |
foreach ($errors[ $level ] as $hash => $error) { |
| 60 |
$td_style = ($index % 2 === 0) ? $td_style_even : $td_style_odd; |
| 61 |
$row = '' . |
| 62 |
'<tr>' . |
| 63 |
' <td ' . $td_style . '>%s</td>' . |
| 64 |
' <td ' . $td_style . '>%s</td>' . |
| 65 |
' <td ' . $td_style . '>%s</td>' . |
| 66 |
' <td ' . $td_style . '>%s</td>' . |
| 67 |
' <td ' . $td_style . '>%s</td>' . |
| 68 |
' <td ' . $td_style . '>%d</td>' . |
| 69 |
'</tr>'; |
| 70 |
$body .= sprintf($row, $index, $error['type'], $error['description']['text'], $error['file'], $error['line'], $error['hits']); |
| 71 |
$index += 1; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $body; |
| 76 |
} |
| 77 |
|
| 78 |
public function send() { |
| 79 |
$emails = $this->getEmails(); |
| 80 |
if (!empty($emails)) { |
| 81 |
// Send to all emails in bulk |
| 82 |
$headers = [ 'Content-Type: text/html; charset=UTF-8' ]; |
| 83 |
$result = wp_mail($emails, $this->getSubject(), $this->getBody(), $headers); |
| 84 |
|
| 85 |
// Log or handle errors if sending fails |
| 86 |
if (!$result) { |
| 87 |
error_log('Failed to send emails to: ' . implode(', ', $emails)); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|