Timber - Version 0.10.7

Version Description

  • more normalization of menus, users
  • fixed bug in post.get_content (thanks @paulwilde)
  • fixed bug in way menu items with children got their children (thanks @EloB)
Download this release

Release Info

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

Code changes from version 0.10.6 to 0.10.7

functions/functions-twig.php CHANGED
@@ -1,215 +1,182 @@
1
  <?php
2
 
3
- class TimberTwig
4
- {
5
 
6
- function __construct()
7
- {
8
- add_action('twig_apply_filters', array(&$this, 'add_twig_filters'));
9
- }
10
-
11
- /**
12
- * @param Twig_Environment $twig
13
- * @return Twig_Environment
14
- */
15
- function add_twig_filters($twig)
16
- {
17
- $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
18
- $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
19
- $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
20
- $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
21
- $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
22
- $twig->addFilter('docs', new Twig_Filter_function('twig_object_docs'));
23
-
24
- $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
25
- $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
26
- $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
27
- $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
28
- $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
29
- $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
30
- $twig->addFilter('function', new Twig_Filter_Function(array(&$this, 'exec_function')));
31
-
32
- $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
33
- $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
34
- $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
35
- $twig->addFilter('pretags', new Twig_Filter_Function(array(&$this, 'twig_pretags')));
36
- $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
37
- $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
38
- $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
39
- $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
40
-
41
- $twig->addFunction('bloginfo', new Twig_SimpleFunction('bloginfo', function($show = '', $filter = 'raw'){
42
- return get_bloginfo($show, $filter);
43
- }));
44
- $twig->addFunction('__', new Twig_SimpleFunction('__', function($text, $domain = 'default'){
45
- return __($text, $domain);
46
- }));
47
-
48
- $twig = apply_filters('get_twig', $twig);
49
-
50
- return $twig;
51
- }
52
-
53
- function exec_function($function_name){
54
- return call_user_func(trim($function_name));
55
- }
56
-
57
- function twig_pretags( $content ) {
58
- return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , array(&$this, 'convert_pre_entities'), $content );
59
- }
60
- function convert_pre_entities( $matches ) {
61
- return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
62
- }
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- function add_dir_name_to_locations($locs)
66
- {
67
- $locs = array_filter($locs);
68
- foreach ($locs as &$loc) {
69
- $loc = trailingslashit($loc) . trailingslashit(self::$dir_name);
70
  }
71
- return $locs;
72
- }
73
 
74
- function template_exists($file, $dirs)
75
- {
76
- if (is_string($dirs)) {
77
- $dirs = array($dirs);
78
  }
79
- foreach ($dirs as $dir) {
80
- $look_for = trailingslashit($dir) . trailingslashit(self::$dir_name) . $file;
81
- if (file_exists($look_for)) {
82
- return true;
83
- }
84
  }
85
- return false;
86
- }
87
 
88
- function twig_choose_template($filenames, $dirs)
89
- {
90
- if (is_array($filenames)) {
91
- /* its an array so we have to figure out which one the dev wants */
92
- foreach ($filenames as $filename) {
93
- if (self::template_exists($filename, $dirs)) {
94
- return $filename;
95
  }
96
- }
97
- return false;
98
- } else {
99
- /* its a single, but we still need to figure out if it exists, default to index.html */
100
- // if (!twig_template_exists($filenames, $dirs)){
101
- // $filenames = 'index.html';
102
- // }
103
  }
104
- return $filenames;
105
- }
106
-
107
 
108
- function get_twig($loader)
109
- {
 
 
 
 
 
 
 
 
 
 
110
 
111
- $loader_loc = TIMBER_LOC . '/Twig/lib/Twig/Autoloader.php';
112
- require_once($loader_loc);
113
- Twig_Autoloader::register();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
- $params = array('debug' => WP_DEBUG, 'autoescape' => false);
116
- if (Timber::$cache) {
117
- $params['cache'] = TIMBER_LOC . '/twig-cache';
 
 
 
 
 
 
 
 
 
 
 
118
  }
119
- $twig = new Twig_Environment($loader, $params);
120
- $twig->addExtension(new Twig_Extension_Debug());
121
- $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
122
- $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
123
- $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
124
- $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
125
- $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
126
- $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
127
- $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
128
- $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
129
- $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
130
- $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
131
- $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
132
-
133
- $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
134
- $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
135
- $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
136
-
137
- $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
138
- $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
139
- $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
140
- $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
141
-
142
- $twig = apply_filters('get_twig', $twig);
143
- return $twig;
144
- }
145
  }
146
 
147
- function twig_shortcodes($text)
148
- {
149
- return do_shortcode($text);
150
- //apply_filters('the_content', ($text));
151
  }
152
 
153
- function twig_get_class($this)
154
- {
155
- return get_class($this);
156
  }
157
 
158
- function twig_get_type($this)
159
- {
160
- return gettype($this);
161
  }
162
 
163
- function wp_resize_external($src, $w, $h)
164
- {
165
- $upload = wp_upload_dir();
166
- $dir = $upload['path'];
167
- $file = parse_url($src);
168
- $path_parts = pathinfo($file['path']);
169
- $basename = $path_parts['filename'];
170
- $newbase = $basename . '-r-' . $w . 'x' . $h;
171
- $ext = $path_parts['extension'];
172
-
173
- $new_root_path = $dir . '/' . $newbase . '.' . $ext;
174
-
175
- $new_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $new_root_path);
176
- if (strpos($new_path, '/') != 0) {
177
- $new_path = '/' . $new_path;
178
- }
179
- $ret = array('new_root_path' => $new_root_path, 'old_root_path' => $dir . '/' . $basename . '.' . $ext, 'new_path' => $new_path);
180
 
181
- if (file_exists($new_root_path)) {
 
 
 
182
  return $ret;
183
- }
184
- $image = WPHelper::sideload_image($src);
185
- return $ret;
186
  }
187
 
188
- function hexrgb($hexstr)
189
- {
190
- $int = hexdec($hexstr);
191
-
192
- return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
193
  }
194
 
195
- function wp_resize_letterbox($src, $w, $h, $color = '#000000')
196
- {
197
- $old_file = WPHelper::get_full_path($src);
198
- $new_file = WPHelper::get_letterbox_file_path($src, $w, $h);
199
- $new_file_rel = WPHelper::get_letterbox_file_rel($src, $w, $h);
200
- $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
201
- if (file_exists($new_file_boxed)) {
202
- $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
203
- return $new_file_rel;
204
- }
205
 
206
- $bg = imagecreatetruecolor($w, $h);
207
- $c = hexrgb($color);
208
 
209
- $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
210
- imagefill($bg, 0, 0, $white);
211
 
212
- $image = wp_get_image_editor($old_file);
213
  if (!is_wp_error($image)) {
214
  $current_size = $image->get_size();
215
  $ow = $current_size['width'];
@@ -249,8 +216,7 @@ function wp_resize_letterbox($src, $w, $h, $color = '#000000')
249
  return null;
250
  }
251
 
252
- function wp_resize($src, $w, $h = 0)
253
- {
254
  $root = $_SERVER['DOCUMENT_ROOT'];
255
  if (strstr($src, 'http')) {
256
  //Its a URL so we need to fetch it
@@ -306,123 +272,84 @@ function wp_resize($src, $w, $h = 0)
306
  }
307
  }
308
 
309
- function twig_twitterify($ret)
310
- {
311
- $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
312
- $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
313
- $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
314
- $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
315
- $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
316
- $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
317
- $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
318
- return $ret;
319
- }
320
 
321
- function twig_time_ago($from, $to = null)
322
- {
323
- $to = (($to === null) ? (time()) : ($to));
324
- $to = ((is_int($to)) ? ($to) : (strtotime($to)));
325
- $from = ((is_int($from)) ? ($from) : (strtotime($from)));
326
-
327
- $units = array(
328
- "year" => 29030400, // seconds in a year (12 months)
329
- "month" => 2419200, // seconds in a month (4 weeks)
330
- "week" => 604800, // seconds in a week (7 days)
331
- "day" => 86400, // seconds in a day (24 hours)
332
- "hour" => 3600, // seconds in an hour (60 minutes)
333
- "minute" => 60, // seconds in a minute (60 seconds)
334
- "second" => 1 // 1 second
335
- );
336
-
337
- $diff = abs($from - $to);
338
- $suffix = (($from > $to) ? ("from now") : ("ago"));
339
- $output = '';
340
- foreach ($units as $unit => $mult) {
341
- if ($diff >= $mult) {
342
- $and = (($mult != 1) ? ("") : ("and "));
343
- $output .= ", " . $and . intval($diff / $mult) . " " . $unit . ((intval($diff / $mult) == 1) ? ("") : ("s"));
344
- $diff -= intval($diff / $mult) * $mult;
345
- break;
346
- }
347
- }
348
- $output .= " " . $suffix;
349
- $output = substr($output, strlen(", "));
350
- return $output;
351
- }
352
 
353
- function twig_wp_sidebar($arg)
354
- {
355
- get_sidebar($arg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  }
357
 
358
- function twig_wp_title()
359
- {
360
- return wp_title('|', false, 'right');
361
  }
362
 
363
- function twig_body_class($body_classes)
364
- {
365
- ob_start();
366
- if (is_array($body_classes)) {
367
- $body_classes = explode(' ', $body_classes);
368
- }
369
- body_class($body_classes);
370
- $return = ob_get_contents();
371
- ob_end_clean();
372
- return $return;
373
  }
374
 
375
- function render_twig_string($string, $data = array())
376
- {
377
- $loader = new Twig_Loader_String();
378
- $twig = new Twig_Environment($loader);
379
- return $twig->render($string, $data);
380
  }
381
 
382
- function get_calling_script_dir($backtrace)
383
- {
384
- $caller = $backtrace[0]['file'];
385
- $pathinfo = pathinfo($caller);
386
- $dir = $pathinfo['dirname'];
387
- return $dir . '/';
388
  }
389
 
390
  //deprecated
391
- function render_twig($filenames, $data = array(), $echo = true)
392
- {
393
- $caller = Timber::get_calling_script_dir();
394
- $loader = new TimberLoader($caller);
395
- $file = $loader->choose_template($filenames);
396
- $output = '';
397
- if (strlen($file)) {
398
- $output = $loader->render($file, $data);
399
- }
400
- if ($echo) {
401
- echo $output;
402
- }
403
- return $output;
404
- }
405
 
406
- function twig_get_src_from_attachment_id($aid)
407
- {
408
  return WPHelper::get_image_path($aid);
409
  }
410
 
411
-
412
-
413
- function twig_get_path($url)
414
- {
415
- $url = parse_url($url);
416
- return $url['path'];
417
  }
418
 
419
- function twig_make_excerpt($text, $length = 55)
420
- {
421
- return wp_trim_words($text, $length);
422
  }
423
 
424
- function twig_invoke($method, $obj)
425
- {
426
  $product = '';
427
  $totalParams = $method->getNumberOfParameters();
428
  $reqParams = $method->getNumberOfRequiredParameters();
@@ -454,18 +381,15 @@ function twig_invoke($method, $obj)
454
  return $product;
455
  }
456
 
457
- function twig_print_r($arr)
458
- {
459
- return print_r($arr, true);
460
  }
461
 
462
- function twig_print_a($arr)
463
- {
464
- return '<pre>' . twig_object_docs($arr, true) . '</pre>';
465
  }
466
 
467
- function twig_object_docs($obj)
468
- {
469
  if (!class_exists(get_class($obj))){
470
  return false;
471
  }
@@ -497,21 +421,4 @@ function twig_object_docs($obj)
497
  return '<pre>' . (print_r($rets, true)) . '</pre>';
498
  }
499
 
500
- function twig_img_to_jpg($src)
501
- {
502
- $output = str_replace('.png', '.jpg', $src);
503
- $oldpath = $_SERVER['DOCUMENT_ROOT'] . $src;
504
- $newpath = $_SERVER['DOCUMENT_ROOT'] . $output;
505
- if (file_exists($newpath)) {
506
- return $output;
507
- }
508
- //make it!
509
- $image = wp_get_image_editor($oldpath);
510
- if (!is_wp_error($image)){
511
- $image->save($newpath);
512
- return $output;
513
- }
514
- return $src;
515
- }
516
-
517
- new TimberTwig();
1
  <?php
2
 
3
+ class TimberTwig {
 
4
 
5
+ function __construct() {
6
+ add_action('twig_apply_filters', array(&$this, 'add_twig_filters'));
7
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ /**
10
+ * @param Twig_Environment $twig
11
+ * @return Twig_Environment
12
+ */
13
+ function add_twig_filters($twig) {
14
+ $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
15
+ $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
16
+ $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
17
+ $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
18
+ $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
19
+ $twig->addFilter('docs', new Twig_Filter_function('twig_object_docs'));
20
+
21
+ $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
22
+ $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
23
+ $twig->addFilter('tojpg', new Twig_Filter_Function(array(&$this, 'img_to_jpg')));
24
+ $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
25
+ $twig->addFilter('twitterify', new Twig_Filter_Function(array('WPHelper', 'twitterify')));
26
+ $twig->addFilter('twitterfy', new Twig_Filter_Function(array('WPHelper', 'twitterify')));
27
+ $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
28
+ $twig->addFilter('function', new Twig_Filter_Function(array(&$this, 'exec_function')));
29
+
30
+ $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
31
+ $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
32
+ $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
33
+ $twig->addFilter('pretags', new Twig_Filter_Function(array(&$this, 'twig_pretags')));
34
+ $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
35
+ $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
36
+ $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
37
+
38
+ $twig->addFunction('bloginfo', new Twig_SimpleFunction('bloginfo', function($show = '', $filter = 'raw'){
39
+ return get_bloginfo($show, $filter);
40
+ }));
41
+ $twig->addFunction('__', new Twig_SimpleFunction('__', function($text, $domain = 'default'){
42
+ return __($text, $domain);
43
+ }));
44
+
45
+ $twig = apply_filters('get_twig', $twig);
46
+
47
+ return $twig;
48
+ }
49
 
50
+ function exec_function($function_name){
51
+ $args = func_get_args();
52
+ array_shift($args);
53
+ return call_user_func_array(trim($function_name), ($args));
 
54
  }
 
 
55
 
56
+ function twig_pretags( $content ) {
57
+ return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , array(&$this, 'convert_pre_entities'), $content );
 
 
58
  }
59
+
60
+ function convert_pre_entities( $matches ) {
61
+ return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
 
 
62
  }
 
 
63
 
64
+ function add_dir_name_to_locations($locs) {
65
+ $locs = array_filter($locs);
66
+ foreach ($locs as &$loc) {
67
+ $loc = trailingslashit($loc) . trailingslashit(self::$dir_name);
 
 
 
68
  }
69
+ return $locs;
 
 
 
 
 
 
70
  }
 
 
 
71
 
72
+ function template_exists($file, $dirs) {
73
+ if (is_string($dirs)) {
74
+ $dirs = array($dirs);
75
+ }
76
+ foreach ($dirs as $dir) {
77
+ $look_for = trailingslashit($dir) . trailingslashit(self::$dir_name) . $file;
78
+ if (file_exists($look_for)) {
79
+ return true;
80
+ }
81
+ }
82
+ return false;
83
+ }
84
 
85
+ function twig_choose_template($filenames, $dirs) {
86
+ if (is_array($filenames)) {
87
+ /* its an array so we have to figure out which one the dev wants */
88
+ foreach ($filenames as $filename) {
89
+ if (self::template_exists($filename, $dirs)) {
90
+ return $filename;
91
+ }
92
+ }
93
+ return false;
94
+ } else {
95
+ /* its a single, but we still need to figure out if it exists, default to index.html */
96
+ // if (!twig_template_exists($filenames, $dirs)){
97
+ // $filenames = 'index.html';
98
+ // }
99
+ }
100
+ return $filenames;
101
+ }
102
 
103
+ function img_to_jpg($src) {
104
+ $output = str_replace('.png', '.jpg', $src);
105
+ $oldpath = $_SERVER['DOCUMENT_ROOT'] . $src;
106
+ $newpath = $_SERVER['DOCUMENT_ROOT'] . $output;
107
+ if (file_exists($newpath)) {
108
+ return $output;
109
+ }
110
+ //make it!
111
+ $image = wp_get_image_editor($oldpath);
112
+ if (!is_wp_error($image)){
113
+ $image->save($newpath);
114
+ return $output;
115
+ }
116
+ return $src;
117
  }
118
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  }
120
 
121
+ function twig_shortcodes($text) {
122
+ return do_shortcode($text);
123
+ //apply_filters('the_content', ($text));
 
124
  }
125
 
126
+ function twig_get_class($this) {
127
+ return get_class($this);
 
128
  }
129
 
130
+ function twig_get_type($this) {
131
+ return gettype($this);
 
132
  }
133
 
134
+ function wp_resize_external($src, $w, $h) {
135
+ $upload = wp_upload_dir();
136
+ $dir = $upload['path'];
137
+ $file = parse_url($src);
138
+ $path_parts = pathinfo($file['path']);
139
+ $basename = $path_parts['filename'];
140
+ $newbase = $basename . '-r-' . $w . 'x' . $h;
141
+ $ext = $path_parts['extension'];
142
+
143
+ $new_root_path = $dir . '/' . $newbase . '.' . $ext;
144
+
145
+ $new_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $new_root_path);
146
+ if (strpos($new_path, '/') != 0) {
147
+ $new_path = '/' . $new_path;
148
+ }
149
+ $ret = array('new_root_path' => $new_root_path, 'old_root_path' => $dir . '/' . $basename . '.' . $ext, 'new_path' => $new_path);
 
150
 
151
+ if (file_exists($new_root_path)) {
152
+ return $ret;
153
+ }
154
+ $image = WPHelper::sideload_image($src);
155
  return $ret;
 
 
 
156
  }
157
 
158
+ function hexrgb($hexstr) {
159
+ $int = hexdec($hexstr);
160
+ return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
 
 
161
  }
162
 
163
+ function wp_resize_letterbox($src, $w, $h, $color = '#000000') {
164
+ $old_file = WPHelper::get_full_path($src);
165
+ $new_file = WPHelper::get_letterbox_file_path($src, $w, $h);
166
+ $new_file_rel = WPHelper::get_letterbox_file_rel($src, $w, $h);
167
+ $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
168
+ if (file_exists($new_file_boxed)) {
169
+ $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
170
+ return $new_file_rel;
171
+ }
 
172
 
173
+ $bg = imagecreatetruecolor($w, $h);
174
+ $c = hexrgb($color);
175
 
176
+ $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
177
+ imagefill($bg, 0, 0, $white);
178
 
179
+ $image = wp_get_image_editor($old_file);
180
  if (!is_wp_error($image)) {
181
  $current_size = $image->get_size();
182
  $ow = $current_size['width'];
216
  return null;
217
  }
218
 
219
+ function wp_resize($src, $w, $h = 0) {
 
220
  $root = $_SERVER['DOCUMENT_ROOT'];
221
  if (strstr($src, 'http')) {
222
  //Its a URL so we need to fetch it
272
  }
273
  }
274
 
 
 
 
 
 
 
 
 
 
 
 
275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
+ function twig_time_ago($from, $to = null) {
278
+ $to = (($to === null) ? (time()) : ($to));
279
+ $to = ((is_int($to)) ? ($to) : (strtotime($to)));
280
+ $from = ((is_int($from)) ? ($from) : (strtotime($from)));
281
+
282
+ $units = array(
283
+ "year" => 29030400, // seconds in a year (12 months)
284
+ "month" => 2419200, // seconds in a month (4 weeks)
285
+ "week" => 604800, // seconds in a week (7 days)
286
+ "day" => 86400, // seconds in a day (24 hours)
287
+ "hour" => 3600, // seconds in an hour (60 minutes)
288
+ "minute" => 60, // seconds in a minute (60 seconds)
289
+ "second" => 1 // 1 second
290
+ );
291
+
292
+ $diff = abs($from - $to);
293
+ $suffix = (($from > $to) ? ("from now") : ("ago"));
294
+ $output = '';
295
+ foreach ($units as $unit => $mult) {
296
+ if ($diff >= $mult) {
297
+ $and = (($mult != 1) ? ("") : ("and "));
298
+ $output .= ", " . $and . intval($diff / $mult) . " " . $unit . ((intval($diff / $mult) == 1) ? ("") : ("s"));
299
+ $diff -= intval($diff / $mult) * $mult;
300
+ break;
301
+ }
302
+ }
303
+ $output .= " " . $suffix;
304
+ $output = substr($output, strlen(", "));
305
+ return $output;
306
  }
307
 
308
+ function twig_wp_title() {
309
+ return wp_title('|', false, 'right');
 
310
  }
311
 
312
+ function twig_body_class($body_classes) {
313
+ ob_start();
314
+ if (is_array($body_classes)) {
315
+ $body_classes = explode(' ', $body_classes);
316
+ }
317
+ body_class($body_classes);
318
+ $return = ob_get_contents();
319
+ ob_end_clean();
320
+ return $return;
 
321
  }
322
 
323
+ function render_twig_string($string, $data = array()) {
324
+ $loader = new Twig_Loader_String();
325
+ $twig = new Twig_Environment($loader);
326
+ return $twig->render($string, $data);
 
327
  }
328
 
329
+ function get_calling_script_dir($backtrace) {
330
+ $caller = $backtrace[0]['file'];
331
+ $pathinfo = pathinfo($caller);
332
+ $dir = $pathinfo['dirname'];
333
+ return $dir . '/';
 
334
  }
335
 
336
  //deprecated
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
+
339
+ function twig_get_src_from_attachment_id($aid) {
340
  return WPHelper::get_image_path($aid);
341
  }
342
 
343
+ function twig_get_path($url) {
344
+ $url = parse_url($url);
345
+ return $url['path'];
 
 
 
346
  }
347
 
348
+ function twig_make_excerpt($text, $length = 55){
349
+ return wp_trim_words($text, $length);
 
350
  }
351
 
352
+ function twig_invoke($method, $obj) {
 
353
  $product = '';
354
  $totalParams = $method->getNumberOfParameters();
355
  $reqParams = $method->getNumberOfRequiredParameters();
381
  return $product;
382
  }
383
 
384
+ function twig_print_r($arr) {
385
+ return print_r($arr, true);
 
386
  }
387
 
388
+ function twig_print_a($arr) {
389
+ return '<pre>' . twig_object_docs($arr, true) . '</pre>';
 
390
  }
391
 
392
+ function twig_object_docs($obj) {
 
393
  if (!class_exists(get_class($obj))){
394
  return false;
395
  }
421
  return '<pre>' . (print_r($rets, true)) . '</pre>';
422
  }
423
 
424
+ new TimberTwig();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
functions/functions-wp-helper.php CHANGED
@@ -409,4 +409,15 @@ class WPHelper
409
  return ($i % 2) != 0;
410
  }
411
 
 
 
 
 
 
 
 
 
 
 
 
412
  }
409
  return ($i % 2) != 0;
410
  }
411
 
412
+ public static function twitterify($ret) {
413
+ $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
414
+ $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
415
+ $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
416
+ $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
417
+ $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
418
+ $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
419
+ $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
420
+ return $ret;
421
+ }
422
+
423
  }
objects/timber-loader.php CHANGED
@@ -5,13 +5,11 @@ class TimberLoader
5
 
6
  var $locations;
7
 
8
- function __construct($caller = false)
9
- {
10
  $this->locations = $this->get_locations($caller);
11
  }
12
 
13
- function render($file, $data = null)
14
- {
15
  $twig = $this->get_twig();
16
  return $twig->render($file, $data);
17
  }
@@ -121,8 +119,7 @@ class TimberLoader
121
  return $loader;
122
  }
123
 
124
- function get_twig()
125
- {
126
  $loader_loc = trailingslashit(TIMBER_LOC) . 'Twig/lib/Twig/Autoloader.php';
127
  require_once($loader_loc);
128
  Twig_Autoloader::register();
5
 
6
  var $locations;
7
 
8
+ function __construct($caller = false) {
 
9
  $this->locations = $this->get_locations($caller);
10
  }
11
 
12
+ function render($file, $data = null) {
 
13
  $twig = $this->get_twig();
14
  return $twig->render($file, $data);
15
  }
119
  return $loader;
120
  }
121
 
122
+ function get_twig() {
 
123
  $loader_loc = trailingslashit(TIMBER_LOC) . 'Twig/lib/Twig/Autoloader.php';
124
  require_once($loader_loc);
125
  Twig_Autoloader::register();
objects/timber-menu.php CHANGED
@@ -1,102 +1,107 @@
1
  <?php
2
 
3
- class TimberMenu extends TimberCore
4
- {
5
-
6
- var $items = null;
7
-
8
- function __construct($slug)
9
- {
10
- //$menu = wp_get_nav_menu_object($slug);
11
- $locations = get_nav_menu_locations();
12
- if (isset($locations[$slug])) {
13
- $menu = wp_get_nav_menu_object($locations[$slug]);
14
- $menu = wp_get_nav_menu_items($menu);
15
- $menu = self::order_children($menu);
16
- $this->items = $menu;
 
 
 
 
 
17
  }
18
- return null;
19
- }
20
-
21
- function find_parent_item_in_menu($menu_items, $parent_id)
22
- {
23
- foreach ($menu_items as &$item) {
24
- if ($item->ID == $parent_id) {
25
- return $item;
26
- }
27
  }
28
- return null;
29
- }
30
-
31
- function order_children($items)
32
- {
33
- $menu = array();
34
- foreach ($items as $item) {
35
- if ($item->menu_item_parent == 0) {
36
- $menu[] = new TimberMenuItem($item);
37
- continue;
38
- }
39
- $parent_id = $item->menu_item_parent;
40
- $parent = self::find_parent_item_in_menu($menu, $parent_id);
41
- if ($parent) {
42
- $parent->add_child(new TimberMenuItem($item));
43
- }
44
  }
45
- return $menu;
46
- }
47
 
48
- function get_items()
49
- {
50
- if (is_array($this->items)) {
51
- return $this->items;
 
52
  }
53
- return array();
54
- }
55
  }
56
 
57
- class TimberMenuItem extends TimberCore
58
- {
59
 
60
- var $children;
61
 
62
- function __construct($data)
63
- {
64
- $this->import($data);
65
- }
66
 
67
- function get_link()
68
- {
69
- return $this->get_path();
70
- }
71
 
72
- function name()
73
- {
74
- return $this->post_title;
75
- }
76
 
77
- function slug()
78
- {
79
- return $this->post_name;
80
- }
81
 
82
- function get_path()
83
- {
84
- return $this->url_to_path($this->url);
85
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- function add_child($item)
88
- {
89
- if (!isset($this->children)) {
90
- $this->children = array();
91
  }
92
- $this->children[] = $item;
93
- }
94
 
95
- function get_children()
96
- {
97
- if (isset($this->children)) {
98
- return $this->children;
99
  }
100
- return false;
101
- }
102
  }
1
  <?php
2
 
3
+ class TimberMenu extends TimberCore {
4
+
5
+ var $items = null;
6
+
7
+ function __construct($slug) {
8
+ $menu_id = $slug;
9
+ if (!is_numeric($slug)){
10
+ $locations = get_nav_menu_locations();
11
+ $menu_id = wp_get_nav_menu_object($locations[$slug]);
12
+ }
13
+ if (isset($locations[$slug]) || is_numeric($menu_id)) {
14
+ $menu = wp_get_nav_menu_items($menu_id);
15
+ $menu = self::order_children($menu);
16
+ $this->items = $menu;
17
+ } else {
18
+ WPHelper::error_log("Sorry, the menu you were looking for wasn't found ('".$slug."'). Here's what Timber did find:");
19
+ WPHelper::error_log($locations);
20
+ }
21
+ return null;
22
  }
23
+
24
+ function find_parent_item_in_menu($menu_items, $parent_id) {
25
+ foreach ($menu_items as &$item) {
26
+ if ($item->ID == $parent_id) {
27
+ return $item;
28
+ }
29
+ }
30
+ return null;
 
31
  }
32
+
33
+ function order_children($items) {
34
+ $index = array();
35
+ $menu = array();
36
+ foreach($items as $item) {
37
+ $index[$item->ID] = new TimberMenuItem($item);
38
+ }
39
+ foreach($index as $item) {
40
+ if($item->menu_item_parent) {
41
+ $index[$item->menu_item_parent]->add_child($item);
42
+ } else {
43
+ $menu[] = $item;
44
+ }
45
+ }
46
+ return $menu;
 
47
  }
 
 
48
 
49
+ function get_items() {
50
+ if (is_array($this->items)) {
51
+ return $this->items;
52
+ }
53
+ return array();
54
  }
 
 
55
  }
56
 
57
+ class TimberMenuItem extends TimberCore {
 
58
 
59
+ var $children;
60
 
61
+ function __construct($data) {
62
+ $this->import($data);
63
+ }
 
64
 
65
+ function get_link() {
66
+ return $this->get_path();
67
+ }
 
68
 
69
+ function name() {
70
+ return $this->post_title;
71
+ }
 
72
 
73
+ function slug() {
74
+ return $this->post_name;
75
+ }
 
76
 
77
+ function get_path() {
78
+ return $this->url_to_path($this->url);
79
+ }
80
+
81
+ function add_child($item) {
82
+ if (!isset($this->children)) {
83
+ $this->children = array();
84
+ }
85
+ $this->children[] = $item;
86
+ }
87
+
88
+ function get_children() {
89
+ if (isset($this->children)) {
90
+ return $this->children;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ /* Aliases */
96
+ function link(){
97
+ return $this->get_link();
98
+ }
99
 
100
+ function permalink(){
101
+ return $this->get_link();
 
 
102
  }
 
 
103
 
104
+ function get_permalink(){
105
+ return $this->get_link();
 
 
106
  }
 
 
107
  }
objects/timber-post.php CHANGED
@@ -399,7 +399,7 @@ class TimberPost extends TimberCore
399
  {
400
  $content = $this->post_content;
401
  if ($len) {
402
- wp_trim_words($content, $len);
403
  }
404
  if ($page) {
405
  $contents = explode('<!--nextpage-->', $content);
399
  {
400
  $content = $this->post_content;
401
  if ($len) {
402
+ $content = wp_trim_words($content, $len);
403
  }
404
  if ($page) {
405
  $contents = explode('<!--nextpage-->', $content);
objects/timber-user.php CHANGED
@@ -72,4 +72,9 @@ class TimberUser extends TimberCore {
72
  function slug() {
73
  return $this->user_nicename;
74
  }
 
 
 
 
 
75
  }
72
  function slug() {
73
  return $this->user_nicename;
74
  }
75
+
76
+ function link(){
77
+ return $this->get_link();
78
+ }
79
+
80
  }
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.10.6
6
  Tested up to: 3.6
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -32,6 +32,11 @@ Timber is great for any WordPress developer who cares about writing good, mainta
32
 
33
  == Changelog ==
34
 
 
 
 
 
 
35
  = 0.10.6 =
36
  * more normalization of comments
37
  * Lots of cleanup of starter theme
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.5
5
+ Stable tag: 0.10.7
6
  Tested up to: 3.6
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
32
 
33
  == Changelog ==
34
 
35
+ = 0.10.7 =
36
+ * more normalization of menus, users
37
+ * fixed bug in post.get_content (thanks @paulwilde)
38
+ * fixed bug in way menu items with children got their children (thanks @EloB)
39
+
40
  = 0.10.6 =
41
  * more normalization of comments
42
  * Lots of cleanup of starter theme
timber-starter-theme/views/html-header.twig CHANGED
@@ -1,7 +1,7 @@
1
  <!doctype html>
2
- <!--[if lt IE 7]><html class="no-js ie ie6 lt-ie9 lt-ie8 lt-ie7" lang="{{language_attributes}}"> <![endif]-->
3
- <!--[if IE 7]><html class="no-js ie ie7 lt-ie9 lt-ie8" lang="{{language_attributes}}"> <![endif]-->
4
- <!--[if IE 8]><html class="no-js ie ie8 lt-ie9" lang="{{language_attributes}}"> <![endif]-->
5
  <!--[if gt IE 8]><!--><html class="no-js" {{language_attributes}}> <!--<![endif]-->
6
  <head>
7
  <meta charset="{{ bloginfo('charset') }}" />
1
  <!doctype html>
2
+ <!--[if lt IE 7]><html class="no-js ie ie6 lt-ie9 lt-ie8 lt-ie7" {{language_attributes}}> <![endif]-->
3
+ <!--[if IE 7]><html class="no-js ie ie7 lt-ie9 lt-ie8" {{language_attributes}}> <![endif]-->
4
+ <!--[if IE 8]><html class="no-js ie ie8 lt-ie9" {{language_attributes}}> <![endif]-->
5
  <!--[if gt IE 8]><!--><html class="no-js" {{language_attributes}}> <!--<![endif]-->
6
  <head>
7
  <meta charset="{{ bloginfo('charset') }}" />
timber.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Timber
4
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
5
  Author: Jared Novack + Upstatement
6
- Version: 0.10.6
7
  Author URI: http://timber.upstatement.com/
8
  */
9
 
@@ -329,11 +329,17 @@ class Timber {
329
  error_log('error loading your sidebar, check to make sure the file exists');
330
  }
331
  $ret = ob_get_contents();
332
- error_log($ret);
333
  ob_end_clean();
334
  return $ret;
335
  }
336
 
 
 
 
 
 
 
 
337
 
338
  /* Routes
339
  ================================ */
3
  Plugin Name: Timber
4
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
5
  Author: Jared Novack + Upstatement
6
+ Version: 0.10.7
7
  Author URI: http://timber.upstatement.com/
8
  */
9
 
329
  error_log('error loading your sidebar, check to make sure the file exists');
330
  }
331
  $ret = ob_get_contents();
 
332
  ob_end_clean();
333
  return $ret;
334
  }
335
 
336
+ /* Widgets
337
+ ================================ */
338
+
339
+ public static function get_widgets($widget_id){
340
+ return WPHelper::ob_function('dynamic_sidebar', array($widget_id));
341
+ }
342
+
343
 
344
  /* Routes
345
  ================================ */