From de65272a8a8d43f8157f75560ff26df5f72a939e Mon Sep 17 00:00:00 2001 From: Piotr Potrebka <piotr.potrebka@wpdesk.net> Date: Wed, 25 Jan 2023 13:09:50 +0100 Subject: [PATCH] feat: email abstract --- src/Abstracts/EmailAbstract.php | 38 ++------ ...ettersInterface.php => EmailInterface.php} | 9 +- src/EmailSender.php | 88 +++++++++++++++---- src/Integration.php | 32 ------- src/Parser/HTMLDecorator.php | 40 +++------ 5 files changed, 104 insertions(+), 103 deletions(-) rename src/Abstracts/{EmailGettersInterface.php => EmailInterface.php} (89%) delete mode 100644 src/Integration.php diff --git a/src/Abstracts/EmailAbstract.php b/src/Abstracts/EmailAbstract.php index 4ad465f..9d3524e 100644 --- a/src/Abstracts/EmailAbstract.php +++ b/src/Abstracts/EmailAbstract.php @@ -2,55 +2,44 @@ namespace WPDesk\Library\WPEmail\Abstracts; -use WPDesk\View\Renderer\Renderer; - -abstract class EmailAbstract implements EmailGettersInterface { +abstract class EmailAbstract implements EmailInterface { /** * @var array */ - private $recipients = []; + protected $recipients = []; /** * @var array */ - private $placeholders = []; + protected $placeholders = []; /** * @var string */ - private $subject = ''; + protected $subject = ''; /** * @var string */ - private $heading = ''; + protected $heading = ''; /** * @var array */ - private $attachments = []; + protected $attachments = []; /** * @var string */ - private $content = ''; + protected $content = ''; /** * @var string */ - private $type = 'text/html'; + protected $type = 'text/html'; /** * @var array */ - private $headers; - - /** - * @var Renderer - */ - private $renderer; - - public function __construct( Renderer $renderer ) { - $this->renderer = $renderer; - } + protected $headers; /** * Define unique email ID. @@ -147,13 +136,4 @@ abstract class EmailAbstract implements EmailGettersInterface { return ''; } - /** - * Get email content. - * - * @return string - */ - public function render(): string { - return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] ); - } - } diff --git a/src/Abstracts/EmailGettersInterface.php b/src/Abstracts/EmailInterface.php similarity index 89% rename from src/Abstracts/EmailGettersInterface.php rename to src/Abstracts/EmailInterface.php index f55967b..de5a423 100644 --- a/src/Abstracts/EmailGettersInterface.php +++ b/src/Abstracts/EmailInterface.php @@ -2,7 +2,7 @@ namespace WPDesk\Library\WPEmail\Abstracts; -interface EmailGettersInterface { +interface EmailInterface { /** * Define unique email ID. @@ -11,6 +11,13 @@ interface EmailGettersInterface { */ public function get_id(): string; + /** + * Is enable. + * + * @return bool + */ + public function get_is_enable(): bool; + /** * Get defined placeholders. * diff --git a/src/EmailSender.php b/src/EmailSender.php index 8bc2d1a..f6fe7f2 100644 --- a/src/EmailSender.php +++ b/src/EmailSender.php @@ -2,35 +2,56 @@ namespace WPDesk\Library\WPEmail; -use WPDesk\Library\WPEmail\Abstracts\EmailGettersInterface; +use WPDesk\Library\WPEmail\Abstracts\EmailInterface; +use WPDesk\Library\WPEmail\Parser\HTMLDecorator; +use WPDesk\View\Renderer\Renderer; +use WPDesk\View\Renderer\SimplePhpRenderer; +use WPDesk\View\Resolver\ChainResolver; +use WPDesk\View\Resolver\DirResolver; +use WPDesk\View\Resolver\WPThemeResolver; class EmailSender { /** - * @var EmailGettersInterface[] + * @var EmailInterface[] */ private $emails = []; /** * @var string */ - private $from; + private $from = 'wordpress@wordpress.org'; /** * @var string */ - private $from_name; + private $from_name = 'WordPress'; + + /** + * @var Renderer + */ + private $renderer; /** * @param $from * @param $from_name */ - public function __construct( $from = '', $from_name = '' ) { - $this->from = $from; - $this->from_name = $from_name; + public function __construct() { + $this->init_renderer(); } - public function add_email( Email $email ) { + public function init_renderer() { + $resolver = new ChainResolver(); + $resolver->appendResolver( new DirResolver( __DIR__ ) ); + + $this->renderer = new SimplePhpRenderer( $resolver ); + } + + public function set_renderer( Renderer $renderer ) { + $this->renderer = $renderer; + } + + public function add_email( EmailInterface $email ) { $this->emails[ $email->get_id() ] = $email; } @@ -38,6 +59,11 @@ class EmailSender { return $this->emails; } + public function set_from( string $from ) { + $this->from = $from; + } + + /** * WordPress callback for setting the from email * @@ -45,7 +71,7 @@ class EmailSender { * * @return string */ - public function from( $email ) { + public function from( $email ): string { if ( ! empty( $this->from ) && is_email( $this->from ) ) { $email = $this->from; } @@ -53,6 +79,12 @@ class EmailSender { return $email; } + + public function set_from_name( string $from_name ) { + $this->from_name = $from_name; + } + + /** * WordPress callback for setting the from name * @@ -78,16 +110,42 @@ class EmailSender { add_filter( 'wp_mail_from_name', array( $this, 'from_name' ) ); } - public function send() { + public function send( array $placeholders = [] ) { foreach ( $this->get_emails() as $email ) { - $this->before_wp_mail(); - wp_mail( - $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments() - ); - $this->after_wp_mail(); + $content = $this->renderer->render( $email->get_id(), [] ); + $content = $this->replace_placeholders( $content, $placeholders ); + if ( $email->get_is_enable() ) { + $this->before_wp_mail(); + wp_mail( + $email->get_recipients(), $email->get_subject(), $this->get_html_content( $content ), $email->get_headers(), $email->get_attachments() + ); + $this->after_wp_mail(); + } + } } + /** + * @param string $content + * @param array $placeholders + * + * @return array|string|string[] + */ + private function replace_placeholders( string $content, array $placeholders = [] ): string { + return str_replace( array_keys( $placeholders ), array_values( $placeholders ), $content ); + } + + /** + * @param string $content + * + * @return mixed|string + */ + private function get_html_content( string $content ): string { + $styles = $this->renderer->render( 'styles', [] ); + + return HTMLDecorator::style_inline( $content, $styles ); + } + /** * Remove filters after fire wp_mail. * diff --git a/src/Integration.php b/src/Integration.php deleted file mode 100644 index 9d33b70..0000000 --- a/src/Integration.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace WPDesk\Library\WPEmail; - -use WPDesk\Library\WPEmail\Emails\AdminEmail; -use WPDesk\Library\WPEmail\Emails\Email; -use WPDesk\Library\WPEmail\Emails\EmailSender; -use WPDesk\Persistence\Adapter\WordPress\WordpressOptionsContainer; -use WPDesk\Persistence\Adapter\WordPress\WordpressTransientContainer; -use WPDesk\View\Renderer\SimplePhpRenderer; -use WPDesk\View\Resolver\ChainResolver; -use WPDesk\View\Resolver\DirResolver; -use WPDesk\View\Resolver\WPThemeResolver; - -class Integration { - - public function __construct() { - $chain_resolver = new ChainResolver(); - $chain_resolver->appendResolver( new WPThemeResolver( 'email_templates' ) ); - $chain_resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) ); - $renderer = new SimplePhpRenderer( $chain_resolver ); - - $email_sender = new EmailSender( 'email@mojastron.pl', 'Moj sklep' ); - $email = new Email( $renderer, [] ); - $email->set_subject( 'Tytuł')->set_heading( 'Nowa wiadomość' ); - - $email_sender->add_email( $email ); - $email_sender->send(); - - } - -} diff --git a/src/Parser/HTMLDecorator.php b/src/Parser/HTMLDecorator.php index 9bdba00..baac6b1 100644 --- a/src/Parser/HTMLDecorator.php +++ b/src/Parser/HTMLDecorator.php @@ -2,38 +2,26 @@ namespace WPDesk\Library\WPEmail\Parser; +use Pelago\Emogrifier\CssInliner; use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter; use Pelago\Emogrifier\HtmlProcessor\HtmlPruner; class HTMLDecorator { - public static function style_inline( $content ) { - if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) { - ob_start(); - wc_get_template( 'emails/email-styles.php' ); - $css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), $this ); - - $css_inliner_class = \Pelago\Emogrifier\CssInliner::class; - - if ( $this->supports_emogrifier() && class_exists( $css_inliner_class ) ) { - try { - $css_inliner = \Pelago\Emogrifier\CssInliner::fromHtml( $content )->inlineCss( $css ); - - do_action( 'woocommerce_emogrifier', $css_inliner, $this ); - - $dom_document = $css_inliner->getDomDocument(); - - HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone(); - $content = CssToAttributeConverter::fromDomDocument( $dom_document ) - ->convertCssToVisualAttributes() - ->render(); - } catch ( \Exception $e ) { - $logger = wc_get_logger(); - $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) ); - } - } else { - $content = '<style type="text/css">' . $css . '</style>' . $content; + public static function style_inline( $content, $styles = '' ) { + if ( class_exists( 'DOMDocument' ) ) { + try { + $css_inliner = CssInliner::fromHtml( $content )->inlineCss( $styles ); + $dom_document = $css_inliner->getDomDocument(); + HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone(); + $content = CssToAttributeConverter::fromDomDocument( $dom_document ) + ->convertCssToVisualAttributes() + ->render(); + } catch ( \Exception $e ) { + error_log( $e->getMessage() ); } + } else { + $content = '<style type="text/css">' . strip_tags( $styles ) . '</style>' . $content; } return $content; -- GitLab