forked from kingkool68/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-sprig.php
251 lines (231 loc) · 7.42 KB
/
class-sprig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* The main Sprig class
*
* @package Sprig
*/
/**
* Sprig for minimal Twig templating in WordPress
*/
class Sprig {
/**
* An instance of Twig
*
* @var Twig\Environment
*/
public static $twig;
/**
* Get an instance of this class
*/
public static function get_instance() {
static $instance = null;
if ( null === $instance ) {
$instance = new static();
$instance->setup_actions();
$instance->setup_filters();
}
return $instance;
}
/**
* Empty constructor since we don't allow it
*/
public function __construct() {}
/**
* Hook into WordPress via actions
*/
public function setup_actions() {
add_action( 'init', array( $this, 'action_init' ) );
}
/**
* Setup Twig and allow for other customizations
*/
public function action_init() {
$this->setup_twig();
// Add custom filters to Twig
// See https://twig.symfony.com/doc/3.x/advanced.html#filters
$twig_filters = apply_filters( 'sprig/twig/filters', array() );
foreach ( $twig_filters as $name => $filter_callback ) {
if ( is_callable( $filter_callback ) ) {
static::$twig->addFilter( new \Twig\TwigFilter( $name, $filter_callback ) );
}
}
// Add custom functions to Twig
// See https://twig.symfony.com/doc/3.x/advanced.html#functions
$twig_functions = apply_filters( 'sprig/twig/functions', array() );
foreach ( $twig_functions as $name => $function_callback ) {
if ( is_callable( $function_callback ) ) {
static::$twig->addFunction( new \Twig\TwigFunction( $name, $function_callback ) );
}
}
// Modify Twig itself
// See https://twig.symfony.com/doc/3.x/advanced.html#extending-twig
static::$twig = apply_filters( 'sprig/twig', static::$twig );
}
/**
* Setup Twig and define locations to look for templates
*/
public function setup_twig() {
$open_basedir = ini_get( 'open_basedir' );
$paths = array_merge( $this->get_template_locations(), array( $open_basedir ? ABSPATH : '/' ) );
$root_path = '/';
if ( $open_basedir ) {
$root_path = null;
}
$twig_loader = new \Twig\Loader\FilesystemLoader( $paths, $root_path );
$twig_loader = apply_filters( 'sprig/twig_loader', $twig_loader );
$twig = new \Twig\Environment(
$twig_loader,
array(
'debug' => WP_DEBUG,
'autoescape' => false,
)
);
if ( WP_DEBUG ) {
$twig->addExtension( new \Twig\Extension\DebugExtension() );
}
static::$twig = $twig;
}
/**
* Hook into WordPress via filters
*/
public function setup_filters() {
add_filter( 'sprig/twig/filters', array( $this, 'filter_sprig_twig_default_filters' ) );
add_filter( 'sprig/twig/functions', array( $this, 'filter_sprig_twig_default_functions' ) );
}
/**
* Setup default filters for use in .twig files
* These are mostly escaping functions
*
* @param array $filters List of Twig filters.
* @return array Modified list of Twig filters to make avaialble
*/
public function filter_sprig_twig_default_filters( $filters = array() ) {
$filters['esc_attr'] = 'esc_attr';
$filters['esc_html'] = 'esc_html';
$filters['esc_url'] = 'esc_url';
$filters['esc_js'] = 'esc_js';
$filters['esc_textarea'] = 'esc_textarea';
$filters['tag_escape'] = 'tag_escape';
$filters['sanitize_email'] = 'sanitize_email';
$filters['sanitize_html_class'] = 'sanitize_html_class';
$filters['antispambot'] = 'antispambot';
$filters['wptexturize'] = 'wptexturize';
$filters['absint'] = 'absint';
return $filters;
}
/**
* Setup default functions for use in .twig files
*
* @param array $functions List of Twig functions.
* @return array Modified list of Twig functions to make avaialble
*/
public function filter_sprig_twig_default_functions( $functions = array() ) {
$functions['checked'] = 'checked';
$functions['selected'] = 'selected';
$functions['disabled'] = 'disabled';
$functions['wp_head'] = 'wp_head';
$functions['wp_footer'] = 'wp_footer';
$functions['body_class'] = 'body_class';
$functions['get_header'] = 'get_header';
$functions['get_footer'] = 'get_footer';
$functions['wp_title'] = 'wp_title';
return $functions;
}
/**
* Get paths of directories Twig should look for template files
*
* @return array List of locations for Twig to look for templates
*/
public function get_template_locations() {
$theme_locations = array();
$theme_dirs = apply_filters( 'sprig/theme_dirs', array( 'views', 'twig' ) );
$roots = array( get_stylesheet_directory(), get_template_directory() );
$roots = apply_filters( 'sprig/roots', $roots );
$roots = array_map( 'realpath', $roots );
$roots = array_unique( $roots );
foreach ( $roots as $root ) {
if ( ! is_dir( $root ) ) {
continue;
}
$theme_locations[] = $root;
$root = trailingslashit( $root );
foreach ( $theme_dirs as $dirname ) {
$theme_location = realpath( $root . $dirname );
if ( is_dir( $theme_location ) ) {
$theme_locations[] = $theme_location;
}
}
}
return $theme_locations;
}
/**
* Given a list of file names find the first template
* that exists and can be used for rendering
*
* @param array $filenames List of template file names.
* @return string|false Name of first template found or false if no templates are found
*/
public static function choose_template( $filenames = array() ) {
$loader = static::$twig->getLoader();
foreach ( $filenames as $filename ) {
if ( $loader->exists( $filename ) ) {
return $filename;
}
}
return false;
}
/**
* Get the full path of the chosen template file
*
* @param array $filenames List of template file names.
* @return string Path of the chosen template file
*/
public static function get_template_path( $filenames = array() ) {
$template_file_name = static::choose_template( $filenames );
if ( empty( $template_file_name ) ) {
return '';
}
$loader = static::$twig->getLoader();
return $loader->getSourceContext( $template_file_name )->getPath();
}
/**
* Render a template using the provided data
*
* @param array $filenames List of template file names.
* @param array $data Data to use when rendering the template.
* @return string Rendered template
*/
public static function render( $filenames = array(), $data = array() ) {
if ( ! is_array( $filenames ) ) {
$filenames = array( $filenames );
}
$template_file_name = static::choose_template( $filenames );
return static::$twig->render( $template_file_name, $data );
}
/**
* Helper method to echo the rendered output
*
* @param array $filenames List of template file names.
* @param array $data Data to use when rendering the template.
*/
public static function out( $filenames = array(), $data = array() ) {
echo static::render( $filenames, $data );
}
/**
* Trigger an action and return the captured output buffer
*
* @param string $action_name The name of the action to be executed.
* @param mixed $arg Additional arguments which are passed on to the functions hooked to the action.
* @return string Output of the triggered action
*/
public static function do_action( $action_name = '', $arg = '' ) {
if ( empty( $action_name ) ) {
return;
}
ob_start();
call_user_func_array( 'do_action', func_get_args() );
return ob_get_clean();
}
}
// Kick things off.
Sprig::get_instance();