diff --git a/composer.json b/composer.json index f6cfaded48c8cba7cec452d4467d01fbe81f0004..6875666f0b6594246e593b1ac5ac6a62a8c6e5f7 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.com/wpdesk/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 2c5d929f88b969868aaaf658445367d9336bd3d0..b904e0ee070ef576ed47b7c5335e2463350a8764 100644 --- a/src/Abstracts/EmailAbstract.php +++ b/src/Abstracts/EmailAbstract.php @@ -2,8 +2,19 @@ namespace WPDesk\Library\WPEmail\Abstracts; +use WPDesk\View\Renderer\Renderer; + abstract class EmailAbstract implements EmailInterface { + /** + * @var Renderer + */ + private $renderer; + + public function __construct( Renderer $renderer ) { + $this->renderer = $renderer; + } + /** * Define unique email ID. * @@ -14,10 +25,10 @@ abstract class EmailAbstract implements EmailInterface { /** * Get defined placeholders. * - * @return array + * @return string[] */ public function get_placeholders(): array { - + return []; } /** @@ -26,7 +37,7 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_subject(): string { - + return ''; } /** @@ -35,25 +46,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 []; } /** * Get email headers. * - * @return string + * @return string[] */ - public function get_headers(): string { - + public function get_headers(): array { + return []; } /** @@ -62,7 +73,7 @@ abstract class EmailAbstract implements EmailInterface { * @return array */ public function get_attachments(): array { - + return []; } /** @@ -71,7 +82,7 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_type(): string { - + return 'text/html'; } /** @@ -80,18 +91,23 @@ abstract class EmailAbstract implements EmailInterface { * @return string */ public function get_content(): string { + return ''; + } + /** + * @return mixed + */ + public function get_object() { + return ''; } /** - * Send email (wywalić do osobnej klasy ;) ) + * 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..4e0fc286e3902d823bbbc586c57a141b82ac55c0 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. 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.