PopulatorData
2 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
2 weeks ago
Capabilities.php
2 months ago
Changelog.php
2 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
6 months ago
Hooks.php
1 month ago
HooksWooCommerce.php
1 month ago
Initializer.php
1 month ago
Installer.php
10 months ago
Localizer.php
3 years ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
2 weeks ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
2 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 month ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
2 months ago
TranslationUpdater.php
3 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
5 days ago
index.php
3 years ago
Renderer.php
151 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Twig; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | use MailPoetVendor\Twig\Extension\DebugExtension; |
| 11 | use MailPoetVendor\Twig\Lexer as TwigLexer; |
| 12 | use MailPoetVendor\Twig\Loader\FilesystemLoader as TwigFileSystem; |
| 13 | |
| 14 | class Renderer { |
| 15 | protected $cachePath; |
| 16 | protected $debuggingEnabled; |
| 17 | protected $renderer; |
| 18 | public $assetsManifestJs; |
| 19 | public $assetsManifestCss; |
| 20 | |
| 21 | public function __construct( |
| 22 | bool $debuggingEnabled, |
| 23 | string $cachePath, |
| 24 | TwigFileSystem $fileSystem, |
| 25 | bool $autoReload = false |
| 26 | ) { |
| 27 | $this->debuggingEnabled = $debuggingEnabled; |
| 28 | $this->cachePath = $cachePath; |
| 29 | $this->renderer = new TwigEnvironment( |
| 30 | $fileSystem, |
| 31 | [ |
| 32 | 'cache' => new TwigFileSystemCache($cachePath), |
| 33 | 'debug' => $this->debuggingEnabled, |
| 34 | 'auto_reload' => $autoReload, |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | $this->assetsManifestJs = $this->getAssetManifest(Env::$assetsPath . '/dist/js/manifest.json'); |
| 39 | $this->assetsManifestCss = $this->getAssetManifest(Env::$assetsPath . '/dist/css/manifest.json'); |
| 40 | $this->setupDebug(); |
| 41 | $this->setupTranslations(); |
| 42 | $this->setupFunctions(); |
| 43 | $this->setupFilters(); |
| 44 | $this->setupHandlebars(); |
| 45 | $this->setupAnalytics(); |
| 46 | $this->setupGlobalVariables(); |
| 47 | $this->setupSyntax(); |
| 48 | } |
| 49 | |
| 50 | public function getTwig(): TwigEnvironment { |
| 51 | return $this->renderer; |
| 52 | } |
| 53 | |
| 54 | public function setupTranslations() { |
| 55 | $this->renderer->addExtension(new Twig\I18n(Env::$pluginName)); |
| 56 | } |
| 57 | |
| 58 | public function setupFunctions() { |
| 59 | $this->renderer->addExtension(new Twig\Functions()); |
| 60 | } |
| 61 | |
| 62 | public function setupFilters() { |
| 63 | $this->renderer->addExtension(new Twig\Filters()); |
| 64 | } |
| 65 | |
| 66 | public function setupHandlebars() { |
| 67 | $this->renderer->addExtension(new Twig\Handlebars()); |
| 68 | } |
| 69 | |
| 70 | public function setupAnalytics() { |
| 71 | $this->renderer->addExtension(new Twig\Analytics()); |
| 72 | } |
| 73 | |
| 74 | public function setupGlobalVariables() { |
| 75 | $this->renderer->addExtension( |
| 76 | new Twig\Assets( |
| 77 | [ |
| 78 | 'version' => Env::$version, |
| 79 | 'assets_url' => Env::$assetsUrl, |
| 80 | 'assets_manifest_js' => $this->assetsManifestJs, |
| 81 | 'assets_manifest_css' => $this->assetsManifestCss, |
| 82 | ], |
| 83 | WPFunctions::get() |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public function setupSyntax() { |
| 89 | $lexer = new TwigLexer($this->renderer, [ |
| 90 | 'tag_comment' => ['<#', '#>'], |
| 91 | 'tag_block' => ['<%', '%>'], |
| 92 | 'tag_variable' => ['<%=', '%>'], |
| 93 | 'interpolation' => ['%{', '}'], |
| 94 | ]); |
| 95 | $this->renderer->setLexer($lexer); |
| 96 | } |
| 97 | |
| 98 | public function setupDebug() { |
| 99 | if ($this->debuggingEnabled) { |
| 100 | $this->renderer->addExtension(new DebugExtension()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public function render($template, $context = []) { |
| 105 | try { |
| 106 | $loaded = $this->renderer->load($template); |
| 107 | |
| 108 | // schedule "after_javascript" block to be printed only after other scripts |
| 109 | if ($loaded->hasBlock('after_javascript')) { |
| 110 | $afterJs = $loaded->renderBlock('after_javascript', $context); |
| 111 | WPFunctions::get()->addAction('admin_print_footer_scripts', function () use ($afterJs): void { |
| 112 | // This is content is generated by Twig. |
| 113 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 114 | echo $afterJs; |
| 115 | }); |
| 116 | } |
| 117 | return $loaded->render($context); |
| 118 | } catch (\RuntimeException $e) { |
| 119 | throw new \Exception(sprintf( |
| 120 | // translators: %1$s is the name of the render, %2$s the folder path, %3$s the error message. |
| 121 | __('Failed to render template "%1$s". Please ensure the template cache folder "%2$s" exists and has write permissions. Terminated with error: "%3$s"', 'mailpoet'), |
| 122 | $template, |
| 123 | $this->cachePath, |
| 124 | $e->getMessage() |
| 125 | )); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function getAssetManifest($manifestFile) { |
| 130 | if (is_readable($manifestFile)) { |
| 131 | $contents = file_get_contents($manifestFile); |
| 132 | if (is_string($contents)) { |
| 133 | return json_decode($contents, true); |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | public function getJsAsset($asset) { |
| 140 | return (!empty($this->assetsManifestJs[$asset])) ? |
| 141 | $this->assetsManifestJs[$asset] : |
| 142 | $asset; |
| 143 | } |
| 144 | |
| 145 | public function getCssAsset($asset) { |
| 146 | return (!empty($this->assetsManifestCss[$asset])) ? |
| 147 | $this->assetsManifestCss[$asset] : |
| 148 | $asset; |
| 149 | } |
| 150 | } |
| 151 |