diff --git a/src/Mailer.php b/src/Mailer.php index 8e2cc68428ea68e5fc8636eb4025946133a39336..306dc5cfed693c827789cdae3b9feafd85b31508 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -26,17 +26,30 @@ class Mailer { /** * @var array */ - private $attributes; + 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 $attributes = [] + array $template_attributes = [] ) { - $this->attributes = $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' ) ); } /** @@ -79,6 +92,38 @@ 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( $include = [] ) { foreach ( $this->get_emails() as $email ) { @@ -88,22 +133,14 @@ class Mailer { if ( $email->get_is_enable() ) { add_filter( 'wp_mail_from', - $from_cb = static function ( $from ) use ( $email ) { - if ( ! empty( $email->get_from() ) ) { - return $email->get_from(); - } - - return $from; + $from_cb = static function ( $from ) { + return $this->get_from(); } ); add_filter( 'wp_mail_from_name', - $from_name_cb = static function ( $from_name ) use ( $email ) { - if ( ! empty( $email->get_from_name() ) ) { - return $email->get_from_name(); - } - - return $from_name; + $from_name_cb = static function ( $from_name ) { + return $this->get_from_name(); } ); add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); @@ -144,12 +181,12 @@ class Mailer { $output = $this->renderer->render( 'html/email-header', [ - 'heading' => $this->attributes['heading'] ?? $email->get_heading(), - 'logo' => $this->attributes['logo'] + '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->attributes['footer'] ] ); + $output .= $this->renderer->render( 'html/email-footer', [ 'footer' => $this->template_attributes['footer'] ] ); return $this->css_inline( $output ); } @@ -163,10 +200,10 @@ class Mailer { $styles = $this->renderer->render( 'html/email-styles', [ - 'primary' => $this->attributes['primary'] ?? '#d15291', - 'text' => $this->attributes['text'] ?? '#303030', - 'bg' => $this->attributes['bg'] ?? '#f9f9f9', - 'body' => $this->attributes['body'] ?? '#ffffff', + 'primary' => $this->template_attributes['primary'] ?? '#d15291', + 'text' => $this->template_attributes['text'] ?? '#303030', + 'bg' => $this->template_attributes['bg'] ?? '#f9f9f9', + 'body' => $this->template_attributes['body'] ?? '#ffffff', ] );