This commit changes the WC action that MailPoet uses to hook into WC and customize the footer and the header of WC e-mails. Before, MailPoet was using the woommerce_init action. The problem with using this action to customize the e-mails is that it runs on every request. Furthermore, since we were calling WC->mailer() inside the callback, we were instantiating WC_Emails on every single request (WooCommerce instantiate this class only when needed). Replacing woocommerce_init with woocommerce_email means that our code will run only when needed (right after WooCommerce adds callbacks to the actions woocommerce_email_header and woocommerce_email_footer) and that we won't be unnecessarily instantiating WC_Emails ourselves. We get the singleton instance of this class as a parameter. [MAILPOET-3421]
76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace MailPoet\WooCommerce;
|
|
|
|
use MailPoet\Entities\NewsletterEntity;
|
|
use MailPoet\Models\Newsletter;
|
|
use MailPoet\Settings\SettingsController;
|
|
use MailPoet\WooCommerce\TransactionalEmails\Renderer;
|
|
use MailPoet\WP\Functions as WPFunctions;
|
|
|
|
class TransactionalEmailHooks {
|
|
/** @var WPFunctions */
|
|
private $wp;
|
|
|
|
/** @var SettingsController */
|
|
private $settings;
|
|
|
|
/** @var Renderer */
|
|
private $renderer;
|
|
|
|
public function __construct(
|
|
WPFunctions $wp,
|
|
SettingsController $settings,
|
|
Renderer $renderer
|
|
) {
|
|
$this->wp = $wp;
|
|
$this->settings = $settings;
|
|
$this->renderer = $renderer;
|
|
}
|
|
|
|
public function useTemplateForWoocommerceEmails() {
|
|
$this->wp->addAction('woocommerce_email', function($wcEmails) {
|
|
/** @var callable */
|
|
$emailHeaderCallback = [$wcEmails, 'email_header'];
|
|
/** @var callable */
|
|
$emailFooterCallback = [$wcEmails, 'email_footer'];
|
|
$this->wp->removeAction('woocommerce_email_header', $emailHeaderCallback);
|
|
$this->wp->removeAction('woocommerce_email_footer', $emailFooterCallback);
|
|
$this->wp->addAction('woocommerce_email_header', function($emailHeading) {
|
|
$this->renderer->render($this->getNewsletter(), $emailHeading);
|
|
echo $this->renderer->getHTMLBeforeContent($emailHeading);
|
|
});
|
|
$this->wp->addAction('woocommerce_email_footer', function() {
|
|
echo $this->renderer->getHTMLAfterContent();
|
|
});
|
|
$this->wp->addAction('woocommerce_email_styles', [$this->renderer, 'prefixCss']);
|
|
});
|
|
}
|
|
|
|
private function getNewsletter() {
|
|
return Newsletter::findOne($this->settings->get(TransactionalEmails::SETTING_EMAIL_ID));
|
|
}
|
|
|
|
public function enableEmailSettingsSyncToWooCommerce() {
|
|
$this->wp->addFilter('mailpoet_api_newsletters_save_after', [$this, 'syncEmailSettingsToWooCommerce']);
|
|
}
|
|
|
|
public function syncEmailSettingsToWooCommerce(array $newsletterData) {
|
|
if ($newsletterData['type'] !== NewsletterEntity::TYPE_WC_TRANSACTIONAL_EMAIL) {
|
|
return $newsletterData;
|
|
}
|
|
|
|
$styles = $newsletterData['body']['globalStyles'];
|
|
$optionsToSync = [
|
|
'woocommerce_email_background_color' => $styles['body']['backgroundColor'],
|
|
'woocommerce_email_base_color' => $styles['woocommerce']['brandingColor'],
|
|
'woocommerce_email_body_background_color' => $styles['wrapper']['backgroundColor'],
|
|
'woocommerce_email_text_color' => $styles['text']['fontColor'],
|
|
];
|
|
foreach ($optionsToSync as $wcName => $value) {
|
|
$this->wp->updateOption($wcName, $value);
|
|
}
|
|
return $newsletterData;
|
|
}
|
|
}
|