diff --git a/src/Abstracts/Email.php b/src/Abstracts/Email.php index e3c35d1ab99f3d65382b529b7d582ce399c95a5d..d21f2bb7ec8f7d3bc2af47af6a784e82d8444959 100644 --- a/src/Abstracts/Email.php +++ b/src/Abstracts/Email.php @@ -41,6 +41,11 @@ class Email { */ private $from_name; + /** + * @var array + */ + private $template_attributes; + /** * @param string $from_email * @@ -56,7 +61,7 @@ class Email { * @return string */ public function get_from(): string { - return sanitize_email( $this->from_email ); + return $this->from_email; } /** @@ -134,7 +139,6 @@ class Email { return $this->headers[ $header ] ?? ''; } - /** * @param array $attachments * @@ -196,4 +200,14 @@ class Email { return $this->content; } + public function set_template_attributes( string $name, string $value ): self { + $this->template_attributes[ $name ] = $value; + + return $this; + } + + public function get_template_attributes(): array { + return $this->template_attributes; + } + } diff --git a/src/Abstracts/Mailer.php b/src/Abstracts/Mailer.php index 5c02806e3c72cce20405d0f2bfb49920595c3bf4..0602ff20db09f65bf87632c259a677d28c75a367 100644 --- a/src/Abstracts/Mailer.php +++ b/src/Abstracts/Mailer.php @@ -2,12 +2,8 @@ namespace WPDesk\Library\WPEmail\Abstracts; - use WPDesk\Library\WPEmail\Exceptions\MailerException; -/** - * Object-oriented wp_mail wrapper. - */ interface Mailer { /** diff --git a/src/EmailTemplate.php b/src/EmailTemplate.php index 8930c87be9809429ee55a47afeeffafb8ce1beef..d69be36cd1138e6d3c65cccce7d3c38d8c86eaef 100644 --- a/src/EmailTemplate.php +++ b/src/EmailTemplate.php @@ -1,20 +1,30 @@ <?php -use WPDesk\Library\WPEmail\Abstracts\Email; +namespace WPDesk\Library\WPEmail; + use WPDesk\Library\WPEmail\Helpers\StyleInliner; +use WPDesk\View\Renderer\Renderer; class EmailTemplate { + /** + * @var Renderer + */ + private $renderer; + + /** + * @var array + */ + private $template_attributes; + + public function __construct( Renderer $renderer, array $template_attributes ) { + $this->renderer = $renderer; + $this->template_attributes = wp_parse_args( $template_attributes, $this->get_default_template_attributes() ); + } - protected function get_email_template( Email $email ): string { - $output = $this->renderer->render( - 'html/email-header', - [ - 'heading' => $this->template_attributes['heading'] ?? $email->get_heading(), - 'logo' => $this->template_attributes['logo'] - ] - ); - $output .= $this->renderer->render( 'html/' . $email->get_id(), [ 'content' => $email->get_content() ] ); + public function get_email_template( string $content ): string { + $output = $this->renderer->render( 'html/email-header', $this->template_attributes ); + $output .= $this->renderer->render( 'html/email-content', [ 'content' => $content ] ); $output .= $this->renderer->render( 'html/email-footer', [ 'footer' => $this->template_attributes['footer'] ] ); return $this->css_inline( $output ); @@ -25,18 +35,22 @@ class EmailTemplate { * * @return mixed|string */ - protected function css_inline( string $content ): string { - $styles = $this->renderer->render( - 'html/email-styles', - [ - 'primary' => $this->template_attributes['primary'] ?? '#d15291', - 'text' => $this->template_attributes['text'] ?? '#303030', - 'bg' => $this->template_attributes['bg'] ?? '#f9f9f9', - 'body' => $this->template_attributes['body'] ?? '#ffffff', - ] - ); + public function css_inline( string $content ): string { + $styles = $this->renderer->render( 'html/email-styles', $this->template_attributes ); return StyleInliner::inline( $content, $styles ); } + public function get_default_template_attributes(): array { + return [ + 'heading' => '', + 'logo' => '', + 'footer' => '', + 'primary' => '#d15291', + 'text' => '#303030', + 'bg' => '#f9f9f9', + 'body' => '#ffffff', + ]; + } + } diff --git a/src/WPMailer.php b/src/WPMailer.php index 91299baa3fe9d42542bf3a4ecba2b420125bca38..d17cdaace605841524d547620887ea26b252adfc 100644 --- a/src/WPMailer.php +++ b/src/WPMailer.php @@ -7,9 +7,34 @@ use WP_Error; use WPDesk\Library\WPEmail\Abstracts\Email; use WPDesk\Library\WPEmail\Abstracts\Mailer; use WPDesk\Library\WPEmail\Exceptions\MailerException; +use WPDesk\View\Renderer\Renderer; +use WPDesk\View\Renderer\SimplePhpRenderer; +use WPDesk\View\Resolver\ChainResolver; +use WPDesk\View\Resolver\DirResolver; + class WPMailer implements Mailer { + /** + * @var Renderer + */ + private $renderer; + + public function __construct() { + $resolver = new ChainResolver(); + $resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) ); + $renderer = new SimplePhpRenderer( $resolver ); + $this->set_renderer( $renderer ); + } + + public function set_renderer( Renderer $renderer ) { + $this->renderer = $renderer; + } + + public function get_renderer(): Renderer { + return $this->renderer; + } + /** @return void */ public function send( Email $email ): void { $mailer_from = $email->get_from(); @@ -29,11 +54,13 @@ class WPMailer implements Mailer { ); add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); + $email_template = new EmailTemplate( $this->renderer, $email->get_template_attributes() ); + try { $success = wp_mail( $email->get_recipients(), $email->get_subject(), - $email->get_content(), + $email_template->get_email_template( $email->get_content() ), $email->get_headers(), $email->get_attachments() ); diff --git a/templates/html/default.php b/templates/html/email-content.php similarity index 100% rename from templates/html/default.php rename to templates/html/email-content.php diff --git a/templates/plain/default.php b/templates/plain/email-content.php similarity index 100% rename from templates/plain/default.php rename to templates/plain/email-content.php