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

Merge branch 'devel' into 'main'

Devel

See merge request !5
parents 2bed3fe4 4932c63b
Branches main
Tags 1.0.0
1 merge request!5Devel
Pipeline #432324 passed with warnings with stages
in 53 seconds
## [1.0.0] - 2023-03-06 ## [1.0.0] - 2024-10-23
### Added ### Added
- init - init
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
"config": { "config": {
"sort-packages": true, "sort-packages": true,
"platform": { "platform": {
"php": "7.3" "php": "7.4"
}, },
"allow-plugins": { "allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true, "dealerdirect/phpcodesniffer-composer-installer": true,
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
} }
}, },
"require": { "require": {
"php": ">=7.3", "php": ">=7.4",
"psr/log": "^1.1.0", "psr/log": "^1.1.0",
"wpdesk/wp-persistence": "^3.0", "wpdesk/wp-persistence": "^3.0",
"wpdesk/wp-view": "^2.0", "wpdesk/wp-view": "^2.0",
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
"require-dev": { "require-dev": {
"10up/wp_mock": "*", "10up/wp_mock": "*",
"mockery/mockery": "*", "mockery/mockery": "*",
"phpunit/phpunit": "<7" "phpunit/phpunit": "^9"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
...@@ -9,42 +9,42 @@ class Email { ...@@ -9,42 +9,42 @@ class Email {
/** /**
* @var array * @var array
*/ */
private $recipients = []; private array $recipients = [];
/** /**
* @var string * @var string
*/ */
private $subject = ''; private string $subject = '';
/** /**
* @var array * @var array
*/ */
private $attachments = []; private array $attachments = [];
/** /**
* @var string * @var string
*/ */
private $content = ''; private string $content = '';
/** /**
* @var array * @var array
*/ */
private $headers = [ 'Content-Type' => 'text/html' ]; private array $headers = [ 'Content-Type' => 'text/html' ];
/** /**
* @var string * @var string
*/ */
private $from_email; private string $from_email;
/** /**
* @var string * @var string
*/ */
private $from_name; private string $from_name;
/** /**
* @var array * @var array
*/ */
private $template_attributes; private array $template_attributes;
/** /**
* @param string $from_email * @param string $from_email
...@@ -106,7 +106,7 @@ class Email { ...@@ -106,7 +106,7 @@ class Email {
* *
* @return self * @return self
*/ */
public function set_recipients( array $recipients = [] ): self { public function set_recipients( array $recipients ): self {
$this->recipients = $recipients; $this->recipients = $recipients;
return $this; return $this;
...@@ -162,9 +162,11 @@ class Email { ...@@ -162,9 +162,11 @@ class Email {
} }
/** /**
* @return string * @param $type
*
* @return Email
*/ */
public function set_content_type( $type = 'html' ): self { public function set_content_type( $type ): self {
switch ( $type ) { switch ( $type ) {
case 'plain': case 'plain':
$content_type = 'text/plain'; $content_type = 'text/plain';
...@@ -200,12 +202,20 @@ class Email { ...@@ -200,12 +202,20 @@ class Email {
return $this->content; return $this->content;
} }
public function set_template_attributes( string $name, string $value ): self { /**
$this->template_attributes[ $name ] = $value; * @param array $template_attributes
*
* @return $this
*/
public function set_template_attributes( array $template_attributes ): self {
$this->template_attributes= $template_attributes;
return $this; return $this;
} }
/**
* @return array
*/
public function get_template_attributes(): array { public function get_template_attributes(): array {
return $this->template_attributes; return $this->template_attributes;
} }
......
...@@ -4,9 +4,12 @@ declare( strict_types=1 ); ...@@ -4,9 +4,12 @@ declare( strict_types=1 );
namespace WPDesk\Library\WPEmail\Exceptions; namespace WPDesk\Library\WPEmail\Exceptions;
class MailerException extends \RuntimeException { use RuntimeException;
use WP_Error;
public static function with_wp_error( \WP_Error $error ): self { class MailerException extends RuntimeException {
public static function with_wp_error( WP_Error $error ): self {
$errors = $error->get_error_messages( 'wp_mail_failed' ); $errors = $error->get_error_messages( 'wp_mail_failed' );
$message = implode( "\n", $errors ); $message = implode( "\n", $errors );
......
...@@ -11,7 +11,7 @@ class ColorConversion { ...@@ -11,7 +11,7 @@ class ColorConversion {
* *
* @return bool True if a light color. * @return bool True if a light color.
*/ */
public static function is_hex_light( $color ) { public static function is_hex_light( $color ): bool {
$hex = str_replace( '#', '', $color ); $hex = str_replace( '#', '', $color );
$c_r = hexdec( substr( $hex, 0, 2 ) ); $c_r = hexdec( substr( $hex, 0, 2 ) );
...@@ -31,11 +31,11 @@ class ColorConversion { ...@@ -31,11 +31,11 @@ class ColorConversion {
/** /**
* Convert RGB to HEX. * Convert RGB to HEX.
* *
* @param mixed $color Color. * @param string $color Color.
* *
* @return array * @return array
*/ */
public static function rgb_from_hex( $color ) { public static function rgb_from_hex( string $color ): array {
$color = str_replace( '#', '', $color ); $color = str_replace( '#', '', $color );
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF". // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF".
$color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
...@@ -52,17 +52,17 @@ class ColorConversion { ...@@ -52,17 +52,17 @@ class ColorConversion {
/** /**
* Make HEX color darker. * Make HEX color darker.
* *
* @param mixed $color Color. * @param string $color Color.
* @param int $factor Darker factor. * @param int $factor Darker factor.
* Defaults to 30. * Defaults to 30.
* *
* @return string * @return string
*/ */
public static function hex_darker( $color, $factor = 30 ) { public static function hex_darker( string $color, int $factor = 30 ): string {
$base = self::rgb_from_hex( $color ); $base = self::rgb_from_hex( $color );
$color = '#'; $color = '#';
foreach ( $base as $k => $v ) { foreach ( $base as $v ) {
$amount = $v / 100; $amount = $v / 100;
$amount = self::round( $amount * $factor ); $amount = self::round( $amount * $factor );
$new_decimal = $v - $amount; $new_decimal = $v - $amount;
...@@ -81,17 +81,17 @@ class ColorConversion { ...@@ -81,17 +81,17 @@ class ColorConversion {
/** /**
* Make HEX color lighter. * Make HEX color lighter.
* *
* @param mixed $color Color. * @param string $color Color.
* @param int $factor Lighter factor. * @param int $factor Lighter factor.
* Defaults to 30. * Defaults to 30.
* *
* @return string * @return string
*/ */
public static function hex_lighter( $color, $factor = 30 ) { public static function hex_lighter( string $color, int $factor = 30 ): string {
$base = self::rgb_from_hex( $color ); $base = self::rgb_from_hex( $color );
$color = '#'; $color = '#';
foreach ( $base as $k => $v ) { foreach ( $base as $v ) {
$amount = 255 - $v; $amount = 255 - $v;
$amount = $amount / 100; $amount = $amount / 100;
$amount = self::round( $amount * $factor ); $amount = self::round( $amount * $factor );
......
<?php
namespace WPDesk\Library\WPEmail\Helpers;
use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
class StyleInliner {
public static function inline( string $content, string $styles = '' ): string {
if ( class_exists( 'DOMDocument' ) ) {
try {
$css_inliner = CssInliner::fromHtml( $content )->inlineCss( $styles );
$dom_document = $css_inliner->getDomDocument();
HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone();
$content = CssToAttributeConverter::fromDomDocument( $dom_document )
->convertCssToVisualAttributes()
->render();
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
}
} else {
$content = '<style>' . strip_tags( $styles ) . '</style>' . $content;
}
return $content;
}
/**
* @return array|string|string[]
*/
protected function replace_placeholders( string $string ): string {
if ( empty( $this->placeholders ) ) {
return $string;
}
return (string) str_replace( array_keys( $this->placeholders ), array_values( $this->placeholders ), $string );
}
}
...@@ -2,22 +2,32 @@ ...@@ -2,22 +2,32 @@
namespace WPDesk\Library\WPEmail; namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Helpers\StyleInliner; use Exception;
use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
use Psr\Log\LoggerInterface;
use WPDesk\View\Renderer\Renderer; use WPDesk\View\Renderer\Renderer;
class Template { class Template {
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/** /**
* @var Renderer * @var Renderer
*/ */
private $renderer; private Renderer $renderer;
/** /**
* @var array * @var array
*/ */
private $template_attributes; private array $template_attributes;
public function __construct( Renderer $renderer, array $template_attributes ) { public function __construct( LoggerInterface $logger, Renderer $renderer, array $template_attributes ) {
$this->logger = $logger;
$this->renderer = $renderer; $this->renderer = $renderer;
$this->template_attributes = wp_parse_args( $template_attributes, $this->get_default_template_attributes() ); $this->template_attributes = wp_parse_args( $template_attributes, $this->get_default_template_attributes() );
} }
...@@ -38,7 +48,7 @@ class Template { ...@@ -38,7 +48,7 @@ class Template {
public function css_inline( string $content ): string { public function css_inline( string $content ): string {
$styles = $this->renderer->render( 'html/email-styles', $this->template_attributes ); $styles = $this->renderer->render( 'html/email-styles', $this->template_attributes );
return StyleInliner::inline( $content, $styles ); return $this->convert_css( $content, $styles );
} }
public function get_default_template_attributes(): array { public function get_default_template_attributes(): array {
...@@ -53,4 +63,23 @@ class Template { ...@@ -53,4 +63,23 @@ class Template {
]; ];
} }
public function convert_css( string $content, string $styles = '' ): string {
if ( class_exists( 'DOMDocument' ) ) {
try {
$css_inliner = CssInliner::fromHtml( $content )->inlineCss( $styles );
$dom_document = $css_inliner->getDomDocument();
HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone();
$content = CssToAttributeConverter::fromDomDocument( $dom_document )->convertCssToVisualAttributes()->render();
} catch ( Exception $e ) {
$this->logger->debug( $e->getMessage() );
return $content;
}
} else {
$content = '<style>' . strip_tags( $styles ) . '</style>' . $content;
}
return $content;
}
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace WPDesk\Library\WPEmail; namespace WPDesk\Library\WPEmail;
use Exception; use Exception;
use Psr\Log\LoggerInterface;
use WP_Error; 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;
...@@ -14,14 +15,24 @@ use WPDesk\View\Resolver\DirResolver; ...@@ -14,14 +15,24 @@ use WPDesk\View\Resolver\DirResolver;
class WPMailer implements Mailer { class WPMailer implements Mailer {
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/** /**
* @var Renderer * @var Renderer
*/ */
private $renderer; private Renderer $renderer;
public function __construct( LoggerInterface $logger ) {
$this->logger = $logger;
$this->init_renderer();
}
public function __construct() { public function init_renderer() {
$resolver = new ChainResolver(); $resolver = new ChainResolver();
$resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) ); $resolver->appendResolver( new DirResolver( dirname( __DIR__ ) . '/templates' ) );
$renderer = new SimplePhpRenderer( $resolver ); $renderer = new SimplePhpRenderer( $resolver );
$this->set_renderer( $renderer ); $this->set_renderer( $renderer );
} }
...@@ -34,7 +45,11 @@ class WPMailer implements Mailer { ...@@ -34,7 +45,11 @@ class WPMailer implements Mailer {
return $this->renderer; return $this->renderer;
} }
/** @return void */ /**
* @param Email $email
*
* @return void
*/
public function send( Email $email ): void { public function send( Email $email ): void {
$mailer_from = $email->get_from(); $mailer_from = $email->get_from();
add_filter( add_filter(
...@@ -53,7 +68,7 @@ class WPMailer implements Mailer { ...@@ -53,7 +68,7 @@ class WPMailer implements Mailer {
); );
add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
$email_template = new Template( $this->renderer, $email->get_template_attributes() ); $email_template = new Template( $this->logger, $this->get_renderer(), $email->get_template_attributes() );
try { try {
$success = wp_mail( $success = wp_mail(
...@@ -84,4 +99,5 @@ class WPMailer implements Mailer { ...@@ -84,4 +99,5 @@ class WPMailer implements Mailer {
throw MailerException::with_wp_error( $error ); throw MailerException::with_wp_error( $error );
} }
} }
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