Skip to content
Snippets Groups Projects

Devel

Merged Piotr Potrebka requested to merge devel into main
Compare and Show latest version
2 files
+ 114
47
Compare changes
  • Side-by-side
  • Inline

Files

+ 110
43
@@ -23,13 +23,33 @@ class Mailer {
*/
private $renderer;
/**
* @var array
*/
private $template_attributes;
/**
* @var string
*/
private $from;
/**
* @var string
*/
private $from_name;
/**
* @param array $dirs
* @param array $template_attributes
*/
public function __construct(
array $dirs = []
array $dirs = [],
array $template_attributes = []
) {
$this->template_attributes = $template_attributes;
$this->set_renderer( $this->init_renderer( $dirs ) );
$this->set_from( get_bloginfo( 'admin_email' ) );
$this->set_from_name( get_bloginfo( 'name', 'display' ) );
}
/**
@@ -72,54 +92,87 @@ class Mailer {
return $this->emails;
}
/**
* @param string $from
*
* @return void
*/
public function set_from( string $from ) {
$this->from = $from;
}
/**
* @return string
*/
public function get_from(): string {
return $this->from;
}
/**
* @param string $from_name
*
* @return void
*/
public function set_from_name( string $from_name ) {
$this->from_name = $from_name;
}
/**
* @return string
*/
public function get_from_name(): string {
return $this->from_name;
}
/** @return void */
public function send() {
public function send( $include = [] ) {
foreach ( $this->get_emails() as $email ) {
add_filter(
'wp_mail_from',
$from_cb = static function () use ( $email ) {
if ( ! empty( $email->get_from() ) ) {
return $email->get_from();
if ( ! empty( $include ) && ! in_array( $email->get_id(), $include, true ) ) {
continue;
}
if ( $email->get_is_enable() ) {
$mailer_from = $this->get_from();
add_filter(
'wp_mail_from',
$from_cb = static function ( $from ) use ( $mailer_from ) {
return $mailer_from;
}
);
return 'wordpress@siteurl.pl';
}
);
add_filter(
'wp_mail_from_name',
$from_name_cb = static function () use ( $email ) {
if ( ! empty( $email->get_from_name() ) ) {
return $email->get_from_name();
$mailer_from_name = $this->get_from_name();
add_filter(
'wp_mail_from_name',
$from_name_cb = static function ( $from_name ) use ( $mailer_from_name ) {
return $mailer_from_name;
}
return 'wordpress';
}
);
add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
try {
$success = wp_mail(
$email->get_recipients(),
$email->get_subject(),
$this->get_email_template( $email ),
$email->get_headers(),
$email->get_attachments()
);
if ( ! $success ) {
throw new MailerException( 'Count not send the mail with wp_mail()' );
}
} catch ( Exception $e ) {
if ( $e instanceof MailerException ) {
throw $e;
}
add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
try {
$success = wp_mail(
$email->get_recipients(),
$email->get_subject(),
$this->get_email_template( $email ),
$email->get_headers(),
$email->get_attachments()
);
if ( ! $success ) {
throw new MailerException( 'Count not send the mail with wp_mail()' );
}
} catch ( Exception $e ) {
if ( $e instanceof MailerException ) {
throw $e;
}
throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e );
} finally {
remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 );
remove_filter( 'wp_mail_from', $from_cb );
remove_filter( 'wp_mail_from_name', $from_name_cb );
throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e );
} finally {
remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 );
remove_filter( 'wp_mail_from', $from_cb );
remove_filter( 'wp_mail_from_name', $from_name_cb );
}
}
}
}
/** @return void */
@@ -128,9 +181,15 @@ class Mailer {
}
protected function get_email_template( EmailInterface $email ): string {
$output = $this->renderer->render( 'html/email-header', [ 'heading' => $email->get_heading(), 'logo' => '' ] );
$output = $this->renderer->render(
'html/email-header',
[
'heading' => $this->template_attributes['heading'] ?? $email->get_heading(),
'logo' => $this->template_attributes['logo']
]
);
$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' => $this->template_attributes['footer'] ] );
return $this->css_inline( $output );
}
@@ -141,7 +200,15 @@ class Mailer {
* @return mixed|string
*/
protected function css_inline( string $content ): string {
$styles = $this->renderer->render( 'html/email-styles', [] );
$styles = $this->renderer->render(
'html/email-styles',
[
'primary' => $this->template_attributes['primary'] ?? '#d15291',
'text' => $this->template_attributes['text'] ?? '#303030',
'bg' => $this->template_attributes['bg'] ?? '#f9f9f9',
'body' => $this->template_attributes['body'] ?? '#ffffff',
]
);
return HTML::style_inline( $content, $styles );
}
Loading