| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements\TemplateRenderer; |
| 4 |
|
| 5 |
use Elementor\Utils; |
| 6 |
use ElementorDeps\Twig\Environment; |
| 7 |
use ElementorDeps\Twig\Runtime\EscaperRuntime; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Template_Renderer { |
| 14 |
private static ?self $instance = null; |
| 15 |
|
| 16 |
private Single_File_Loader $loader; |
| 17 |
|
| 18 |
private Environment $env; |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
$this->loader = new Single_File_Loader(); |
| 22 |
|
| 23 |
$this->env = new Environment( |
| 24 |
$this->loader, |
| 25 |
[ |
| 26 |
'debug' => Utils::is_elementor_debug(), |
| 27 |
'autoescape' => 'name', |
| 28 |
] |
| 29 |
); |
| 30 |
|
| 31 |
$escaper = $this->env->getRuntime( EscaperRuntime::class ); |
| 32 |
|
| 33 |
$escaper->setEscaper( 'full_url', 'esc_url' ); |
| 34 |
$escaper->setEscaper( 'html_tag', [ Utils::class, 'validate_html_tag' ] ); |
| 35 |
} |
| 36 |
|
| 37 |
public static function instance(): self { |
| 38 |
if ( null === self::$instance ) { |
| 39 |
self::$instance = new self(); |
| 40 |
} |
| 41 |
|
| 42 |
return self::$instance; |
| 43 |
} |
| 44 |
|
| 45 |
public static function reset() { |
| 46 |
self::$instance = null; |
| 47 |
} |
| 48 |
|
| 49 |
public function is_registered( string $name ): bool { |
| 50 |
return $this->loader->is_registered( $name ); |
| 51 |
} |
| 52 |
|
| 53 |
public function register( string $name, string $path ): self { |
| 54 |
$this->loader->register( $name, $path ); |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
public function render( string $name, array $context = [] ): string { |
| 60 |
return $this->env->render( $name, $context ); |
| 61 |
} |
| 62 |
} |
| 63 |
|