Email templates now use Markdown
Move mail template management to MailManager
This commit is contained in:
parent
3b7330f06a
commit
5a4e2b6b11
|
|
@ -5,7 +5,7 @@ use Backend;
|
|||
use BackendMenu;
|
||||
use BackendAuth;
|
||||
use Backend\Classes\WidgetManager;
|
||||
use System\Models\MailTemplate;
|
||||
use System\Classes\MailManager;
|
||||
use System\Classes\CombineAssets;
|
||||
use System\Classes\SettingsManager;
|
||||
use October\Rain\Support\ModuleServiceProvider;
|
||||
|
|
@ -51,7 +51,7 @@ class ServiceProvider extends ModuleServiceProvider
|
|||
*/
|
||||
protected function registerMailer()
|
||||
{
|
||||
MailTemplate::registerCallback(function ($template) {
|
||||
MailManager::instance()->registerCallback(function ($template) {
|
||||
$template->registerMailTemplates([
|
||||
'backend::mail.invite' => 'Invitation for newly created administrators.',
|
||||
'backend::mail.restore' => 'Password reset instructions for backend-end administrators.',
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use BackendMenu;
|
|||
use BackendAuth;
|
||||
use Twig_Environment;
|
||||
use Twig_Loader_String;
|
||||
use System\Classes\MailManager;
|
||||
use System\Classes\ErrorHandler;
|
||||
use System\Classes\MarkupManager;
|
||||
use System\Classes\PluginManager;
|
||||
|
|
@ -299,7 +300,7 @@ class ServiceProvider extends ModuleServiceProvider
|
|||
* Override standard Mailer content with template
|
||||
*/
|
||||
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) {
|
||||
MailTemplate::addContentToMailer($message, $view, $data);
|
||||
MailManager::instance()->addContentToMailer($message, $view, $data);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,159 @@
|
|||
<?php namespace System\Classes;
|
||||
|
||||
use Twig;
|
||||
use Markdown;
|
||||
use System\Models\MailTemplate;
|
||||
use System\Helpers\View as ViewHelper;
|
||||
use System\Classes\PluginManager;
|
||||
|
||||
/**
|
||||
* This class manages Mail sending functions
|
||||
*
|
||||
* @package october\system
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
*/
|
||||
class MailManager
|
||||
{
|
||||
use \October\Rain\Support\Traits\Singleton;
|
||||
|
||||
/**
|
||||
* @var array Cache of registration callbacks.
|
||||
*/
|
||||
protected $callbacks = [];
|
||||
|
||||
/**
|
||||
* @var array A cache of customised mail templates.
|
||||
*/
|
||||
protected $templateCache = [];
|
||||
|
||||
/**
|
||||
* @var array List of registered templates in the system
|
||||
*/
|
||||
protected $registeredTemplates;
|
||||
|
||||
/**
|
||||
* This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer`
|
||||
* class, using the `mailer.beforeAddContent` event.
|
||||
*/
|
||||
public function addContentToMailer($message, $code, $data)
|
||||
{
|
||||
if (isset($this->templateCache[$code])) {
|
||||
$template = $this->templateCache[$code];
|
||||
}
|
||||
else {
|
||||
$this->templateCache[$code] = $template = MailTemplate::findOrMakeTemplate($code);
|
||||
}
|
||||
|
||||
/*
|
||||
* Inject global view variables
|
||||
*/
|
||||
$globalVars = ViewHelper::getGlobalVars();
|
||||
if (!empty($globalVars)) {
|
||||
$data = (array) $data + $globalVars;
|
||||
}
|
||||
|
||||
/*
|
||||
* Subject
|
||||
*/
|
||||
$customSubject = $message->getSwiftMessage()->getSubject();
|
||||
if (empty($customSubject)) {
|
||||
$message->subject(Twig::parse($template->subject, $data));
|
||||
}
|
||||
|
||||
/*
|
||||
* HTML contents
|
||||
*/
|
||||
$templateHtml = Markdown::parse($template->content_html);
|
||||
|
||||
$html = Twig::parse($templateHtml, $data);
|
||||
if ($template->layout) {
|
||||
$html = Twig::parse($template->layout->content_html, [
|
||||
'content' => $html,
|
||||
'css' => $template->layout->content_css
|
||||
] + (array) $data);
|
||||
}
|
||||
|
||||
$message->setBody($html, 'text/html');
|
||||
|
||||
/*
|
||||
* Text contents
|
||||
*/
|
||||
if (strlen($template->content_text)) {
|
||||
$text = Twig::parse($template->content_text, $data);
|
||||
if ($template->layout) {
|
||||
$text = Twig::parse($template->layout->content_text, [
|
||||
'content' => $text
|
||||
] + (array) $data);
|
||||
}
|
||||
|
||||
$message->addPart($text, 'text/plain');
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Registration
|
||||
//
|
||||
|
||||
/**
|
||||
* Loads registered mail templates from modules and plugins
|
||||
* @return void
|
||||
*/
|
||||
public function loadRegisteredTemplates()
|
||||
{
|
||||
foreach ($this->callbacks as $callback) {
|
||||
$callback($this);
|
||||
}
|
||||
|
||||
$plugins = PluginManager::instance()->getPlugins();
|
||||
foreach ($plugins as $pluginId => $pluginObj) {
|
||||
$templates = $pluginObj->registerMailTemplates();
|
||||
if (!is_array($templates)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->registerMailTemplates($templates);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the registered templates.
|
||||
* @return array
|
||||
*/
|
||||
public function listRegisteredTemplates()
|
||||
{
|
||||
if ($this->registeredTemplates === null) {
|
||||
$this->loadRegisteredTemplates();
|
||||
}
|
||||
|
||||
return $this->registeredTemplates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a callback function that defines mail templates.
|
||||
* The callback function should register templates by calling the manager's
|
||||
* registerMailTemplates() function. Thi instance is passed to the
|
||||
* callback function as an argument. Usage:
|
||||
*
|
||||
* MailManager::registerCallback(function($manager) {
|
||||
* $manager->registerMailTemplates([...]);
|
||||
* });
|
||||
*
|
||||
* @param callable $callback A callable function.
|
||||
*/
|
||||
public function registerCallback(callable $callback)
|
||||
{
|
||||
$this->callbacks[] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers mail views and manageable templates.
|
||||
*/
|
||||
public function registerMailTemplates(array $definitions)
|
||||
{
|
||||
if (!$this->registeredTemplates) {
|
||||
$this->registeredTemplates = [];
|
||||
}
|
||||
|
||||
$this->registeredTemplates = array_merge($this->registeredTemplates, $definitions);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ use System\Classes\PluginManager;
|
|||
/**
|
||||
* This class manages Twig functions, token parsers and filters.
|
||||
*
|
||||
* @package october\cms
|
||||
* @package october\system
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
*/
|
||||
class MarkupManager
|
||||
|
|
|
|||
|
|
@ -2,12 +2,10 @@
|
|||
|
||||
use App;
|
||||
use File;
|
||||
use Twig;
|
||||
use View;
|
||||
use Model;
|
||||
use System\Classes\MailManager;
|
||||
use October\Rain\Mail\MailParser;
|
||||
use System\Classes\PluginManager;
|
||||
use System\Helpers\View as ViewHelper;
|
||||
|
||||
/**
|
||||
* Mail template
|
||||
|
|
@ -35,25 +33,13 @@ class MailTemplate extends Model
|
|||
'layout' => MailLayout::class
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array A cache of customised mail templates.
|
||||
*/
|
||||
protected static $cache = [];
|
||||
|
||||
/**
|
||||
* @var array Cache of registration callbacks.
|
||||
*/
|
||||
private static $callbacks = [];
|
||||
|
||||
protected static $registeredTemplates;
|
||||
|
||||
/**
|
||||
* Returns an array of template codes and descriptions.
|
||||
* @return array
|
||||
*/
|
||||
public static function listAllTemplates()
|
||||
{
|
||||
$fileTemplates = (array) self::make()->listRegisteredTemplates();
|
||||
$fileTemplates = (array) MailManager::instance()->listRegisteredTemplates();
|
||||
$dbTemplates = (array) self::lists('description', 'code');
|
||||
$templates = $fileTemplates + $dbTemplates;
|
||||
ksort($templates);
|
||||
|
|
@ -66,7 +52,7 @@ class MailTemplate extends Model
|
|||
*/
|
||||
public static function syncAll()
|
||||
{
|
||||
$templates = self::make()->listRegisteredTemplates();
|
||||
$templates = MailManager::instance()->listRegisteredTemplates();
|
||||
$dbTemplates = self::lists('is_custom', 'code');
|
||||
$newTemplates = array_diff_key($templates, $dbTemplates);
|
||||
|
||||
|
|
@ -133,123 +119,13 @@ class MailTemplate extends Model
|
|||
return $template;
|
||||
}
|
||||
|
||||
public static function addContentToMailer($message, $code, $data)
|
||||
{
|
||||
if (isset(self::$cache[$code])) {
|
||||
$template = self::$cache[$code];
|
||||
}
|
||||
else {
|
||||
self::$cache[$code] = $template = self::findOrMakeTemplate($code);
|
||||
}
|
||||
|
||||
/*
|
||||
* Inject global view variables
|
||||
*/
|
||||
$globalVars = ViewHelper::getGlobalVars();
|
||||
if (!empty($globalVars)) {
|
||||
$data = (array) $data + $globalVars;
|
||||
}
|
||||
|
||||
/*
|
||||
* Subject
|
||||
*/
|
||||
$customSubject = $message->getSwiftMessage()->getSubject();
|
||||
if (empty($customSubject)) {
|
||||
$message->subject(Twig::parse($template->subject, $data));
|
||||
}
|
||||
|
||||
/*
|
||||
* HTML contents
|
||||
*/
|
||||
$html = Twig::parse($template->content_html, $data);
|
||||
if ($template->layout) {
|
||||
$html = Twig::parse($template->layout->content_html, [
|
||||
'content' => $html,
|
||||
'css' => $template->layout->content_css
|
||||
] + (array) $data);
|
||||
}
|
||||
|
||||
$message->setBody($html, 'text/html');
|
||||
|
||||
/*
|
||||
* Text contents
|
||||
*/
|
||||
if (strlen($template->content_text)) {
|
||||
$text = Twig::parse($template->content_text, $data);
|
||||
if ($template->layout) {
|
||||
$text = Twig::parse($template->layout->content_text, [
|
||||
'content' => $text
|
||||
] + (array) $data);
|
||||
}
|
||||
|
||||
$message->addPart($text, 'text/plain');
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Registration
|
||||
//
|
||||
|
||||
/**
|
||||
* Loads registered mail templates from modules and plugins
|
||||
* @return void
|
||||
*/
|
||||
public function loadRegisteredTemplates()
|
||||
{
|
||||
foreach (static::$callbacks as $callback) {
|
||||
$callback($this);
|
||||
}
|
||||
|
||||
$plugins = PluginManager::instance()->getPlugins();
|
||||
foreach ($plugins as $pluginId => $pluginObj) {
|
||||
$templates = $pluginObj->registerMailTemplates();
|
||||
if (!is_array($templates)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->registerMailTemplates($templates);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the registered templates.
|
||||
* @return array
|
||||
*/
|
||||
public function listRegisteredTemplates()
|
||||
{
|
||||
if (self::$registeredTemplates === null) {
|
||||
$this->loadRegisteredTemplates();
|
||||
}
|
||||
|
||||
return self::$registeredTemplates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a callback function that defines mail templates.
|
||||
* The callback function should register templates by calling the manager's
|
||||
* registerMailTemplates() function. Thi instance is passed to the
|
||||
* callback function as an argument. Usage:
|
||||
*
|
||||
* MailTemplate::registerCallback(function($template){
|
||||
* $template->registerMailTemplates([...]);
|
||||
* });
|
||||
*
|
||||
* @param callable $callback A callable function.
|
||||
* @deprecated see System\Classes\MailManager::registerCallback
|
||||
* Remove if year >= 2019
|
||||
*/
|
||||
public static function registerCallback(callable $callback)
|
||||
{
|
||||
self::$callbacks[] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers mail views and manageable templates.
|
||||
*/
|
||||
public function registerMailTemplates(array $definitions)
|
||||
{
|
||||
if (!static::$registeredTemplates) {
|
||||
static::$registeredTemplates = [];
|
||||
}
|
||||
|
||||
static::$registeredTemplates = array_merge(static::$registeredTemplates, $definitions);
|
||||
traceLog('MailTemplate::registerCallback is deprecated, use System\Classes\MailManager::registerCallback instead');
|
||||
MailManager::instance()->registerCallback($callback);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ secondaryTabs:
|
|||
fields:
|
||||
|
||||
content_html:
|
||||
type: codeeditor
|
||||
type: markdown
|
||||
size: giant
|
||||
language: html
|
||||
tab: system::lang.mail_templates.content_html
|
||||
|
|
|
|||
Loading…
Reference in New Issue