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

init

parent 865fa1e7
No related branches found
No related tags found
1 merge request!1Devel
Pipeline #150320 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.com/wpdesk/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,19 @@
namespace WPDesk\Library\WPEmail\Abstracts;
use WPDesk\View\Renderer\Renderer;
abstract class EmailAbstract implements EmailInterface {
/**
* @var Renderer
*/
private $renderer;
public function __construct( Renderer $renderer ) {
$this->renderer = $renderer;
}
/**
* Define unique email ID.
*
......@@ -14,10 +25,10 @@ abstract class EmailAbstract implements EmailInterface {
/**
* Get defined placeholders.
*
* @return array
* @return string[]
*/
public function get_placeholders(): array {
return [];
}
/**
......@@ -26,7 +37,7 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_subject(): string {
return '';
}
/**
......@@ -35,25 +46,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 [];
}
/**
* Get email headers.
*
* @return string
* @return string[]
*/
public function get_headers(): string {
public function get_headers(): array {
return [];
}
/**
......@@ -62,7 +73,7 @@ abstract class EmailAbstract implements EmailInterface {
* @return array
*/
public function get_attachments(): array {
return [];
}
/**
......@@ -71,7 +82,7 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_type(): string {
return 'text/html';
}
/**
......@@ -80,18 +91,23 @@ abstract class EmailAbstract implements EmailInterface {
* @return string
*/
public function get_content(): string {
return '';
}
/**
* @return mixed
*/
public function get_object() {
return '';
}
/**
* Send email (wywalić do osobnej klasy ;) )
* 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.
......
<?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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment