File size: 1,918 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
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Twig\Extension;

use Mautic\CoreBundle\Twig\Helper\ContentHelper;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ContentExtension extends AbstractExtension
{
    public function __construct(
        protected ContentHelper $contentHelper
    ) {
    }

    public function getFunctions()
    {
        return [
            new TwigFunction('customContent', [$this, 'getCustomContent'], ['is_safe' => ['all']]),
            new TwigFunction('showScriptTags', [$this, 'showScriptTags'], ['is_safe' => ['all']]),
            new TwigFunction('getSortedEditorFonts', [$this, 'sortEditorFonts']),
        ];
    }

    /**
     * Dispatch an event to collect custom content.
     *
     * @param ?mixed              $context  Context of the content requested for the viewName
     * @param array<string,mixed> $vars     twig vars
     * @param ?string             $viewName The main identifier for the content requested. Will be etracted from $vars if get_defined
     */
    public function getCustomContent($context = null, array $vars = [], ?string $viewName = null): string
    {
        return $this->contentHelper->getCustomContent($context, $vars, $viewName);
    }

    /**
     * Replaces HTML script tags with non HTML tags so the JS inside them won't
     * execute and will be readable.
     */
    public function showScriptTags(string $html): string
    {
        return $this->contentHelper->showScriptTags($html);
    }

    /**
     * @param array<mixed> $fonts
     *
     * @return array<mixed>
     */
    public function sortEditorFonts(array $fonts): array
    {
        usort($fonts, static function ($fontA, $fontB): int {
            $fontAName = $fontA['name'] ?? '';
            $fontBName = $fontB['name'] ?? '';

            return strcasecmp($fontAName, $fontBName);
        });

        return $fonts;
    }
}