diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45b1244 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +/vendor \ No newline at end of file diff --git a/Assets/img/icon.png b/Assets/img/icon.png new file mode 100644 index 0000000..9042bc8 Binary files /dev/null and b/Assets/img/icon.png differ diff --git a/Config/config.php b/Config/config.php new file mode 100644 index 0000000..1ebc54e --- /dev/null +++ b/Config/config.php @@ -0,0 +1,29 @@ + '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', + ], + ] + ], + ], +]; \ No newline at end of file diff --git a/EventListener/EmailSubscriber.php b/EventListener/EmailSubscriber.php new file mode 100644 index 0000000..0a61347 --- /dev/null +++ b/EventListener/EmailSubscriber.php @@ -0,0 +1,55 @@ +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); + } +} \ No newline at end of file diff --git a/Helper/TemplateProcessor.php b/Helper/TemplateProcessor.php new file mode 100644 index 0000000..100858a --- /dev/null +++ b/Helper/TemplateProcessor.php @@ -0,0 +1,76 @@ +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; + }; + } +} \ No newline at end of file diff --git a/MauticAdvancedTemplatesBundle.php b/MauticAdvancedTemplatesBundle.php new file mode 100644 index 0000000..34a1972 --- /dev/null +++ b/MauticAdvancedTemplatesBundle.php @@ -0,0 +1,9 @@ +