Skip to content
Snippets Groups Projects
Commit 83f57709 authored by Piotr Potrebka's avatar Piotr Potrebka
Browse files

feat: email abstract

parent 310a2804
No related branches found
No related tags found
1 merge request!2Devel
Pipeline #153086 passed
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
...@@ -58,6 +58,20 @@ abstract class EmailAbstract implements EmailInterface { ...@@ -58,6 +58,20 @@ abstract class EmailAbstract implements EmailInterface {
*/ */
abstract public function get_id(): string; abstract public function get_id(): string;
/**
* @return string
*/
public function get_from(): string {
return '';
}
/**
* @return string
*/
public function get_from_name(): string {
return '';
}
/** /**
* @return bool * @return bool
*/ */
...@@ -274,5 +288,14 @@ abstract class EmailAbstract implements EmailInterface { ...@@ -274,5 +288,14 @@ abstract class EmailAbstract implements EmailInterface {
return $this->content; return $this->content;
} }
/**
* @param string $string
*
* @return array|string|string[]
*/
protected function replace_placeholders( string $string ): string {
return (string) str_replace( array_keys( $this->placeholders ), array_values( $this->placeholders ), $string );
}
} }
...@@ -11,6 +11,16 @@ interface EmailInterface { ...@@ -11,6 +11,16 @@ interface EmailInterface {
*/ */
public function get_id(): string; public function get_id(): string;
/**
* @return string
*/
public function get_from(): string;
/**
* @return string
*/
public function get_from_name(): string;
/** /**
* Is enable. * Is enable.
* *
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
namespace WPDesk\Library\WPEmail; namespace WPDesk\Library\WPEmail;
use Exception;
use WP_Error;
use WPDesk\Library\WPEmail\Abstracts\EmailInterface; use WPDesk\Library\WPEmail\Abstracts\EmailInterface;
use WPDesk\Library\WPEmail\Helpers\HTML; use WPDesk\Library\WPEmail\Helpers\HTML;
use WPDesk\View\Renderer\Renderer; use WPDesk\View\Renderer\Renderer;
use WPDesk\View\Renderer\SimplePhpRenderer; use WPDesk\View\Renderer\SimplePhpRenderer;
use WPDesk\View\Resolver\ChainResolver; use WPDesk\View\Resolver\ChainResolver;
use WPDesk\View\Resolver\DirResolver; use WPDesk\View\Resolver\DirResolver;
use WPDesk\View\Resolver\Resolver;
class Mailer { class Mailer {
...@@ -17,52 +18,29 @@ class Mailer { ...@@ -17,52 +18,29 @@ class Mailer {
*/ */
private $emails = []; private $emails = [];
/**
* @var string
*/
private $from;
/**
* @var string
*/
private $from_name;
/** /**
* @var Renderer * @var Renderer
*/ */
private $renderer; private $renderer;
/** /**
* @var array * @param array $dirs
*/
protected $placeholders = [];
/**
* @param string $from
* @param string $from_name
* @param Resolver[] $dir_resolvers
*/ */
public function __construct( public function __construct(
string $from = 'wordpress@wordpress.org', array $dirs = []
string $from_name = 'WordPress',
array $dir_resolvers = []
) { ) {
$this->from = $from; $this->set_renderer( $this->init_renderer( $dirs ) );
$this->from_name = $from_name;
$this->set_renderer( $this->init_renderer( $dir_resolvers ) );
} }
/** /**
* @param array $dir_resolvers * @param array $dirs
* *
* @return Renderer * @return Renderer
*/ */
private function init_renderer( array $dir_resolvers ): Renderer { private function init_renderer( array $dirs = [] ): Renderer {
$resolver = new ChainResolver(); $resolver = new ChainResolver();
foreach ( $dir_resolvers as $dir_resolver ) { foreach ( $dirs as $dir ) {
if ( $dir_resolver instanceof Resolver ) { $resolver->appendResolver( new DirResolver( $dir ) );
$resolver->appendResolver( $dir_resolver );
}
} }
$resolver->appendResolver( new DirResolver( __DIR__ ) ); $resolver->appendResolver( new DirResolver( __DIR__ ) );
...@@ -94,101 +72,61 @@ class Mailer { ...@@ -94,101 +72,61 @@ class Mailer {
return $this->emails; return $this->emails;
} }
/** @return void */
/** public function send() {
* Set placeholders. foreach ( $this->get_emails() as $email ) {
* add_filter(
* @param array $placeholders 'wp_mail_from',
* $from_cb = static function () use ( $email ) {
* @return self return $email->get_from();
*/
public function set_placeholders( array $placeholders = [] ): self {
$this->placeholders = array_merge( $this->placeholders, $placeholders );
return $this;
} }
);
/** add_filter(
* Get defined placeholders. 'wp_mail_from_name',
* $from_name_cb = static function () use ( $email ) {
* @return string[] return $email->get_from_name();
*/
public function get_placeholders(): array {
return $this->placeholders;
} }
);
/** add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
* WordPress callback for setting the from email
* try {
* @param string $email $success = wp_mail(
* $email->get_recipients(),
* @return string $email->get_subject(),
*/ $this->get_email_template( $email ),
public function from_filter( string $email ): string { $email->get_headers(),
if ( ! empty( $this->from ) && is_email( $this->from ) ) { $email->get_attachments()
$email = $this->from; );
if ( ! $success ) {
throw new MailerException( 'Count not send the mail with wp_mail()' );
} }
} catch ( Exception $e ) {
return $email; if ( $e instanceof MailerException ) {
throw $e;
} }
throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e );
/** } finally {
* WordPress callback for setting the from name remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 );
* remove_filter( 'wp_mail_from', $from_cb );
* @param string $name remove_filter( 'wp_mail_from_name', $from_name_cb );
*
* @return string
*/
public function from_name_filter( string $name ): string {
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
*/
protected function before_wp_mail() {
add_filter( 'wp_mail_from', array( $this, 'from_filter' ) );
add_filter( 'wp_mail_from_name', array( $this, 'from_name_filter' ) );
} }
public function send() { /** @return void */
$this->before_wp_mail(); public function catch_error( WP_Error $error ) {
foreach ( $this->get_emails() as $email ) { throw MailerException::with_wp_error( $error );
$subject = $this->replace_placeholders( $email->get_subject() );
if ( $email->get_is_enable() ) {
wp_mail(
$email->get_recipients(), $subject, $this->get_body( $email ), $email->get_headers(), $email->get_attachments()
);
}
}
$this->after_wp_mail();
} }
protected function get_body( EmailInterface $email ): string { protected function get_email_template( EmailInterface $email ): string {
$content = $this->replace_placeholders( $email->get_content() );
$output = $this->renderer->render( 'html/email-header', [ 'heading' => $email->get_heading(), 'logo' => '' ] ); $output = $this->renderer->render( 'html/email-header', [ 'heading' => $email->get_heading(), 'logo' => '' ] );
$output .= $this->renderer->render( 'html/' . $email->get_id(), [ 'content' => $content ] ); $output .= $this->renderer->render( 'html/' . $email->get_id(), [ 'content' => $email->get_content() ] );
$output .= $this->renderer->render( 'html/email-footer', [ 'footer' => '' ] ); $output .= $this->renderer->render( 'html/email-footer', [ 'footer' => '' ] );
return $this->css_inline( $output ); return $this->css_inline( $output );
} }
/**
* @param string $string
*
* @return array|string|string[]
*/
protected function replace_placeholders( string $string ): string {
return (string) str_replace( array_keys( $this->placeholders ), array_values( $this->placeholders ), $string );
}
/** /**
* @param string $content * @param string $content
* *
...@@ -200,14 +138,5 @@ class Mailer { ...@@ -200,14 +138,5 @@ class Mailer {
return HTML::style_inline( $content, $styles ); return HTML::style_inline( $content, $styles );
} }
/**
* Remove filters after fire wp_mail.
*
* @return void
*/
protected function after_wp_mail() {
remove_filter( 'wp_mail_from', array( $this, 'from_filter' ) );
remove_filter( 'wp_mail_from_name', array( $this, 'from_name_filter' ) );
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment