-
-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathTwig.php
More file actions
403 lines (361 loc) · 13 KB
/
Twig.php
File metadata and controls
403 lines (361 loc) · 13 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
namespace Timber;
use Timber\URLHelper;
use Timber\Helper;
use Timber\Post;
use Timber\Term;
use Timber\Image;
use Timber\User;
class Twig {
public static $dir_name;
/**
* @codeCoverageIgnore
*/
public static function init() {
$self = new self();
add_action( 'timber/twig/filters', array( $self, 'add_timber_filters' ) );
add_action( 'timber/twig/functions', array( $self, 'add_timber_functions' ) );
add_action( 'timber/twig/escapers', array( $self, 'add_timber_escapers' ) );
}
/**
* Adds functions to Twig.
*
* @param \Twig\Environment $twig The Twig Environment.
* @return \Twig\Environment
*/
public function add_timber_functions( $twig ) {
/* actions and filters */
$twig->addFunction(new Twig_Function('action', function( $context ) {
$args = func_get_args();
array_shift($args);
$args[] = $context;
call_user_func_array('do_action', $args);
}, array('needs_context' => true)));
$twig->addFunction(new Twig_Function('function', array(&$this, 'exec_function')));
$twig->addFunction(new Twig_Function('fn', array(&$this, 'exec_function')));
$twig->addFunction(new Twig_Function('shortcode', 'do_shortcode'));
/* TimberObjects */
$twig->addFunction(new Twig_Function('TimberPost', function( $pid, $PostClass = 'Timber\Post' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $PostClass($p);
}
return $pid;
}
return new $PostClass($pid);
} ));
$twig->addFunction(new Twig_Function('TimberImage', function( $pid = false, $ImageClass = 'Timber\Image' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $ImageClass($p);
}
return $pid;
}
return new $ImageClass($pid);
} ));
$twig->addFunction(new Twig_Function('TimberTerm', array($this, 'handle_term_object')));
$twig->addFunction(new Twig_Function('TimberUser', function( $pid, $UserClass = 'Timber\User' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $UserClass($p);
}
return $pid;
}
return new $UserClass($pid);
} ));
/* TimberObjects Alias */
$twig->addFunction(new Twig_Function('Post', function( $pid, $PostClass = 'Timber\Post' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $PostClass($p);
}
return $pid;
}
return new $PostClass($pid);
} ));
$twig->addFunction(new Twig_Function('Image', function( $pid, $ImageClass = 'Timber\Image' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $ImageClass($p);
}
return $pid;
}
return new $ImageClass($pid);
} ));
$twig->addFunction(new Twig_Function('Term', array($this, 'handle_term_object')));
$twig->addFunction(new Twig_Function('User', function( $pid, $UserClass = 'Timber\User' ) {
if ( is_array($pid) && !Helper::is_array_assoc($pid) ) {
foreach ( $pid as &$p ) {
$p = new $UserClass($p);
}
return $pid;
}
return new $UserClass($pid);
} ));
/* bloginfo and translate */
$twig->addFunction(new Twig_Function('bloginfo', 'bloginfo'));
$twig->addFunction(new Twig_Function('__', '__'));
$twig->addFunction(new Twig_Function('translate', 'translate'));
$twig->addFunction(new Twig_Function('_e', '_e'));
$twig->addFunction(new Twig_Function('_n', '_n'));
$twig->addFunction(new Twig_Function('_x', '_x'));
$twig->addFunction(new Twig_Function('_ex', '_ex'));
$twig->addFunction(new Twig_Function('_nx', '_nx'));
$twig->addFunction(new Twig_Function('_n_noop', '_n_noop'));
$twig->addFunction(new Twig_Function('_nx_noop', '_nx_noop'));
$twig->addFunction(new Twig_Function('translate_nooped_plural', 'translate_nooped_plural'));
return $twig;
}
/**
* Function for Term or TimberTerm() within Twig
* @since 1.5.1
* @author @jarednova
* @param integer $tid the term ID to search for
* @param string $taxonomy the taxonomy to search inside of. If sent a class name, it will use that class to support backwards compatibility
* @param string $TermClass the class to use for processing the term
* @return Term|array
*/
function handle_term_object( $tid, $taxonomy = '', $TermClass = 'Timber\Term' ) {
if ( $taxonomy != $TermClass ) {
// user has sent any additonal parameters, process
$processed_args = self::process_term_args($taxonomy, $TermClass);
$taxonomy = $processed_args['taxonomy'];
$TermClass = $processed_args['TermClass'];
}
if ( is_array($tid) && !Helper::is_array_assoc($tid) ) {
foreach ( $tid as &$p ) {
$p = new $TermClass($p, $taxonomy);
}
return $tid;
}
return new $TermClass($tid, $taxonomy);
}
/**
* Process the arguments for handle_term_object to determine what arguments the user is sending
* @since 1.5.1
* @author @jarednova
* @param string $maybe_taxonomy probably a taxonomy, but it could be a Timber\Term subclass
* @param string $TermClass a string for the Timber\Term subclass
* @return array of processed arguments
*/
protected static function process_term_args( $maybe_taxonomy, $TermClass ) {
// A user could be sending a TermClass in the first arg, let's test for that ...
if ( class_exists($maybe_taxonomy) ) {
$tc = new $maybe_taxonomy;
if ( is_subclass_of($tc, 'Timber\Term') ) {
return array('taxonomy' => '', 'TermClass' => $maybe_taxonomy);
}
}
return array('taxonomy' => $maybe_taxonomy, 'TermClass' => $TermClass);
}
/**
* Adds filters to Twig.
*
* @param \Twig\Environment $twig The Twig Environment.
* @return \Twig\Environment
*/
public function add_timber_filters( $twig ) {
$twig->addFilter(new Twig_Filter('resize', array('Timber\ImageHelper', 'resize')));
$twig->addFilter(new Twig_Filter('retina', array('Timber\ImageHelper', 'retina_resize')));
$twig->addFilter(new Twig_Filter('letterbox', array('Timber\ImageHelper', 'letterbox')));
$twig->addFilter(new Twig_Filter('tojpg', array('Timber\ImageHelper', 'img_to_jpg')));
$twig->addFilter(new Twig_Filter('towebp', array('Timber\ImageHelper', 'img_to_webp')));
/* debugging filters */
$twig->addFilter(new Twig_Filter('get_class', 'get_class'));
$twig->addFilter(new Twig_Filter('get_type', 'get_type'));
$twig->addFilter(new Twig_Filter('print_r', function( $arr ) {
return print_r($arr, true);
} ));
/* other filters */
$twig->addFilter(new Twig_Filter('stripshortcodes', 'strip_shortcodes'));
$twig->addFilter(new Twig_Filter('array', array($this, 'to_array')));
$twig->addFilter(new Twig_Filter('excerpt', 'wp_trim_words'));
$twig->addFilter(new Twig_Filter('excerpt_chars', array('Timber\TextHelper', 'trim_characters')));
$twig->addFilter(new Twig_Filter('function', array($this, 'exec_function')));
$twig->addFilter(new Twig_Filter('pretags', array($this, 'twig_pretags')));
$twig->addFilter(new Twig_Filter('sanitize', 'sanitize_title'));
$twig->addFilter(new Twig_Filter('shortcodes', 'do_shortcode'));
$twig->addFilter(new Twig_Filter('time_ago', array($this, 'time_ago')));
$twig->addFilter(new Twig_Filter('wpautop', 'wpautop'));
$twig->addFilter(new Twig_Filter('list', array($this, 'add_list_separators')));
$twig->addFilter(new Twig_Filter('pluck', array('Timber\Helper', 'pluck')));
/**
* @deprecated since 1.13 (to be removed in 2.0). Use Twig's native filter filter instead
* @todo remove this in 2.x so that filter merely passes to Twig's filter without any modification
* @ticket #1594 #2120
*/
$twig->addFilter(new Twig_Filter('filter', array('Timber\Helper', 'filter_array')));
$twig->addFilter(new Twig_Filter('wp_list_filter', array('Timber\Helper', 'wp_list_filter')));
$twig->addFilter(new Twig_Filter('relative', function( $link ) {
return URLHelper::get_rel_url($link, true);
} ));
$twig->addFilter(new Twig_Filter('date', array($this, 'intl_date')));
$twig->addFilter(new Twig_Filter('truncate', function( $text, $len ) {
return TextHelper::trim_words($text, $len);
} ));
/* actions and filters */
$twig->addFilter(new Twig_Filter('apply_filters', function() {
$args = func_get_args();
$tag = current(array_splice($args, 1, 1));
return apply_filters_ref_array($tag, $args);
} ));
return $twig;
}
/**
* Adds escapers to Twig.
*
* @param \Twig\Environment $twig The Twig Environment.
* @return \Twig\Environment
*/
public function add_timber_escapers( $twig ) {
$esc_url = function( \Twig\Environment $env, $string ) {
return esc_url( $string );
};
$wp_kses_post = function( \Twig\Environment $env, $string ) {
return wp_kses_post( $string );
};
$esc_html = function( \Twig\Environment $env, $string ) {
return esc_html( $string );
};
$esc_js = function( \Twig\Environment $env, $string ) {
return esc_js( $string );
};
if ( class_exists( 'Twig\Extension\EscaperExtension' ) ) {
$escaper_extension = $twig->getExtension('Twig\Extension\EscaperExtension');
if ( method_exists($escaper_extension, 'setEscaper') ) {
$escaper_extension->setEscaper('esc_url', $esc_url);
$escaper_extension->setEscaper('wp_kses_post', $wp_kses_post);
$escaper_extension->setEscaper('esc_html', $esc_html);
$escaper_extension->setEscaper('esc_js', $esc_js);
return $twig;
}
}
$escaper_extension = $twig->getExtension('Twig\Extension\CoreExtension');
$escaper_extension->setEscaper('esc_url', $esc_url);
$escaper_extension->setEscaper('wp_kses_post', $wp_kses_post);
$escaper_extension->setEscaper('esc_html', $esc_html);
$escaper_extension->setEscaper('esc_js', $esc_js);
return $twig;
}
/**
*
*
* @param mixed $arr
* @return array
*/
public function to_array( $arr ) {
if ( is_array($arr) ) {
return $arr;
}
$arr = array($arr);
return $arr;
}
/**
*
*
* @param string $function_name
* @return mixed
*/
public function exec_function( $function_name ) {
$args = func_get_args();
array_shift($args);
if ( is_string($function_name) ) {
$function_name = trim($function_name);
}
return call_user_func_array($function_name, ($args));
}
/**
*
*
* @param string $content
* @return string
*/
public function twig_pretags( $content ) {
return preg_replace_callback('|<pre.*>(.*)</pre|isU', array(&$this, 'convert_pre_entities'), $content);
}
/**
*
*
* @param array $matches
* @return string
*/
public function convert_pre_entities( $matches ) {
return str_replace($matches[1], htmlentities($matches[1]), $matches[0]);
}
/**
*
*
* @param string $date
* @param string $format (optional)
* @return string
*/
public function intl_date( $date, $format = null ) {
if ( $format === null ) {
$format = get_option('date_format');
}
if ( $date instanceof \DateTime ) {
$timestamp = $date->getTimestamp() + $date->getOffset();
} else if ( is_numeric($date) && (strtotime($date) === false || strlen($date) !== 8) ) {
$timestamp = intval($date);
} else {
$timestamp = strtotime($date);
}
return date_i18n($format, $timestamp);
}
/**
* Returns the difference between two times in a human readable format.
*
* Differentiates between past and future dates.
*
* @see \human_time_diff()
*
* @param int|string $from Base date as a timestamp or a date string.
* @param int|string $to Optional. Date to calculate difference to as a timestamp or
* a date string. Default to current time.
* @param string $format_past Optional. String to use for past dates. To be used with
* `sprintf()`. Default `%s ago`.
* @param string $format_future Optional. String to use for future dates. To be used with
* `sprintf()`. Default `%s from now`.
*
* @return string
*/
public static function time_ago( $from, $to = null, $format_past = null, $format_future = null ) {
if ( null === $format_past ) {
/* translators: %s: Human-readable time difference. */
$format_past = __( '%s ago' );
}
if ( null === $format_future ) {
/* translators: %s: Human-readable time difference. */
$format_future = __( '%s from now' );
}
$to = $to === null ? time() : $to;
$to = is_int($to) ? $to : strtotime($to);
$from = is_int($from) ? $from : strtotime($from);
if ( $from < $to ) {
return sprintf($format_past, human_time_diff($from, $to));
} else {
return sprintf($format_future, human_time_diff($to, $from));
}
}
/**
* @param array $arr
* @param string $first_delimiter
* @param string $second_delimiter
* @return string
*/
public function add_list_separators( $arr, $first_delimiter = ',', $second_delimiter = ' and' ) {
$length = count($arr);
$list = '';
foreach ( $arr as $index => $item ) {
if ( $index < $length - 2 ) {
$delimiter = $first_delimiter.' ';
} elseif ( $index == $length - 2 ) {
$delimiter = $second_delimiter.' ';
} else {
$delimiter = '';
}
$list = $list.$item.$delimiter;
}
return $list;
}
}