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

Merge branch 'devel' into 'main'

Devel

See merge request !1
parents 54430822 53209129
No related branches found
No related tags found
1 merge request!1Devel
Pipeline #150347 passed
{
"name": "wpdesk/wp-coupons-core",
"description": "Coupons library",
"name": "wpdesk/wp-mail",
"description": "WP Email library",
"license": "MIT",
"keywords": [
"wordpress",
"woocommerce",
"woocommerce-coupons",
"pdf",
"pdf builder"
"email",
"wp-email",
"email templates",
"admin email"
],
"homepage": "https://gitlab.com/wpdesk/wp-canva-editor",
"homepage": "https://gitlab.wpdesk.dev/wpdesk/library/wp-mail",
"minimum-stability": "stable",
"authors": [
{
"name": "piotr.potrebka",
"email": "piotr.potrebka@wpdesk.net"
"name": "eryk.mika",
"email": "eryk.mika@wpdesk.eu"
}
],
"config": {
......@@ -47,12 +47,12 @@
},
"autoload": {
"psr-4": {
"WPDesk\\Library\\WPCoupons\\": "src/Coupons"
"WPDesk\\Library\\WPEmail\\": "src"
}
},
"autoload-dev": {},
"extra": {
"text-domain": "wp-coupons-core",
"text-domain": "wp-email",
"translations-folder": "lang",
"po-files": {
"pl_PL": "pl_PL.po"
......
......@@ -2,8 +2,20 @@
namespace WPDesk\Library\WPEmail\Abstracts;
use WPDesk\View\Renderer\Renderer;
abstract class EmailAbstract implements EmailInterface {
/**
* @var Renderer
*/
private $renderer;
public function __construct( Renderer $renderer, array $recipients ) {
$this->renderer = $renderer;
$this->recipients = $recipients;
}
/**
* Define unique email ID.
*
......@@ -14,10 +26,10 @@ abstract class EmailAbstract implements EmailInterface {
/**
* Get defined placeholders.
*
* @return array
* @return string[]
*/
public function get_placeholders(): array {
return [];
}
/**
......@@ -26,7 +38,11 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_subject(): string {
if ( ! $this->subject ) {
throw new \Exception( 'Empty email subject' );
}
return '';
}
/**
......@@ -35,25 +51,25 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_heading(): string {
return '';
}
/**
* Get valid recipients.
*
* @return array
* @return string[]
*/
public function get_recipients(): array {
return $this->recipients;
}
/**
* Get email headers.
*
* @return string
* @return string[]
*/
public function get_headers(): string {
public function get_headers(): array {
return [];
}
/**
......@@ -62,7 +78,7 @@ abstract class EmailAbstract implements EmailInterface {
* @return array
*/
public function get_attachments(): array {
return [];
}
/**
......@@ -71,7 +87,7 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_type(): string {
return 'text/html';
}
/**
......@@ -80,18 +96,27 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_content(): string {
if ( ! $this->content ) {
throw new \Exception( 'Empty email subject' );
}
return '';
}
/**
* Send email.
* @return mixed
*/
public function get_object() {
return '';
}
/**
* Get email content.
*
* @return void
* @return string
*/
public function send(): void {
wp_mail(
$this->get_recipients(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()
);
public function render(): string {
return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] );
}
}
......@@ -35,16 +35,16 @@ interface EmailInterface {
/**
* Get valid recipients.
*
* @return array
* @return string[]
*/
public function get_recipients(): array;
/**
* Get email headers.
*
* @return string
* @return string[]
*/
public function get_headers(): string;
public function get_headers(): array;
/**
* Get email attachments.
......@@ -67,11 +67,4 @@ interface EmailInterface {
*/
public function get_content(): string;
/**
* Send email.
*
* @return void
*/
public function send(): void;
}
<?php
namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Abstracts\EmailAbstract;
class CustomerEmail extends EmailAbstract {
const ID = 'customer_email';
/**
* Define unique email ID.
*
* @return string
*/
public function get_id(): string {
return self::ID;
}
}
<?php
namespace WPDesk\Library\WPEmail\Emails;
use WPDesk\Library\WPEmail\Abstracts\EmailInterface;
class EmailSender {
/**
* @var EmailInterface[]
*/
private $emails = [];
/**
* @var string
*/
private $from;
/**
* @var string
*/
private $from_name;
/**
* @param $from
* @param $from_name
*/
public function __construct( $from = '', $from_name = '' ) {
$this->from = $from;
$this->from_name = $from_name;
}
public function add_email( EmailInterface $email ) {
$this->emails[ $email->get_id() ] = $email;
}
public function get_emails(): array {
return $this->emails;
}
/**
* WordPress callback for setting the from email
*
* @param string $email
*
* @return string
*/
public function from( $email ) {
if ( ! empty( $this->from ) && is_email( $this->from ) ) {
$email = $this->from;
}
return $email;
}
/**
* WordPress callback for setting the from name
*
* @param string $name
*
* @return string
*/
public function from_name( $name ) {
if ( ! empty( $this->from_name ) ) {
$name = html_entity_decode( sanitize_text_field( $this->from_name ) );
}
return $name;
}
/**
* Add filters before fire wp_mail.
*
* @return void
*/
private function before_wp_mail() {
add_filter( 'wp_mail_from', array( $this, 'from' ) );
add_filter( 'wp_mail_from_name', array( $this, 'from_name' ) );
}
public function send() {
foreach ( $this->get_emails() as $email ) {
$this->before_wp_mail();
wp_mail(
$email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments()
);
$this->after_wp_mail();
}
}
/**
* Remove filters after fire wp_mail.
*
* @return void
*/
private function after_wp_mail() {
remove_filter( 'wp_mail_from', array( $this, 'from' ) );
remove_filter( 'wp_mail_from_name', array( $this, 'from_name' ) );
}
}
<?php
namespace WPDesk\Library\WPEmail;
namespace WPDesk\Library\WPEmail\Emails;
use WPDesk\Library\WPEmail\Abstracts\EmailAbstract;
class AdminEmail extends EmailAbstract {
class Email extends EmailAbstract {
const ID = 'admin_email';
const ID = 'email';
/**
* Define unique email ID.
......
<?php
namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Emails\AdminEmail;
use WPDesk\Library\WPEmail\Emails\Email;
use WPDesk\Library\WPEmail\Emails\EmailSender;
use WPDesk\Persistence\Adapter\WordPress\WordpressOptionsContainer;
use WPDesk\Persistence\Adapter\WordPress\WordpressTransientContainer;
use WPDesk\View\Renderer\SimplePhpRenderer;
use WPDesk\View\Resolver\ChainResolver;
use WPDesk\View\Resolver\DirResolver;
use WPDesk\View\Resolver\WPThemeResolver;
class Integration {
public function __construct() {
$chain_resolver = new ChainResolver();
$chain_resolver->appendResolver( new WPThemeResolver( 'email_templates' ) );
$chain_resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) );
$renderer = new SimplePhpRenderer( $chain_resolver );
$email_sender = new EmailSender( 'email@mojastron.pl', 'Moj sklep' );
$email = new Email( $renderer, [] );
$email_sender->add_email( $email );
$email_sender->send();
}
}
<?php
namespace WPDesk\Library\WPEmail\Parser;
class HTMLDecorator {
public static function style_inline( $content ) {
if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) {
ob_start();
wc_get_template( 'emails/email-styles.php' );
$css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), $this );
$css_inliner_class = \Pelago\Emogrifier\CssInliner::class;
if ( $this->supports_emogrifier() && class_exists( $css_inliner_class ) ) {
try {
$css_inliner = \Pelago\Emogrifier\CssInliner::fromHtml( $content )->inlineCss( $css );
do_action( 'woocommerce_emogrifier', $css_inliner, $this );
$dom_document = $css_inliner->getDomDocument();
HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone();
$content = CssToAttributeConverter::fromDomDocument( $dom_document )
->convertCssToVisualAttributes()
->render();
} catch ( Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
}
} else {
$content = '<style type="text/css">' . $css . '</style>' . $content;
}
}
return $content;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment