1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class TimberTwig { |
4
|
|
|
|
5
|
|
|
public static $dir_name; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Initialization |
9
|
|
|
*/ |
10
|
|
|
public static function init() { |
11
|
|
|
new TimberTwig(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
function __construct() { |
|
|
|
|
15
|
|
|
add_action( 'timber/twig/filters', array( $this, 'add_timber_filters_deprecated' ) ); |
16
|
|
|
add_action( 'timber/twig/filters', array( $this, 'add_timber_filters' ) ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* These are all deprecated and will be removed in 0.21.0 |
21
|
|
|
* |
22
|
|
|
* @param Twig_Environment $twig |
23
|
|
|
* @deprecated since 0.20.7 |
24
|
|
|
* @return Twig_Environment |
25
|
|
|
*/ |
26
|
|
|
function add_timber_filters_deprecated( $twig ) { |
|
|
|
|
27
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'get_src_from_attachment_id', 'twig_get_src_from_attachment_id' ) ); |
28
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'wp_body_class', array( $this, 'body_class' ) ) ); |
29
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'twitterify', array( 'TimberHelper', 'twitterify' ) ) ); |
30
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'twitterfy', array( 'TimberHelper', 'twitterify' ) ) ); |
31
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'string', function($arr, $glue = ' '){ |
32
|
|
|
return twig_join_filter($arr, $glue); |
|
|
|
|
33
|
|
|
} ) ); |
34
|
|
|
return $twig; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* |
40
|
|
|
* @param Twig_Environment $twig |
41
|
|
|
* @return Twig_Environment |
42
|
|
|
*/ |
43
|
|
|
function add_timber_filters( $twig ) { |
|
|
|
|
44
|
|
|
/* image filters */ |
45
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'resize', array( 'TimberImageHelper', 'resize' ) ) ); |
46
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'retina', array( 'TimberImageHelper', 'retina_resize' ) ) ); |
47
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'letterbox', array( 'TimberImageHelper', 'letterbox' ) ) ); |
48
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'tojpg', array( 'TimberImageHelper', 'img_to_jpg' ) ) ); |
49
|
|
|
|
50
|
|
|
/* debugging filters */ |
51
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'docs', 'twig_object_docs' ) ); |
52
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'get_class', 'get_class' ) ); |
53
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'get_type', 'get_type' ) ); |
54
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'print_r', function( $arr ) { |
55
|
|
|
return print_r( $arr, true ); |
|
|
|
|
56
|
|
|
} ) ); |
|
|
|
|
57
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'print_a', function( $arr ) { |
58
|
|
|
return '<pre>' . self::object_docs( $arr, true ) . '</pre>'; |
59
|
|
|
} ) ); |
|
|
|
|
60
|
|
|
|
61
|
|
|
/* other filters */ |
62
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'stripshortcodes', 'strip_shortcodes' ) ); |
63
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'array', array( $this, 'to_array' ) ) ); |
64
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'excerpt', 'wp_trim_words' ) ); |
65
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'function', array( $this, 'exec_function' ) ) ); |
66
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'pretags', array( $this, 'twig_pretags' ) ) ); |
67
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'sanitize', 'sanitize_title' ) ); |
68
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'shortcodes', 'do_shortcode' ) ); |
69
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'time_ago', array( $this, 'time_ago' ) ) ); |
70
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'wpautop', 'wpautop' ) ); |
71
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'list', array( $this, 'add_list_separators' ) ) ); |
72
|
|
|
|
73
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'relative', function ( $link ) { |
74
|
|
|
return TimberURLHelper::get_rel_url( $link, true ); |
75
|
|
|
} ) ); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'date', array( $this, 'intl_date' ) ) ); |
78
|
|
|
|
79
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'truncate', function ( $text, $len ) { |
80
|
|
|
return TimberHelper::trim_words( $text, $len ); |
81
|
|
|
} ) ); |
|
|
|
|
82
|
|
|
|
83
|
|
|
/* actions and filters */ |
84
|
|
|
$twig->addFunction( new Twig_SimpleFunction( 'action', function ( $context ) { |
85
|
|
|
$args = func_get_args(); |
86
|
|
|
array_shift( $args ); |
87
|
|
|
$args[] = $context; |
88
|
|
|
call_user_func_array( 'do_action', $args ); |
89
|
|
|
}, array( 'needs_context' => true ) ) ); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$twig->addFilter( new Twig_SimpleFilter( 'apply_filters', function () { |
92
|
|
|
$args = func_get_args(); |
93
|
|
|
$tag = current( array_splice( $args, 1, 1 ) ); |
94
|
|
|
|
95
|
|
|
return apply_filters_ref_array( $tag, $args ); |
96
|
|
|
} ) ); |
|
|
|
|
97
|
|
|
$twig->addFunction( new Twig_SimpleFunction( 'function', array( &$this, 'exec_function' ) ) ); |
98
|
|
|
$twig->addFunction( new Twig_SimpleFunction( 'fn', array( &$this, 'exec_function' ) ) ); |
99
|
|
|
|
100
|
|
|
$twig->addFunction( new Twig_SimpleFunction( 'shortcode', 'do_shortcode' ) ); |
101
|
|
|
|
102
|
|
|
/* TimberObjects */ |
103
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'TimberPost', function ( $pid, $PostClass = 'TimberPost' ) { |
|
|
|
|
104
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
105
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
106
|
|
|
$p = new $PostClass( $p ); |
107
|
|
|
} |
108
|
|
|
return $pid; |
109
|
|
|
} |
110
|
|
|
return new $PostClass( $pid ); |
111
|
|
|
} ) ); |
|
|
|
|
112
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'TimberImage', function ( $pid, $ImageClass = 'TimberImage' ) { |
|
|
|
|
113
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
114
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
115
|
|
|
$p = new $ImageClass( $p ); |
116
|
|
|
} |
117
|
|
|
return $pid; |
118
|
|
|
} |
119
|
|
|
return new $ImageClass( $pid ); |
120
|
|
|
} ) ); |
|
|
|
|
121
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'TimberTerm', function ( $pid, $TermClass = 'TimberTerm' ) { |
|
|
|
|
122
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
123
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
124
|
|
|
$p = new $TermClass( $p ); |
125
|
|
|
} |
126
|
|
|
return $pid; |
127
|
|
|
} |
128
|
|
|
return new $TermClass( $pid ); |
129
|
|
|
} ) ); |
|
|
|
|
130
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'TimberUser', function ( $pid, $UserClass = 'TimberUser' ) { |
|
|
|
|
131
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
132
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
133
|
|
|
$p = new $UserClass( $p ); |
134
|
|
|
} |
135
|
|
|
return $pid; |
136
|
|
|
} |
137
|
|
|
return new $UserClass( $pid ); |
138
|
|
|
} ) ); |
|
|
|
|
139
|
|
|
|
140
|
|
|
/* TimberObjects Alias */ |
141
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'Post', function ( $pid, $PostClass = 'TimberPost' ) { |
|
|
|
|
142
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
143
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
144
|
|
|
$p = new $PostClass( $p ); |
145
|
|
|
} |
146
|
|
|
return $pid; |
147
|
|
|
} |
148
|
|
|
return new $PostClass( $pid ); |
149
|
|
|
} ) ); |
|
|
|
|
150
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'Image', function ( $pid, $ImageClass = 'TimberImage' ) { |
|
|
|
|
151
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
152
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
153
|
|
|
$p = new $ImageClass( $p ); |
154
|
|
|
} |
155
|
|
|
return $pid; |
156
|
|
|
} |
157
|
|
|
return new $ImageClass( $pid ); |
158
|
|
|
} ) ); |
|
|
|
|
159
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'Term', function ( $pid, $TermClass = 'TimberTerm' ) { |
|
|
|
|
160
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
161
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
162
|
|
|
$p = new $TermClass( $p ); |
163
|
|
|
} |
164
|
|
|
return $pid; |
165
|
|
|
} |
166
|
|
|
return new $TermClass( $pid ); |
167
|
|
|
} ) ); |
|
|
|
|
168
|
|
View Code Duplication |
$twig->addFunction( new Twig_SimpleFunction( 'User', function ( $pid, $UserClass = 'TimberUser' ) { |
|
|
|
|
169
|
|
|
if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) { |
|
|
|
|
170
|
|
|
foreach ( $pid as &$p ) { |
|
|
|
|
171
|
|
|
$p = new $UserClass( $p ); |
172
|
|
|
} |
173
|
|
|
return $pid; |
174
|
|
|
} |
175
|
|
|
return new $UserClass( $pid ); |
176
|
|
|
} ) ); |
|
|
|
|
177
|
|
|
|
178
|
|
|
/* bloginfo and translate */ |
179
|
|
|
$twig->addFunction( 'bloginfo', new Twig_SimpleFunction( 'bloginfo', function ( $show = '', $filter = 'raw' ) { |
180
|
|
|
return get_bloginfo( $show, $filter ); |
181
|
|
|
} ) ); |
|
|
|
|
182
|
|
|
$twig->addFunction( '__', new Twig_SimpleFunction( '__', function ( $text, $domain = 'default' ) { |
183
|
|
|
return __( $text, $domain ); |
|
|
|
|
184
|
|
|
} ) ); |
|
|
|
|
185
|
|
|
/* get_twig is deprecated, use timber/twig */ |
186
|
|
|
$twig = apply_filters( 'get_twig', $twig ); |
187
|
|
|
$twig = apply_filters( 'timber/twig', $twig ); |
188
|
|
|
return $twig; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* |
193
|
|
|
* |
194
|
|
|
* @param mixed $arr |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
function to_array( $arr ) { |
|
|
|
|
198
|
|
|
if ( is_array( $arr ) ) { |
199
|
|
|
return $arr; |
200
|
|
|
} |
201
|
|
|
$arr = array( $arr ); |
202
|
|
|
return $arr; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* |
207
|
|
|
* |
208
|
|
|
* @param string $function_name |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
|
|
function exec_function( $function_name ) { |
|
|
|
|
212
|
|
|
$args = func_get_args(); |
213
|
|
|
array_shift( $args ); |
214
|
|
|
if ( is_string($function_name) ) { |
|
|
|
|
215
|
|
|
$function_name = trim( $function_name ); |
216
|
|
|
} |
217
|
|
|
return call_user_func_array( $function_name, ( $args ) ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* |
222
|
|
|
* |
223
|
|
|
* @param string $content |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
function twig_pretags( $content ) { |
|
|
|
|
227
|
|
|
return preg_replace_callback( '|<pre.*>(.*)</pre|isU', array( &$this, 'convert_pre_entities' ), $content ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* |
232
|
|
|
* |
233
|
|
|
* @param array $matches |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
function convert_pre_entities( $matches ) { |
|
|
|
|
237
|
|
|
return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param mixed $body_classes |
242
|
|
|
* @deprecated 0.20.7 |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
function body_class( $body_classes ) { |
|
|
|
|
246
|
|
|
ob_start(); |
247
|
|
|
if ( is_array( $body_classes ) ) { |
248
|
|
|
$body_classes = explode( ' ', $body_classes ); |
249
|
|
|
} |
250
|
|
|
body_class( $body_classes ); |
251
|
|
|
$return = ob_get_contents(); |
252
|
|
|
ob_end_clean(); |
253
|
|
|
return $return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* |
258
|
|
|
* |
259
|
|
|
* @param string $date |
260
|
|
|
* @param string $format (optional) |
|
|
|
|
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
function intl_date( $date, $format = null ) { |
|
|
|
|
264
|
|
|
if ( $format === null ) { |
265
|
|
|
$format = get_option( 'date_format' ); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if ( $date instanceof DateTime ) { |
269
|
|
|
$timestamp = $date->getTimestamp(); |
270
|
|
|
} else if (is_numeric( $date ) && strtotime( $date ) === false ) { |
|
|
|
|
271
|
|
|
$timestamp = intval( $date ); |
272
|
|
|
} else { |
273
|
|
|
$timestamp = strtotime( $date ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return date_i18n( $format, $timestamp ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
//debug |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* |
283
|
|
|
* |
284
|
|
|
* @param mixed $obj |
285
|
|
|
* @param bool $methods |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
function object_docs( $obj, $methods = true ) { |
|
|
|
|
289
|
|
|
$class = get_class( $obj ); |
290
|
|
|
$properties = (array)$obj; |
|
|
|
|
291
|
|
|
if ( $methods ) { |
292
|
|
|
/** @var array $methods */ |
293
|
|
|
$methods = $obj->get_method_values(); |
294
|
|
|
} |
295
|
|
|
$rets = array_merge( $properties, $methods ); |
296
|
|
|
ksort( $rets ); |
297
|
|
|
$str = print_r( $rets, true ); |
|
|
|
|
298
|
|
|
$str = str_replace( 'Array', $class . ' Object', $str ); |
299
|
|
|
return $str; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param int|string $from |
304
|
|
|
* @param int|string $to |
|
|
|
|
305
|
|
|
* @param string $format_past |
306
|
|
|
* @param string $format_future |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
function time_ago( $from, $to = null, $format_past = '%s ago', $format_future = '%s from now' ) { |
|
|
|
|
310
|
|
|
$to = $to === null ? time() : $to; |
311
|
|
|
$to = is_int( $to ) ? $to : strtotime( $to ); |
312
|
|
|
$from = is_int( $from ) ? $from : strtotime( $from ); |
313
|
|
|
|
314
|
|
|
if ( $from < $to ) { |
315
|
|
|
return sprintf( $format_past, human_time_diff( $from, $to ) ); |
316
|
|
|
} else { |
317
|
|
|
return sprintf( $format_future, human_time_diff( $to, $from ) ); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param array $arr |
323
|
|
|
* @param string $first_delimiter |
324
|
|
|
* @param string $second_delimiter |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
function add_list_separators( $arr, $first_delimiter = ',', $second_delimiter = 'and' ) { |
|
|
|
|
328
|
|
|
$length = count( $arr ); |
329
|
|
|
$list = ''; |
330
|
|
|
foreach( $arr as $index => $item ) { |
|
|
|
|
331
|
|
|
if ( $index < $length - 2 ) { |
332
|
|
|
$delimiter = $first_delimiter.' '; |
333
|
|
|
} elseif ( $index == $length - 2 ) { |
334
|
|
|
$delimiter = ' '.$second_delimiter.' '; |
335
|
|
|
} else { |
336
|
|
|
$delimiter = ''; |
337
|
|
|
} |
338
|
|
|
$list = $list.$item.$delimiter; |
339
|
|
|
} |
340
|
|
|
return $list; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.