Skip to content
Snippets Groups Projects

feat: email styles

Merged Piotr Potrebka requested to merge refactor/templates into devel
6 files
+ 78
27
Compare changes
  • Side-by-side
  • Inline

Files

@@ -4,33 +4,18 @@ namespace WPDesk\Library\WPEmail\Abstracts;
use Exception;
abstract class EmailAbstract implements EmailInterface {
/**
* @var bool
*/
private $is_enable;
class Email {
/**
* @var array
*/
private $recipients = [];
/**
* @var array
*/
private $placeholders = [];
/**
* @var string
*/
private $subject = '';
/**
* @var string
*/
private $heading = '';
/**
* @var array
*/
@@ -41,15 +26,10 @@ abstract class EmailAbstract implements EmailInterface {
*/
private $content = '';
/**
* @var string
*/
private $type = 'html';
/**
* @var array
*/
private $headers = [ 'Content-Type: text/html; charset=UTF-8' ];
private $headers = [ 'Content-Type' => 'text/html' ];
/**
* @var string
@@ -62,23 +42,9 @@ abstract class EmailAbstract implements EmailInterface {
private $from_name;
/**
* @return string
*/
abstract public function get_id(): string;
/**
* @return string
*/
public function get_template_name(): string {
return 'default';
}
/**
* @return string
* @var array
*/
public function get_from(): string {
return sanitize_email( $this->from_email );
}
private $template_attributes;
/**
* @param string $from_email
@@ -91,6 +57,13 @@ abstract class EmailAbstract implements EmailInterface {
return $this;
}
/**
* @return string
*/
public function get_from(): string {
return $this->from_email;
}
/**
* @param string $from_name
*
@@ -109,42 +82,6 @@ abstract class EmailAbstract implements EmailInterface {
return wp_specialchars_decode( esc_html( $this->from_name ), ENT_QUOTES );
}
/**
* @return bool
*/
public function get_is_enable(): bool {
return $this->is_enable;
}
/**
* @param $enable
*
* @return EmailAbstract
*/
public function set_is_enable( $enable ): self {
$this->is_enable = $enable;
return $this;
}
/**
* @param array $placeholders
*
* @return self
*/
public function set_placeholders( array $placeholders = [] ): self {
$this->placeholders = array_merge( $this->placeholders, $placeholders );
return $this;
}
/**
* @return string[]
*/
public function get_placeholders(): array {
return $this->placeholders;
}
/**
* @param string $subject
*
@@ -161,29 +98,7 @@ abstract class EmailAbstract implements EmailInterface {
* @throws Exception
*/
public function get_subject(): string {
if ( ! $this->subject ) {
throw new Exception( 'Empty email subject' );
}
return $this->replace_placeholders( $this->subject );
}
/**
* @param string $heading
*
* @return self
*/
public function set_heading( string $heading ): self {
$this->heading = $heading;
return $this;
}
/**
* @return string
*/
public function get_heading(): string {
return $this->replace_placeholders( $this->heading );
return $this->subject;
}
/**
@@ -204,22 +119,24 @@ abstract class EmailAbstract implements EmailInterface {
return $this->recipients;
}
/**
* @param array $headers
*
* @return self
*/
public function set_headers( array $headers = [] ): self {
$this->headers = $headers;
public function set_header( string $header, string $value ): self {
$this->headers[ $header ] = $value;
return $this;
}
/**
* @return string[]
*/
/** @return string[] */
public function get_headers(): array {
return $this->headers;
$result = [];
foreach ( $this->headers as $header => $value ) {
$result[] = "$header: $value";
}
return $result;
}
public function get_header( string $header ): string {
return $this->headers[ $header ] ?? '';
}
/**
@@ -240,36 +157,28 @@ abstract class EmailAbstract implements EmailInterface {
return $this->attachments;
}
/**
* @param string $type
*
* @return self
*/
public function set_type( string $type = 'html' ): self {
$this->type = $type;
return $this;
public function is_html(): bool {
return $this->get_header( 'Content-Type' ) === 'text/html';
}
/**
* @return string
*/
public function get_type(): string {
return $this->type;
}
/**
* @return string
*/
public function get_content_type(): string {
switch ( $this->get_type() ) {
case 'html':
return 'text/html';
public function set_content_type( $type = 'html' ): self {
switch ( $type ) {
case 'plain':
$content_type = 'text/plain';
break;
case 'multipart':
return 'multipart/alternative';
$content_type = 'multipart/alternative';
break;
default:
return 'text/plain';
$content_type = 'text/html';
}
$this->set_header( 'Content-Type', $content_type );
return $this;
}
/**
@@ -288,19 +197,17 @@ abstract class EmailAbstract implements EmailInterface {
* @throws Exception
*/
public function get_content(): string {
return $this->replace_placeholders( $this->content );
return $this->content;
}
/**
* @return array|string|string[]
*/
protected function replace_placeholders( string $string ): string {
if ( empty( $this->placeholders ) ) {
return $string;
}
public function set_template_attributes( string $name, string $value ): self {
$this->template_attributes[ $name ] = $value;
return (string) str_replace( array_keys( $this->placeholders ), array_values( $this->placeholders ), $string );
return $this;
}
public function get_template_attributes(): array {
return $this->template_attributes;
}
}
Loading