diff --git a/composer.json b/composer.json index f6cfaded48c8cba7cec452d4467d01fbe81f0004..193f1c953a0e4950f3a15268957e772c20ee9513 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,20 @@ { - "name": "wpdesk/wp-coupons-core", - "description": "Coupons library", + "name": "wpdesk/wp-mail", + "description": "WP Email library", "license": "MIT", "keywords": [ "wordpress", - "woocommerce", - "woocommerce-coupons", - "pdf", - "pdf builder" + "email", + "wp-email", + "email templates", + "admin email" ], - "homepage": "https://gitlab.com/wpdesk/wp-canva-editor", + "homepage": "https://gitlab.wpdesk.dev/wpdesk/library/wp-mail", "minimum-stability": "stable", "authors": [ { - "name": "piotr.potrebka", - "email": "piotr.potrebka@wpdesk.net" + "name": "eryk.mika", + "email": "eryk.mika@wpdesk.eu" } ], "config": { @@ -47,12 +47,12 @@ }, "autoload": { "psr-4": { - "WPDesk\\Library\\WPCoupons\\": "src/Coupons" + "WPDesk\\Library\\WPEmail\\": "src" } }, "autoload-dev": {}, "extra": { - "text-domain": "wp-coupons-core", + "text-domain": "wp-email", "translations-folder": "lang", "po-files": { "pl_PL": "pl_PL.po" diff --git a/src/Abstracts/EmailAbstract.php b/src/Abstracts/EmailAbstract.php index 0085b6863b4f947ec847aeff28ae50a7857a4844..5aba38c803aee18da6a6a7df37bbbf178e5f4e80 100644 --- a/src/Abstracts/EmailAbstract.php +++ b/src/Abstracts/EmailAbstract.php @@ -2,8 +2,20 @@ namespace WPDesk\Library\WPEmail\Abstracts; +use WPDesk\View\Renderer\Renderer; + abstract class EmailAbstract implements EmailInterface { + /** + * @var Renderer + */ + private $renderer; + + public function __construct( Renderer $renderer, array $recipients ) { + $this->renderer = $renderer; + $this->recipients = $recipients; + } + /** * Define unique email ID. * @@ -14,10 +26,10 @@ abstract class EmailAbstract implements EmailInterface { /** * Get defined placeholders. * - * @return array + * @return string[] */ public function get_placeholders(): array { - + return []; } /** @@ -26,7 +38,11 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_subject(): string { + if ( ! $this->subject ) { + throw new \Exception( 'Empty email subject' ); + } + return ''; } /** @@ -35,25 +51,25 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_heading(): string { - + return ''; } /** * Get valid recipients. * - * @return array + * @return string[] */ public function get_recipients(): array { - + return $this->recipients; } /** * Get email headers. * - * @return string + * @return string[] */ - public function get_headers(): string { - + public function get_headers(): array { + return []; } /** @@ -62,7 +78,7 @@ abstract class EmailAbstract implements EmailInterface { * @return array */ public function get_attachments(): array { - + return []; } /** @@ -71,7 +87,7 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_type(): string { - + return 'text/html'; } /** @@ -80,18 +96,27 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_content(): string { + if ( ! $this->content ) { + throw new \Exception( 'Empty email subject' ); + } + + return ''; + } + /** + * @return mixed + */ + public function get_object() { + return ''; } /** - * Send email. + * Get email content. * - * @return void + * @return string */ - public function send(): void { - wp_mail( - $this->get_recipients(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() - ); + 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/EmailInterface.php b/src/Abstracts/EmailInterface.php index aff140ee8c072959b964719f7bb4a33e06c0df5a..8439a4472b69b22af289730473a349ea92c62e52 100644 --- a/src/Abstracts/EmailInterface.php +++ b/src/Abstracts/EmailInterface.php @@ -35,16 +35,16 @@ interface EmailInterface { /** * Get valid recipients. * - * @return array + * @return string[] */ public function get_recipients(): array; /** * Get email headers. * - * @return string + * @return string[] */ - public function get_headers(): string; + public function get_headers(): array; /** * Get email attachments. @@ -67,11 +67,4 @@ interface EmailInterface { */ public function get_content(): string; - /** - * Send email. - * - * @return void - */ - public function send(): void; - } diff --git a/src/CustomerEmail.php b/src/CustomerEmail.php deleted file mode 100644 index 44eff7467e1508b42615e8e88ba28c58f41c417e..0000000000000000000000000000000000000000 --- a/src/CustomerEmail.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -namespace WPDesk\Library\WPEmail; - -use WPDesk\Library\WPEmail\Abstracts\EmailAbstract; - -class CustomerEmail extends EmailAbstract { - - const ID = 'customer_email'; - - /** - * Define unique email ID. - * - * @return string - */ - public function get_id(): string { - return self::ID; - } - -} diff --git a/src/EmailSender.php b/src/EmailSender.php new file mode 100644 index 0000000000000000000000000000000000000000..f249ad9c4e4f2d88214480633992db1186942492 --- /dev/null +++ b/src/EmailSender.php @@ -0,0 +1,101 @@ +<?php + +namespace WPDesk\Library\WPEmail\Emails; + +use WPDesk\Library\WPEmail\Abstracts\EmailInterface; + +class EmailSender { + + /** + * @var EmailInterface[] + */ + private $emails = []; + + /** + * @var string + */ + private $from; + + /** + * @var string + */ + private $from_name; + + /** + * @param $from + * @param $from_name + */ + public function __construct( $from = '', $from_name = '' ) { + $this->from = $from; + $this->from_name = $from_name; + } + + public function add_email( EmailInterface $email ) { + $this->emails[ $email->get_id() ] = $email; + } + + public function get_emails(): array { + return $this->emails; + } + + /** + * WordPress callback for setting the from email + * + * @param string $email + * + * @return string + */ + public function from( $email ) { + if ( ! empty( $this->from ) && is_email( $this->from ) ) { + $email = $this->from; + } + + return $email; + } + + /** + * WordPress callback for setting the from name + * + * @param string $name + * + * @return string + */ + public function from_name( $name ) { + if ( ! empty( $this->from_name ) ) { + $name = html_entity_decode( sanitize_text_field( $this->from_name ) ); + } + + return $name; + } + + /** + * Add filters before fire wp_mail. + * + * @return void + */ + private function before_wp_mail() { + add_filter( 'wp_mail_from', array( $this, 'from' ) ); + add_filter( 'wp_mail_from_name', array( $this, 'from_name' ) ); + } + + public function send() { + 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(); + } + } + + /** + * Remove filters after fire wp_mail. + * + * @return void + */ + private function after_wp_mail() { + remove_filter( 'wp_mail_from', array( $this, 'from' ) ); + remove_filter( 'wp_mail_from_name', array( $this, 'from_name' ) ); + } + +} diff --git a/src/AdminEmail.php b/src/Emails/Email.php similarity index 66% rename from src/AdminEmail.php rename to src/Emails/Email.php index 420b8b3eca970cf91c02cbaa98180cac5a07d1f4..e5b499128cb50c323db83f3d943df6edfc417d51 100644 --- a/src/AdminEmail.php +++ b/src/Emails/Email.php @@ -1,12 +1,12 @@ <?php -namespace WPDesk\Library\WPEmail; +namespace WPDesk\Library\WPEmail\Emails; use WPDesk\Library\WPEmail\Abstracts\EmailAbstract; -class AdminEmail extends EmailAbstract { +class Email extends EmailAbstract { - const ID = 'admin_email'; + const ID = 'email'; /** * Define unique email ID. diff --git a/src/Integration.php b/src/Integration.php new file mode 100644 index 0000000000000000000000000000000000000000..087e0fe892708d83ca74624d9bdc1fdff7388be6 --- /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 0000000000000000000000000000000000000000..dd6bf5643386b7b71e4ccf715cecd38e42278af3 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/tempates/plain/default.php b/src/tempates/plain/default.php new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391