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

Merge branch 'devel' into 'main'

Devel

See merge request !2
parents caa78e84 e73a91bc
No related branches found
No related tags found
1 merge request!2Devel
Pipeline #430153 failed
Showing
with 971 additions and 396 deletions
......@@ -41,16 +41,18 @@
"require-dev": {
"10up/wp_mock": "*",
"mockery/mockery": "*",
"phpunit/phpunit": "<7",
"wp-coding-standards/wpcs": "^2.3.0",
"squizlabs/php_codesniffer": "^3.0.2"
"phpunit/phpunit": "<7"
},
"autoload": {
"psr-4": {
"WPDesk\\Library\\WPEmail\\": "src"
}
},
"autoload-dev": {},
"autoload-dev": {
"psr-4": {
"Tests\\Mailer\\": "tests/unit"
}
},
"extra": {
"text-domain": "wp-email",
"translations-folder": "lang",
......
msgid ""
msgstr ""
"Project-Id-Version: Plugin Template\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/flexible-"
"donation\n"
"POT-Creation-Date: 2023-02-02 09:05+0100\n"
"PO-Revision-Date: 2023-02-02 09:05+0100\n"
"Last-Translator: Krzysztof Dyszczyk <krzysztof.dyszczyk@gmail.com>\n"
"Language-Team: Polish\n"
"Language: pl_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10 >= 2 && n%10<=4 &&(n"
"%100<10||n%100 >= 20)? 1 : 2);\n"
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 "
"|| n%100>=20) ? 1 : 2);\n"
"X-Generator: Poedit 3.2.2\n"
"X-Loco-Version: 2.3.0; wp-5.2.2X-Poedit-Basepath: ..\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;_n:1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;esc_attr__;"
"esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c;_n_noop:1,2;"
"_nx_noop:3c,1,2;__ngettext_noop:1,2\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPathExcluded-0: *.js\n"
"X-Poedit-SearchPathExcluded-1: vendor\n"
"X-Poedit-SearchPathExcluded-2: vendor_prefixed\n"
#~ msgid "Flexible Donation"
#~ msgstr "Elastyczne Darowizny"
#~ msgid "Create a donation form in conjunction with popular payment gateways"
#~ msgstr ""
#~ "Utwórz formularz darowizny w połączeniu z popularnymi bramkami płatności"
#~ msgid "Settings"
#~ msgstr "Ustawienia"
#~ msgid "Support"
#~ msgstr "Wsparcie"
#~ msgid "Amount"
#~ msgstr "Kwota"
#~ msgid "Payment status"
#~ msgstr "Status płatności"
#~ msgid "Payment method"
#~ msgstr "Metoda płatności"
#~ msgid "Form"
#~ msgstr "Formularz"
#~ msgid "%s PLN"
#~ msgstr "%s PLN"
#~ msgid "Unknown"
#~ msgstr "Nieznany"
#~ msgid "Donation data"
#~ msgstr "Dane darowizny"
#~ msgid "Form settings"
#~ msgstr "Ustawienia formularza"
#~ msgid "I accept terms & conditions"
#~ msgstr "Akceptuję regulamin darowizn"
#~ msgid "First name"
#~ msgstr "Imię"
#~ msgid "Last name"
#~ msgstr "Nazwisko"
#~ msgid "E-mail"
#~ msgstr "E-mail"
#~ msgid "Comment"
#~ msgstr "Komentarz"
#~ msgid "Terms"
#~ msgstr "Regulamin"
#~ msgid "Send donation"
#~ msgstr "Wyślij darowiznę"
#~ msgid "terms & conditions"
#~ msgstr "regulamin"
#~ msgid "Bank transfer"
#~ msgstr "Przelew bankowy"
#~ msgid "Bank Transfer Settings"
#~ msgstr "Ustawienia przelewu bankowego"
#~ msgid "Recipient data"
#~ msgstr "Dane odbiorcy"
#~ msgid "Company name, street, street number, post code, city etc."
#~ msgstr "Nazwa firmy, ulica, numer ulicy, kod pocztowy, miasto itp."
#~ msgid "Account number"
#~ msgstr "Nr konta"
#~ msgid "Bank name"
#~ msgstr "Nazwa banku"
#~ msgid "Payment title"
#~ msgstr "Tytuł płatności"
#~ msgid "SWIFT"
#~ msgstr "SWIFT"
#~ msgid "Sort code"
#~ msgstr "Sort code"
#~ msgid "Description"
#~ msgstr "Opis"
#~ msgid ""
#~ "Please pay directly to our bank account. Please use defined payment title "
#~ "as the payment title. Your order will be processed after the payment is "
#~ "credited to our account."
#~ msgstr ""
#~ "Prosimy o wpłatę bezpośrednio na nasze konto bankowe. Jako tytuł "
#~ "płatności proszę użyć numeru zamówienia. Twoje zamówienie zostanie "
#~ "zrealizowane po zaksięgowaniu wpłaty na naszym koncie."
#~ msgid "PayU"
#~ msgstr "PayU"
#~ msgid "Niepoprawne zamówienie PayU!"
#~ msgstr "Niepoprawne zamówienie PayU!"
#~ msgid "Payu Settings"
#~ msgstr "Ustawienia PayU"
#~ msgid "POS ID"
#~ msgstr "POS ID"
#~ msgid "MD5"
#~ msgstr "MD5"
#~ msgid "Client ID"
#~ msgstr "Client ID"
#~ msgid "Client secret"
#~ msgstr "Client secret"
#~ msgid "Currency"
#~ msgstr "Waluta"
#~ msgid "Is sandbox"
#~ msgstr "Włącz sanbox"
#~ msgid "%1$s, Darowizna %2$s"
#~ msgstr "%1$s, Darowizna %2$s"
#~ msgid "Darowizna nr %s"
#~ msgstr "Darowizna nr %s"
#~ msgid "Donations"
#~ msgstr "Darowizny"
#~ msgid "Donation"
#~ msgstr "Darowizna"
#~ msgid "Not donations found."
#~ msgstr "Nie znaleziono darowizn."
#~ msgid "Forms"
#~ msgstr "Formularze"
#~ msgid "Not forms found."
#~ msgstr "Nie znaleziono formularzy."
#~ msgid "Donation Summary"
#~ msgstr "Podsumowanie Darowizny"
#~ msgid "Pending"
#~ msgstr "Oczekujący"
#~ msgid "Completed"
#~ msgstr "Opłacone"
#~ msgid "Failed"
#~ msgstr "Anulowane"
#~ msgid "Save Changes"
#~ msgstr "Zapisz zmiany"
#~ msgid "Summary Page"
#~ msgstr "Strona podsumowania"
#~ msgid "Select or create summary page."
#~ msgstr "Wybierz stronę podsumowania."
#~ msgid "Summary Page Content"
#~ msgstr "Treść strony podsumowania"
#~ msgid "This content will be displayed on summary page."
#~ msgstr "Ta treść pojawi się na stronie podsumowania."
#~ msgid "Customer e-mail"
#~ msgstr "E-mail darczyńcy"
#~ msgid "Enable"
#~ msgstr "Włącz"
#~ msgid "Subject"
#~ msgstr "Temat"
#~ msgid "[{site_title}]: Your donation has been made"
#~ msgstr "[{site_title}]: Twoja darowizna została przekazana"
#~ msgid "Email heading"
#~ msgstr "Nagłówek maila"
#~ msgid "Thank you for donating"
#~ msgstr "Dziękujemy za darowiznę"
#~ msgid "Email body"
#~ msgstr "Treść maila"
#~ msgid ""
#~ "Hi {donation_first_name},<br/><br/>Thank you for your donation of "
#~ "£{donation_amount}.<br/><br/>{donate_summary_table}"
#~ msgstr ""
#~ "Cześć {donation_first_name},<br/><br/>Dziękujemy za darowiznę w kwocie "
#~ "{donation_amount}zł.<br/><br/>{donate_summary_table}"
#~ msgid "Admin e-mail"
#~ msgstr "E-mail administratora"
#~ msgid ""
#~ "Hi Admin,<br/><br/>A new donation in the amount of £{donation_amount} has "
#~ "been received.<br/><br/>You can check the details <a href="
#~ "\"{donation_url}\">here</a>.<br/><br/>{donation_summary_table}"
#~ msgstr ""
#~ "Cześć Admin,<br/><br/>Nowa darowizna w kwocie {donation_amount}zł została "
#~ "przekazana.<br/><br/>Sprawdź szczegóły <a href=\"{donation_url}\">tutaj</"
#~ "a>.<br/><br/>{donation_summary_table}"
#~ msgid "User Price"
#~ msgstr "Kwota użytkownika"
#~ msgid "Minimum price:"
#~ msgstr "Kwota minimalna:"
#~ msgid "Preset Prices"
#~ msgstr "Zdefiniowane ceny"
#~ msgid "Add item"
#~ msgstr "Dodaj wartość"
#~ msgid "Remove item"
#~ msgstr "Usuń wartość"
#~ msgid "Terms and Conditions"
#~ msgstr "Regulamin"
#~ msgid "Show terms and conditions"
#~ msgstr "Pokaż regulamin użytkownika"
#~ msgid "Link label:"
#~ msgstr "Etykieta linku:"
#~ msgid "Terms page:"
#~ msgstr "Strona z warunkami:"
#~ msgid "Payment methods"
#~ msgstr "Metody płatności"
#~ msgid "First name:"
#~ msgstr "Imię:"
#~ msgid "Last name:"
#~ msgstr "Nazwisko:"
#~ msgid "E-mail:"
#~ msgstr "E-mail:"
#~ msgid "Amount:"
#~ msgstr "Kwota:"
#~ msgid "Comment:"
#~ msgstr "Komentarz:"
#~ msgid "Payment method:"
#~ msgstr "Metoda płatności:"
#~ msgid "Payment status:"
#~ msgstr "Status płatności:"
#~ msgid "Sort Code"
#~ msgstr "Sort Code"
#~ msgid "Donation details"
#~ msgstr "Szczegóły darowizny"
#~ msgid "Thank You Page"
#~ msgstr "Strona podziękowania"
#~ msgid "Minimum amount"
#~ msgstr "Minimalna kwota"
#~ msgid "Set minimum amount. Default: 1."
#~ msgstr "Ustaw kwotę minimalną. Domyślnie 10."
#~ msgid "Maximum amount"
#~ msgstr "Maksymalna kwota"
#~ msgid "Set maximum amount or leave empty."
#~ msgstr "Ustaw kwotę maksymalną lub pozostaw puste."
#~ msgid "Purposes"
#~ msgstr "Cele"
#~ msgid "Purpose"
#~ msgstr "Cek"
#~ msgid "Search Purpose"
#~ msgstr "Szukaj celu"
#~ msgid "Popular Purposes"
#~ msgstr "Popularne cele"
#~ msgid "All Purposes"
#~ msgstr "Wszystki cele"
#~ msgid "Edit Purposes"
#~ msgstr "Edytuj cel"
#~ msgid "Update Purposes"
#~ msgstr "Aktualizuj cel"
#~ msgid "Add New Purposes"
#~ msgstr "Dodaj nowy cel"
#~ msgid "New Purpose Name"
#~ msgstr "Nazwa celu"
#~ msgid "Add or remove purposes"
#~ msgstr "Dodaj lub usuń cele"
#~ msgid "Choose from the most used purposes"
#~ msgstr "Wybierz jeden z najczęściej używanych celów"
#~ msgid "No purposes found."
#~ msgstr "Nie znaleziono celów."
#~ msgid "Enable email."
#~ msgstr "Włącz powiadomienie."
#~ msgid "Define prices:"
#~ msgstr "Zdefiniuj ceny"
<phpunit bootstrap="tests/unit/bootstrap.php">
<testsuites>
<testsuite>
<directory prefix="Test_" suffix=".php">./tests/unit/</directory>
<testsuite name="unit">
<directory suffix="Test.php">./tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/templates</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
</phpunit>
<?php
namespace WPDesk\Library\WPEmail\Abstracts;
use Exception;
class Email {
/**
* @var array
*/
private $recipients = [];
/**
* @var string
*/
private $subject = '';
/**
* @var array
*/
private $attachments = [];
/**
* @var string
*/
private $content = '';
/**
* @var array
*/
private $headers = [ 'Content-Type' => 'text/html' ];
/**
* @var string
*/
private $from_email;
/**
* @var string
*/
private $from_name;
/**
* @var array
*/
private $template_attributes;
/**
* @param string $from_email
*
* @return self
*/
public function set_from( string $from_email ): self {
$this->from_email = $from_email;
return $this;
}
/**
* @return string
*/
public function get_from(): string {
return $this->from_email;
}
/**
* @param string $from_name
*
* @return self
*/
public function set_from_name( string $from_name ): self {
$this->from_name = $from_name;
return $this;
}
/**
* @return string
*/
public function get_from_name(): string {
return wp_specialchars_decode( esc_html( $this->from_name ), ENT_QUOTES );
}
/**
* @param string $subject
*
* @return self
*/
public function set_subject( string $subject ): self {
$this->subject = $subject;
return $this;
}
/**
* @return string
* @throws Exception
*/
public function get_subject(): string {
return $this->subject;
}
/**
* @param array $recipients
*
* @return self
*/
public function set_recipients( array $recipients = [] ): self {
$this->recipients = $recipients;
return $this;
}
/**
* @return string[]
*/
public function get_recipients(): array {
return $this->recipients;
}
public function set_header( string $header, string $value ): self {
$this->headers[ $header ] = $value;
return $this;
}
/** @return string[] */
public function get_headers(): array {
$result = [];
foreach ( $this->headers as $header => $value ) {
$result[] = "$header: $value";
}
return $result;
}
public function get_header( string $header ): string {
return $this->headers[ $header ] ?? '';
}
/**
* @param array $attachments
*
* @return self
*/
public function set_attachments( array $attachments ): self {
$this->attachments = $attachments;
return $this;
}
/**
* @return array
*/
public function get_attachments(): array {
return $this->attachments;
}
public function is_html(): bool {
return $this->get_header( 'Content-Type' ) === 'text/html';
}
/**
* @return string
*/
public function set_content_type( $type = 'html' ): self {
switch ( $type ) {
case 'plain':
$content_type = 'text/plain';
break;
case 'multipart':
$content_type = 'multipart/alternative';
break;
default:
$content_type = 'text/html';
}
$this->set_header( 'Content-Type', $content_type );
return $this;
}
/**
* @param string $content
*
* @return self
*/
public function set_content( string $content ): self {
$this->content = $content;
return $this;
}
/**
* @return string
* @throws Exception
*/
public function get_content(): string {
return $this->content;
}
public function set_template_attributes( string $name, string $value ): self {
$this->template_attributes[ $name ] = $value;
return $this;
}
public function get_template_attributes(): array {
return $this->template_attributes;
}
}
<?php
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.
*
* @return string
*/
abstract public function get_id(): string;
/**
* Get defined placeholders.
*
* @return string[]
*/
public function get_placeholders(): array {
return [];
}
/**
* Get email subject.
*
* @return string
*/
public function get_subject(): string {
if ( ! $this->subject ) {
throw new \Exception( 'Empty email subject' );
}
return '';
}
/**
* Get email heading.
*
* @return string
*/
public function get_heading(): string {
return '';
}
/**
* Get valid recipients.
*
* @return string[]
*/
public function get_recipients(): array {
return $this->recipients;
}
/**
* Get email headers.
*
* @return string[]
*/
public function get_headers(): array {
return [];
}
/**
* Get email attachments.
*
* @return array
*/
public function get_attachments(): array {
return [];
}
/**
* Get email type.
*
* @return string
*/
public function get_type(): string {
return 'text/html';
}
/**
* Get email content.
*
* @return string
*/
public function get_content(): string {
if ( ! $this->content ) {
throw new \Exception( 'Empty email subject' );
}
return '';
}
/**
* @return mixed
*/
public function get_object() {
return '';
}
/**
* Get email content.
*
* @return string
*/
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() ] );
}
}
<?php
namespace WPDesk\Library\WPEmail\Abstracts;
interface EmailInterface {
/**
* Define unique email ID.
*
* @return string
*/
public function get_id(): string;
/**
* Get defined placeholders.
*
* @return array
*/
public function get_placeholders(): array;
/**
* Get email subject.
*
* @return string
*/
public function get_subject(): string;
/**
* Get email heading.
*
* @return string
*/
public function get_heading(): string;
/**
* Get valid recipients.
*
* @return string[]
*/
public function get_recipients(): array;
/**
* Get email headers.
*
* @return string[]
*/
public function get_headers(): array;
/**
* Get email attachments.
*
* @return array
*/
public function get_attachments(): array;
/**
* Get email type.
*
* @return string
*/
public function get_type(): string;
/**
* Get email content.
*
* @return string
*/
public function get_content(): string;
}
<?php
namespace WPDesk\Library\WPEmail\Abstracts;
use WPDesk\Library\WPEmail\Exceptions\MailerException;
interface Mailer {
/**
* @throws MailerException
*/
public function send( Email $email ): void;
}
<?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\Emails;
use WPDesk\Library\WPEmail\Abstracts\EmailAbstract;
class Email extends EmailAbstract {
const ID = 'email';
/**
* Define unique email ID.
*
* @return string
*/
public function get_id(): string {
return self::ID;
}
}
<?php
declare( strict_types=1 );
namespace WPDesk\Library\WPEmail\Exceptions;
class MailerException extends \RuntimeException {
public static function with_wp_error( \WP_Error $error ): self {
$errors = $error->get_error_messages( 'wp_mail_failed' );
$message = implode( "\n", $errors );
return new self( sprintf( 'wp_mail() failure. Message [%s]', $message ) );
}
}
<?php
namespace WPDesk\Library\WPEmail\Helpers;
class ColorConversion {
/**
* Determine whether a hex color is light.
*
* @param mixed $color Color.
*
* @return bool True if a light color.
*/
public static function is_hex_light( $color ) {
$hex = str_replace( '#', '', $color );
$c_r = hexdec( substr( $hex, 0, 2 ) );
$c_g = hexdec( substr( $hex, 2, 2 ) );
$c_b = hexdec( substr( $hex, 4, 2 ) );
$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
return $brightness > 155;
}
public static function light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
return self::is_hex_light( $color ) ? $dark : $light;
}
/**
* Convert RGB to HEX.
*
* @param mixed $color Color.
*
* @return array
*/
public static function rgb_from_hex( $color ) {
$color = str_replace( '#', '', $color );
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF".
$color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
$rgb = [];
$rgb['R'] = hexdec( $color[0] . $color[1] );
$rgb['G'] = hexdec( $color[2] . $color[3] );
$rgb['B'] = hexdec( $color[4] . $color[5] );
return $rgb;
}
/**
* Make HEX color darker.
*
* @param mixed $color Color.
* @param int $factor Darker factor.
* Defaults to 30.
*
* @return string
*/
public static function hex_darker( $color, $factor = 30 ) {
$base = self::rgb_from_hex( $color );
$color = '#';
foreach ( $base as $k => $v ) {
$amount = $v / 100;
$amount = self::round( $amount * $factor );
$new_decimal = $v - $amount;
$new_hex_component = dechex( $new_decimal );
if ( strlen( $new_hex_component ) < 2 ) {
$new_hex_component = '0' . $new_hex_component;
}
$color .= $new_hex_component;
}
return $color;
}
/**
* Make HEX color lighter.
*
* @param mixed $color Color.
* @param int $factor Lighter factor.
* Defaults to 30.
*
* @return string
*/
public static function hex_lighter( $color, $factor = 30 ) {
$base = self::rgb_from_hex( $color );
$color = '#';
foreach ( $base as $k => $v ) {
$amount = 255 - $v;
$amount = $amount / 100;
$amount = self::round( $amount * $factor );
$new_decimal = $v + $amount;
$new_hex_component = dechex( $new_decimal );
if ( strlen( $new_hex_component ) < 2 ) {
$new_hex_component = '0' . $new_hex_component;
}
$color .= $new_hex_component;
}
return $color;
}
/**
* @param $val
* @param int $precision
* @param int $mode
*
* @return float
*/
public static function round( $val, int $precision = 0, int $mode = PHP_ROUND_HALF_UP ): float {
if ( ! is_numeric( $val ) ) {
$val = floatval( $val );
}
return round( $val, $precision, $mode );
}
}
<?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 );
}
}
<?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;
}
}
<?php
namespace WPDesk\Library\WPEmail;
use WPDesk\Library\WPEmail\Helpers\StyleInliner;
use WPDesk\View\Renderer\Renderer;
class Template {
/**
* @var Renderer
*/
private $renderer;
/**
* @var array
*/
private $template_attributes;
public function __construct( Renderer $renderer, array $template_attributes ) {
$this->renderer = $renderer;
$this->template_attributes = wp_parse_args( $template_attributes, $this->get_default_template_attributes() );
}
public function get_body( string $content ): string {
$output = $this->renderer->render( 'html/email-header', $this->template_attributes );
$output .= $this->renderer->render( 'html/email-content', [ 'content' => $content ] );
$output .= $this->renderer->render( 'html/email-footer', [ 'footer' => $this->template_attributes['footer'] ] );
return $this->css_inline( $output );
}
/**
* @param string $content
*
* @return mixed|string
*/
public function css_inline( string $content ): string {
$styles = $this->renderer->render( 'html/email-styles', $this->template_attributes );
return StyleInliner::inline( $content, $styles );
}
public function get_default_template_attributes(): array {
return [
'heading' => '',
'logo' => '',
'footer' => '',
'primary' => '#d15291',
'text' => '#303030',
'bg' => '#f9f9f9',
'body' => '#ffffff',
];
}
}
<?php
namespace WPDesk\Library\WPEmail;
use Exception;
use WP_Error;
use WPDesk\Library\WPEmail\Abstracts\Email;
use WPDesk\Library\WPEmail\Abstracts\Mailer;
use WPDesk\Library\WPEmail\Exceptions\MailerException;
use WPDesk\View\Renderer\Renderer;
use WPDesk\View\Renderer\SimplePhpRenderer;
use WPDesk\View\Resolver\ChainResolver;
use WPDesk\View\Resolver\DirResolver;
class WPMailer implements Mailer {
/**
* @var Renderer
*/
private $renderer;
public function __construct() {
$resolver = new ChainResolver();
$resolver->appendResolver( new DirResolver( __DIR__ . '/templates' ) );
$renderer = new SimplePhpRenderer( $resolver );
$this->set_renderer( $renderer );
}
public function set_renderer( Renderer $renderer ) {
$this->renderer = $renderer;
}
public function get_renderer(): Renderer {
return $this->renderer;
}
/** @return void */
public function send( Email $email ): void {
$mailer_from = $email->get_from();
add_filter(
'wp_mail_from',
$from_cb = static function ( $from ) use ( $mailer_from ) {
return $mailer_from;
}
);
$mailer_from_name = $email->get_from_name();
add_filter(
'wp_mail_from_name',
$from_name_cb = static function ( $from_name ) use ( $mailer_from_name ) {
return $mailer_from_name;
}
);
add_action( 'wp_mail_failed', [ $this, 'catch_error' ] );
$email_template = new Template( $this->renderer, $email->get_template_attributes() );
try {
$success = wp_mail(
$email->get_recipients(),
$email->get_subject(),
$email_template->get_body( $email->get_content() ),
$email->get_headers(),
$email->get_attachments()
);
if ( ! $success ) {
throw new MailerException( 'Count not send the mail with wp_mail()' );
}
} catch ( Exception $e ) {
if ( $e instanceof MailerException ) {
throw $e;
}
throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e );
} finally {
remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 );
remove_filter( 'wp_mail_from', $from_cb );
remove_filter( 'wp_mail_from_name', $from_name_cb );
}
}
/** @return void */
public function catch_error( WP_Error $error ) {
throw MailerException::with_wp_error( $error );
}
}
<?php
$params = $params ?? [];
?>
<?php echo wp_kses_post( $params['content'] ); ?>
<?php
/**
* Email Footer
*/
defined( 'ABSPATH' ) || exit;
?>
</div>
</td>
</tr>
</table>
<!-- End Content -->
</td>
</tr>
</table>
<!-- End Body -->
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" valign="top">
<!-- Footer -->
<table border="0" cellpadding="10" cellspacing="0" width="600" id="template_footer">
<tr>
<td valign="top">
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td colspan="2" valign="middle" id="credit">
<?php echo $params['footer'] ?? ''; ?>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- End Footer -->
</td>
</tr>
</table>
</div>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment