Timber - Version 0.16.1

Version Description

  • Bug fix on ugly permalinks for pagination
  • Fixed issue where posts retrieved via an array of IDs was truncated at the default post count
  • Fixed issue where loading terms from multi taxonomies (thanks @WL-hohoho)
  • Added support for post_class on TimberPost (thanks @slimndap)
  • new array filter to convert single-values into array in twig
  • Cleaned-up and added translation support to time_ago filter (thanks @WL-hohoho)
Download this release

Release Info

Developer jarednova
Plugin Icon 128x128 Timber
Version 0.16.1
Comparing to
See all releases

Code changes from version 0.16.0 to 0.16.1

Twig/lib/Twig/Compiler.php CHANGED
File without changes
Twig/lib/Twig/Environment.php CHANGED
File without changes
Twig/lib/Twig/Error.php CHANGED
File without changes
Twig/lib/Twig/ExpressionParser.php CHANGED
File without changes
Twig/lib/Twig/Extension/Core.php CHANGED
File without changes
Twig/lib/Twig/Extension/StringLoader.php CHANGED
File without changes
Twig/lib/Twig/Lexer.php CHANGED
File without changes
Twig/lib/Twig/LexerInterface.php CHANGED
File without changes
Twig/lib/Twig/Loader/Array.php CHANGED
File without changes
Twig/lib/Twig/Loader/Chain.php CHANGED
File without changes
Twig/lib/Twig/Loader/Filesystem.php CHANGED
File without changes
Twig/lib/Twig/Node/Expression/Call.php CHANGED
File without changes
Twig/lib/Twig/Node/Expression/GetAttr.php CHANGED
File without changes
Twig/lib/Twig/Node/If.php CHANGED
File without changes
Twig/lib/Twig/NodeVisitor/Optimizer.php CHANGED
File without changes
Twig/lib/Twig/NodeVisitor/SafeAnalysis.php CHANGED
File without changes
Twig/lib/Twig/Parser.php CHANGED
File without changes
Twig/lib/Twig/ParserInterface.php CHANGED
File without changes
Twig/lib/Twig/Template.php CHANGED
File without changes
Twig/lib/Twig/Token.php CHANGED
File without changes
Twig/lib/Twig/TokenParserInterface.php CHANGED
File without changes
functions/functions-twig.php CHANGED
@@ -25,10 +25,11 @@ class TimberTwig {
25
  $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
26
 
27
  /* other filters */
 
28
  $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
29
- $twig->addFilter('function', new Twig_Filter_Function(array(&$this, 'exec_function')));
30
  $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
31
- $twig->addFilter('pretags', new Twig_Filter_Function(array(&$this, 'twig_pretags')));
32
  $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
33
  $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
34
  $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
@@ -95,6 +96,14 @@ class TimberTwig {
95
  return $twig;
96
  }
97
 
 
 
 
 
 
 
 
 
98
  function exec_function($function_name){
99
  $args = func_get_args();
100
  array_shift($args);
@@ -209,35 +218,16 @@ function wp_resize_letterbox($src, $w, $h, $color = '#000000') {
209
  return null;
210
  }
211
 
212
- function twig_time_ago($from, $to = null) {
213
  $to = (($to === null) ? (time()) : ($to));
214
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
215
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));
216
 
217
- $units = array(
218
- "year" => 29030400, // seconds in a year (12 months)
219
- "month" => 2419200, // seconds in a month (4 weeks)
220
- "week" => 604800, // seconds in a week (7 days)
221
- "day" => 86400, // seconds in a day (24 hours)
222
- "hour" => 3600, // seconds in an hour (60 minutes)
223
- "minute" => 60, // seconds in a minute (60 seconds)
224
- "second" => 1 // 1 second
225
- );
226
-
227
- $diff = abs($from - $to);
228
- $suffix = (($from > $to) ? ("from now") : ("ago"));
229
- $output = '';
230
- foreach ($units as $unit => $mult) {
231
- if ($diff >= $mult) {
232
- $and = (($mult != 1) ? ("") : ("and "));
233
- $output .= ", " . $and . intval($diff / $mult) . " " . $unit . ((intval($diff / $mult) == 1) ? ("") : ("s"));
234
- $diff -= intval($diff / $mult) * $mult;
235
- break;
236
- }
237
  }
238
- $output .= " " . $suffix;
239
- $output = substr($output, strlen(", "));
240
- return $output;
241
  }
242
 
243
  function twig_body_class($body_classes) {
@@ -354,4 +344,4 @@ function twig_object_docs($obj) {
354
  return '<pre>' . (print_r($rets, true)) . '</pre>';
355
  }
356
 
357
- new TimberTwig();
25
  $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
26
 
27
  /* other filters */
28
+ $twig->addFilter('array', new Twig_Filter_Function(array($this, 'to_array')));
29
  $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
30
+ $twig->addFilter('function', new Twig_Filter_Function(array($this, 'exec_function')));
31
  $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
32
+ $twig->addFilter('pretags', new Twig_Filter_Function(array($this, 'twig_pretags')));
33
  $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
34
  $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
35
  $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
96
  return $twig;
97
  }
98
 
99
+ function to_array($arr){
100
+ if (is_array($arr)){
101
+ return $arr;
102
+ }
103
+ $arr = array($arr);
104
+ return $arr;
105
+ }
106
+
107
  function exec_function($function_name){
108
  $args = func_get_args();
109
  array_shift($args);
218
  return null;
219
  }
220
 
221
+ function twig_time_ago($from, $to = null, $format_past='%s ago', $format_future='%s from now') {
222
  $to = (($to === null) ? (time()) : ($to));
223
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
224
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));
225
 
226
+ if ($from < $to) {
227
+ return sprintf($format_past, human_time_diff($from, $to));
228
+ } else {
229
+ return sprintf($format_future, human_time_diff($to, $from));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  }
 
 
 
231
  }
232
 
233
  function twig_body_class($body_classes) {
344
  return '<pre>' . (print_r($rets, true)) . '</pre>';
345
  }
346
 
347
+ new TimberTwig();
functions/timber-helper.php CHANGED
@@ -396,7 +396,7 @@ class TimberHelper {
396
  'next_text' => __('Next &raquo;'),
397
  'end_size' => 1,
398
  'mid_size' => 2,
399
- 'type' => 'plain',
400
  'add_args' => false, // array of query args to add
401
  'add_fragment' => ''
402
  );
@@ -406,8 +406,9 @@ class TimberHelper {
406
 
407
  // Who knows what else people pass in $args
408
  $total = (int) $total;
409
- if ( $total < 2 )
410
  return;
 
411
  $current = (int) $current;
412
  $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
413
  $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
@@ -416,64 +417,46 @@ class TimberHelper {
416
  $page_links = array();
417
  $n = 0;
418
  $dots = false;
419
-
420
- if ( $prev_next && $current && 1 < $current ) :
421
  $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
422
  $link = str_replace('%#%', $current - 1, $link);
423
  if ( $add_args ){
424
  $link = add_query_arg( $add_args, $link );
425
  }
426
  $link .= $add_fragment;
427
- $page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $prev_text . '</a>';
428
- endif;
429
- for ( $n = 1; $n <= $total; $n++ ) :
430
  $n_display = number_format_i18n($n);
431
- if ( $n == $current ) :
432
- //$page_links[] = "<span class='page-numbers current'>$n_display</span>";
433
  $page_links[] = array('class' => 'page-number current', 'title' => $n_display, 'text' => $n_display);
434
  $dots = true;
435
- else :
436
- if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
437
  $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
438
  $link = str_replace('%#%', $n, $link);
439
  if ( $add_args ) {
440
  $link = add_query_arg( $add_args, $link );
441
  }
442
-
443
  $link = trailingslashit($link).ltrim($add_fragment, '/');
444
- //$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>$n_display</a>";
445
  $page_links[] = array('class' => 'page-number', 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), 'title' => $n_display);
446
  $dots = true;
447
- elseif ( $dots && !$show_all ) :
448
  $page_links[] = array('class' => 'dots', 'title' => __( '&hellip;' ));
449
- //$page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
450
  $dots = false;
451
- endif;
452
- endif;
453
- endfor;
454
- if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
455
  $link = str_replace('%_%', $format, $base);
456
  $link = str_replace('%#%', $current + 1, $link);
457
- if ( $add_args )
458
  $link = add_query_arg( $add_args, $link );
 
459
  $link = trailingslashit($link).$add_fragment;
460
-
461
- $page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
462
- endif;
463
- switch ( $type ) :
464
- case 'array' :
465
- return $page_links;
466
- break;
467
- case 'list' :
468
- $r .= "<ul class='page-numbers'>\n\t<li>";
469
- $r .= join("</li>\n\t<li>", $page_links);
470
- $r .= "</li>\n</ul>\n";
471
- break;
472
- default :
473
- $r = join("\n", $page_links);
474
- break;
475
- endswitch;
476
- return $r;
477
  }
478
  }
479
 
396
  'next_text' => __('Next &raquo;'),
397
  'end_size' => 1,
398
  'mid_size' => 2,
399
+ 'type' => 'array',
400
  'add_args' => false, // array of query args to add
401
  'add_fragment' => ''
402
  );
406
 
407
  // Who knows what else people pass in $args
408
  $total = (int) $total;
409
+ if ( $total < 2 ){
410
  return;
411
+ }
412
  $current = (int) $current;
413
  $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
414
  $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
417
  $page_links = array();
418
  $n = 0;
419
  $dots = false;
420
+ if ( $prev_next && $current && 1 < $current ){
 
421
  $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
422
  $link = str_replace('%#%', $current - 1, $link);
423
  if ( $add_args ){
424
  $link = add_query_arg( $add_args, $link );
425
  }
426
  $link .= $add_fragment;
427
+ $page_links[] = array('class' => 'prev page-numbers', 'link' => esc_url( apply_filters( 'paginate_links', $link )), 'title' => $prev_text);
428
+ }
429
+ for ( $n = 1; $n <= $total; $n++ ) {
430
  $n_display = number_format_i18n($n);
431
+ if ( $n == $current ) {
 
432
  $page_links[] = array('class' => 'page-number current', 'title' => $n_display, 'text' => $n_display);
433
  $dots = true;
434
+ } else {
435
+ if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) {
436
  $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
437
  $link = str_replace('%#%', $n, $link);
438
  if ( $add_args ) {
439
  $link = add_query_arg( $add_args, $link );
440
  }
 
441
  $link = trailingslashit($link).ltrim($add_fragment, '/');
 
442
  $page_links[] = array('class' => 'page-number', 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), 'title' => $n_display);
443
  $dots = true;
444
+ } elseif ( $dots && !$show_all ) {
445
  $page_links[] = array('class' => 'dots', 'title' => __( '&hellip;' ));
 
446
  $dots = false;
447
+ }
448
+ }
449
+ }
450
+ if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) {
451
  $link = str_replace('%_%', $format, $base);
452
  $link = str_replace('%#%', $current + 1, $link);
453
+ if ( $add_args ) {
454
  $link = add_query_arg( $add_args, $link );
455
+ }
456
  $link = trailingslashit($link).$add_fragment;
457
+ $page_links[] = array('class' => 'next page-numbers', 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), 'title' => $next_text);
458
+ }
459
+ return $page_links;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  }
461
  }
462
 
functions/timber-post.php CHANGED
@@ -375,7 +375,7 @@ class TimberPost extends TimberCore {
375
 
376
  } else {
377
  foreach ($terms as &$term) {
378
- $term = new $TermClass($term->term_id);
379
  }
380
  if ($merge && is_array($terms)) {
381
  $ret = array_merge($ret, $terms);
@@ -454,7 +454,7 @@ class TimberPost extends TimberCore {
454
  $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this);
455
  if ($value === null){
456
  $value = get_post_meta($this->ID, $field_name);
457
- if (is_array($value) && count($value == 1)){
458
  $value = $value[0];
459
  }
460
  }
@@ -547,4 +547,9 @@ class TimberPost extends TimberCore {
547
  return $this->get_title();
548
  }
549
 
 
 
 
 
 
550
  }
375
 
376
  } else {
377
  foreach ($terms as &$term) {
378
+ $term = new $TermClass($term->term_id, $tax);
379
  }
380
  if ($merge && is_array($terms)) {
381
  $ret = array_merge($ret, $terms);
454
  $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this);
455
  if ($value === null){
456
  $value = get_post_meta($this->ID, $field_name);
457
+ if (is_array($value) && count($value) == 1){
458
  $value = $value[0];
459
  }
460
  }
547
  return $this->get_title();
548
  }
549
 
550
+ function post_class($class='') {
551
+ return implode(' ', get_post_class($class,$this->ID));
552
+ }
553
+
554
+
555
  }
functions/timber-site.php CHANGED
@@ -2,14 +2,20 @@
2
 
3
  class TimberSite extends TimberCore {
4
  function __construct($site_name_or_id = null){
5
- $this->init($site_name_or_id);
 
 
 
 
6
  }
7
 
8
- function init($site_name_or_id){
9
  if ($site_name_or_id === null){
10
  //this is necessary for some reason, otherwise returns 1 all the time
11
- restore_current_blog();
12
- $site_name_or_id = get_current_blog_id();
 
 
13
  }
14
  $info = get_blog_details($site_name_or_id);
15
  $this->import($info);
@@ -17,11 +23,18 @@
17
  $this->name = $this->blogname;
18
  $this->title = $this->blogname;
19
  $theme_slug = get_blog_option($info->blog_id, 'stylesheet');
20
- //echo 'init '.$theme_slug;
21
  $this->theme = new TimberTheme($theme_slug);
22
  $this->description = get_blog_option($info->blog_id, 'blogdescription');
23
  }
24
 
 
 
 
 
 
 
 
 
25
  function __get($field){
26
  if (!isset($this->$field)){
27
  $this->$field = get_blog_option($this->ID, $field);
2
 
3
  class TimberSite extends TimberCore {
4
  function __construct($site_name_or_id = null){
5
+ if (is_multisite()){
6
+ $this->init_with_multisite($site_name_or_id);
7
+ } else {
8
+ $this->init();
9
+ }
10
  }
11
 
12
+ function init_with_multisite($site_name_or_id){
13
  if ($site_name_or_id === null){
14
  //this is necessary for some reason, otherwise returns 1 all the time
15
+ if (is_multisite()){
16
+ restore_current_blog();
17
+ $site_name_or_id = get_current_blog_id();
18
+ }
19
  }
20
  $info = get_blog_details($site_name_or_id);
21
  $this->import($info);
23
  $this->name = $this->blogname;
24
  $this->title = $this->blogname;
25
  $theme_slug = get_blog_option($info->blog_id, 'stylesheet');
 
26
  $this->theme = new TimberTheme($theme_slug);
27
  $this->description = get_blog_option($info->blog_id, 'blogdescription');
28
  }
29
 
30
+ function init(){
31
+ $this->name = get_bloginfo('name');
32
+ $this->title = $this->name;
33
+ $this->description = get_bloginfo('description');
34
+ $this->url = get_bloginfo('url');
35
+ $this->language = get_bloginfo('language');
36
+ }
37
+
38
  function __get($field){
39
  if (!isset($this->$field)){
40
  $this->$field = get_blog_option($this->ID, $field);
functions/timber-term.php CHANGED
@@ -10,10 +10,12 @@ class TimberTerm extends TimberCore {
10
 
11
  public static $representation = 'term';
12
 
13
- function __construct($tid = null) {
14
  if ($tid === null) {
15
  $tid = $this->get_term_from_query();
16
  }
 
 
17
  $this->init($tid);
18
  }
19
 
@@ -61,12 +63,17 @@ class TimberTerm extends TimberCore {
61
  return $tid;
62
  }
63
  $tid = self::get_tid($tid);
64
- global $wpdb;
65
- $query = $wpdb->prepare("SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $tid);
66
- $tax = $wpdb->get_var($query);
67
- if (isset($tax) && strlen($tax)) {
68
- $term = get_term($tid, $tax);
69
- return $term;
 
 
 
 
 
70
  }
71
  return null;
72
  }
10
 
11
  public static $representation = 'term';
12
 
13
+ function __construct($tid = null, $tax='') {
14
  if ($tid === null) {
15
  $tid = $this->get_term_from_query();
16
  }
17
+ if(strlen($tax))
18
+ $this->taxonomy = $tax;
19
  $this->init($tid);
20
  }
21
 
63
  return $tid;
64
  }
65
  $tid = self::get_tid($tid);
66
+
67
+ if(isset($this->taxonomy) && strlen($this->taxonomy)) {
68
+ return get_term($tid, $this->taxonomy);
69
+ } else {
70
+ global $wpdb;
71
+ $query = $wpdb->prepare("SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $tid);
72
+ $tax = $wpdb->get_var($query);
73
+ if (isset($tax) && strlen($tax)) {
74
+ $term = get_term($tid, $tax);
75
+ return $term;
76
+ }
77
  }
78
  return null;
79
  }
readme.txt CHANGED
@@ -2,7 +2,7 @@
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.5
5
- Stable tag: 0.16.0
6
  Tested up to: 3.7.1
7
  PHP version: 5.3.0 or greater
8
  License: GPLv2 or later
@@ -41,6 +41,14 @@ Timber is great for any WordPress developer who cares about writing good, mainta
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
 
 
 
44
  = 0.16.0 =
45
  * TimberTheme is now available in default context as .theme
46
  * Post meta now respects arrays (watch out for some possible compatiblity issues here)
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.5
5
+ Stable tag: 0.16.1
6
  Tested up to: 3.7.1
7
  PHP version: 5.3.0 or greater
8
  License: GPLv2 or later
41
 
42
  == Changelog ==
43
 
44
+ = 0.16.1 =
45
+ * Bug fix on ugly permalinks for pagination
46
+ * Fixed issue where posts retrieved via an array of IDs was truncated at the default post count
47
+ * Fixed issue where loading terms from multi taxonomies (thanks @WL-hohoho)
48
+ * Added support for post_class on TimberPost (thanks @slimndap)
49
+ * new `array` filter to convert single-values into array in twig
50
+ * Cleaned-up and added translation support to `time_ago` filter (thanks @WL-hohoho)
51
+
52
  = 0.16.0 =
53
  * TimberTheme is now available in default context as .theme
54
  * Post meta now respects arrays (watch out for some possible compatiblity issues here)
timber.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Timber
4
  Plugin URI: http://timber.upstatement.com
5
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
6
  Author: Jared Novack + Upstatement
7
- Version: 0.16.0
8
  Author URI: http://upstatement.com/
9
  */
10
 
@@ -200,7 +200,7 @@ class Timber {
200
  if (!is_array($query) || !count($query)) {
201
  return null;
202
  }
203
- $results = get_posts(array('post_type'=>'any', 'post__in' =>$query, 'orderby' => 'post__in'));
204
  return self::handle_post_results($results, $PostClass);
205
  }
206
 
@@ -512,12 +512,15 @@ class Timber {
512
  $args['total'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
513
  if (strlen(trim(get_option('permalink_structure')))){
514
  $args['format'] = 'page/%#%';
 
 
 
 
515
  }
516
  $args['type'] = 'array';
517
 
518
  $args['current'] = max( 1, get_query_var('paged') );
519
  $args['mid_size'] = max(9 - $args['current'], 3);
520
- $args['base'] = trailingslashit(get_pagenum_link(0)).'%_%';
521
  $args['prev_next'] = false;
522
  $args = array_merge($args, $prefs);
523
  $data['pages'] = TimberHelper::paginate_links($args);
4
  Plugin URI: http://timber.upstatement.com
5
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
6
  Author: Jared Novack + Upstatement
7
+ Version: 0.16.1
8
  Author URI: http://upstatement.com/
9
  */
10
 
200
  if (!is_array($query) || !count($query)) {
201
  return null;
202
  }
203
+ $results = get_posts(array('post_type'=>'any', 'post__in' =>$query, 'orderby' => 'post__in', 'numberposts' => -1));
204
  return self::handle_post_results($results, $PostClass);
205
  }
206
 
512
  $args['total'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
513
  if (strlen(trim(get_option('permalink_structure')))){
514
  $args['format'] = 'page/%#%';
515
+ $args['base'] = trailingslashit(get_pagenum_link(0)).'%_%';
516
+ } else {
517
+ $big = 999999999;
518
+ $args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );
519
  }
520
  $args['type'] = 'array';
521
 
522
  $args['current'] = max( 1, get_query_var('paged') );
523
  $args['mid_size'] = max(9 - $args['current'], 3);
 
524
  $args['prev_next'] = false;
525
  $args = array_merge($args, $prefs);
526
  $data['pages'] = TimberHelper::paginate_links($args);