From 82b8ba8deeb55bfee8264e9d1489af16ee9ff42d Mon Sep 17 00:00:00 2001 From: Piotr Potrebka <piotr.potrebka@wpdesk.net> Date: Mon, 23 Jan 2023 23:29:32 +0100 Subject: [PATCH] feat: email abstract --- ...lGettersAbstract.php => EmailAbstract.php} | 72 +++++++++++-------- src/EmailSender.php | 15 ++-- src/Emails/Email.php | 4 +- src/Parser/HTMLDecorator.php | 5 +- 4 files changed, 51 insertions(+), 45 deletions(-) rename src/Abstracts/{EmailGettersAbstract.php => EmailAbstract.php} (72%) diff --git a/src/Abstracts/EmailGettersAbstract.php b/src/Abstracts/EmailAbstract.php similarity index 72% rename from src/Abstracts/EmailGettersAbstract.php rename to src/Abstracts/EmailAbstract.php index 2c1fc7a..4ad465f 100644 --- a/src/Abstracts/EmailGettersAbstract.php +++ b/src/Abstracts/EmailAbstract.php @@ -4,25 +4,52 @@ namespace WPDesk\Library\WPEmail\Abstracts; use WPDesk\View\Renderer\Renderer; -abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionInterface { +abstract class EmailAbstract implements EmailGettersInterface { /** - * @var Renderer + * @var array */ - private $renderer; + private $recipients = []; + /** + * @var array + */ + private $placeholders = []; + /** + * @var string + */ + private $subject = ''; + /** + * @var string + */ + private $heading = ''; + + /** + * @var array + */ + private $attachments = []; /** * @var string */ - private $subject; + private $content = ''; + /** * @var string */ - private $heading; + private $type = 'text/html'; + + /** + * @var array + */ + private $headers; - public function __construct( Renderer $renderer, array $recipients ) { + /** + * @var Renderer + */ + private $renderer; + + public function __construct( Renderer $renderer ) { $this->renderer = $renderer; - $this->recipients = $recipients; } /** @@ -38,27 +65,21 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI * @return string[] */ public function get_placeholders(): array { - return []; + return $this->placeholders; } /** * Get email subject. * * @return string + * @throws \Exception */ public function get_subject(): string { if ( ! $this->subject ) { throw new \Exception( 'Empty email subject' ); } - return ''; - } - - - public function set_subject( string $subject ): self { - $this->subject = $subject; - - return $this; + return $this->subject; } /** @@ -67,14 +88,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI * @return string */ public function get_heading(): string { - return ''; - } - - - public function set_heading( string $heading ): self { - $this->heading = $heading; - - return $this; + return $this->heading; } /** @@ -92,7 +106,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI * @return string[] */ public function get_headers(): array { - return []; + return $this->headers; } /** @@ -101,7 +115,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI * @return array */ public function get_attachments(): array { - return []; + return $this->attachments; } /** @@ -110,7 +124,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI * @return string */ public function get_type(): string { - return 'text/html'; + return $this->type; } /** @@ -142,8 +156,4 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] ); } - public function is_valid(): bool { - return true; - } - } diff --git a/src/EmailSender.php b/src/EmailSender.php index 3511483..8bc2d1a 100644 --- a/src/EmailSender.php +++ b/src/EmailSender.php @@ -1,8 +1,7 @@ <?php -namespace WPDesk\Library\WPEmail\Emails; +namespace WPDesk\Library\WPEmail; -use WPDesk\Library\WPEmail\Abstracts\ConditionInterface; use WPDesk\Library\WPEmail\Abstracts\EmailGettersInterface; class EmailSender { @@ -82,15 +81,9 @@ class EmailSender { public function send() { foreach ( $this->get_emails() as $email ) { $this->before_wp_mail(); - - if( $email instanceof ConditionInterface ) { - if( $email->is_valid() ) { - wp_mail( - $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments() - ); - } - } - + wp_mail( + $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments() + ); $this->after_wp_mail(); } } diff --git a/src/Emails/Email.php b/src/Emails/Email.php index 1decb4d..e5b4991 100644 --- a/src/Emails/Email.php +++ b/src/Emails/Email.php @@ -2,9 +2,9 @@ namespace WPDesk\Library\WPEmail\Emails; -use WPDesk\Library\WPEmail\Abstracts\EmailGettersAbstract; +use WPDesk\Library\WPEmail\Abstracts\EmailAbstract; -class Email extends EmailGettersAbstract { +class Email extends EmailAbstract { const ID = 'email'; diff --git a/src/Parser/HTMLDecorator.php b/src/Parser/HTMLDecorator.php index dd6bf56..9bdba00 100644 --- a/src/Parser/HTMLDecorator.php +++ b/src/Parser/HTMLDecorator.php @@ -2,6 +2,9 @@ namespace WPDesk\Library\WPEmail\Parser; +use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter; +use Pelago\Emogrifier\HtmlProcessor\HtmlPruner; + class HTMLDecorator { public static function style_inline( $content ) { @@ -24,7 +27,7 @@ class HTMLDecorator { $content = CssToAttributeConverter::fromDomDocument( $dom_document ) ->convertCssToVisualAttributes() ->render(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $logger = wc_get_logger(); $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) ); } -- GitLab