|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* As the name suggests these are helpers for Timber (and you!) when developing. You can find additional (mainly internally-focused helpers) in TimberURLHelper |
|
5
|
|
|
*/ |
|
6
|
|
|
class TimberHelper { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A utility for a one-stop shop for Transients |
|
10
|
|
|
* @api |
|
11
|
|
|
* @example |
|
12
|
|
|
* ```php |
|
13
|
|
|
* $favorites = Timber::transient('user-'.$uid.'-favorites', function() use ($uid) { |
|
14
|
|
|
* //some expensive query here that's doing something you want to store to a transient |
|
15
|
|
|
* return $favorites; |
|
16
|
|
|
* }, 600); |
|
17
|
|
|
* Timber::context['favorites'] = $favorites; |
|
18
|
|
|
* Timber::render('single.twig', $context); |
|
19
|
|
|
* ``` |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $slug Unique identifier for transient |
|
22
|
|
|
* @param callable $callback Callback that generates the data that's to be cached |
|
23
|
|
|
* @param int $transient_time (optional) Expiration of transients in seconds |
|
24
|
|
|
* @param int $lock_timeout (optional) How long (in seconds) to lock the transient to prevent race conditions |
|
25
|
|
|
* @param bool $force (optional) Force callback to be executed when transient is locked |
|
26
|
|
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function transient( $slug, $callback, $transient_time = 0, $lock_timeout = 5, $force = false ) { |
|
29
|
|
|
|
|
30
|
|
|
$enable_transients = ( $transient_time === false || ( defined( 'WP_DISABLE_TRANSIENTS' ) && WP_DISABLE_TRANSIENTS ) ) ? false : true; |
|
31
|
|
|
$data = $enable_transients ? get_transient( $slug ) : false; |
|
32
|
|
|
|
|
33
|
|
|
if ( false === $data ) { |
|
34
|
|
|
|
|
35
|
|
|
if ( $enable_transients && self::_is_transient_locked( $slug ) ) { |
|
36
|
|
|
|
|
37
|
|
|
$force = apply_filters( 'timber_force_transients', $force ); |
|
38
|
|
|
$force = apply_filters( 'timber_force_transient_' . $slug, $force ); |
|
39
|
|
|
|
|
40
|
|
|
if ( !$force ) { |
|
|
|
|
|
|
41
|
|
|
//the server is currently executing the process. |
|
42
|
|
|
//We're just gonna dump these users. Sorry! |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$enable_transients = false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// lock timeout shouldn't be higher than 5 seconds, unless |
|
50
|
|
|
// remote calls with high timeouts are made here |
|
51
|
|
|
if ( $enable_transients ) |
|
|
|
|
|
|
52
|
|
|
self::_lock_transient( $slug, $lock_timeout ); |
|
53
|
|
|
|
|
54
|
|
|
$data = $callback(); |
|
55
|
|
|
|
|
56
|
|
|
if ( $enable_transients ) { |
|
57
|
|
|
set_transient( $slug, $data, $transient_time ); |
|
58
|
|
|
self::_unlock_transient( $slug ); |
|
59
|
|
|
} |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $data; |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @internal |
|
69
|
|
|
* @param string $slug |
|
70
|
|
|
* @param integer $lock_timeout |
|
71
|
|
|
*/ |
|
72
|
|
|
static function _lock_transient( $slug, $lock_timeout ) { |
|
|
|
|
|
|
73
|
|
|
set_transient( $slug . '_lock', true, $lock_timeout ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @internal |
|
78
|
|
|
* @param string $slug |
|
79
|
|
|
*/ |
|
80
|
|
|
static function _unlock_transient( $slug ) { |
|
|
|
|
|
|
81
|
|
|
delete_transient( $slug . '_lock', true ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @internal |
|
86
|
|
|
* @param string $slug |
|
87
|
|
|
*/ |
|
88
|
|
|
static function _is_transient_locked( $slug ) { |
|
|
|
|
|
|
89
|
|
|
return (bool)get_transient( $slug . '_lock' ); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* These are for measuring page render time */ |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* For measuring time, this will start a timer |
|
96
|
|
|
* @api |
|
97
|
|
|
* @return float |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function start_timer() { |
|
100
|
|
|
$time = microtime(); |
|
101
|
|
|
$time = explode( ' ', $time ); |
|
102
|
|
|
$time = $time[1] + $time[0]; |
|
103
|
|
|
return $time; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* For stopping time and getting the data |
|
108
|
|
|
* @example |
|
109
|
|
|
* ```php |
|
110
|
|
|
* $start = TimberHelper::start_timer(); |
|
111
|
|
|
* // do some stuff that takes awhile |
|
112
|
|
|
* echo TimberHelper::stop_timer( $start ); |
|
113
|
|
|
* ``` |
|
114
|
|
|
* @param int $start |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function stop_timer( $start ) { |
|
118
|
|
|
$time = microtime(); |
|
119
|
|
|
$time = explode( ' ', $time ); |
|
120
|
|
|
$time = $time[1] + $time[0]; |
|
121
|
|
|
$finish = $time; |
|
122
|
|
|
$total_time = round( ( $finish - $start ), 4 ); |
|
123
|
|
|
return $total_time . ' seconds.'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/* Function Utilities |
|
127
|
|
|
======================== */ |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Calls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template. |
|
131
|
|
|
* @example |
|
132
|
|
|
* ```php |
|
133
|
|
|
* function the_form() { |
|
134
|
|
|
* echo '<form action="form.php"><input type="text" /><input type="submit /></form>'; |
|
135
|
|
|
* } |
|
136
|
|
|
* |
|
137
|
|
|
* $context = Timber::get_context(); |
|
138
|
|
|
* $context['post'] = new TimberPost(); |
|
139
|
|
|
* $context['my_form'] = TimberHelper::ob_function('the_form'); |
|
140
|
|
|
* Timber::render('single-form.twig', $context); |
|
141
|
|
|
* ``` |
|
142
|
|
|
* ```twig |
|
143
|
|
|
* <h1>{{ post.title }}</h1> |
|
144
|
|
|
* {{ my_form }} |
|
145
|
|
|
* ``` |
|
146
|
|
|
* ```html |
|
147
|
|
|
* <h1>Apply to my contest!</h1> |
|
148
|
|
|
* <form action="form.php"><input type="text" /><input type="submit /></form> |
|
149
|
|
|
* ``` |
|
150
|
|
|
* @api |
|
151
|
|
|
* @param callback $function |
|
152
|
|
|
* @param array $args |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function ob_function( $function, $args = array( null ) ) { |
|
156
|
|
|
ob_start(); |
|
157
|
|
|
call_user_func_array( $function, $args ); |
|
158
|
|
|
$data = ob_get_contents(); |
|
159
|
|
|
ob_end_clean(); |
|
160
|
|
|
return $data; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $function_name |
|
167
|
|
|
* @param integer[] $defaults |
|
168
|
|
|
* @param bool $return_output_buffer |
|
169
|
|
|
* @return TimberFunctionWrapper |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function function_wrapper( $function_name, $defaults = array(), $return_output_buffer = false ) { |
|
172
|
|
|
return new TimberFunctionWrapper( $function_name, $defaults, $return_output_buffer ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* |
|
177
|
|
|
* |
|
178
|
|
|
* @param mixed $arg that you want to error_log |
|
179
|
|
|
* @return void |
|
|
|
|
|
|
180
|
|
|
*/ |
|
181
|
|
|
public static function error_log( $arg ) { |
|
182
|
|
|
if ( !WP_DEBUG ) { |
|
|
|
|
|
|
183
|
|
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
if ( is_object( $arg ) || is_array( $arg ) ) { |
|
186
|
|
|
$arg = print_r( $arg, true ); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
return error_log( $arg ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $separator |
|
195
|
|
|
* @param string $seplocation |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function get_wp_title( $separator = ' ', $seplocation = 'left' ) { |
|
199
|
|
|
$separator = apply_filters( 'timber_wp_title_seperator', $separator ); |
|
200
|
|
|
return trim( wp_title( $separator, false, $seplocation ) ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/* Text Utilities |
|
204
|
|
|
======================== */ |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $text |
|
210
|
|
|
* @param int $num_words |
|
211
|
|
|
* @param string|null|false $more text to appear in "Read more...". Null to use default, false to hide |
|
212
|
|
|
* @param string $allowed_tags |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
public static function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote' ) { |
|
216
|
|
|
if ( null === $more ) { |
|
217
|
|
|
$more = __( '…' ); |
|
218
|
|
|
} |
|
219
|
|
|
$original_text = $text; |
|
220
|
|
|
$allowed_tag_string = ''; |
|
221
|
|
|
foreach ( explode( ' ', apply_filters( 'timber/trim_words/allowed_tags', $allowed_tags ) ) as $tag ) { |
|
222
|
|
|
$allowed_tag_string .= '<' . $tag . '>'; |
|
223
|
|
|
} |
|
224
|
|
|
$text = strip_tags( $text, $allowed_tag_string ); |
|
225
|
|
|
/* translators: If your word count is based on single characters (East Asian characters), enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ |
|
226
|
|
|
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { |
|
227
|
|
|
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); |
|
228
|
|
|
preg_match_all( '/./u', $text, $words_array ); |
|
229
|
|
|
$words_array = array_slice( $words_array[0], 0, $num_words + 1 ); |
|
230
|
|
|
$sep = ''; |
|
231
|
|
|
} else { |
|
232
|
|
|
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); |
|
233
|
|
|
$sep = ' '; |
|
234
|
|
|
} |
|
235
|
|
|
if ( count( $words_array ) > $num_words ) { |
|
236
|
|
|
array_pop( $words_array ); |
|
237
|
|
|
$text = implode( $sep, $words_array ); |
|
238
|
|
|
$text = $text . $more; |
|
239
|
|
|
} else { |
|
240
|
|
|
$text = implode( $sep, $words_array ); |
|
241
|
|
|
} |
|
242
|
|
|
$text = self::close_tags( $text ); |
|
243
|
|
|
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $html |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
|
|
public static function close_tags( $html ) { |
|
253
|
|
|
//put all opened tags into an array |
|
254
|
|
|
preg_match_all( '#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result ); |
|
255
|
|
|
$openedtags = $result[1]; |
|
256
|
|
|
//put all closed tags into an array |
|
257
|
|
|
preg_match_all( '#</([a-z]+)>#iU', $html, $result ); |
|
258
|
|
|
$closedtags = $result[1]; |
|
259
|
|
|
$len_opened = count( $openedtags ); |
|
260
|
|
|
// all tags are closed |
|
261
|
|
|
if ( count( $closedtags ) == $len_opened ) { |
|
262
|
|
|
return $html; |
|
263
|
|
|
} |
|
264
|
|
|
$openedtags = array_reverse( $openedtags ); |
|
265
|
|
|
// close tags |
|
266
|
|
|
for ( $i = 0; $i < $len_opened; $i++ ) { |
|
267
|
|
|
if ( !in_array( $openedtags[$i], $closedtags ) ) { |
|
|
|
|
|
|
268
|
|
|
$html .= '</' . $openedtags[$i] . '>'; |
|
|
|
|
|
|
269
|
|
|
} else { |
|
270
|
|
|
unset( $closedtags[array_search( $openedtags[$i], $closedtags )] ); |
|
|
|
|
|
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
$html = str_replace(array('</br>','</hr>','</wbr>'), '', $html); |
|
|
|
|
|
|
274
|
|
|
$html = str_replace(array('<br>','<hr>','<wbr>'), array('<br />','<hr />','<wbr />'), $html); |
|
|
|
|
|
|
275
|
|
|
return $html; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/* WordPress Query Utilities |
|
279
|
|
|
======================== */ |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param string $key |
|
283
|
|
|
* @param string $value |
|
284
|
|
|
* @return array|int |
|
285
|
|
|
* @deprecated 0.20.0 |
|
286
|
|
|
*/ |
|
287
|
|
|
public static function get_posts_by_meta( $key, $value ) { |
|
288
|
|
|
global $wpdb; |
|
289
|
|
|
$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value ); |
|
290
|
|
|
$results = $wpdb->get_col( $query ); |
|
|
|
|
|
|
291
|
|
|
$pids = array(); |
|
292
|
|
|
foreach ( $results as $result ) { |
|
293
|
|
|
if ( get_post( $result ) ) { |
|
294
|
|
|
$pids[] = $result; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
if ( count( $pids ) ) { |
|
298
|
|
|
return $pids; |
|
299
|
|
|
} |
|
300
|
|
|
return 0; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* |
|
305
|
|
|
* |
|
306
|
|
|
* @param string $key |
|
307
|
|
|
* @param string $value |
|
308
|
|
|
* @return int |
|
309
|
|
|
* @deprecated 0.20.0 |
|
310
|
|
|
*/ |
|
311
|
|
|
public static function get_post_by_meta( $key, $value ) { |
|
312
|
|
|
global $wpdb; |
|
313
|
|
|
$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value ); |
|
314
|
|
|
$results = $wpdb->get_col( $query ); |
|
|
|
|
|
|
315
|
|
|
foreach ( $results as $result ) { |
|
316
|
|
|
if ( $result && get_post( $result ) ) { |
|
317
|
|
|
return $result; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
return 0; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* |
|
325
|
|
|
* @deprecated 0.21.8 |
|
326
|
|
|
* @param int $ttid |
|
327
|
|
|
* @return mixed |
|
328
|
|
|
*/ |
|
329
|
|
|
public static function get_term_id_by_term_taxonomy_id( $ttid ) { |
|
330
|
|
|
global $wpdb; |
|
331
|
|
|
$query = $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid ); |
|
332
|
|
|
return $wpdb->get_var( $query ); |
|
|
|
|
|
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/* Object Utilities |
|
336
|
|
|
======================== */ |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* |
|
340
|
|
|
* |
|
341
|
|
|
* @param array $array |
|
342
|
|
|
* @param string $prop |
|
343
|
|
|
* @return void |
|
344
|
|
|
*/ |
|
345
|
|
|
public static function osort( &$array, $prop ) { |
|
346
|
|
|
usort( $array, function ( $a, $b ) use ( $prop ) { |
|
347
|
|
|
return $a->$prop > $b->$prop ? 1 : -1; |
|
348
|
|
|
} ); |
|
|
|
|
|
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* |
|
353
|
|
|
* |
|
354
|
|
|
* @param array $arr |
|
355
|
|
|
* @return bool |
|
356
|
|
|
*/ |
|
357
|
|
|
public static function is_array_assoc( $arr ) { |
|
358
|
|
|
if ( !is_array( $arr ) ) { |
|
|
|
|
|
|
359
|
|
|
return false; |
|
360
|
|
|
} |
|
361
|
|
|
return (bool)count( array_filter( array_keys( $arr ), 'is_string' ) ); |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* |
|
366
|
|
|
* |
|
367
|
|
|
* @param array $array |
|
368
|
|
|
* @return stdClass |
|
369
|
|
|
*/ |
|
370
|
|
|
public static function array_to_object( $array ) { |
|
371
|
|
|
$obj = new stdClass; |
|
372
|
|
|
foreach ( $array as $k => $v ) { |
|
373
|
|
|
if ( is_array( $v ) ) { |
|
374
|
|
|
$obj->{$k} = self::array_to_object( $v ); //RECURSION |
|
375
|
|
|
} else { |
|
376
|
|
|
$obj->{$k} = $v; |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
return $obj; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* |
|
384
|
|
|
* |
|
385
|
|
|
* @param array $array |
|
386
|
|
|
* @param string $key |
|
387
|
|
|
* @param mixed $value |
|
388
|
|
|
* @return bool|int |
|
389
|
|
|
*/ |
|
390
|
|
|
public static function get_object_index_by_property( $array, $key, $value ) { |
|
391
|
|
|
if ( is_array( $array ) ) { |
|
392
|
|
|
$i = 0; |
|
393
|
|
|
foreach ( $array as $arr ) { |
|
394
|
|
|
if ( is_array( $arr ) ) { |
|
395
|
|
|
if ( $arr[$key] == $value ) { |
|
|
|
|
|
|
396
|
|
|
return $i; |
|
397
|
|
|
} |
|
398
|
|
|
} else { |
|
399
|
|
|
if ( $arr->$key == $value ) { |
|
400
|
|
|
return $i; |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
$i++; |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
return false; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* |
|
411
|
|
|
* |
|
412
|
|
|
* @param array $array |
|
413
|
|
|
* @param string $key |
|
414
|
|
|
* @param mixed $value |
|
415
|
|
|
* @return array|null |
|
416
|
|
|
* @throws Exception |
|
417
|
|
|
*/ |
|
418
|
|
|
public static function get_object_by_property( $array, $key, $value ) { |
|
419
|
|
|
if ( is_array( $array ) ) { |
|
420
|
|
|
foreach ( $array as $arr ) { |
|
421
|
|
|
if ( $arr->$key == $value ) { |
|
422
|
|
|
return $arr; |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
} else { |
|
426
|
|
|
throw new InvalidArgumentException( '$array is not an array, got:' ); |
|
427
|
|
|
TimberHelper::error_log( $array ); |
|
|
|
|
|
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* |
|
433
|
|
|
* |
|
434
|
|
|
* @param array $array |
|
435
|
|
|
* @param int $len |
|
436
|
|
|
* @return array |
|
437
|
|
|
*/ |
|
438
|
|
|
public static function array_truncate( $array, $len ) { |
|
439
|
|
|
if ( sizeof( $array ) > $len ) { |
|
440
|
|
|
$array = array_splice( $array, 0, $len ); |
|
441
|
|
|
} |
|
442
|
|
|
return $array; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/* Bool Utilities |
|
446
|
|
|
======================== */ |
|
447
|
|
|
|
|
448
|
|
|
/** |
|
449
|
|
|
* |
|
450
|
|
|
* |
|
451
|
|
|
* @param mixed $value |
|
452
|
|
|
* @return bool |
|
453
|
|
|
*/ |
|
454
|
|
|
public static function is_true( $value ) { |
|
455
|
|
|
if ( isset( $value ) ) { |
|
456
|
|
|
if (is_string($value)) { |
|
|
|
|
|
|
457
|
|
|
$value = strtolower($value); |
|
|
|
|
|
|
458
|
|
|
} |
|
459
|
|
|
if ( ($value == 'true' || $value === 1 || $value === '1' || $value == true) && $value !== false && $value !== 'false') { |
|
|
|
|
|
|
460
|
|
|
return true; |
|
461
|
|
|
} |
|
462
|
|
|
} |
|
463
|
|
|
return false; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* |
|
468
|
|
|
* |
|
469
|
|
|
* @param int $i |
|
470
|
|
|
* @return bool |
|
471
|
|
|
*/ |
|
472
|
|
|
public static function iseven( $i ) { |
|
473
|
|
|
return ( $i % 2 ) == 0; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* |
|
478
|
|
|
* |
|
479
|
|
|
* @param int $i |
|
480
|
|
|
* @return bool |
|
481
|
|
|
*/ |
|
482
|
|
|
public static function isodd( $i ) { |
|
483
|
|
|
return ( $i % 2 ) != 0; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/* Links, Forms, Etc. Utilities |
|
487
|
|
|
======================== */ |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* |
|
491
|
|
|
* Gets the comment form for use on a single article page |
|
492
|
|
|
* @deprecated 0.21.8 use `{{ function('comment_form') }}` instead |
|
493
|
|
|
* @param int $post_id which post_id should the form be tied to? |
|
|
|
|
|
|
494
|
|
|
* @param array $args this $args thing is a fucking mess, [fix at some point](http://codex.wordpress.org/Function_Reference/comment_form) |
|
495
|
|
|
* @return string |
|
496
|
|
|
*/ |
|
497
|
|
|
public static function get_comment_form( $post_id = null, $args = array() ) { |
|
498
|
|
|
return self::ob_function( 'comment_form', array( $args, $post_id ) ); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* |
|
503
|
|
|
* |
|
504
|
|
|
* @param string $args |
|
505
|
|
|
* @return array |
|
506
|
|
|
*/ |
|
507
|
|
|
public static function paginate_links( $args = '' ) { |
|
508
|
|
|
$defaults = array( |
|
509
|
|
|
'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
|
510
|
|
|
'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
|
511
|
|
|
'total' => 1, |
|
512
|
|
|
'current' => 0, |
|
513
|
|
|
'show_all' => false, |
|
514
|
|
|
'prev_next' => false, |
|
515
|
|
|
'prev_text' => __( '« Previous' ), |
|
516
|
|
|
'next_text' => __( 'Next »' ), |
|
517
|
|
|
'end_size' => 1, |
|
518
|
|
|
'mid_size' => 2, |
|
519
|
|
|
'type' => 'array', |
|
520
|
|
|
'add_args' => false, // array of query args to add |
|
521
|
|
|
'add_fragment' => '' |
|
|
|
|
|
|
522
|
|
|
); |
|
523
|
|
|
$args = wp_parse_args( $args, $defaults ); |
|
524
|
|
|
// Who knows what else people pass in $args |
|
525
|
|
|
$args['total'] = intval( (int)$args['total'] ); |
|
|
|
|
|
|
526
|
|
|
if ( $args['total'] < 2 ) { |
|
527
|
|
|
return array(); |
|
528
|
|
|
} |
|
529
|
|
|
$args['current'] = (int)$args['current']; |
|
|
|
|
|
|
530
|
|
|
$args['end_size'] = 0 < (int)$args['end_size'] ? (int)$args['end_size'] : 1; // Out of bounds? Make it the default. |
|
|
|
|
|
|
531
|
|
|
$args['mid_size'] = 0 <= (int)$args['mid_size'] ? (int)$args['mid_size'] : 2; |
|
|
|
|
|
|
532
|
|
|
$args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
|
533
|
|
|
$page_links = array(); |
|
534
|
|
|
$dots = false; |
|
535
|
|
|
for ( $n = 1; $n <= $args['total']; $n++ ) { |
|
536
|
|
|
$n_display = number_format_i18n( $n ); |
|
537
|
|
|
if ( $n == $args['current'] ) { |
|
538
|
|
|
$page_links[] = array( |
|
539
|
|
|
'class' => 'page-number page-numbers current', |
|
540
|
|
|
'title' => $n_display, |
|
541
|
|
|
'text' => $n_display, |
|
542
|
|
|
'name' => $n_display, |
|
543
|
|
|
'current' => true |
|
|
|
|
|
|
544
|
|
|
); |
|
545
|
|
|
$dots = true; |
|
546
|
|
|
} else { |
|
547
|
|
|
if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
|
548
|
|
|
$link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); |
|
549
|
|
|
$link = str_replace( '%#%', $n, $link ); |
|
550
|
|
|
$link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
|
551
|
|
|
if ( $args['add_args'] ) { |
|
552
|
|
|
$link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
|
553
|
|
|
} |
|
554
|
|
|
$link = str_replace(' ', '+', $link); |
|
|
|
|
|
|
555
|
|
|
$link = untrailingslashit( $link ); |
|
556
|
|
|
$page_links[] = array( |
|
557
|
|
|
'class' => 'page-number page-numbers', |
|
558
|
|
|
'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
|
559
|
|
|
'title' => $n_display, |
|
560
|
|
|
'name' => $n_display, |
|
561
|
|
|
'current' => $args['current'] == $n |
|
|
|
|
|
|
562
|
|
|
); |
|
563
|
|
|
$dots = true; |
|
564
|
|
|
} elseif ( $dots && !$args['show_all'] ) { |
|
|
|
|
|
|
565
|
|
|
$page_links[] = array( |
|
566
|
|
|
'class' => 'dots', |
|
567
|
|
|
'title' => __( '…' ) |
|
568
|
|
|
); |
|
569
|
|
|
$dots = false; |
|
570
|
|
|
} |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
return $page_links; |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* @deprecated 0.18.0 |
|
578
|
|
|
*/ |
|
579
|
|
|
static function get_current_url() { |
|
|
|
|
|
|
580
|
|
|
return TimberURLHelper::get_current_url(); |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
/** |
|
584
|
|
|
* @deprecated 0.18.0 |
|
585
|
|
|
*/ |
|
586
|
|
|
static function is_url( $url ) { |
|
|
|
|
|
|
587
|
|
|
return TimberURLHelper::is_url( $url ); |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
/** |
|
591
|
|
|
* @deprecated 0.18.0 |
|
592
|
|
|
*/ |
|
593
|
|
|
static function get_path_base() { |
|
|
|
|
|
|
594
|
|
|
return TimberURLHelper::get_path_base(); |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
/** |
|
598
|
|
|
* @deprecated 0.18.0 |
|
599
|
|
|
*/ |
|
600
|
|
|
static function get_rel_url( $url, $force = false ) { |
|
|
|
|
|
|
601
|
|
|
return TimberURLHelper::get_rel_url( $url, $force ); |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
/** |
|
605
|
|
|
* @deprecated 0.18.0 |
|
606
|
|
|
*/ |
|
607
|
|
|
static function is_local( $url ) { |
|
|
|
|
|
|
608
|
|
|
return TimberURLHelper::is_local( $url ); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
/** |
|
612
|
|
|
* @deprecated 0.18.0 |
|
613
|
|
|
*/ |
|
614
|
|
|
static function get_full_path( $src ) { |
|
|
|
|
|
|
615
|
|
|
return TimberURLHelper::get_full_path( $src ); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* @deprecated 0.18.0 |
|
620
|
|
|
*/ |
|
621
|
|
|
static function get_rel_path( $src ) { |
|
|
|
|
|
|
622
|
|
|
return TimberURLHelper::get_rel_path( $src ); |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
/** |
|
626
|
|
|
* @deprecated 0.18.0 |
|
627
|
|
|
*/ |
|
628
|
|
|
static function remove_double_slashes( $url ) { |
|
|
|
|
|
|
629
|
|
|
return TimberURLHelper::remove_double_slashes( $url ); |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
/** |
|
633
|
|
|
* @deprecated 0.18.0 |
|
634
|
|
|
*/ |
|
635
|
|
|
static function prepend_to_url( $url, $path ) { |
|
|
|
|
|
|
636
|
|
|
return TimberURLHelper::prepend_to_url( $url, $path ); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
/** |
|
640
|
|
|
* @deprecated 0.18.0 |
|
641
|
|
|
*/ |
|
642
|
|
|
static function preslashit( $path ) { |
|
|
|
|
|
|
643
|
|
|
return TimberURLHelper::preslashit( $path ); |
|
644
|
|
|
} |
|
645
|
|
|
|
|
646
|
|
|
/** |
|
647
|
|
|
* @deprecated 0.18.0 |
|
648
|
|
|
*/ |
|
649
|
|
|
static function is_external( $url ) { |
|
|
|
|
|
|
650
|
|
|
return TimberURLHelper::is_external( $url ); |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
/** |
|
654
|
|
|
* @deprecated 0.18.0 |
|
655
|
|
|
*/ |
|
656
|
|
|
static function download_url( $url, $timeout = 300 ) { |
|
|
|
|
|
|
657
|
|
|
return TimberURLHelper::download_url( $url, $timeout ); |
|
|
|
|
|
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* @deprecated 0.18.0 |
|
662
|
|
|
*/ |
|
663
|
|
|
static function get_params( $i = -1 ) { |
|
|
|
|
|
|
664
|
|
|
return TimberURLHelper::get_params( $i ); |
|
665
|
|
|
} |
|
666
|
|
|
|
|
667
|
|
|
} |
|
668
|
|
|
|