| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Email; |
| 6 |
|
| 7 |
use Packetery\Latte\Engine; |
| 8 |
use Packetery\Module\Framework\WpAdapter; |
| 9 |
|
| 10 |
class BugReportEmail { |
| 11 |
|
| 12 |
/** @var WpAdapter */ |
| 13 |
private $wpAdapter; |
| 14 |
|
| 15 |
/** @var string */ |
| 16 |
private $supportEmailAddress; |
| 17 |
|
| 18 |
/** @var Engine */ |
| 19 |
private $latteEngine; |
| 20 |
|
| 21 |
/** @var BugReportAttachment */ |
| 22 |
private $bugReportAttachment; |
| 23 |
|
| 24 |
public function __construct( |
| 25 |
WpAdapter $wpAdapter, |
| 26 |
string $supportEmailAddress, |
| 27 |
Engine $latteEngine, |
| 28 |
BugReportAttachment $bugReportAttachment |
| 29 |
) { |
| 30 |
$this->wpAdapter = $wpAdapter; |
| 31 |
$this->supportEmailAddress = $supportEmailAddress; |
| 32 |
$this->latteEngine = $latteEngine; |
| 33 |
$this->bugReportAttachment = $bugReportAttachment; |
| 34 |
} |
| 35 |
|
| 36 |
public function sendBugReport( string $replyEmail, string $message, bool $sendCopy ): bool { |
| 37 |
$siteName = $this->wpAdapter->getBlogInfo( 'name', 'raw' ); |
| 38 |
// translators: %s is site name |
| 39 |
$subject = sprintf( $this->wpAdapter->__( 'Packeta: Plugin WP - bug report - %s', 'packeta' ), $siteName ); |
| 40 |
|
| 41 |
/** |
| 42 |
* @var non-empty-list<string> $headers |
| 43 |
*/ |
| 44 |
$headers = [ |
| 45 |
'Content-Type: text/html; charset=UTF-8', |
| 46 |
"From: {$replyEmail}", |
| 47 |
"Reply-To: {$replyEmail}", |
| 48 |
]; |
| 49 |
|
| 50 |
$to = $this->supportEmailAddress; |
| 51 |
if ( $sendCopy ) { |
| 52 |
$to = [ $this->supportEmailAddress, $replyEmail ]; |
| 53 |
} |
| 54 |
|
| 55 |
return $this->wpAdapter->wpMail( |
| 56 |
$to, |
| 57 |
$subject, |
| 58 |
$this->createEmailBody( $replyEmail, $message, $siteName ), |
| 59 |
$headers, |
| 60 |
$this->bugReportAttachment->createAttachments() |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
private function createEmailBody( string $replyEmail, string $message, string $siteName ): string { |
| 65 |
return $this->latteEngine->renderToString( |
| 66 |
PACKETERY_PLUGIN_DIR . '/template/email/bug-report.latte', |
| 67 |
[ |
| 68 |
'replyEmail' => $replyEmail, |
| 69 |
'message' => $message, |
| 70 |
'siteName' => $siteName, |
| 71 |
'zipArchiveAvailable' => class_exists( 'ZipArchive' ), |
| 72 |
'translations' => [ |
| 73 |
'bugReportTitle' => $this->wpAdapter->__( 'Bug report from Packeta plugin', 'packeta' ), |
| 74 |
'website' => $this->wpAdapter->__( 'Website:', 'packeta' ), |
| 75 |
'replyEmail' => $this->wpAdapter->__( 'Email for reply:', 'packeta' ), |
| 76 |
'message' => $this->wpAdapter->__( 'Message:', 'packeta' ), |
| 77 |
'autoGenerated' => $this->wpAdapter->__( 'This report was submitted via the Packeta WordPress plugin.', 'packeta' ), |
| 78 |
'attachmentsNotAvailable' => $this->wpAdapter->__( 'Attachments could not be added to the email because the e-shop server is missing the extension required to create a ZIP file.', 'packeta' ), |
| 79 |
], |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|