File size: 2,051 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace Mautic\CoreBundle\Twig\Helper;

use Mautic\CoreBundle\CoreEvents;
use Mautic\CoreBundle\Event\CustomContentEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

final class ContentHelper
{
    public function __construct(
        private Environment $twig,
        private EventDispatcherInterface $dispatcher
    ) {
    }

    /**
     * Dispatch an event to collect custom content.
     *
     * @param string|null          $context  Context of the content requested for the viewName
     * @param array<string,string> $vars     twig vars
     * @param string|null          $viewName The main identifier for the content requested. Will be etracted from $vars if get_
     *defined
     */
    public function getCustomContent($context = null, array $vars = [], $viewName = null): string
    {
        if (null === $viewName && isset($vars['mauticTemplate'])) {
            $viewName = $vars['mauticTemplate'];
        }

        /** @var CustomContentEvent $event */
        $event = $this->dispatcher->dispatch(
            new CustomContentEvent($viewName, $context, $vars),
            CoreEvents::VIEW_INJECT_CUSTOM_CONTENT
        );

        $content = $event->getContent();

        if ($templatProps = $event->getTemplates()) {
            foreach ($templatProps as $props) {
                $content[] = $this->twig->render($props['template'], array_merge($vars, $props['vars']));
            }
        }

        return implode("\n\n", $content);
    }

    /**
     * Replaces HTML script tags with non HTML tags so the JS inside them won't execute and will be readable.
     *
     * @param string $html
     */
    public function showScriptTags($html): string
    {
        $tagsToShow = ['script', 'style'];

        foreach ($tagsToShow as $tag) {
            $html = preg_replace('/<'.$tag.'(.*?)>(.*?)<\/'.$tag.'>/s', '['.$tag.'$1]$2[/'.$tag.']', $html);
        }

        return $html;
    }

    public function getName(): string
    {
        return 'content';
    }
}