forked from Logicify/mautic-advanced-templates-bundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitry Berezovsky
committed
Nov 7, 2018
0 parents
commit 243b59a
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
/vendor |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
return [ | ||
'name' => 'Advanced Templates', | ||
'description' => 'Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc', | ||
'version' => '1.0', | ||
'author' => 'Dmitry Berezovsky', | ||
'services' => [ | ||
'events' => [ | ||
// Register any event listeners | ||
'mautic.plugin.advanced_templates.email.subscriber' => [ | ||
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\EventListener\EmailSubscriber::class, | ||
'arguments' => [ | ||
'mautic.plugin.advanced_templates.helper.template_processor' | ||
], | ||
], | ||
], | ||
'other' => [ | ||
// Template processor | ||
'mautic.plugin.advanced_templates.helper.template_processor' => [ | ||
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor::class, | ||
'tag' => 'advanced_templates', | ||
'arguments' => [ | ||
'monolog.logger.mautic', | ||
], | ||
] | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace MauticPlugin\MauticAdvancedTemplatesBundle\EventListener; | ||
use Mautic\CampaignBundle\Entity\Lead; | ||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||
use Mautic\EmailBundle\EmailEvents; | ||
use Mautic\EmailBundle\Event as Events; | ||
use Mautic\CoreBundle\Exception as MauticException; | ||
use MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Class EmailSubscriber. | ||
*/ | ||
class EmailSubscriber extends CommonSubscriber | ||
{ | ||
/** | ||
* @var TokenHelper $tokenHelper ; | ||
*/ | ||
protected $templateProcessor; | ||
|
||
|
||
/** | ||
* EmailSubscriber constructor. | ||
* | ||
* @param TokenHelper $tokenHelper | ||
*/ | ||
public function __construct(TemplateProcessor $templateProcessor) | ||
{ | ||
$this->templateProcessor = $templateProcessor; | ||
} | ||
/** | ||
* @return array | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0], | ||
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0] | ||
]; | ||
} | ||
|
||
/** | ||
* Search and replace tokens with content | ||
* | ||
* @param Events\EmailSendEvent $event | ||
*/ | ||
public function onEmailGenerate(Events\EmailSendEvent $event) | ||
{ | ||
$this->logger->info('onEmailGenerate MauticAdvancedTemplatesBundle\EmailSubscriber'); | ||
$content = $event->getContent(); | ||
$content = $this->templateProcessor->processTemplate($content, $event->getLead()); | ||
$event->setContent($content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace MauticPlugin\MauticAdvancedTemplatesBundle\Helper; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
class TemplateProcessor | ||
{ | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var \Twig_Environment | ||
*/ | ||
private $twigEnv; | ||
|
||
private static $matchTwigBlockRegex = '/{%\s?TWIG_BLOCK\s?%}(.*?){%\s?END_TWIG_BLOCK\s?%}/ism'; | ||
|
||
/** | ||
* TemplateProcessor constructor. | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
$this->twigEnv = new \Twig_Environment(new \Twig_Loader_Array([])); | ||
$this->configureTwig($this->twigEnv); | ||
} | ||
|
||
|
||
/** | ||
* @param string $content | ||
* @param array $lead | ||
* @return string | ||
* @throws \Throwable | ||
* @throws \Twig_Error_Loader | ||
* @throws \Twig_Error_Syntax | ||
*/ | ||
public function processTemplate($content, $lead) | ||
{ | ||
$this->logger->debug('TemplateProcessor: Processing template'); | ||
$this->logger->debug('LEAD: ' . var_export($lead, true)); | ||
$content = preg_replace_callback_array([ | ||
TemplateProcessor::$matchTwigBlockRegex => $this->processTwigBlock($lead) | ||
], $content); | ||
$this->logger->debug('TemplateProcessor: Template processed'); | ||
return $content; | ||
} | ||
|
||
protected function configureTwig(\Twig_Environment $twig) | ||
{ | ||
// You might want to register some custom TWIG tags or functions here | ||
|
||
// TWIG filter json_decode | ||
$twig->addFilter(new \Twig_SimpleFilter('json_decode', function ($string) { | ||
return json_decode($string, true); | ||
})); | ||
} | ||
|
||
private function processTwigBlock($lead) | ||
{ | ||
return function ($matches) use ($lead) { | ||
$templateSource = $matches[1]; | ||
$this->logger->debug('BLOCK SOURCE: ' . var_export($templateSource, true)); | ||
$template = $this->twigEnv->createTemplate($templateSource); | ||
$renderedTemplate = $template->render([ | ||
'lead' => $lead | ||
]); | ||
$this->logger->debug('RENDERED BLOCK: ' . var_export($renderedTemplate, true)); | ||
return $renderedTemplate; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace MauticPlugin\MauticAdvancedTemplatesBundle; | ||
|
||
use Mautic\PluginBundle\Bundle\PluginBundleBase; | ||
|
||
class MauticAdvancedTemplatesBundle extends PluginBundleBase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "logicify/mautic-advanced-templates-bundle", | ||
"type": "mautic-plugin", | ||
"require": { | ||
"mautic/composer-plugin": "^1.0" | ||
} | ||
} |