Files
piratepoet/lib/WooCommerce/TransactionalEmails.php
Amine Ben hammou 9f594f7666 Add check
[MAILPOET-2282]
2019-11-13 10:44:27 +00:00

809 lines
24 KiB
PHP

<?php
namespace MailPoet\WooCommerce;
use MailPoet\Config\Env;
use MailPoet\Models\Newsletter;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions;
class TransactionalEmails {
const SETTING_EMAIL_ID = 'woocommerce.transactional_email_id';
/** @var WPFunctions */
private $wp;
/** @var SettingsController */
private $settings;
private $email_headings;
function __construct(WPFunctions $wp, SettingsController $settings) {
$this->wp = $wp;
$this->settings = $settings;
$this->email_headings = [
'new_account' => [
'option_name' => 'woocommerce_new_order_settings',
'default' => __('New Order: #{order_number}', 'woocommerce'),
],
'processing_order' => [
'option_name' => 'woocommerce_customer_processing_order_settings',
'default' => __('Thank you for your order', 'woocommerce'),
],
'completed_order' => [
'option_name' => 'woocommerce_customer_completed_order_settings',
'default' => __('Thanks for shopping with us', 'woocommerce'),
],
'customer_note' => [
'option_name' => 'woocommerce_customer_note_settings',
'default' => __('A note has been added to your order', 'woocommerce'),
],
];
}
public function init() {
$saved_email_id = (bool)$this->settings->get(self::SETTING_EMAIL_ID, false);
if (!$saved_email_id) {
$email = Newsletter::createOrUpdate([
'type' => Newsletter::TYPE_WC_TRANSACTIONAL_EMAIL,
'subject' => 'WooCommerce Transactional Email',
'preheader' => '',
'body' => json_encode($this->getBody()),
]);
$this->settings->set(self::SETTING_EMAIL_ID, $email->id);
}
}
public function getEmailHeadings() {
$values = [];
foreach ($this->email_headings as $name => $heading) {
$settings = $this->wp->getOption($heading['option_name']);
if (!$settings) {
$values[$name] = $this->replacePlaceholders($heading['default']);
} else {
$value = isset($settings['heading']) ? $settings['heading'] : $heading['default'];
$values[$name] = $this->replacePlaceholders($value);
}
}
return $values;
}
private function replacePlaceholders($text) {
$title = $this->wp->wpSpecialcharsDecode($this->wp->getOption('blogname'), ENT_QUOTES);
$address = $this->wp->wpParseUrl($this->wp->homeUrl(), PHP_URL_HOST);
$order_date = date('Y-m-d');
return str_replace(
['{site_title}','{site_address}', '{order_date}', '{order_number}'],
[$title, $address, $order_date, '0001'],
$text
);
}
private function getWCEmailSettings() {
$wc_email_settings = [
'woocommerce_email_background_color' => '#ffffff',
'woocommerce_email_base_color' => '#333333',
'woocommerce_email_body_background_color' => '#eeeeee',
'woocommerce_email_footer_text' => $this->wp->_x('Footer text', 'Default footer text for a WooCommerce transactional email', 'mailpoet'),
'woocommerce_email_header_image' => '',
'woocommerce_email_text_color' => '#111111',
];
$result = [];
foreach ($wc_email_settings as $name => $default) {
$value = $this->wp->getOption($name, $default);
$key = preg_replace('/^woocommerce_email_/', '', $name);
$result[$key] = $value;
}
$result['footer_text'] = $this->replacePlaceholders($result['footer_text']);
return $result;
}
private function getBody() {
$social_icon_url = Env::$assets_url . '/img/newsletter_editor/social-icons';
$wc_email_settings = $this->getWCEmailSettings();
return [
'content' =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'vertical',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'horizontal',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => '#eeeeee',
],
],
'blocks' =>
[
0 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'vertical',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'spacer',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
],
1 =>
[
'type' => 'image',
'link' => '',
'src' => $wc_email_settings['header_image'],
'alt' => 'mailpoet-logo',
'fullWidth' => false,
'width' => '160px',
'height' => '490px',
'styles' =>
[
'block' =>
[
'textAlign' => 'center',
],
],
],
2 =>
[
'type' => 'spacer',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
],
],
],
],
],
1 =>
[
'type' => 'woocommerceHeading',
],
2 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'horizontal',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'vertical',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'spacer',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
],
],
],
],
],
3 =>
[
'type' => 'woocommerceContent',
],
4 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'horizontal',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => '#ffffff',
],
],
'blocks' =>
[
0 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'vertical',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'spacer',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
],
],
],
],
],
5 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'horizontal',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => '#eeeeee',
],
],
'blocks' =>
[
0 =>
[
'type' => 'container',
'columnLayout' => false,
'orientation' => 'vertical',
'image' =>
[
'src' => null,
'display' => 'scale',
],
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
],
'blocks' =>
[
0 =>
[
'type' => 'spacer',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
],
1 =>
[
'type' => 'text',
'text' => '<p style="text-align: center;">' . $wc_email_settings['footer_text'] . '</p>',
],
],
],
],
],
],
],
'globalStyles' =>
[
'text' =>
[
'fontColor' => $wc_email_settings['text_color'],
'fontFamily' => 'Arial',
'fontSize' => '16px',
'lineHeight' => '1.6',
],
'h1' =>
[
'fontColor' => $wc_email_settings['base_color'],
'fontFamily' => 'Source Sans Pro',
'fontSize' => '36px',
'lineHeight' => '1.6',
],
'h2' =>
[
'fontColor' => $wc_email_settings['base_color'],
'fontFamily' => 'Verdana',
'fontSize' => '24px',
'lineHeight' => '1.6',
],
'h3' =>
[
'fontColor' => $wc_email_settings['base_color'],
'fontFamily' => 'Trebuchet MS',
'fontSize' => '22px',
'lineHeight' => '1.6',
],
'link' =>
[
'fontColor' => '#21759B',
'textDecoration' => 'underline',
],
'wrapper' =>
[
'backgroundColor' => $wc_email_settings['background_color'],
],
'body' =>
[
'backgroundColor' => $wc_email_settings['body_background_color'],
],
],
'blockDefaults' =>
[
'automatedLatestContent' =>
[
'amount' => '5',
'withLayout' => false,
'contentType' => 'post',
'inclusionType' => 'include',
'displayType' => 'excerpt',
'titleFormat' => 'h1',
'titleAlignment' => 'left',
'titleIsLink' => false,
'imageFullWidth' => false,
'featuredImagePosition' => 'belowTitle',
'showAuthor' => 'no',
'authorPrecededBy' => 'Author:',
'showCategories' => 'no',
'categoriesPrecededBy' => 'Categories:',
'readMoreType' => 'button',
'readMoreText' => 'Read more',
'readMoreButton' =>
[
'text' => 'Read more',
'url' => '[postLink]',
'context' => 'automatedLatestContent.readMoreButton',
'styles' =>
[
'block' =>
[
'backgroundColor' => '#2ea1cd',
'borderColor' => '#0074a2',
'borderWidth' => '1px',
'borderRadius' => '5px',
'borderStyle' => 'solid',
'width' => '180px',
'lineHeight' => '40px',
'fontColor' => '#ffffff',
'fontFamily' => 'Verdana',
'fontSize' => '18px',
'fontWeight' => 'normal',
'textAlign' => 'center',
],
],
],
'sortBy' => 'newest',
'showDivider' => true,
'divider' =>
[
'context' => 'automatedLatestContent.divider',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'padding' => '13px',
'borderStyle' => 'solid',
'borderWidth' => '3px',
'borderColor' => '#aaaaaa',
],
],
],
'backgroundColor' => '#ffffff',
'backgroundColorAlternate' => '#eeeeee',
],
'automatedLatestContentLayout' =>
[
'amount' => '5',
'withLayout' => true,
'contentType' => 'post',
'inclusionType' => 'include',
'displayType' => 'excerpt',
'titleFormat' => 'h1',
'titleAlignment' => 'left',
'titleIsLink' => false,
'imageFullWidth' => false,
'featuredImagePosition' => 'alternate',
'showAuthor' => 'no',
'authorPrecededBy' => 'Author:',
'showCategories' => 'no',
'categoriesPrecededBy' => 'Categories:',
'readMoreType' => 'button',
'readMoreText' => 'Read more',
'readMoreButton' =>
[
'text' => 'Read more',
'url' => '[postLink]',
'context' => 'automatedLatestContentLayout.readMoreButton',
'styles' =>
[
'block' =>
[
'backgroundColor' => '#2ea1cd',
'borderColor' => '#0074a2',
'borderWidth' => '1px',
'borderRadius' => '5px',
'borderStyle' => 'solid',
'width' => '180px',
'lineHeight' => '40px',
'fontColor' => '#ffffff',
'fontFamily' => 'Verdana',
'fontSize' => '18px',
'fontWeight' => 'normal',
'textAlign' => 'center',
],
],
],
'sortBy' => 'newest',
'showDivider' => true,
'divider' =>
[
'context' => 'automatedLatestContentLayout.divider',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'padding' => '13px',
'borderStyle' => 'solid',
'borderWidth' => '3px',
'borderColor' => '#aaaaaa',
],
],
],
'backgroundColor' => '#ffffff',
'backgroundColorAlternate' => '#eeeeee',
],
'button' =>
[
'text' => 'Button',
'url' => '',
'styles' =>
[
'block' =>
[
'backgroundColor' => '#2ea1cd',
'borderColor' => '#0074a2',
'borderWidth' => '1px',
'borderRadius' => '5px',
'borderStyle' => 'solid',
'width' => '180px',
'lineHeight' => '40px',
'fontColor' => '#ffffff',
'fontFamily' => 'Verdana',
'fontSize' => '18px',
'fontWeight' => 'normal',
'textAlign' => 'center',
],
],
],
'divider' =>
[
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'padding' => '13px',
'borderStyle' => 'solid',
'borderWidth' => '3px',
'borderColor' => '#aaaaaa',
],
],
],
'footer' =>
[
'text' => '<p><a href="[link:subscription_unsubscribe_url]">Unsubscribe</a> | <a href="[link:subscription_manage_url]">Manage subscription</a><br />Add your postal address here!</p>',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
'text' =>
[
'fontColor' => '#222222',
'fontFamily' => 'Arial',
'fontSize' => '12px',
'textAlign' => 'center',
],
'link' =>
[
'fontColor' => '#6cb7d4',
'textDecoration' => 'none',
],
],
],
'posts' =>
[
'amount' => '10',
'withLayout' => true,
'contentType' => 'post',
'postStatus' => 'publish',
'inclusionType' => 'include',
'displayType' => 'excerpt',
'titleFormat' => 'h1',
'titleAlignment' => 'left',
'titleIsLink' => false,
'imageFullWidth' => false,
'featuredImagePosition' => 'alternate',
'showAuthor' => 'no',
'authorPrecededBy' => 'Author:',
'showCategories' => 'no',
'categoriesPrecededBy' => 'Categories:',
'readMoreType' => 'link',
'readMoreText' => 'Read more',
'readMoreButton' =>
[
'text' => 'Read more',
'url' => '[postLink]',
'context' => 'posts.readMoreButton',
'styles' =>
[
'block' =>
[
'backgroundColor' => '#2ea1cd',
'borderColor' => '#0074a2',
'borderWidth' => '1px',
'borderRadius' => '5px',
'borderStyle' => 'solid',
'width' => '180px',
'lineHeight' => '40px',
'fontColor' => '#ffffff',
'fontFamily' => 'Verdana',
'fontSize' => '18px',
'fontWeight' => 'normal',
'textAlign' => 'center',
],
],
],
'sortBy' => 'newest',
'showDivider' => true,
'divider' =>
[
'context' => 'posts.divider',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'padding' => '13px',
'borderStyle' => 'solid',
'borderWidth' => '3px',
'borderColor' => '#aaaaaa',
],
],
],
'backgroundColor' => '#ffffff',
'backgroundColorAlternate' => '#eeeeee',
],
'products' =>
[
'amount' => '10',
'withLayout' => true,
'contentType' => 'product',
'postStatus' => 'publish',
'inclusionType' => 'include',
'displayType' => 'excerpt',
'titleFormat' => 'h1',
'titleAlignment' => 'left',
'titleIsLink' => false,
'imageFullWidth' => false,
'featuredImagePosition' => 'alternate',
'pricePosition' => 'below',
'readMoreType' => 'link',
'readMoreText' => 'Buy now',
'readMoreButton' =>
[
'text' => 'Buy now',
'url' => '[postLink]',
'context' => 'posts.readMoreButton',
'styles' =>
[
'block' =>
[
'backgroundColor' => '#2ea1cd',
'borderColor' => '#0074a2',
'borderWidth' => '1px',
'borderRadius' => '5px',
'borderStyle' => 'solid',
'width' => '180px',
'lineHeight' => '40px',
'fontColor' => '#ffffff',
'fontFamily' => 'Verdana',
'fontSize' => '18px',
'fontWeight' => 'normal',
'textAlign' => 'center',
],
],
],
'sortBy' => 'newest',
'showDivider' => true,
'divider' =>
[
'context' => 'posts.divider',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'padding' => '13px',
'borderStyle' => 'solid',
'borderWidth' => '3px',
'borderColor' => '#aaaaaa',
],
],
],
'backgroundColor' => '#ffffff',
'backgroundColorAlternate' => '#eeeeee',
],
'social' =>
[
'iconSet' => 'default',
'styles' =>
[
'block' =>
[
'textAlign' => 'center',
],
],
'icons' =>
[
0 =>
[
'type' => 'socialIcon',
'iconType' => 'facebook',
'link' => 'http://www.facebook.com',
'image' => $social_icon_url . '/01-social/Facebook.png',
'height' => '32px',
'width' => '32px',
'text' => 'Facebook',
],
1 =>
[
'type' => 'socialIcon',
'iconType' => 'twitter',
'link' => 'http://www.twitter.com',
'image' => $social_icon_url . '/01-social/Twitter.png',
'height' => '32px',
'width' => '32px',
'text' => 'Twitter',
],
],
],
'spacer' =>
[
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
'height' => '20px',
],
],
'type' => 'spacer',
],
'header' =>
[
'text' => 'Display problems?&nbsp;<a href="[link:newsletter_view_in_browser_url]">Open this email in your web browser.</a>',
'styles' =>
[
'block' =>
[
'backgroundColor' => 'transparent',
],
'text' =>
[
'fontColor' => '#222222',
'fontFamily' => 'Arial',
'fontSize' => '12px',
'textAlign' => 'center',
],
'link' =>
[
'fontColor' => '#6cb7d4',
'textDecoration' => 'underline',
],
],
],
],
];
}
}