Complex classes like TimberHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TimberHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 ) { |
||
66 | |||
67 | /** |
||
68 | * @internal |
||
69 | * @param string $slug |
||
70 | * @param integer $lock_timeout |
||
71 | */ |
||
72 | static function _lock_transient( $slug, $lock_timeout ) { |
||
75 | |||
76 | /** |
||
77 | * @internal |
||
78 | * @param string $slug |
||
79 | */ |
||
80 | static function _unlock_transient( $slug ) { |
||
83 | |||
84 | /** |
||
85 | * @internal |
||
86 | * @param string $slug |
||
87 | */ |
||
88 | static function _is_transient_locked( $slug ) { |
||
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() { |
||
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 ) { |
||
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 ) ) { |
||
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 ) { |
||
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 ) { |
||
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' ) { |
||
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 ) { |
||
444 | |||
445 | /* Bool Utilities |
||
446 | ======================== */ |
||
447 | |||
448 | /** |
||
449 | * |
||
450 | * |
||
451 | * @param mixed $value |
||
452 | * @return bool |
||
453 | */ |
||
454 | public static function is_true( $value ) { |
||
465 | |||
466 | /** |
||
467 | * |
||
468 | * |
||
469 | * @param int $i |
||
470 | * @return bool |
||
471 | */ |
||
472 | public static function iseven( $i ) { |
||
475 | |||
476 | /** |
||
477 | * |
||
478 | * |
||
479 | * @param int $i |
||
480 | * @return bool |
||
481 | */ |
||
482 | public static function isodd( $i ) { |
||
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() ) { |
||
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 |
||
575 | |||
576 | /** |
||
577 | * @deprecated 0.18.0 |
||
578 | */ |
||
579 | static function get_current_url() { |
||
582 | |||
583 | /** |
||
584 | * @deprecated 0.18.0 |
||
585 | */ |
||
586 | static function is_url( $url ) { |
||
589 | |||
590 | /** |
||
591 | * @deprecated 0.18.0 |
||
592 | */ |
||
593 | static function get_path_base() { |
||
596 | |||
597 | /** |
||
598 | * @deprecated 0.18.0 |
||
599 | */ |
||
600 | static function get_rel_url( $url, $force = false ) { |
||
603 | |||
604 | /** |
||
605 | * @deprecated 0.18.0 |
||
606 | */ |
||
607 | static function is_local( $url ) { |
||
610 | |||
611 | /** |
||
612 | * @deprecated 0.18.0 |
||
613 | */ |
||
614 | static function get_full_path( $src ) { |
||
617 | |||
618 | /** |
||
619 | * @deprecated 0.18.0 |
||
620 | */ |
||
621 | static function get_rel_path( $src ) { |
||
624 | |||
625 | /** |
||
626 | * @deprecated 0.18.0 |
||
627 | */ |
||
628 | static function remove_double_slashes( $url ) { |
||
631 | |||
632 | /** |
||
633 | * @deprecated 0.18.0 |
||
634 | */ |
||
635 | static function prepend_to_url( $url, $path ) { |
||
638 | |||
639 | /** |
||
640 | * @deprecated 0.18.0 |
||
641 | */ |
||
642 | static function preslashit( $path ) { |
||
645 | |||
646 | /** |
||
647 | * @deprecated 0.18.0 |
||
648 | */ |
||
649 | static function is_external( $url ) { |
||
652 | |||
653 | /** |
||
654 | * @deprecated 0.18.0 |
||
655 | */ |
||
656 | static function download_url( $url, $timeout = 300 ) { |
||
659 | |||
660 | /** |
||
661 | * @deprecated 0.18.0 |
||
662 | */ |
||
663 | static function get_params( $i = -1 ) { |
||
666 | |||
667 | } |
||
668 |