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';
const BlockModel = BaseBlock.BlockModel.extend({
stale: ['styles.backgroundColor', 'content'],
stale: ['styles.backgroundColor', 'contents', 'selected'],
defaults() {
return this._getDefaults({
type: 'woocommerceHeading',
content: '',
selected: 'new_account',
styles: {
fontColor: '#000000',
backgroundColor: '#FFFFFF',
@ -77,6 +77,10 @@ const BlockView = BaseBlock.BlockView.extend({
BaseBlock.BlockView.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, 'change:styles.fontColor', 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'),
getTemplate() { return window.templates.woocommerceHeadingBlock; },
@ -92,10 +96,12 @@ const BlockView = BaseBlock.BlockView.extend({
this.showChildView('toolsRegion', this.toolsView);
},
templateContext() {
const contents = this.model.get('contents').toJSON();
const selected = this.model.get('selected');
return {
viewCid: this.cid,
model: this.model.toJSON(),
content: this.model.get('content'),
content: contents[selected],
styles: this.model.get('styles').toJSON(),
};
},

View File

@ -73,8 +73,8 @@ class NewsletterEditor {
'sub_menu' => Menu::MAIN_PAGE_SLUG,
'mss_active' => Bridge::isMPSendingServiceEnabled(),
'woocommerce' => [
'email_headings' => $this->wc_transactional_emails->getEmailHeadings(),
'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'),
],
];

View File

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

View File

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