PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.2
Elementor Website Builder – more than just a page builder v3.32.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / atomic-widgets / template-renderer / single-file-loader.php

single-file-loader.php in Elementor Website Builder – more than just a page builder 3.32.2, at modules/atomic-widgets/template-renderer/single-file-loader.php

91 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\TemplateRenderer;
4
5 use ElementorDeps\Twig\Error\LoaderError;
6 use ElementorDeps\Twig\Loader\LoaderInterface;
7 use ElementorDeps\Twig\Source;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Single_File_Loader implements LoaderInterface {
14 private $templates = [];
15
16 private $validity_cache = [];
17
18 public function getSourceContext( string $name ): Source {
19 $path = $this->get_template_path( $name );
20
21 return new Source(
22 // This is safe to use because we're validating the file path inside `get_template_path`.
23 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
24 file_get_contents( $path ),
25 $name,
26 $path
27 );
28 }
29
30 public function getCacheKey( string $name ): string {
31 return $this->get_template_path( $name );
32 }
33
34 public function isFresh( string $name, int $time ): bool {
35 $path = $this->get_template_path( $name );
36
37 return filemtime( $path ) < $time;
38 }
39
40 public function exists( string $name ) {
41 $path = $this->templates[ $name ] ?? null;
42
43 return $this->is_valid_file( $path );
44 }
45
46 public function is_registered( string $name ): bool {
47 return isset( $this->templates[ $name ] );
48 }
49
50 public function register( string $name, string $path ): self {
51 if ( ! $this->is_valid_file( $path ) ) {
52 throw new LoaderError( esc_html( "Invalid template '{$name}': {$path}" ) );
53 }
54
55 $this->templates[ $name ] = $path;
56
57 return $this;
58 }
59
60 private function get_template_path( string $name ): string {
61 $path = $this->templates[ $name ] ?? null;
62
63 if ( ! $this->is_valid_file( $path ) ) {
64 throw new LoaderError( esc_html( "Invalid template '{$name}': {$path}" ) );
65 }
66
67 return $path;
68 }
69
70 private function is_valid_file( $path ): bool {
71 if ( ! $path ) {
72 return false;
73 }
74
75 if ( isset( $this->validity_cache[ $path ] ) ) {
76 return $this->validity_cache[ $path ];
77 }
78
79 // Ref: https://github.com/twigphp/Twig/blob/8432946eeeca009d75fc7fc568f3c3f4650f5a0f/src/Loader/FilesystemLoader.php#L260
80 if ( str_contains( $path, "\0" ) ) {
81 throw new LoaderError( 'A template name cannot contain NULL bytes.' );
82 }
83
84 $is_valid = is_file( $path ) && is_readable( $path );
85
86 $this->validity_cache[ $path ] = $is_valid;
87
88 return $is_valid;
89 }
90 }
91