From e2290d660c3fcd16112a153172b165c5cf639eef Mon Sep 17 00:00:00 2001 From: Piotr Potrebka <piotr.potrebka@wpdesk.net> Date: Mon, 23 Jan 2023 13:22:16 +0100 Subject: [PATCH] init --- src/Abstracts/EmailAbstract.php | 15 +++++++++--- src/Abstracts/EmailInterface.php | 7 ------ src/Integration.php | 31 +++++++++++++++++++++++++ src/Parser/HTMLDecorator.php | 39 ++++++++++++++++++++++++++++++++ src/tempates/html/default.php | 0 src/tempates/plain/default.php | 0 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/Integration.php create mode 100644 src/Parser/HTMLDecorator.php create mode 100644 src/tempates/html/default.php create mode 100644 src/tempates/plain/default.php diff --git a/src/Abstracts/EmailAbstract.php b/src/Abstracts/EmailAbstract.php index b904e0e..5aba38c 100644 --- a/src/Abstracts/EmailAbstract.php +++ b/src/Abstracts/EmailAbstract.php @@ -11,8 +11,9 @@ abstract class EmailAbstract implements EmailInterface { */ private $renderer; - public function __construct( Renderer $renderer ) { - $this->renderer = $renderer; + public function __construct( Renderer $renderer, array $recipients ) { + $this->renderer = $renderer; + $this->recipients = $recipients; } /** @@ -37,6 +38,10 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_subject(): string { + if ( ! $this->subject ) { + throw new \Exception( 'Empty email subject' ); + } + return ''; } @@ -55,7 +60,7 @@ abstract class EmailAbstract implements EmailInterface { * @return string[] */ public function get_recipients(): array { - return []; + return $this->recipients; } /** @@ -91,6 +96,10 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_content(): string { + if ( ! $this->content ) { + throw new \Exception( 'Empty email subject' ); + } + return ''; } diff --git a/src/Abstracts/EmailInterface.php b/src/Abstracts/EmailInterface.php index 4e0fc28..8439a44 100644 --- a/src/Abstracts/EmailInterface.php +++ b/src/Abstracts/EmailInterface.php @@ -67,11 +67,4 @@ interface EmailInterface { */ public function get_content(): string; - /** - * Send email. - * - * @return void - */ - public function send(): void; - } diff --git a/src/Integration.php b/src/Integration.php new file mode 100644 index 0000000..087e0fe --- /dev/null +++ b/src/Integration.php @@ -0,0 +1,31 @@ +<?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_sender->add_email( $email ); + $email_sender->send(); + + } + +} diff --git a/src/Parser/HTMLDecorator.php b/src/Parser/HTMLDecorator.php new file mode 100644 index 0000000..dd6bf56 --- /dev/null +++ b/src/Parser/HTMLDecorator.php @@ -0,0 +1,39 @@ +<?php + +namespace WPDesk\Library\WPEmail\Parser; + +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; + } + } + + return $content; + } + +} diff --git a/src/tempates/html/default.php b/src/tempates/html/default.php new file mode 100644 index 0000000..e69de29 diff --git a/src/tempates/plain/default.php b/src/tempates/plain/default.php new file mode 100644 index 0000000..e69de29 -- GitLab