Load all headings and display the selected one

[MAILPOET-2282]
This commit is contained in:
Amine Ben hammou
2019-10-18 19:47:50 +01:00
committed by Jack Kitterhing
parent 9949ba986f
commit bd5f18af29
4 changed files with 41 additions and 11 deletions

View File

@ -5,11 +5,11 @@ import App from 'newsletter_editor/App';
import BaseBlock from 'newsletter_editor/blocks/base'; import BaseBlock from 'newsletter_editor/blocks/base';
const BlockModel = BaseBlock.BlockModel.extend({ const BlockModel = BaseBlock.BlockModel.extend({
stale: ['styles.backgroundColor', 'content'], stale: ['styles.backgroundColor', 'contents', 'selected'],
defaults() { defaults() {
return this._getDefaults({ return this._getDefaults({
type: 'woocommerceHeading', type: 'woocommerceHeading',
content: '', selected: 'new_account',
styles: { styles: {
fontColor: '#000000', fontColor: '#000000',
backgroundColor: '#FFFFFF', backgroundColor: '#FFFFFF',
@ -77,6 +77,10 @@ const BlockView = BaseBlock.BlockView.extend({
BaseBlock.BlockView.prototype.initialize.apply(this, arguments); BaseBlock.BlockView.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, 'change:styles.fontColor', this.render); this.listenTo(this.model, 'change:styles.fontColor', this.render);
this.listenTo(this.model, 'change:styles.backgroundColor', this.render); this.listenTo(this.model, 'change:styles.backgroundColor', this.render);
this.listenTo(App.getChannel(), 'changeWCEmailType', (value) => {
this.model.set('selected', value);
this.render();
});
}, },
modelEvents: _.omit(BaseBlock.BlockView.prototype.modelEvents, 'change'), modelEvents: _.omit(BaseBlock.BlockView.prototype.modelEvents, 'change'),
getTemplate() { return window.templates.woocommerceHeadingBlock; }, getTemplate() { return window.templates.woocommerceHeadingBlock; },
@ -92,10 +96,12 @@ const BlockView = BaseBlock.BlockView.extend({
this.showChildView('toolsRegion', this.toolsView); this.showChildView('toolsRegion', this.toolsView);
}, },
templateContext() { templateContext() {
const contents = this.model.get('contents').toJSON();
const selected = this.model.get('selected');
return { return {
viewCid: this.cid, viewCid: this.cid,
model: this.model.toJSON(), model: this.model.toJSON(),
content: this.model.get('content'), content: contents[selected],
styles: this.model.get('styles').toJSON(), styles: this.model.get('styles').toJSON(),
}; };
}, },

View File

@ -73,8 +73,8 @@ class NewsletterEditor {
'sub_menu' => Menu::MAIN_PAGE_SLUG, 'sub_menu' => Menu::MAIN_PAGE_SLUG,
'mss_active' => Bridge::isMPSendingServiceEnabled(), 'mss_active' => Bridge::isMPSendingServiceEnabled(),
'woocommerce' => [ 'woocommerce' => [
'email_headings' => $this->wc_transactional_emails->getEmailHeadings(),
'email_base_color' => $this->wp->getOption('woocommerce_email_base_color', '#000000'), 'email_base_color' => $this->wp->getOption('woocommerce_email_base_color', '#000000'),
'email_heading' => $this->wc_transactional_emails->getEmailHeading(),
'email_text_color' => $this->wp->getOption('woocommerce_email_text_color', '#000000'), 'email_text_color' => $this->wp->getOption('woocommerce_email_text_color', '#000000'),
], ],
]; ];

View File

@ -16,9 +16,29 @@ class TransactionalEmails {
/** @var SettingsController */ /** @var SettingsController */
private $settings; private $settings;
private $email_headings;
function __construct(WPFunctions $wp, SettingsController $settings) { function __construct(WPFunctions $wp, SettingsController $settings) {
$this->wp = $wp; $this->wp = $wp;
$this->settings = $settings; $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() { public function init() {
@ -34,13 +54,17 @@ class TransactionalEmails {
} }
} }
public function getEmailHeading() { public function getEmailHeadings() {
$default_heading = __('Thank you for your order', 'woocommerce'); $values = [];
$settings = $this->wp->getOption('woocommerce_customer_processing_order_settings'); foreach ($this->email_headings as $name => $heading) {
if (!$settings) { $settings = $this->wp->getOption($heading['option_name']);
return $default_heading; if (!$settings) {
$values[$name] = $heading['default'];
} else {
$values[$name] = $this->replacePlaceholders($settings['heading'] ?: $heading['default']);
}
} }
return $this->replacePlaceholders($settings['heading'] ?: $default_heading); return $values;
} }
private function replacePlaceholders($text) { private function replacePlaceholders($text) {

View File

@ -1435,7 +1435,7 @@
}, },
}, },
woocommerceHeading: { woocommerceHeading: {
content: '<%= woocommerce.email_heading %>', contents: <%= json_encode(woocommerce.email_headings) %>,
styles: { styles: {
fontColor: '<%= woocommerce.email_text_color %>', fontColor: '<%= woocommerce.email_text_color %>',
backgroundColor: '<%= woocommerce.email_base_color %>', backgroundColor: '<%= woocommerce.email_base_color %>',