<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing namespace MailPoet\Config; if (!defined('ABSPATH')) exit; use MailPoet\Twig; use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Twig\Extension\DebugExtension; use MailPoetVendor\Twig\Lexer as TwigLexer; use MailPoetVendor\Twig\Loader\FilesystemLoader as TwigFileSystem; class Renderer { protected $cachePath; protected $debuggingEnabled; protected $renderer; public $assetsManifestJs; public $assetsManifestCss; public function __construct( bool $debuggingEnabled, string $cachePath, TwigFileSystem $fileSystem, bool $autoReload = false ) { $this->debuggingEnabled = $debuggingEnabled; $this->cachePath = $cachePath; $this->renderer = new TwigEnvironment( $fileSystem, [ 'cache' => new TwigFileSystemCache($cachePath), 'debug' => $this->debuggingEnabled, 'auto_reload' => $autoReload, ] ); $this->assetsManifestJs = $this->getAssetManifest(Env::$assetsPath . '/dist/js/manifest.json'); $this->assetsManifestCss = $this->getAssetManifest(Env::$assetsPath . '/dist/css/manifest.json'); $this->setupDebug(); $this->setupTranslations(); $this->setupFunctions(); $this->setupFilters(); $this->setupHandlebars(); $this->setupAnalytics(); $this->setupGlobalVariables(); $this->setupSyntax(); } public function getTwig(): TwigEnvironment { return $this->renderer; } public function setupTranslations() { $this->renderer->addExtension(new Twig\I18n(Env::$pluginName)); } public function setupFunctions() { $this->renderer->addExtension(new Twig\Functions()); } public function setupFilters() { $this->renderer->addExtension(new Twig\Filters()); } public function setupHandlebars() { $this->renderer->addExtension(new Twig\Handlebars()); } public function setupAnalytics() { $this->renderer->addExtension(new Twig\Analytics()); } public function setupGlobalVariables() { $this->renderer->addExtension( new Twig\Assets( [ 'version' => Env::$version, 'assets_url' => Env::$assetsUrl, 'assets_manifest_js' => $this->assetsManifestJs, 'assets_manifest_css' => $this->assetsManifestCss, ], WPFunctions::get() ) ); } public function setupSyntax() { $lexer = new TwigLexer($this->renderer, [ 'tag_comment' => ['<#', '#>'], 'tag_block' => ['<%', '%>'], 'tag_variable' => ['<%=', '%>'], 'interpolation' => ['%{', '}'], ]); $this->renderer->setLexer($lexer); } public function setupDebug() { if ($this->debuggingEnabled) { $this->renderer->addExtension(new DebugExtension()); } } public function render($template, $context = []) { try { $loaded = $this->renderer->load($template); // schedule "after_javascript" block to be printed only after other scripts if ($loaded->hasBlock('after_javascript')) { $afterJs = $loaded->renderBlock('after_javascript', $context); WPFunctions::get()->addAction('admin_print_footer_scripts', function () use ($afterJs): void { // This is content is generated by Twig. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $afterJs; }); } return $loaded->render($context); } catch (\RuntimeException $e) { throw new \Exception(sprintf( // translators: %1$s is the name of the render, %2$s the folder path, %3$s the error message. __('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'), $template, $this->cachePath, $e->getMessage() )); } } public function getAssetManifest($manifestFile) { if (is_readable($manifestFile)) { $contents = file_get_contents($manifestFile); if (is_string($contents)) { return json_decode($contents, true); } } return false; } public function getJsAsset($asset) { return (!empty($this->assetsManifestJs[$asset])) ? $this->assetsManifestJs[$asset] : $asset; } public function getCssAsset($asset) { return (!empty($this->assetsManifestCss[$asset])) ? $this->assetsManifestCss[$asset] : $asset; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
PopulatorData | Folder | 0755 |
|
|
AccessControl.php | File | 3.89 KB | 0644 |
|
Activator.php | File | 4.94 KB | 0644 |
|
AssetsLoader.php | File | 1.68 KB | 0644 |
|
Capabilities.php | File | 2.87 KB | 0644 |
|
Changelog.php | File | 5.04 KB | 0644 |
|
DeactivationPoll.php | File | 1.43 KB | 0644 |
|
DeferredAdminNotices.php | File | 1.16 KB | 0644 |
|
Env.php | File | 4.16 KB | 0644 |
|
Hooks.php | File | 20.01 KB | 0644 |
|
HooksReCaptcha.php | File | 2.92 KB | 0644 |
|
HooksWooCommerce.php | File | 6.76 KB | 0644 |
|
Initializer.php | File | 16.88 KB | 0644 |
|
Installer.php | File | 3.12 KB | 0644 |
|
Localizer.php | File | 2.49 KB | 0644 |
|
Menu.php | File | 23.54 KB | 0644 |
|
PersonalDataErasers.php | File | 769 B | 0644 |
|
PersonalDataExporters.php | File | 3.22 KB | 0644 |
|
PluginActivatedHook.php | File | 776 B | 0644 |
|
Populator.php | File | 23.37 KB | 0644 |
|
PrivacyPolicy.php | File | 4.53 KB | 0644 |
|
Renderer.php | File | 4.34 KB | 0644 |
|
RendererFactory.php | File | 670 B | 0644 |
|
RequirementsChecker.php | File | 3.73 KB | 0644 |
|
Router.php | File | 1006 B | 0644 |
|
ServicesChecker.php | File | 7.04 KB | 0644 |
|
Shortcodes.php | File | 8.63 KB | 0644 |
|
SilentUpgraderSkin.php | File | 570 B | 0644 |
|
SubscriberChangesNotifier.php | File | 5 KB | 0644 |
|
TranslationUpdater.php | File | 7.92 KB | 0644 |
|
TwigEnvironment.php | File | 694 B | 0644 |
|
TwigFileSystemCache.php | File | 853 B | 0644 |
|
Updater.php | File | 1.97 KB | 0644 |
|
index.php | File | 6 B | 0644 |
|