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

feat: email styles

parent df40c2e7
No related branches found
No related tags found
2 merge requests!3feat: email styles,!2Devel
Pipeline #400620 passed with warnings with stages
in 29 seconds
...@@ -41,6 +41,11 @@ class Email { ...@@ -41,6 +41,11 @@ class Email {
*/ */
private $from_name; private $from_name;
/**
* @var array
*/
private $template_attributes;
/** /**
* @param string $from_email * @param string $from_email
* *
...@@ -56,7 +61,7 @@ class Email { ...@@ -56,7 +61,7 @@ class Email {
* @return string * @return string
*/ */
public function get_from(): string { public function get_from(): string {
return sanitize_email( $this->from_email ); return $this->from_email;
} }
/** /**
...@@ -134,7 +139,6 @@ class Email { ...@@ -134,7 +139,6 @@ class Email {
return $this->headers[ $header ] ?? ''; return $this->headers[ $header ] ?? '';
} }
/** /**
* @param array $attachments * @param array $attachments
* *
...@@ -196,4 +200,14 @@ class Email { ...@@ -196,4 +200,14 @@ class Email {
return $this->content; return $this->content;
} }
public function set_template_attributes( string $name, string $value ): self {
$this->template_attributes[ $name ] = $value;
return $this;
}
public function get_template_attributes(): array {
return $this->template_attributes;
}
} }
...@@ -2,12 +2,8 @@ ...@@ -2,12 +2,8 @@
namespace WPDesk\Library\WPEmail\Abstracts; namespace WPDesk\Library\WPEmail\Abstracts;
use WPDesk\Library\WPEmail\Exceptions\MailerException; use WPDesk\Library\WPEmail\Exceptions\MailerException;
/**
* Object-oriented wp_mail wrapper.
*/
interface Mailer { interface Mailer {
/** /**
......
<?php <?php
use WPDesk\Library\WPEmail\Abstracts\Email; namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Helpers\StyleInliner; use WPDesk\Library\WPEmail\Helpers\StyleInliner;
use WPDesk\View\Renderer\Renderer;
class EmailTemplate { class EmailTemplate {
/**
* @var Renderer
*/
private $renderer;
/**
* @var array
*/
private $template_attributes;
public function __construct( Renderer $renderer, array $template_attributes ) {
$this->renderer = $renderer;
$this->template_attributes = wp_parse_args( $template_attributes, $this->get_default_template_attributes() );
}
protected function get_email_template( Email $email ): string { public function get_email_template( string $content ): string {
$output = $this->renderer->render( $output = $this->renderer->render( 'html/email-header', $this->template_attributes );
'html/email-header', $output .= $this->renderer->render( 'html/email-content', [ 'content' => $content ] );
[
'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' => $this->template_attributes['footer'] ] ); $output .= $this->renderer->render( 'html/email-footer', [ 'footer' => $this->template_attributes['footer'] ] );
return $this->css_inline( $output ); return $this->css_inline( $output );
...@@ -25,18 +35,22 @@ class EmailTemplate { ...@@ -25,18 +35,22 @@ class EmailTemplate {
* *
* @return mixed|string * @return mixed|string
*/ */
protected function css_inline( string $content ): string { public function css_inline( string $content ): string {
$styles = $this->renderer->render( $styles = $this->renderer->render( 'html/email-styles', $this->template_attributes );
'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 StyleInliner::inline( $content, $styles ); return StyleInliner::inline( $content, $styles );
} }
public function get_default_template_attributes(): array {
return [
'heading' => '',
'logo' => '',
'footer' => '',
'primary' => '#d15291',
'text' => '#303030',
'bg' => '#f9f9f9',
'body' => '#ffffff',
];
}
} }
...@@ -7,9 +7,34 @@ use WP_Error; ...@@ -7,9 +7,34 @@ use WP_Error;
use WPDesk\Library\WPEmail\Abstracts\Email; use WPDesk\Library\WPEmail\Abstracts\Email;
use WPDesk\Library\WPEmail\Abstracts\Mailer; use WPDesk\Library\WPEmail\Abstracts\Mailer;
use WPDesk\Library\WPEmail\Exceptions\MailerException; use WPDesk\Library\WPEmail\Exceptions\MailerException;
use WPDesk\View\Renderer\Renderer;
use WPDesk\View\Renderer\SimplePhpRenderer;
use WPDesk\View\Resolver\ChainResolver;
use WPDesk\View\Resolver\DirResolver;
class WPMailer implements Mailer { class WPMailer implements Mailer {
/**
* @var Renderer
*/
private $renderer;
public function __construct() {
$resolver = new ChainResolver();
$resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) );
$renderer = new SimplePhpRenderer( $resolver );
$this->set_renderer( $renderer );
}
public function set_renderer( Renderer $renderer ) {
$this->renderer = $renderer;
}
public function get_renderer(): Renderer {
return $this->renderer;
}
/** @return void */ /** @return void */
public function send( Email $email ): void { public function send( Email $email ): void {
$mailer_from = $email->get_from(); $mailer_from = $email->get_from();
...@@ -29,11 +54,13 @@ class WPMailer implements Mailer { ...@@ -29,11 +54,13 @@ class WPMailer implements Mailer {
); );
add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
$email_template = new EmailTemplate( $this->renderer, $email->get_template_attributes() );
try { try {
$success = wp_mail( $success = wp_mail(
$email->get_recipients(), $email->get_recipients(),
$email->get_subject(), $email->get_subject(),
$email->get_content(), $email_template->get_email_template( $email->get_content() ),
$email->get_headers(), $email->get_headers(),
$email->get_attachments() $email->get_attachments()
); );
......
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment