From 44881e2d8e64b3dfdec7ed67a331713257bf003a Mon Sep 17 00:00:00 2001
From: Piotr Potrebka <piotr.potrebka@wpdesk.net>
Date: Tue, 6 Aug 2024 09:19:20 +0200
Subject: [PATCH] feat: email styles

---
 src/Abstracts/Email.php                       | 18 ++++++-
 src/Abstracts/Mailer.php                      |  4 --
 src/EmailTemplate.php                         | 54 ++++++++++++-------
 src/WPMailer.php                              | 29 +++++++++-
 .../html/{default.php => email-content.php}   |  0
 .../plain/{default.php => email-content.php}  |  0
 6 files changed, 78 insertions(+), 27 deletions(-)
 rename templates/html/{default.php => email-content.php} (100%)
 rename templates/plain/{default.php => email-content.php} (100%)

diff --git a/src/Abstracts/Email.php b/src/Abstracts/Email.php
index e3c35d1..d21f2bb 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 5c02806..0602ff2 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 8930c87..d69be36 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 91299ba..d17cdaa 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
-- 
GitLab