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

feat: email abstract

parent 4ffdc837
No related branches found
No related tags found
1 merge request!2Devel
Pipeline #150471 passed with stages
in 56 seconds
...@@ -4,25 +4,52 @@ namespace WPDesk\Library\WPEmail\Abstracts; ...@@ -4,25 +4,52 @@ namespace WPDesk\Library\WPEmail\Abstracts;
use WPDesk\View\Renderer\Renderer; use WPDesk\View\Renderer\Renderer;
abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionInterface { abstract class EmailAbstract implements EmailGettersInterface {
/** /**
* @var Renderer * @var array
*/ */
private $renderer; private $recipients = [];
/**
* @var array
*/
private $placeholders = [];
/**
* @var string
*/
private $subject = '';
/**
* @var string
*/
private $heading = '';
/**
* @var array
*/
private $attachments = [];
/** /**
* @var string * @var string
*/ */
private $subject; private $content = '';
/** /**
* @var string * @var string
*/ */
private $heading; private $type = 'text/html';
/**
* @var array
*/
private $headers;
public function __construct( Renderer $renderer, array $recipients ) { /**
* @var Renderer
*/
private $renderer;
public function __construct( Renderer $renderer ) {
$this->renderer = $renderer; $this->renderer = $renderer;
$this->recipients = $recipients;
} }
/** /**
...@@ -38,27 +65,21 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -38,27 +65,21 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
* @return string[] * @return string[]
*/ */
public function get_placeholders(): array { public function get_placeholders(): array {
return []; return $this->placeholders;
} }
/** /**
* Get email subject. * Get email subject.
* *
* @return string * @return string
* @throws \Exception
*/ */
public function get_subject(): string { public function get_subject(): string {
if ( ! $this->subject ) { if ( ! $this->subject ) {
throw new \Exception( 'Empty email subject' ); throw new \Exception( 'Empty email subject' );
} }
return ''; return $this->subject;
}
public function set_subject( string $subject ): self {
$this->subject = $subject;
return $this;
} }
/** /**
...@@ -67,14 +88,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -67,14 +88,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
* @return string * @return string
*/ */
public function get_heading(): string { public function get_heading(): string {
return ''; return $this->heading;
}
public function set_heading( string $heading ): self {
$this->heading = $heading;
return $this;
} }
/** /**
...@@ -92,7 +106,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -92,7 +106,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
* @return string[] * @return string[]
*/ */
public function get_headers(): array { public function get_headers(): array {
return []; return $this->headers;
} }
/** /**
...@@ -101,7 +115,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -101,7 +115,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
* @return array * @return array
*/ */
public function get_attachments(): array { public function get_attachments(): array {
return []; return $this->attachments;
} }
/** /**
...@@ -110,7 +124,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -110,7 +124,7 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
* @return string * @return string
*/ */
public function get_type(): string { public function get_type(): string {
return 'text/html'; return $this->type;
} }
/** /**
...@@ -142,8 +156,4 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI ...@@ -142,8 +156,4 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionI
return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] ); return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] );
} }
public function is_valid(): bool {
return true;
}
} }
<?php <?php
namespace WPDesk\Library\WPEmail\Emails; namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Abstracts\ConditionInterface;
use WPDesk\Library\WPEmail\Abstracts\EmailGettersInterface; use WPDesk\Library\WPEmail\Abstracts\EmailGettersInterface;
class EmailSender { class EmailSender {
...@@ -82,15 +81,9 @@ class EmailSender { ...@@ -82,15 +81,9 @@ class EmailSender {
public function send() { public function send() {
foreach ( $this->get_emails() as $email ) { foreach ( $this->get_emails() as $email ) {
$this->before_wp_mail(); $this->before_wp_mail();
wp_mail(
if( $email instanceof ConditionInterface ) { $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments()
if( $email->is_valid() ) { );
wp_mail(
$email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments()
);
}
}
$this->after_wp_mail(); $this->after_wp_mail();
} }
} }
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
namespace WPDesk\Library\WPEmail\Emails; namespace WPDesk\Library\WPEmail\Emails;
use WPDesk\Library\WPEmail\Abstracts\EmailGettersAbstract; use WPDesk\Library\WPEmail\Abstracts\EmailAbstract;
class Email extends EmailGettersAbstract { class Email extends EmailAbstract {
const ID = 'email'; const ID = 'email';
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace WPDesk\Library\WPEmail\Parser; namespace WPDesk\Library\WPEmail\Parser;
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
class HTMLDecorator { class HTMLDecorator {
public static function style_inline( $content ) { public static function style_inline( $content ) {
...@@ -24,7 +27,7 @@ class HTMLDecorator { ...@@ -24,7 +27,7 @@ class HTMLDecorator {
$content = CssToAttributeConverter::fromDomDocument( $dom_document ) $content = CssToAttributeConverter::fromDomDocument( $dom_document )
->convertCssToVisualAttributes() ->convertCssToVisualAttributes()
->render(); ->render();
} catch ( Exception $e ) { } catch ( \Exception $e ) {
$logger = wc_get_logger(); $logger = wc_get_logger();
$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) ); $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
} }
......
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