1 | <?php |
||
13 | class Template { |
||
14 | |||
15 | /** |
||
16 | * The name of the template to render (if not using a Twig string). |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $template_name; |
||
21 | |||
22 | /** |
||
23 | * The Twig string to render (if not using a template file). |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $template_string; |
||
28 | |||
29 | /** |
||
30 | * The template data, all of which is passed to the Twig template. |
||
31 | * |
||
32 | * @var string[] |
||
33 | */ |
||
34 | protected $data; |
||
35 | |||
36 | /** |
||
37 | * Paths at which to find templates. |
||
38 | * |
||
39 | * @var string[] |
||
40 | */ |
||
41 | protected static $paths = array(); |
||
42 | |||
43 | /** |
||
44 | * The name of the transient used to store notices. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $transient_notices; |
||
49 | |||
50 | /** |
||
51 | * Create a new template either with a file-based Twig template, or a Twig string. |
||
52 | * |
||
53 | * @global \wpdb $wpdb |
||
54 | * @param string|false $template_name The name of a Twig file to render. |
||
55 | * @param string|false $template_string A Twig string to render. |
||
56 | */ |
||
57 | public function __construct( $template_name = false, $template_string = false ) { |
||
75 | |||
76 | /** |
||
77 | * Add a filesystem path under which to look for template files. |
||
78 | * |
||
79 | * @param string $new_path The path to add. |
||
80 | */ |
||
81 | public static function add_path( $new_path ) { |
||
82 | $path = realpath( $new_path ); |
||
83 | if ( ! in_array( $path, self::$paths, true ) ) { |
||
84 | self::$paths[] = $path; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get a list of the filesystem paths searched for template files. |
||
90 | * |
||
91 | * @return string[] An array of paths |
||
92 | */ |
||
93 | public static function get_paths() { |
||
94 | return self::$paths; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get a list of templates in a given directory, across all registered template paths. |
||
99 | * |
||
100 | * @param string $directory The directory to search in. |
||
101 | */ |
||
102 | public function get_templates( $directory ) { |
||
103 | $templates = array(); |
||
104 | foreach ( self::$paths as $path ) { |
||
105 | $dir = $path . '/' . ltrim( $directory, '/' ); |
||
106 | foreach ( preg_grep( '/^[^\.].*\.(twig|html)$/', scandir( $dir ) ) as $file ) { |
||
107 | $templates[] = $directory . '/' . $file; |
||
108 | } |
||
109 | } |
||
110 | return $templates; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Magically set a template variable. |
||
115 | * |
||
116 | * @param string $name The name of the variable. |
||
117 | * @param mixed $value The value of the variable. |
||
118 | */ |
||
119 | public function __set( $name, $value ) { |
||
122 | |||
123 | /** |
||
124 | * Find out whether a given item of template data is set. |
||
125 | * |
||
126 | * @param string $name The property name. |
||
127 | * @return boolean |
||
128 | */ |
||
129 | public function __isset( $name ) { |
||
132 | |||
133 | /** |
||
134 | * Get an item from this template's data. |
||
135 | * |
||
136 | * @param string $name The name of the template variable. |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public function __get( $name ) { |
||
142 | |||
143 | /** |
||
144 | * Add a notice. All notices are saved to a Transient, which is deleted when |
||
145 | * the template is rendered but otherwise available to all subsequent |
||
146 | * instances of the Template class. |
||
147 | * |
||
148 | * @param string $type Either 'updated' or 'error'. |
||
149 | * @param string $message The message to display. |
||
150 | */ |
||
151 | public function add_notice( $type, $message ) { |
||
158 | |||
159 | /** |
||
160 | * Render the template and output it. |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function __toString() { |
||
167 | |||
168 | /** |
||
169 | * Render the template and return the output. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function render() { |
||
224 | } |
||
225 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..