Timber - Version 0.18.0

Version Description

  • BREAKING CHANGE ALERT wp_title no longer appends bloginfo('name') to end of string (thanks @aduth)
  • BREAKING CHANGE ALERT get_preview now respects <!-- more --> tag (thanks @jnweaver)
  • TimberHelper::transient is more reliable (thanks @mgmartel)
  • Secure urls in TimberImage if current page is served over SSL (thanks @mgmartel)
  • Re-wrote most of letterboxing functionality
  • Re-organized Helper functions
Download this release

Release Info

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

Code changes from version 0.17.2 to 0.18.0

functions/functions-twig.php CHANGED
@@ -13,7 +13,7 @@ class TimberTwig {
13
  function add_twig_filters($twig) {
14
  /* image filters */
15
  $twig->addFilter('resize', new Twig_Filter_Function(array('TimberImageHelper', 'resize')));
16
- $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
17
  $twig->addFilter('tojpg', new Twig_Filter_Function(array('TimberImageHelper', 'img_to_jpg')));
18
  $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
19
 
@@ -39,7 +39,7 @@ class TimberTwig {
39
  $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
40
  $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
41
  $twig->addFilter('relative', new Twig_Filter_Function(function($link){
42
- return TimberHelper::get_rel_url($link, true);
43
  }));
44
 
45
  $twig->addFilter('truncate', new Twig_Filter_Function(function($text, $len){
@@ -101,7 +101,11 @@ class TimberTwig {
101
  return $twig;
102
  }
103
 
104
- function to_array($arr){
 
 
 
 
105
  if (is_array($arr)){
106
  return $arr;
107
  }
@@ -109,21 +113,37 @@ class TimberTwig {
109
  return $arr;
110
  }
111
 
112
- function exec_function($function_name){
 
 
 
 
113
  $args = func_get_args();
114
  array_shift($args);
115
  return call_user_func_array(trim($function_name), ($args));
116
  }
117
 
118
- function twig_pretags( $content ) {
 
 
 
 
119
  return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , array(&$this, 'convert_pre_entities'), $content );
120
  }
121
 
122
- function convert_pre_entities( $matches ) {
 
 
 
 
123
  return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
124
  }
125
 
126
- function add_dir_name_to_locations($locs) {
 
 
 
 
127
  $locs = array_filter($locs);
128
  foreach ($locs as &$loc) {
129
  $loc = trailingslashit($loc) . trailingslashit(self::$dir_name);
@@ -131,7 +151,12 @@ class TimberTwig {
131
  return $locs;
132
  }
133
 
134
- function template_exists($file, $dirs) {
 
 
 
 
 
135
  if (is_string($dirs)) {
136
  $dirs = array($dirs);
137
  }
@@ -146,89 +171,39 @@ class TimberTwig {
146
 
147
  }
148
 
 
 
 
 
149
  function twig_shortcodes($text) {
150
  return do_shortcode($text);
151
  //apply_filters('the_content', ($text));
152
  }
153
 
 
 
 
 
154
  function twig_get_class($this) {
155
  return get_class($this);
156
  }
157
 
 
 
 
 
158
  function twig_get_type($this) {
159
  return gettype($this);
160
  }
161
 
162
- function hexrgb($hexstr) {
163
- $int = hexdec($hexstr);
164
- return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
165
- }
166
-
167
- function wp_resize_letterbox($src, $w, $h, $color = '#000000') {
168
- //$old_file = TimberHelper::get_full_path($src);
169
- $abspath = substr(ABSPATH, 0, -1);
170
- $urlinfo = parse_url($src);
171
- if( $_SERVER['DOCUMENT_ROOT'] != $abspath ) {
172
- $subdir = str_replace($_SERVER['DOCUMENT_ROOT'].'/', '', $abspath);
173
- $urlinfo['path'] = str_replace('/'.$subdir.'/', '', $urlinfo['path']);
174
- }
175
- $old_file = ABSPATH.$urlinfo['path'];
176
- $new_file = TimberImageHelper::get_letterbox_file_path($urlinfo['path'], $w, $h);
177
- $urlinfo = parse_url($src);
178
- $new_file_rel = TimberImageHelper::get_letterbox_file_rel($urlinfo['path'], $w, $h);
179
- $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
180
- if (file_exists($new_file_boxed)) {
181
- $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
182
- return $new_file_rel;
183
- }
184
- $bg = imagecreatetruecolor($w, $h);
185
- $c = hexrgb($color);
186
-
187
- $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
188
- imagefill($bg, 0, 0, $white);
189
-
190
- $image = wp_get_image_editor($old_file);
191
- if (!is_wp_error($image)) {
192
- $current_size = $image->get_size();
193
- $ow = $current_size['width'];
194
- $oh = $current_size['height'];
195
- $new_aspect = $w / $h;
196
- $old_aspect = $ow / $oh;
197
- if ($new_aspect > $old_aspect) {
198
- //taller than goal
199
- $h_scale = $h / $oh;
200
- $owt = $ow * $h_scale;
201
- $y = 0;
202
- $x = $w / 2 - $owt / 2;
203
- $oht = $h;
204
- $image->crop(0, 0, $ow, $oh, $owt, $oht);
205
- } else {
206
- $w_scale = $w / $ow;
207
- $oht = $oh * $w_scale;
208
- $x = 0;
209
- $y = $h / 2 - $oht / 2;
210
- $owt = $w;
211
- $image->crop(0, 0, $ow, $oh, $owt, $oht);
212
- }
213
- $image->save($new_file);
214
- $func = 'imagecreatefromjpeg';
215
- $ext = pathinfo($new_file, PATHINFO_EXTENSION);
216
- if ($ext == 'gif') {
217
- $func = 'imagecreatefromgif';
218
- } else if ($ext == 'png') {
219
- $func = 'imagecreatefrompng';
220
- }
221
- $image = $func($new_file);
222
- imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
223
- $new_file = str_replace('-lb-', '-lbox-', $new_file);
224
- imagejpeg($bg, $new_file);
225
- return TimberHelper::get_rel_path($new_file);
226
- } else {
227
- TimberHelper::error_log($image);
228
- }
229
- return null;
230
- }
231
 
 
 
 
 
 
 
 
232
  function twig_time_ago($from, $to = null, $format_past='%s ago', $format_future='%s from now') {
233
  $to = (($to === null) ? (time()) : ($to));
234
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
@@ -241,6 +216,10 @@ function twig_time_ago($from, $to = null, $format_past='%s ago', $format_future=
241
  }
242
  }
243
 
 
 
 
 
244
  function twig_body_class($body_classes) {
245
  ob_start();
246
  if (is_array($body_classes)) {
@@ -252,6 +231,11 @@ function twig_body_class($body_classes) {
252
  return $return;
253
  }
254
 
 
 
 
 
 
255
  function render_twig_string($string, $data = array()) {
256
  $timber_loader = new TimberLoader();
257
  $timber_loader->get_twig();
@@ -260,6 +244,10 @@ function render_twig_string($string, $data = array()) {
260
  return $twig->render($string, $data);
261
  }
262
 
 
 
 
 
263
  function get_calling_script_dir($backtrace) {
264
  $caller = $backtrace[0]['file'];
265
  $pathinfo = pathinfo($caller);
@@ -269,30 +257,55 @@ function get_calling_script_dir($backtrace) {
269
 
270
  //deprecated
271
 
272
-
 
 
 
273
  function twig_get_src_from_attachment_id($aid) {
274
- return TimberHelper::get_image_path($aid);
275
  }
276
 
 
 
 
 
277
  function twig_get_path($url) {
278
  $url = parse_url($url);
279
  return $url['path'];
280
  }
281
 
 
 
 
 
 
282
  function twig_make_excerpt($text, $length = 55){
283
  return wp_trim_words($text, $length);
284
  }
285
 
 
 
 
 
286
  function twig_print_r($arr) {
287
  //$rets = twig_object_docs($obj, false);
288
  return print_r($arr, true);
289
  return $rets;
290
  }
291
 
 
 
 
 
292
  function twig_print_a($arr) {
293
  return '<pre>' . twig_object_docs($arr, true) . '</pre>';
294
  }
295
 
 
 
 
 
 
296
  function twig_object_docs($obj, $methods = true) {
297
  $class = get_class($obj);
298
  $properties = (array) $obj;
13
  function add_twig_filters($twig) {
14
  /* image filters */
15
  $twig->addFilter('resize', new Twig_Filter_Function(array('TimberImageHelper', 'resize')));
16
+ $twig->addFilter('letterbox', new Twig_Filter_Function(array('TimberImageHelper', 'letterbox')));
17
  $twig->addFilter('tojpg', new Twig_Filter_Function(array('TimberImageHelper', 'img_to_jpg')));
18
  $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
19
 
39
  $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
40
  $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
41
  $twig->addFilter('relative', new Twig_Filter_Function(function($link){
42
+ return TimberURLHelper::get_rel_url($link, true);
43
  }));
44
 
45
  $twig->addFilter('truncate', new Twig_Filter_Function(function($text, $len){
101
  return $twig;
102
  }
103
 
104
+ /**
105
+ * @param mixed $arr
106
+ * @return array
107
+ */
108
+ function to_array($arr){
109
  if (is_array($arr)){
110
  return $arr;
111
  }
113
  return $arr;
114
  }
115
 
116
+ /**
117
+ * @param string $function_name
118
+ * @return mixed
119
+ */
120
+ function exec_function($function_name){
121
  $args = func_get_args();
122
  array_shift($args);
123
  return call_user_func_array(trim($function_name), ($args));
124
  }
125
 
126
+ /**
127
+ * @param string $content
128
+ * @return string
129
+ */
130
+ function twig_pretags( $content ) {
131
  return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , array(&$this, 'convert_pre_entities'), $content );
132
  }
133
 
134
+ /**
135
+ * @param array $matches
136
+ * @return string
137
+ */
138
+ function convert_pre_entities( $matches ) {
139
  return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
140
  }
141
 
142
+ /**
143
+ * @param array $locs
144
+ * @return array
145
+ */
146
+ function add_dir_name_to_locations($locs) {
147
  $locs = array_filter($locs);
148
  foreach ($locs as &$loc) {
149
  $loc = trailingslashit($loc) . trailingslashit(self::$dir_name);
151
  return $locs;
152
  }
153
 
154
+ /**
155
+ * @param string $file
156
+ * @param array|string $dirs
157
+ * @return bool
158
+ */
159
+ function template_exists($file, $dirs) {
160
  if (is_string($dirs)) {
161
  $dirs = array($dirs);
162
  }
171
 
172
  }
173
 
174
+ /**
175
+ * @param string $text
176
+ * @return string
177
+ */
178
  function twig_shortcodes($text) {
179
  return do_shortcode($text);
180
  //apply_filters('the_content', ($text));
181
  }
182
 
183
+ /**
184
+ * @param mixed $this
185
+ * @return string
186
+ */
187
  function twig_get_class($this) {
188
  return get_class($this);
189
  }
190
 
191
+ /**
192
+ * @param mixed $this
193
+ * @return string
194
+ */
195
  function twig_get_type($this) {
196
  return gettype($this);
197
  }
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
+ /**
201
+ * @param int|string $from
202
+ * @param int|string $to
203
+ * @param string $format_past
204
+ * @param string $format_future
205
+ * @return string
206
+ */
207
  function twig_time_ago($from, $to = null, $format_past='%s ago', $format_future='%s from now') {
208
  $to = (($to === null) ? (time()) : ($to));
209
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
216
  }
217
  }
218
 
219
+ /**
220
+ * @param mixed $body_classes
221
+ * @return string
222
+ */
223
  function twig_body_class($body_classes) {
224
  ob_start();
225
  if (is_array($body_classes)) {
231
  return $return;
232
  }
233
 
234
+ /**
235
+ * @param string $string
236
+ * @param array $data
237
+ * @return string
238
+ */
239
  function render_twig_string($string, $data = array()) {
240
  $timber_loader = new TimberLoader();
241
  $timber_loader->get_twig();
244
  return $twig->render($string, $data);
245
  }
246
 
247
+ /**
248
+ * @param array $backtrace
249
+ * @return string
250
+ */
251
  function get_calling_script_dir($backtrace) {
252
  $caller = $backtrace[0]['file'];
253
  $pathinfo = pathinfo($caller);
257
 
258
  //deprecated
259
 
260
+ /**
261
+ * @param int $aid
262
+ * @return string
263
+ */
264
  function twig_get_src_from_attachment_id($aid) {
265
+ return TimberImageHelper::get_image_path($aid);
266
  }
267
 
268
+ /**
269
+ * @param string $url
270
+ * @return mixed
271
+ */
272
  function twig_get_path($url) {
273
  $url = parse_url($url);
274
  return $url['path'];
275
  }
276
 
277
+ /**
278
+ * @param string $text
279
+ * @param int $length
280
+ * @return string
281
+ */
282
  function twig_make_excerpt($text, $length = 55){
283
  return wp_trim_words($text, $length);
284
  }
285
 
286
+ /**
287
+ * @param array $arr
288
+ * @return mixed
289
+ */
290
  function twig_print_r($arr) {
291
  //$rets = twig_object_docs($obj, false);
292
  return print_r($arr, true);
293
  return $rets;
294
  }
295
 
296
+ /**
297
+ * @param array $arr
298
+ * @return string
299
+ */
300
  function twig_print_a($arr) {
301
  return '<pre>' . twig_object_docs($arr, true) . '</pre>';
302
  }
303
 
304
+ /**
305
+ * @param mixed $obj
306
+ * @param bool $methods
307
+ * @return string
308
+ */
309
  function twig_object_docs($obj, $methods = true) {
310
  $class = get_class($obj);
311
  $properties = (array) $obj;
functions/timber-admin.php CHANGED
@@ -9,7 +9,12 @@ class TimberAdmin {
9
  add_filter( 'plugin_action_links', array(&$this, 'settings_link'), 10, 2 );
10
  }
11
 
12
- function settings_link( $links, $file ) {
 
 
 
 
 
13
  if (strstr($file, '/timber.php')){
14
  return array_merge(
15
  array(
9
  add_filter( 'plugin_action_links', array(&$this, 'settings_link'), 10, 2 );
10
  }
11
 
12
+ /**
13
+ * @param array $links
14
+ * @param string $file
15
+ * @return array
16
+ */
17
+ function settings_link( $links, $file ) {
18
  if (strstr($file, '/timber.php')){
19
  return array_merge(
20
  array(
functions/timber-archives.php CHANGED
@@ -7,18 +7,36 @@ class TimberArchives extends TimberCore {
7
  $this->init($args, $base);
8
  }
9
 
10
- function init($args, $base = ''){
 
 
 
 
11
  $this->base = $base;
12
  $this->items = $this->get_items($args);
13
  }
14
 
15
- function get_archives_link($url, $text) {
 
 
 
 
 
16
  $ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text);
17
- $ret['url'] = $ret['link'] = esc_url(TimberHelper::prepend_to_url($url, $this->base));
18
  return $ret;
19
  }
20
 
21
- function get_items_yearly($args, $last_changed, $join, $where, $order, $limit){
 
 
 
 
 
 
 
 
 
22
  global $wpdb;
23
  $output = array();
24
  $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
@@ -38,7 +56,17 @@ class TimberArchives extends TimberCore {
38
  return $output;
39
  }
40
 
41
- function get_items_montly($args, $last_changed, $join, $where, $order, $limit, $nested = true){
 
 
 
 
 
 
 
 
 
 
42
  global $wpdb, $wp_locale;
43
  $output = array();
44
  $defaults = array(
@@ -82,7 +110,11 @@ class TimberArchives extends TimberCore {
82
  return $output;
83
  }
84
 
85
- function get_items($args){
 
 
 
 
86
  global $wpdb, $wp_locale;
87
 
88
  $defaults = array(
@@ -220,4 +252,4 @@ class TimberArchives extends TimberCore {
220
  }
221
  return $output;
222
  }
223
- }
7
  $this->init($args, $base);
8
  }
9
 
10
+ /**
11
+ * @param array|string $args
12
+ * @param string $base
13
+ */
14
+ function init($args, $base = ''){
15
  $this->base = $base;
16
  $this->items = $this->get_items($args);
17
  }
18
 
19
+ /**
20
+ * @param string $url
21
+ * @param string $text
22
+ * @return mixed
23
+ */
24
+ function get_archives_link($url, $text) {
25
  $ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text);
26
+ $ret['url'] = $ret['link'] = esc_url(TimberURLHelper::prepend_to_url($url, $this->base));
27
  return $ret;
28
  }
29
 
30
+ /**
31
+ * @param array|string $args
32
+ * @param string $last_changed
33
+ * @param string $join
34
+ * @param string $where
35
+ * @param string $order
36
+ * @param string $limit
37
+ * @return array
38
+ */
39
+ function get_items_yearly($args, $last_changed, $join, $where, $order, $limit){
40
  global $wpdb;
41
  $output = array();
42
  $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
56
  return $output;
57
  }
58
 
59
+ /**
60
+ * @param array|string $args
61
+ * @param string $last_changed
62
+ * @param string $join
63
+ * @param string $where
64
+ * @param string $order
65
+ * @param string $limit
66
+ * @param bool $nested
67
+ * @return array
68
+ */
69
+ function get_items_montly($args, $last_changed, $join, $where, $order, $limit, $nested = true){
70
  global $wpdb, $wp_locale;
71
  $output = array();
72
  $defaults = array(
110
  return $output;
111
  }
112
 
113
+ /**
114
+ * @param array|string $args
115
+ * @return array|string
116
+ */
117
+ function get_items($args){
118
  global $wpdb, $wp_locale;
119
 
120
  $defaults = array(
252
  }
253
  return $output;
254
  }
255
+ }
functions/timber-comment.php CHANGED
@@ -7,10 +7,16 @@ class TimberComment extends TimberCore {
7
 
8
  public static $representation = 'comment';
9
 
 
 
 
10
  function __construct($cid) {
11
  $this->init($cid);
12
  }
13
 
 
 
 
14
  function init($cid) {
15
  $comment_data = $cid;
16
  if (is_integer($cid)) {
@@ -22,6 +28,9 @@ class TimberComment extends TimberCore {
22
  $this->import($comment_meta_data);
23
  }
24
 
 
 
 
25
  public function author() {
26
  if ($this->user_id) {
27
  return new TimberUser($this->user_id);
@@ -36,6 +45,11 @@ class TimberComment extends TimberCore {
36
  return $author;
37
  }
38
 
 
 
 
 
 
39
  public function avatar($size=92, $default=''){
40
  // Fetches the Gravatar
41
  // use it like this
@@ -59,18 +73,32 @@ class TimberComment extends TimberCore {
59
  return $avatar;
60
  }
61
 
 
 
 
62
  public function content() {
63
  return $this->comment_content;
64
  }
65
 
 
 
 
66
  public function date() {
67
  return $this->comment_date;
68
  }
69
 
 
 
 
 
70
  public function meta($field_name){
71
  return $this->get_meta_field($field_name);
72
  }
73
 
 
 
 
 
74
  private function get_meta_fields($comment_id = null){
75
  if ($comment_id === null){
76
  $comment_id = $this->ID;
@@ -87,6 +115,10 @@ class TimberComment extends TimberCore {
87
  return $comment_metas;
88
  }
89
 
 
 
 
 
90
  private function get_meta_field($field_name){
91
  $value = apply_filters('timber_comment_get_meta_field_pre', null, $this->ID, $field_name, $this);
92
  if ($value === null){
@@ -99,6 +131,9 @@ class TimberComment extends TimberCore {
99
  /* AVATAR Stuff
100
  ======================= */
101
 
 
 
 
102
  private function avatar_email(){
103
  $email = '';
104
  $id = (int) $this->user_id;
@@ -111,6 +146,10 @@ class TimberComment extends TimberCore {
111
  return $email;
112
  }
113
 
 
 
 
 
114
  private function avatar_host($email_hash){
115
  if ( is_ssl() ) {
116
  $host = 'https://secure.gravatar.com';
@@ -124,6 +163,13 @@ class TimberComment extends TimberCore {
124
  return $host;
125
  }
126
 
 
 
 
 
 
 
 
127
  private function avatar_default($default, $email, $size, $host){
128
  # what if its relative.
129
  if(substr ( $default , 0, 1 ) == "/" ){
@@ -156,6 +202,14 @@ class TimberComment extends TimberCore {
156
  return $default;
157
  }
158
 
 
 
 
 
 
 
 
 
159
  private function avatar_out($email, $default, $host, $email_hash, $size){
160
  $out = "$host/avatar/";
161
  $out .= $email_hash;
7
 
8
  public static $representation = 'comment';
9
 
10
+ /**
11
+ * @param int $cid
12
+ */
13
  function __construct($cid) {
14
  $this->init($cid);
15
  }
16
 
17
+ /**
18
+ * @param int $cid
19
+ */
20
  function init($cid) {
21
  $comment_data = $cid;
22
  if (is_integer($cid)) {
28
  $this->import($comment_meta_data);
29
  }
30
 
31
+ /**
32
+ * @return TimberUser
33
+ */
34
  public function author() {
35
  if ($this->user_id) {
36
  return new TimberUser($this->user_id);
45
  return $author;
46
  }
47
 
48
+ /**
49
+ * @param int $size
50
+ * @param string $default
51
+ * @return bool|mixed|string
52
+ */
53
  public function avatar($size=92, $default=''){
54
  // Fetches the Gravatar
55
  // use it like this
73
  return $avatar;
74
  }
75
 
76
+ /**
77
+ * @return string
78
+ */
79
  public function content() {
80
  return $this->comment_content;
81
  }
82
 
83
+ /**
84
+ * @return string
85
+ */
86
  public function date() {
87
  return $this->comment_date;
88
  }
89
 
90
+ /**
91
+ * @param string $field_name
92
+ * @return mixed
93
+ */
94
  public function meta($field_name){
95
  return $this->get_meta_field($field_name);
96
  }
97
 
98
+ /**
99
+ * @param int $comment_id
100
+ * @return mixed
101
+ */
102
  private function get_meta_fields($comment_id = null){
103
  if ($comment_id === null){
104
  $comment_id = $this->ID;
115
  return $comment_metas;
116
  }
117
 
118
+ /**
119
+ * @param string $field_name
120
+ * @return mixed
121
+ */
122
  private function get_meta_field($field_name){
123
  $value = apply_filters('timber_comment_get_meta_field_pre', null, $this->ID, $field_name, $this);
124
  if ($value === null){
131
  /* AVATAR Stuff
132
  ======================= */
133
 
134
+ /**
135
+ * @return string
136
+ */
137
  private function avatar_email(){
138
  $email = '';
139
  $id = (int) $this->user_id;
146
  return $email;
147
  }
148
 
149
+ /**
150
+ * @param string $email_hash
151
+ * @return string
152
+ */
153
  private function avatar_host($email_hash){
154
  if ( is_ssl() ) {
155
  $host = 'https://secure.gravatar.com';
163
  return $host;
164
  }
165
 
166
+ /**
167
+ * @param string $default
168
+ * @param string $email
169
+ * @param string $size
170
+ * @param string $host
171
+ * @return string
172
+ */
173
  private function avatar_default($default, $email, $size, $host){
174
  # what if its relative.
175
  if(substr ( $default , 0, 1 ) == "/" ){
202
  return $default;
203
  }
204
 
205
+ /**
206
+ * @param string $email
207
+ * @param string $default
208
+ * @param string $host
209
+ * @param string $email_hash
210
+ * @param string $size
211
+ * @return mixed
212
+ */
213
  private function avatar_out($email, $default, $host, $email_hash, $size){
214
  $out = "$host/avatar/";
215
  $out .= $email_hash;
functions/timber-core.php CHANGED
@@ -2,7 +2,10 @@
2
 
3
  class TimberCore {
4
 
5
- function import($info) {
 
 
 
6
  if (is_object($info)) {
7
  $info = get_object_vars($info);
8
  }
@@ -15,11 +18,18 @@ class TimberCore {
15
  }
16
  }
17
 
18
- function update($key, $value){
 
 
 
 
19
  update_metadata($this->object_type, $this->ID, $key, $value);
20
  }
21
 
22
- function can_edit() {
 
 
 
23
  if (isset($this->_can_edit)) {
24
  return $this->_can_edit;
25
  }
@@ -33,14 +43,21 @@ class TimberCore {
33
  return $this->_can_edit;
34
  }
35
 
36
- function get_method_values(){
 
 
 
37
  $ret = array();
38
  $ret['can_edit'] = $this->can_edit();
39
  return $ret;
40
  }
41
 
42
- //deprecated
43
- function url_to_path($url = '') {
 
 
 
 
44
  if (!strlen($url) && $this->url) {
45
  $url = $this->url;
46
  }
2
 
3
  class TimberCore {
4
 
5
+ /**
6
+ * @param array|object $info
7
+ */
8
+ function import($info) {
9
  if (is_object($info)) {
10
  $info = get_object_vars($info);
11
  }
18
  }
19
  }
20
 
21
+ /**
22
+ * @param string $key
23
+ * @param mixed $value
24
+ */
25
+ function update($key, $value){
26
  update_metadata($this->object_type, $this->ID, $key, $value);
27
  }
28
 
29
+ /**
30
+ * @return bool
31
+ */
32
+ function can_edit() {
33
  if (isset($this->_can_edit)) {
34
  return $this->_can_edit;
35
  }
43
  return $this->_can_edit;
44
  }
45
 
46
+ /**
47
+ * @return array
48
+ */
49
+ function get_method_values(){
50
  $ret = array();
51
  $ret['can_edit'] = $this->can_edit();
52
  return $ret;
53
  }
54
 
55
+ /**
56
+ * @deprecated
57
+ * @param string $url
58
+ * @return mixed
59
+ */
60
+ function url_to_path($url = '') {
61
  if (!strlen($url) && $this->url) {
62
  $url = $this->url;
63
  }
functions/timber-function-wrapper.php CHANGED
@@ -10,6 +10,11 @@ class TimberFunctionWrapper {
10
  return $this->call();
11
  }
12
 
 
 
 
 
 
13
  public function __construct( $function, $args = array( ), $return_output_buffer = false ) {
14
  $this->_function = $function;
15
  $this->_args = $args;
@@ -18,6 +23,10 @@ class TimberFunctionWrapper {
18
  add_filter( 'get_twig', array( &$this, 'add_to_twig' ) );
19
  }
20
 
 
 
 
 
21
  public function add_to_twig( $twig ) {
22
  $wrapper = $this;
23
 
@@ -28,6 +37,9 @@ class TimberFunctionWrapper {
28
  return $twig;
29
  }
30
 
 
 
 
31
  public function call() {
32
  $args = $this->_parse_args( func_get_args(), $this->_args );
33
 
@@ -37,15 +49,20 @@ class TimberFunctionWrapper {
37
  return (string) call_user_func_array( $this->_function, $args );
38
  }
39
 
40
- private function _parse_args( $args, $defaults ) {
41
- $_arg = reset( $defaults );
 
 
 
 
 
42
 
43
- foreach ( $args as $index => $arg ) {
44
- $defaults[$index] = is_null( $arg ) ? $_arg : $arg;
45
- $_arg = next( $defaults );
46
- }
47
-
48
- return $defaults;
49
  }
50
 
 
 
 
51
  }
10
  return $this->call();
11
  }
12
 
13
+ /**
14
+ * @param callable $function
15
+ * @param array $args
16
+ * @param bool $return_output_buffer
17
+ */
18
  public function __construct( $function, $args = array( ), $return_output_buffer = false ) {
19
  $this->_function = $function;
20
  $this->_args = $args;
23
  add_filter( 'get_twig', array( &$this, 'add_to_twig' ) );
24
  }
25
 
26
+ /**
27
+ * @param Twig_Environment $twig
28
+ * @return Twig_Environment
29
+ */
30
  public function add_to_twig( $twig ) {
31
  $wrapper = $this;
32
 
37
  return $twig;
38
  }
39
 
40
+ /**
41
+ * @return string
42
+ */
43
  public function call() {
44
  $args = $this->_parse_args( func_get_args(), $this->_args );
45
 
49
  return (string) call_user_func_array( $this->_function, $args );
50
  }
51
 
52
+ /**
53
+ * @param array $args
54
+ * @param array $defaults
55
+ * @return array
56
+ */
57
+ private function _parse_args( $args, $defaults ) {
58
+ $_arg = reset( $defaults );
59
 
60
+ foreach ( $args as $index => $arg ) {
61
+ $defaults[$index] = is_null( $arg ) ? $_arg : $arg;
62
+ $_arg = next( $defaults );
 
 
 
63
  }
64
 
65
+ return $defaults;
66
+ }
67
+
68
  }
functions/timber-helper.php CHANGED
@@ -2,32 +2,70 @@
2
 
3
  class TimberHelper {
4
 
5
- public static function transient($slug, $callback, $transient_time = 0){
6
- $disable_transients = false;
7
- if (defined('WP_DISABLE_TRANSIENTS')){
8
- $disable_transients = WP_DISABLE_TRANSIENTS;
9
- }
10
- $data = null;
11
- if ($transient_time === false){
12
- $data = $callback();
13
- return $data;
14
- }
15
- if (is_callable($callback) && (false === ($data = get_transient($slug)) || $disable_transients) && $transient_time !== false){
16
- $cache_lock_slug = $slug.'_lock';
 
 
17
 
18
- if (get_transient($cache_lock_slug)){
19
- //the server is currently executing the process.
20
- //We're just gonna dump these users. Sorry!
21
- return false;
22
- }
23
- set_transient($cache_lock_slug, true, $transient_time);
24
- $data = $callback();
25
- set_transient($slug, $data, $transient_time);
26
- delete_transient($cache_lock_slug);
27
- }
28
- return $data;
29
- }
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  public static function start_timer(){
32
  $time = microtime();
33
  $time = explode(' ', $time);
@@ -35,6 +73,10 @@ class TimberHelper {
35
  return $time;
36
  }
37
 
 
 
 
 
38
  public static function stop_timer($start){
39
  $time = microtime();
40
  $time = explode(' ', $time);
@@ -44,20 +86,14 @@ class TimberHelper {
44
  return $total_time.' seconds.';
45
  }
46
 
47
- public static function is_array_assoc($arr) {
48
- if (!is_array($arr)) {
49
- return false;
50
- }
51
- return (bool)count(array_filter(array_keys($arr), 'is_string'));
52
- }
53
-
54
- public static function preslashit($path){
55
- if (strpos($path, '/') != 0) {
56
- $path = '/' . $path;
57
- }
58
- return $path;
59
- }
60
 
 
 
 
 
 
61
  public static function ob_function($function, $args = array(null)) {
62
  ob_start();
63
  call_user_func_array($function, $args);
@@ -66,113 +102,20 @@ class TimberHelper {
66
  return $data;
67
  }
68
 
 
 
 
 
 
 
69
  public static function function_wrapper($function_name, $defaults = array(), $return_output_buffer = false) {
70
  return new TimberFunctionWrapper($function_name, $defaults, $return_output_buffer);
71
  }
72
 
73
- public static function is_url($url) {
74
- if (!is_string($url)){
75
- return false;
76
- }
77
- $url = strtolower($url);
78
- if (strstr('://', $url)) {
79
- return true;
80
- }
81
- return false;
82
- }
83
-
84
- public static function get_path_base() {
85
- $struc = get_option('permalink_structure');
86
- $struc = explode('/', $struc);
87
- $p = '/';
88
- foreach ($struc as $s) {
89
- if (!strstr($s, '%') && strlen($s)) {
90
- $p .= $s . '/';
91
- }
92
- }
93
- return $p;
94
- }
95
-
96
- public static function get_full_path($src) {
97
- $root = ABSPATH;
98
- $old_root_path = $root . $src;
99
- $old_root_path = str_replace('//', '/', $old_root_path);
100
- return $old_root_path;
101
- }
102
-
103
- public static function is_local($url){
104
- if (strstr($url, $_SERVER['HTTP_HOST'])){
105
- return true;
106
- }
107
- return false;
108
- }
109
-
110
- /* URL Stuff
111
- ======================== */
112
-
113
- public static function get_rel_url($url, $force = false){
114
- if (!strstr($url, $_SERVER['HTTP_HOST']) && !$force){
115
- return $url;
116
- }
117
- $url_info = parse_url($url);
118
- $link = $url_info['path'];
119
- if (isset($url_info['query']) && strlen($url_info['query'])){
120
- $link .= '?'.$url_info['query'];
121
- }
122
- return $link;
123
- }
124
-
125
- public static function get_rel_path($src) {
126
- return str_replace(ABSPATH, '', $src);
127
- }
128
-
129
- public static function remove_double_slashes($url){
130
- $url = str_replace('//', '/', $url);
131
- if (strstr($url, 'http:') && !strstr($url, 'http://')){
132
- $url = str_replace('http:/', 'http://', $url);
133
- }
134
- return $url;
135
- }
136
-
137
- public static function prepend_to_url($url, $path){
138
- if (strstr(strtolower($url), 'http')){
139
- $url_parts = parse_url($url);
140
- $url = $url_parts['scheme'].'://'.$url_parts['host'].$path.$url_parts['path'];
141
- } else {
142
- $url = $url.$path;
143
- }
144
- return self::remove_double_slashes($url);
145
- }
146
-
147
- public static function download_url($url, $timeout = 300) {
148
- if (!$url) {
149
- return new WP_Error('http_no_url', __('Invalid URL Provided.'));
150
- }
151
-
152
- $tmpfname = wp_tempnam($url);
153
- if (!$tmpfname) {
154
- return new WP_Error('http_no_file', __('Could not create Temporary file.'));
155
- }
156
-
157
- $response = wp_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
158
-
159
- if (is_wp_error($response)) {
160
- unlink($tmpfname);
161
- return $response;
162
- }
163
- if (200 != wp_remote_retrieve_response_code($response)) {
164
- unlink($tmpfname);
165
- return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
166
- }
167
- return $tmpfname;
168
- }
169
-
170
- public static function osort(&$array, $prop) {
171
- usort($array, function ($a, $b) use ($prop) {
172
- return $a->$prop > $b->$prop ? 1 : -1;
173
- });
174
- }
175
-
176
  public static function error_log($arg) {
177
  if (!WP_DEBUG){
178
  return;
@@ -183,39 +126,24 @@ class TimberHelper {
183
  error_log($arg);
184
  }
185
 
186
- public static function get_params($i = -1) {
187
- $args = explode('/', trim(strtolower($_SERVER['REQUEST_URI'])));
188
- $newargs = array();
189
- foreach ($args as $arg) {
190
- if (strlen($arg)) {
191
- $newargs[] = $arg;
192
- }
193
- }
194
- if ($i > -1) {
195
- if (isset($newargs[$i])) {
196
- return $newargs[$i];
197
- }
198
- }
199
- return $newargs;
200
- }
201
-
202
- public static function get_wp_title() {
203
- return wp_title('|', false, 'right');
204
  }
205
 
206
- public static function get_current_url() {
207
- $pageURL = "http://";
208
- if (isset($_SERVER['HTTPS']) && $_SERVER["HTTPS"] == "on"){
209
- $pageURL = "https://";;
210
- }
211
- if ($_SERVER["SERVER_PORT"] != "80") {
212
- $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
213
- } else {
214
- $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
215
- }
216
- return $pageURL;
217
- }
218
 
 
 
 
 
 
 
 
219
  public static function trim_words($text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br') {
220
  if (null === $more) {
221
  $more = __('&hellip;');
@@ -248,6 +176,10 @@ class TimberHelper {
248
  return apply_filters('wp_trim_words', $text, $num_words, $more, $original_text);
249
  }
250
 
 
 
 
 
251
  public static function close_tags($html) {
252
  #put all opened tags into an array
253
  preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
@@ -272,6 +204,29 @@ class TimberHelper {
272
  return $html;
273
  }
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  public static function get_posts_by_meta($key, $value) {
276
  global $wpdb;
277
  $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value);
@@ -288,6 +243,11 @@ class TimberHelper {
288
  return 0;
289
  }
290
 
 
 
 
 
 
291
  public static function get_post_by_meta($key, $value) {
292
  global $wpdb;
293
  $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value);
@@ -300,33 +260,45 @@ class TimberHelper {
300
  return 0;
301
  }
302
 
 
 
 
 
303
  public static function get_term_id_by_term_taxonomy_id($ttid){
304
  global $wpdb;
305
  $query = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid);
306
  return $wpdb->get_var($query);
307
  }
308
 
309
- /* this $args thing is a fucking mess, fix at some point:
310
-
311
- http://codex.wordpress.org/Function_Reference/comment_form */
312
 
313
- public static function get_comment_form($post_id = null, $args = array()) {
314
- ob_start();
315
- comment_form($args, $post_id);
316
- $ret = ob_get_contents();
317
- ob_end_clean();
318
- return $ret;
 
 
 
319
  }
320
 
321
- public static function is_true($property) {
322
- if (isset($property)) {
323
- if ($property == 'true' || $property == 1 || $property == '1' || $property == true) {
324
- return true;
325
- }
 
 
326
  }
327
- return false;
328
  }
329
 
 
 
 
 
330
  public static function array_to_object($array) {
331
  $obj = new stdClass;
332
  foreach ($array as $k => $v) {
@@ -339,6 +311,12 @@ class TimberHelper {
339
  return $obj;
340
  }
341
 
 
 
 
 
 
 
342
  public static function get_object_index_by_property($array, $key, $value) {
343
  if (is_array($array)) {
344
  $i = 0;
@@ -358,6 +336,13 @@ class TimberHelper {
358
  return false;
359
  }
360
 
 
 
 
 
 
 
 
361
  public static function get_object_by_property($array, $key, $value) {
362
  if (is_array($array)) {
363
  foreach ($array as $arr) {
@@ -371,13 +356,11 @@ class TimberHelper {
371
  return null;
372
  }
373
 
374
- public static function get_image_path($iid) {
375
- $size = 'full';
376
- $src = wp_get_attachment_image_src($iid, $size);
377
- $src = $src[0];
378
- return self::get_rel_path($src);
379
- }
380
-
381
  public static function array_truncate($array, $len) {
382
  if (sizeof($array) > $len) {
383
  $array = array_splice($array, 0, $len);
@@ -385,34 +368,58 @@ class TimberHelper {
385
  return $array;
386
  }
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  public static function iseven($i) {
389
  return ($i % 2) == 0;
390
  }
391
 
 
 
 
 
392
  public static function isodd($i) {
393
  return ($i % 2) != 0;
394
  }
395
 
396
- public static function is_external($url){
397
- $has_http = strstr(strtolower($url), 'http');
398
- $on_domain = strstr($url, $_SERVER['HTTP_HOST']);
399
- if ($has_http && !$on_domain){
400
- return true;
401
- }
402
- return false;
403
- }
404
 
405
- public static function twitterify($ret) {
406
- $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
407
- $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
408
- $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
409
- $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
410
- $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
411
- $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
412
- $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
413
- return $ret;
 
 
414
  }
415
 
 
 
 
 
416
  public static function paginate_links( $args = '' ) {
417
  $defaults = array(
418
  'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
@@ -487,8 +494,68 @@ class TimberHelper {
487
  }
488
  return $page_links;
489
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
  }
491
 
492
  class WPHelper extends TimberHelper {
493
  //for backwards compat, will remove eventually
494
- }
2
 
3
  class TimberHelper {
4
 
5
+ /**
6
+ * @param string $slug Unique identifier for transient
7
+ * @param callable $callback Callback that generates the data that's to be cached
8
+ * @param int $transient_time (optional) Expiration of transients in seconds
9
+ * @param int $lock_timeout (optional) How long to lock the transient to prevent race conditions
10
+ * @param bool $force (optional) Force callback to be executed when transient is locked
11
+ * @return mixed
12
+ */
13
+ public static function transient( $slug, $callback, $transient_time = 0, $lock_timeout = 5, $force = false ) {
14
+ if ( $transient_time === false || ( defined( 'WP_DISABLE_TRANSIENTS' ) && WP_DISABLE_TRANSIENTS ) ) {
15
+ $enable_transients = false;
16
+ } else {
17
+ $enable_transients = true;
18
+ }
19
 
20
+ $data = $enable_transients ? get_transient( $slug ) : false;
21
+
22
+ if ( false === $data ) {
23
+
24
+ if ( $enable_transients && self::_is_transient_locked( $slug ) ) {
25
+
26
+ $force = apply_filters( 'timber_force_transients', $force );
27
+ $force = apply_filters( 'timber_force_transient_' . $slug, $force );
28
+
29
+ if ( !$force )
30
+ //the server is currently executing the process.
31
+ //We're just gonna dump these users. Sorry!
32
+ return false;
33
+
34
+ $enable_transients = false;
35
+ }
36
+
37
+ // lock timeout shouldn't be higher than 5 seconds, unless
38
+ // remote calls with high timeouts are made here
39
+ if ( $enable_transients )
40
+ self::_lock_transient( $slug, $lock_timeout );
41
+
42
+ $data = $callback();
43
 
44
+ if ( $enable_transients ) {
45
+ set_transient( $slug, $data, $transient_time );
46
+ self::_unlock_transient( $slug );
47
+ }
48
+ }
49
+ return $data;
50
+ }
51
+
52
+ public static function _lock_transient( $slug, $lock_timeout ) {
53
+ set_transient( $slug . '_lock', true, $lock_timeout );
54
+ }
55
+
56
+ public static function _unlock_transient( $slug ) {
57
+ delete_transient( $slug . '_lock', true );
58
+ }
59
+
60
+ public static function _is_transient_locked( $slug ) {
61
+ return (bool) get_transient( $slug . '_lock' );
62
+ }
63
+
64
+ /* These are for measuring page render time */
65
+
66
+ /**
67
+ * @return float
68
+ */
69
  public static function start_timer(){
70
  $time = microtime();
71
  $time = explode(' ', $time);
73
  return $time;
74
  }
75
 
76
+ /**
77
+ * @param int $start
78
+ * @return string
79
+ */
80
  public static function stop_timer($start){
81
  $time = microtime();
82
  $time = explode(' ', $time);
86
  return $total_time.' seconds.';
87
  }
88
 
89
+ /* Function Utilities
90
+ ======================== */
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ /**
93
+ * @param callback $function
94
+ * @param array $args
95
+ * @return string
96
+ */
97
  public static function ob_function($function, $args = array(null)) {
98
  ob_start();
99
  call_user_func_array($function, $args);
102
  return $data;
103
  }
104
 
105
+ /**
106
+ * @param string $function_name
107
+ * @param array $defaults
108
+ * @param bool $return_output_buffer
109
+ * @return TimberFunctionWrapper
110
+ */
111
  public static function function_wrapper($function_name, $defaults = array(), $return_output_buffer = false) {
112
  return new TimberFunctionWrapper($function_name, $defaults, $return_output_buffer);
113
  }
114
 
115
+ /**
116
+ * @param $arg
117
+ * @return void
118
+ */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  public static function error_log($arg) {
120
  if (!WP_DEBUG){
121
  return;
126
  error_log($arg);
127
  }
128
 
129
+ /**
130
+ * @return string
131
+ */
132
+ public static function get_wp_title($seperator = ' ', $seplocation = 'left') {
133
+ $seperator = apply_filters('timber_wp_title_seperator', $seperator);
134
+ return trim(wp_title($seperator, false, $seplocation));
 
 
 
 
 
 
 
 
 
 
 
 
135
  }
136
 
137
+ /* Text Utilities
138
+ ======================== */
 
 
 
 
 
 
 
 
 
 
139
 
140
+ /**
141
+ * @param string $text
142
+ * @param int $num_words
143
+ * @param string $more
144
+ * @param string $allowed_tags
145
+ * @return string
146
+ */
147
  public static function trim_words($text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br') {
148
  if (null === $more) {
149
  $more = __('&hellip;');
176
  return apply_filters('wp_trim_words', $text, $num_words, $more, $original_text);
177
  }
178
 
179
+ /**
180
+ * @param string $html
181
+ * @return string
182
+ */
183
  public static function close_tags($html) {
184
  #put all opened tags into an array
185
  preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
204
  return $html;
205
  }
206
 
207
+ /**
208
+ * @param string $ret
209
+ * @return mixed
210
+ */
211
+ public static function twitterify($ret) {
212
+ $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
213
+ $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
214
+ $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
215
+ $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
216
+ $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
217
+ $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
218
+ $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
219
+ return $ret;
220
+ }
221
+
222
+ /* WordPress Query Utilities
223
+ ======================== */
224
+
225
+ /**
226
+ * @param string $key
227
+ * @param string $value
228
+ * @return array|int
229
+ */
230
  public static function get_posts_by_meta($key, $value) {
231
  global $wpdb;
232
  $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value);
243
  return 0;
244
  }
245
 
246
+ /**
247
+ * @param string $key
248
+ * @param string $value
249
+ * @return int
250
+ */
251
  public static function get_post_by_meta($key, $value) {
252
  global $wpdb;
253
  $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value);
260
  return 0;
261
  }
262
 
263
+ /**
264
+ * @param int $ttid
265
+ * @return mixed
266
+ */
267
  public static function get_term_id_by_term_taxonomy_id($ttid){
268
  global $wpdb;
269
  $query = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid);
270
  return $wpdb->get_var($query);
271
  }
272
 
273
+ /* Object Utilities
274
+ ======================== */
 
275
 
276
+ /**
277
+ * @param array $array
278
+ * @param string $prop
279
+ * @return void
280
+ */
281
+ public static function osort(&$array, $prop) {
282
+ usort($array, function ($a, $b) use ($prop) {
283
+ return $a->$prop > $b->$prop ? 1 : -1;
284
+ });
285
  }
286
 
287
+ /**
288
+ * @param array $arr
289
+ * @return bool
290
+ */
291
+ public static function is_array_assoc($arr) {
292
+ if (!is_array($arr)) {
293
+ return false;
294
  }
295
+ return (bool)count(array_filter(array_keys($arr), 'is_string'));
296
  }
297
 
298
+ /**
299
+ * @param array $array
300
+ * @return stdClass
301
+ */
302
  public static function array_to_object($array) {
303
  $obj = new stdClass;
304
  foreach ($array as $k => $v) {
311
  return $obj;
312
  }
313
 
314
+ /**
315
+ * @param array $array
316
+ * @param string $key
317
+ * @param mixed $value
318
+ * @return bool|int
319
+ */
320
  public static function get_object_index_by_property($array, $key, $value) {
321
  if (is_array($array)) {
322
  $i = 0;
336
  return false;
337
  }
338
 
339
+ /**
340
+ * @param array $array
341
+ * @param string $key
342
+ * @param mixed $value
343
+ * @return array|null
344
+ * @throws Exception
345
+ */
346
  public static function get_object_by_property($array, $key, $value) {
347
  if (is_array($array)) {
348
  foreach ($array as $arr) {
356
  return null;
357
  }
358
 
359
+ /**
360
+ * @param array $array
361
+ * @param int $len
362
+ * @return array
363
+ */
 
 
364
  public static function array_truncate($array, $len) {
365
  if (sizeof($array) > $len) {
366
  $array = array_splice($array, 0, $len);
368
  return $array;
369
  }
370
 
371
+ /* Bool Utilities
372
+ ======================== */
373
+
374
+ /**
375
+ * @param mixed $property
376
+ * @return bool
377
+ */
378
+ public static function is_true($property) {
379
+ if (isset($property)) {
380
+ if ($property == 'true' || $property == 1 || $property == '1' || $property == true) {
381
+ return true;
382
+ }
383
+ }
384
+ return false;
385
+ }
386
+
387
+ /**
388
+ * @param int $i
389
+ * @return bool
390
+ */
391
  public static function iseven($i) {
392
  return ($i % 2) == 0;
393
  }
394
 
395
+ /**
396
+ * @param int $i
397
+ * @return bool
398
+ */
399
  public static function isodd($i) {
400
  return ($i % 2) != 0;
401
  }
402
 
403
+ /* Links, Forms, Etc. Utilities
404
+ ======================== */
 
 
 
 
 
 
405
 
406
+ /* this $args thing is a fucking mess, fix at some point:
407
+
408
+ http://codex.wordpress.org/Function_Reference/comment_form */
409
+
410
+ /**
411
+ * @param int $post_id
412
+ * @param array $args
413
+ * @return string
414
+ */
415
+ public static function get_comment_form($post_id = null, $args = array()) {
416
+ return self::ob_function('comment_form', array($args, $post_id));
417
  }
418
 
419
+ /**
420
+ * @param string $args
421
+ * @return array
422
+ */
423
  public static function paginate_links( $args = '' ) {
424
  $defaults = array(
425
  'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
494
  }
495
  return $page_links;
496
  }
497
+
498
+ /* LEGACY These have since been re-organized; but keeping linkages for backwards-compatability */
499
+
500
+ public static function get_image_path($iid){
501
+ return TimberImageHelper::get_image_path($iid);
502
+ }
503
+
504
+ public static function get_current_url() {
505
+ return TimberURLHelper::get_current_url();
506
+ }
507
+
508
+ public static function is_url($url) {
509
+ return TimberURLHelper::is_url($url);
510
+ }
511
+
512
+ public static function get_path_base() {
513
+ return TimberURLHelper::get_path_base();
514
+ }
515
+
516
+ public static function get_rel_url($url, $force = false){
517
+ return TimberURLHelper::get_rel_url($url, $force);
518
+ }
519
+
520
+ public static function is_local($url){
521
+ return TimberURLHelper::is_local($url);
522
+ }
523
+
524
+ public static function get_full_path($src) {
525
+ return TimberURLHelper::get_full_path($src);
526
+ }
527
+
528
+ public static function get_rel_path($src) {
529
+ return TimberURLHelper::get_rel_path($src);
530
+ }
531
+
532
+ public static function remove_double_slashes($url){
533
+ return TimberURLHelper::remove_double_slashes($url);
534
+ }
535
+
536
+ public static function prepend_to_url($url, $path){
537
+ return TimberURLHelper::prepend_to_url($url, $path);
538
+ }
539
+
540
+ public static function preslashit($path){
541
+ return TimberURLHelper::preslashit($path);
542
+ }
543
+
544
+ public static function is_external($url){
545
+ return TimberURLHelper::is_external($url);
546
+ }
547
+
548
+ public static function download_url($url, $timeout = 300) {
549
+ return TimberURLHelper::download_url($url, $timeout);
550
+ }
551
+
552
+ public static function get_params($i = -1) {
553
+ return TimberURLHelper::get_params($i);
554
+ }
555
+
556
+
557
  }
558
 
559
  class WPHelper extends TimberHelper {
560
  //for backwards compat, will remove eventually
561
+ }
functions/timber-image-helper.php CHANGED
@@ -2,34 +2,142 @@
2
 
3
  class TimberImageHelper {
4
 
5
- function hexrgb($hexstr) {
6
- $int = hexdec($hexstr);
 
 
 
 
 
 
 
 
 
 
7
  return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
8
  }
9
 
10
- public static function get_letterbox_file_rel($src, $w, $h) {
 
 
 
 
 
 
 
11
  $path_parts = pathinfo($src);
12
  $basename = $path_parts['filename'];
13
  $ext = $path_parts['extension'];
14
  $dir = $path_parts['dirname'];
15
- $newbase = $basename . '-lb-' . $w . 'x' . $h;
 
16
  $new_path = $dir . '/' . $newbase . '.' . $ext;
17
  return $new_path;
18
  }
19
 
20
- public static function get_letterbox_file_path($src, $w, $h) {
 
 
 
 
 
 
 
21
  $path_parts = pathinfo($src);
22
  $basename = $path_parts['filename'];
23
  $ext = $path_parts['extension'];
24
  $dir = $path_parts['dirname'];
25
- $newbase = $basename . '-lb-' . $w . 'x' . $h;
 
26
  $new_path = $dir . '/' . $newbase . '.' . $ext;
27
  $new_root_path = ABSPATH . $new_path;
28
  $new_root_path = str_replace('//', '/', $new_root_path);
29
  return $new_root_path;
30
  }
31
 
32
- function img_to_jpg($src, $bghex = '#FFFFFF'){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  $src = str_replace(site_url(), '', $src);
34
  $output = str_replace('.png', '.jpg', $src);
35
  $input_file = ABSPATH . $src;
@@ -49,7 +157,11 @@
49
  return $filename;
50
  }
51
 
52
- public static function get_sideloaded_file_loc($file){
 
 
 
 
53
  $upload = wp_upload_dir();
54
  $dir = $upload['path'];
55
  $filename = $file;
@@ -63,7 +175,11 @@
63
  return $dir . '/' . $basename. '.' . $ext;
64
  }
65
 
66
- public static function sideload_image($file) {
 
 
 
 
67
  $loc = self::get_sideloaded_file_loc($file);
68
  if (file_exists($loc)){
69
  return str_replace(ABSPATH, '', $loc);
@@ -87,7 +203,15 @@
87
  return $file['url'];
88
  }
89
 
90
- public static function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false ){
 
 
 
 
 
 
 
 
91
  if (empty($src)){
92
  return '';
93
  }
@@ -123,7 +247,7 @@
123
  if ($abs){
124
  return untrailingslashit(content_url()).$new_path;
125
  } else {
126
- return TimberHelper::preslashit($new_path);
127
  }
128
  return $new_path;
129
  }
@@ -139,63 +263,48 @@
139
  $src_h = $current_size['height'];
140
 
141
  $src_ratio = $src_w / $src_h;
142
- if ( $h ) {
 
 
143
 
144
- // Get ratios
145
- $dest_ratio = $w / $h;
146
- $src_wt = $src_h * $dest_ratio;
147
- $src_ht = $src_w / $dest_ratio;
148
 
149
- if ( ! $crop ) {
150
- // Do not crop
151
- $image->resize( $w, $h );
152
- } else {
153
- //start with defaults:
154
- $src_x = $src_w / 2 - $src_wt / 2;
155
- $src_y = ( $src_h - $src_ht ) / 6;
156
- //now specific overrides based on options:
157
- if ( $crop == 'center' ) {
158
- // Get source x and y
159
- $src_x = round( ( $src_w - $src_wt ) / 2 );
160
- $src_y = round( ( $src_h - $src_ht ) / 2 );
161
- } else if ($crop == 'top') {
162
-
163
- error_log('found it on top');
164
- $src_y = 0;
165
- } else if ($crop == 'bottom') {
166
- $src_y = $src_h - $src_ht;
167
- } else if ($crop == 'left') {
168
- $src_x = 0;
169
- } else if ($crop == 'right') {
170
- $src_x = $src_w - $src_wt;
171
- }
172
-
173
- // Crop the image
174
- if ( $dest_ratio > $src_ratio ) {
175
- $image->crop( 0, $src_y, $src_w, $src_ht, $w, $h );
176
- } else {
177
- $image->crop( $src_x, 0, $src_wt, $src_h, $w, $h );
178
- }
179
 
 
 
 
 
 
 
 
 
180
  }
181
 
182
- } else {
183
- $h = $w;
184
- if ( $src_ratio < 1 ){
185
- $h = $w / $src_ratio;
186
- // Get source x and y
187
- if ( $crop == 'center' ) {
188
- $src_x = round( ( $src_w - $w ) / 2 );
189
- $src_y = round( ( $src_h - $h ) / 2 );
190
- } else {
191
- $src_x = 0;
192
- $src_y = 0;
193
- }
194
- $image->crop( $src_x, $src_y, $src_w, $src_h, $w, $h );
195
  } else {
196
- $image->resize( $w, $h );
197
  }
 
198
  }
 
199
  $result = $image->save($new_root_path);
200
  if (is_wp_error($result)){
201
  error_log('Error resizing image');
2
 
3
  class TimberImageHelper {
4
 
5
+ /**
6
+ * @param string $hexstr
7
+ * @return array
8
+ */
9
+ public static function hexrgb($hexstr) {
10
+ if (!strstr($hexstr, '#')){
11
+ $hexstr = '#'.$hexstr;
12
+ }
13
+ if (strlen($hexstr) == 4){
14
+ $hexstr = '#' . $hexstr[1] . $hexstr[1] . $hexstr[2] . $hexstr[2] . $hexstr[3] . $hexstr[3];
15
+ }
16
+ $int = hexdec($hexstr);
17
  return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
18
  }
19
 
20
+ /**
21
+ * @param string $src
22
+ * @param int $w
23
+ * @param int $h
24
+ * @param string $color
25
+ * @return string
26
+ */
27
+ public static function get_letterbox_file_rel($src, $w, $h, $color) {
28
  $path_parts = pathinfo($src);
29
  $basename = $path_parts['filename'];
30
  $ext = $path_parts['extension'];
31
  $dir = $path_parts['dirname'];
32
+ $color = str_replace('#', '', $color);
33
+ $newbase = $basename . '-lbox-' . $w . 'x' . $h . '-' . $color;
34
  $new_path = $dir . '/' . $newbase . '.' . $ext;
35
  return $new_path;
36
  }
37
 
38
+ /**
39
+ * @param string $src
40
+ * @param int $w
41
+ * @param int $h
42
+ * @param string $color
43
+ * @return string
44
+ */
45
+ public static function get_letterbox_file_path($src, $w, $h, $color) {
46
  $path_parts = pathinfo($src);
47
  $basename = $path_parts['filename'];
48
  $ext = $path_parts['extension'];
49
  $dir = $path_parts['dirname'];
50
+ $color = str_replace('#', '', $color);
51
+ $newbase = $basename . '-lbox-' . $w . 'x' . $h . '-' . $color;
52
  $new_path = $dir . '/' . $newbase . '.' . $ext;
53
  $new_root_path = ABSPATH . $new_path;
54
  $new_root_path = str_replace('//', '/', $new_root_path);
55
  return $new_root_path;
56
  }
57
 
58
+ /**
59
+ * @param int $iid
60
+ * @return string
61
+ */
62
+ public static function get_image_path($iid) {
63
+ $size = 'full';
64
+ $src = wp_get_attachment_image_src($iid, $size);
65
+ $src = $src[0];
66
+ return self::get_rel_path($src);
67
+ }
68
+
69
+ /**
70
+ * @param string $src
71
+ * @param int $w
72
+ * @param int $h
73
+ * @param string $color
74
+ * @return mixed|null|string
75
+ */
76
+ public static function letterbox($src, $w, $h, $color = '#000000', $force = false) {
77
+ $abspath = substr(ABSPATH, 0, -1);
78
+ $urlinfo = parse_url($src);
79
+ if( $_SERVER['DOCUMENT_ROOT'] != $abspath ) {
80
+ $subdir = str_replace($_SERVER['DOCUMENT_ROOT'].'/', '', $abspath);
81
+ $urlinfo['path'] = str_replace('/'.$subdir.'/', '', $urlinfo['path']);
82
+ }
83
+ $old_file = ABSPATH.$urlinfo['path'];
84
+ $new_file = self::get_letterbox_file_path($urlinfo['path'], $w, $h, $color);
85
+ $urlinfo = parse_url($src);
86
+ $new_file_rel = self::get_letterbox_file_rel($urlinfo['path'], $w, $h, $color);
87
+ if (file_exists($new_file_rel) && !$force) {
88
+ return $new_file_rel;
89
+ }
90
+ $bg = imagecreatetruecolor($w, $h);
91
+ $c = self::hexrgb($color);
92
+ $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
93
+ imagefill($bg, 0, 0, $white);
94
+ $image = wp_get_image_editor($old_file);
95
+ if (!is_wp_error($image)) {
96
+ $current_size = $image->get_size();
97
+ $ow = $current_size['width'];
98
+ $oh = $current_size['height'];
99
+ $new_aspect = $w / $h;
100
+ $old_aspect = $ow / $oh;
101
+ if ($new_aspect > $old_aspect) {
102
+ //taller than goal
103
+ $h_scale = $h / $oh;
104
+ $owt = $ow * $h_scale;
105
+ $y = 0;
106
+ $x = $w / 2 - $owt / 2;
107
+ $oht = $h;
108
+ $image->crop(0, 0, $ow, $oh, $owt, $oht);
109
+ } else {
110
+ $w_scale = $w / $ow;
111
+ $oht = $oh * $w_scale;
112
+ $x = 0;
113
+ $y = $h / 2 - $oht / 2;
114
+ $owt = $w;
115
+ $image->crop(0, 0, $ow, $oh, $owt, $oht);
116
+ }
117
+ $image->save($new_file);
118
+ $func = 'imagecreatefromjpeg';
119
+ $ext = pathinfo($new_file, PATHINFO_EXTENSION);
120
+ if ($ext == 'gif') {
121
+ $func = 'imagecreatefromgif';
122
+ } else if ($ext == 'png') {
123
+ $func = 'imagecreatefrompng';
124
+ }
125
+ $image = $func($new_file);
126
+ imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
127
+ imagejpeg($bg, $new_file);
128
+ return TimberURLHelper::get_rel_path($new_file);
129
+ } else {
130
+ TimberHelper::error_log($image);
131
+ }
132
+ return null;
133
+ }
134
+
135
+ /**
136
+ * @param string $src
137
+ * @param string $bghex
138
+ * @return string
139
+ */
140
+ public static function img_to_jpg($src, $bghex = '#FFFFFF'){
141
  $src = str_replace(site_url(), '', $src);
142
  $output = str_replace('.png', '.jpg', $src);
143
  $input_file = ABSPATH . $src;
157
  return $filename;
158
  }
159
 
160
+ /**
161
+ * @param string $file
162
+ * @return string
163
+ */
164
+ public static function get_sideloaded_file_loc($file){
165
  $upload = wp_upload_dir();
166
  $dir = $upload['path'];
167
  $filename = $file;
175
  return $dir . '/' . $basename. '.' . $ext;
176
  }
177
 
178
+ /**
179
+ * @param string $file
180
+ * @return string
181
+ */
182
+ public static function sideload_image($file) {
183
  $loc = self::get_sideloaded_file_loc($file);
184
  if (file_exists($loc)){
185
  return str_replace(ABSPATH, '', $loc);
203
  return $file['url'];
204
  }
205
 
206
+ /**
207
+ * @param string $src
208
+ * @param int $w
209
+ * @param int $h
210
+ * @param string $crop
211
+ * @param bool $force_resize
212
+ * @return string
213
+ */
214
+ public static function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false ){
215
  if (empty($src)){
216
  return '';
217
  }
247
  if ($abs){
248
  return untrailingslashit(content_url()).$new_path;
249
  } else {
250
+ return TimberURLHelper::preslashit($new_path);
251
  }
252
  return $new_path;
253
  }
263
  $src_h = $current_size['height'];
264
 
265
  $src_ratio = $src_w / $src_h;
266
+ if ( ! $h ) {
267
+ $h = round( $w / $src_ratio);
268
+ }
269
 
270
+ // Get ratios
271
+ $dest_ratio = $w / $h;
272
+ $src_wt = $src_h * $dest_ratio;
273
+ $src_ht = $src_w / $dest_ratio;
274
 
275
+ if ( ! $crop ) {
276
+ // Always crop, to allow resizing upwards
277
+ $image->crop( 0, 0, $src_w, $src_h, $w, $h );
278
+ } else {
279
+ //start with defaults:
280
+ $src_x = $src_w / 2 - $src_wt / 2;
281
+ $src_y = ( $src_h - $src_ht ) / 6;
282
+ //now specific overrides based on options:
283
+ if ( $crop == 'center' ) {
284
+ // Get source x and y
285
+ $src_x = round( ( $src_w - $src_wt ) / 2 );
286
+ $src_y = round( ( $src_h - $src_ht ) / 2 );
287
+ } else if ($crop == 'top') {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
+ error_log('found it on top');
290
+ $src_y = 0;
291
+ } else if ($crop == 'bottom') {
292
+ $src_y = $src_h - $src_ht;
293
+ } else if ($crop == 'left') {
294
+ $src_x = 0;
295
+ } else if ($crop == 'right') {
296
+ $src_x = $src_w - $src_wt;
297
  }
298
 
299
+ // Crop the image
300
+ if ( $dest_ratio > $src_ratio ) {
301
+ $image->crop( 0, $src_y, $src_w, $src_ht, $w, $h );
 
 
 
 
 
 
 
 
 
 
302
  } else {
303
+ $image->crop( $src_x, 0, $src_wt, $src_h, $w, $h );
304
  }
305
+
306
  }
307
+
308
  $result = $image->save($new_root_path);
309
  if (is_wp_error($result)){
310
  error_log('Error resizing image');
functions/timber-image.php CHANGED
@@ -10,22 +10,35 @@ class TimberImage extends TimberCore {
10
 
11
  public static $representation = 'image';
12
 
13
- function __construct($iid) {
 
 
 
14
  $this->init($iid);
15
  }
16
 
17
- function __toString() {
 
 
 
18
  if ($this->get_src()){
19
  return $this->get_src();
20
  }
21
  return '';
22
  }
23
 
24
- function get_pathinfo(){
 
 
 
25
  return pathinfo($this->file);
26
  }
27
 
28
- function get_dimensions($dim = null){
 
 
 
 
29
  if (isset($this->_dimensions)){
30
  return $this->get_dimensions_loaded($dim);
31
  }
@@ -37,7 +50,11 @@ class TimberImage extends TimberCore {
37
  return $this->get_dimensions_loaded($dim);
38
  }
39
 
40
- function get_dimensions_loaded($dim){
 
 
 
 
41
  if ($dim == null){
42
  return $this->_dimensions;
43
  }
@@ -49,22 +66,32 @@ class TimberImage extends TimberCore {
49
  }
50
  }
51
 
52
- function get_width(){
 
 
 
53
  return $this->get_dimensions('width');
54
  }
55
 
56
- function get_height(){
 
 
 
57
  return $this->get_dimensions('height');
58
  }
59
 
60
- function get_src( $size = '' ) {
 
 
 
 
61
  if (isset($this->abs_url)) {
62
- return $this->abs_url;
63
  }
64
 
65
  if ($size && is_string($size) && isset($this->sizes[$size])) {
66
  $image = image_downsize($this->ID, $size);
67
- return reset($image);
68
  }
69
 
70
  if (!isset($this->file) && isset($this->_wp_attached_file)) {
@@ -74,19 +101,42 @@ class TimberImage extends TimberCore {
74
  if (!isset($this->file))
75
  return false;
76
 
77
- $dir = wp_upload_dir();
78
  $base = ($dir["baseurl"]);
79
- return trailingslashit($base) . $this->file;
 
80
  }
81
 
82
- function get_path() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  if (strlen($this->abs_url)) {
84
  return $this->abs_url;
85
  }
86
  return get_permalink($this->ID);
87
  }
88
 
89
- function get_parent() {
 
 
 
90
  if (!$this->post_parent) {
91
  return false;
92
  }
@@ -98,7 +148,11 @@ class TimberImage extends TimberCore {
98
  return $alt;
99
  }
100
 
101
- function init($iid) {
 
 
 
 
102
  if (!is_numeric($iid) && is_string($iid)) {
103
  if (strstr($iid, '://')) {
104
  $this->init_with_url($iid);
@@ -128,7 +182,7 @@ class TimberImage extends TimberCore {
128
  $image_info = get_object_vars($image_info);
129
  }
130
  $this->import($image_info);
131
- $basedir = wp_upload_dir();
132
  $basedir = $basedir['basedir'];
133
  if (isset($this->file)){
134
  $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
@@ -156,36 +210,55 @@ class TimberImage extends TimberCore {
156
  }
157
  }
158
 
159
- function init_with_url($url) {
 
 
 
160
  $this->abs_url = $url;
161
  $this->file_loc = $url;
162
- if (TimberHelper::is_local($url)){
163
- $this->file = ABSPATH . TimberHelper::get_rel_url($url);
164
  }
165
  }
166
 
167
- /* deprecated */
168
- function get_url() {
 
 
 
169
  return $this->get_src();
170
  }
171
 
172
  /* Alias */
173
 
174
- public function aspect(){
 
 
 
175
  $w = intval($this->width());
176
  $h = intval($this->height());
177
  return $w/$h;
178
  }
179
 
180
- public function height(){
 
 
 
181
  return $this->get_height();
182
  }
183
 
184
- public function src($size = '') {
 
 
 
 
185
  return $this->get_src($size);
186
  }
187
 
188
- public function width(){
 
 
 
189
  return $this->get_width();
190
  }
191
 
10
 
11
  public static $representation = 'image';
12
 
13
+ /**
14
+ * @param int $iid
15
+ */
16
+ function __construct($iid) {
17
  $this->init($iid);
18
  }
19
 
20
+ /**
21
+ * @return string
22
+ */
23
+ function __toString() {
24
  if ($this->get_src()){
25
  return $this->get_src();
26
  }
27
  return '';
28
  }
29
 
30
+ /**
31
+ * @return mixed
32
+ */
33
+ function get_pathinfo(){
34
  return pathinfo($this->file);
35
  }
36
 
37
+ /**
38
+ * @param string $dim
39
+ * @return array|int
40
+ */
41
+ function get_dimensions($dim = null){
42
  if (isset($this->_dimensions)){
43
  return $this->get_dimensions_loaded($dim);
44
  }
50
  return $this->get_dimensions_loaded($dim);
51
  }
52
 
53
+ /**
54
+ * @param string $dim
55
+ * @return array|int
56
+ */
57
+ function get_dimensions_loaded($dim){
58
  if ($dim == null){
59
  return $this->_dimensions;
60
  }
66
  }
67
  }
68
 
69
+ /**
70
+ * @return int
71
+ */
72
+ function get_width(){
73
  return $this->get_dimensions('width');
74
  }
75
 
76
+ /**
77
+ * @return int
78
+ */
79
+ function get_height(){
80
  return $this->get_dimensions('height');
81
  }
82
 
83
+ /**
84
+ * @param string $size
85
+ * @return bool|string
86
+ */
87
+ function get_src( $size = '' ) {
88
  if (isset($this->abs_url)) {
89
+ return $this->_maybe_secure_url( $this->abs_url );
90
  }
91
 
92
  if ($size && is_string($size) && isset($this->sizes[$size])) {
93
  $image = image_downsize($this->ID, $size);
94
+ return $this->_maybe_secure_url( reset($image) );
95
  }
96
 
97
  if (!isset($this->file) && isset($this->_wp_attached_file)) {
101
  if (!isset($this->file))
102
  return false;
103
 
104
+ $dir = self::wp_upload_dir();
105
  $base = ($dir["baseurl"]);
106
+
107
+ return trailingslashit( $this->_maybe_secure_url( $base ) ) . $this->file;
108
  }
109
 
110
+ private static function _maybe_secure_url( $url ) {
111
+ if ( is_ssl() && strpos( $url, 'https' ) !== 0 && strpos( $url, 'http' ) === 0 )
112
+ $url = 'https' . substr( $url, strlen( 'http' ) );
113
+
114
+ return $url;
115
+ }
116
+
117
+ public static function wp_upload_dir() {
118
+ static $wp_upload_dir = false;
119
+
120
+ if ( !$wp_upload_dir )
121
+ $wp_upload_dir = wp_upload_dir();
122
+
123
+ return $wp_upload_dir;
124
+ }
125
+
126
+ /**
127
+ * @return string
128
+ */
129
+ function get_path() {
130
  if (strlen($this->abs_url)) {
131
  return $this->abs_url;
132
  }
133
  return get_permalink($this->ID);
134
  }
135
 
136
+ /**
137
+ * @return bool|TimberImage
138
+ */
139
+ function get_parent() {
140
  if (!$this->post_parent) {
141
  return false;
142
  }
148
  return $alt;
149
  }
150
 
151
+
152
+ /**
153
+ * @param int $iid
154
+ */
155
+ function init($iid) {
156
  if (!is_numeric($iid) && is_string($iid)) {
157
  if (strstr($iid, '://')) {
158
  $this->init_with_url($iid);
182
  $image_info = get_object_vars($image_info);
183
  }
184
  $this->import($image_info);
185
+ $basedir = self::wp_upload_dir();
186
  $basedir = $basedir['basedir'];
187
  if (isset($this->file)){
188
  $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
210
  }
211
  }
212
 
213
+ /**
214
+ * @param string $url
215
+ */
216
+ function init_with_url($url) {
217
  $this->abs_url = $url;
218
  $this->file_loc = $url;
219
+ if (TimberURLHelper::is_local($url)){
220
+ $this->file = ABSPATH . TimberURLHelper::get_rel_url($url);
221
  }
222
  }
223
 
224
+ /**
225
+ * @deprecated
226
+ * @return string
227
+ */
228
+ function get_url() {
229
  return $this->get_src();
230
  }
231
 
232
  /* Alias */
233
 
234
+ /**
235
+ * @return float
236
+ */
237
+ public function aspect(){
238
  $w = intval($this->width());
239
  $h = intval($this->height());
240
  return $w/$h;
241
  }
242
 
243
+ /**
244
+ * @return int
245
+ */
246
+ public function height(){
247
  return $this->get_height();
248
  }
249
 
250
+ /**
251
+ * @param string $size
252
+ * @return bool|string
253
+ */
254
+ public function src($size = '') {
255
  return $this->get_src($size);
256
  }
257
 
258
+ /**
259
+ * @return int
260
+ */
261
+ public function width(){
262
  return $this->get_width();
263
  }
264
 
functions/timber-loader.php CHANGED
@@ -23,12 +23,22 @@ class TimberLoader {
23
 
24
  var $locations;
25
 
26
- function __construct($caller = false) {
 
 
 
27
  $this->locations = $this->get_locations($caller);
28
  $this->cache_mode = apply_filters( 'timber_cache_mode', $this->cache_mode );
29
  }
30
 
31
- function render( $file, $data = null, $expires = false, $cache_mode = self::CACHE_USE_DEFAULT ) {
 
 
 
 
 
 
 
32
  // Different $expires if user is anonymous or logged in
33
  if ( is_array( $expires ) ) {
34
  if ( is_user_logged_in() && isset( $expires[1] ) )
@@ -63,7 +73,11 @@ class TimberLoader {
63
  return $output;
64
  }
65
 
66
- function choose_template($filenames) {
 
 
 
 
67
  if (is_array($filenames)) {
68
  /* its an array so we have to figure out which one the dev wants */
69
  foreach ($filenames as $filename) {
@@ -76,7 +90,11 @@ class TimberLoader {
76
  return $filenames;
77
  }
78
 
79
- function template_exists($file) {
 
 
 
 
80
  foreach ($this->locations as $dir) {
81
  $look_for = trailingslashit($dir) . $file;
82
  if (file_exists($look_for)) {
@@ -86,7 +104,10 @@ class TimberLoader {
86
  return false;
87
  }
88
 
89
- function get_locations_theme() {
 
 
 
90
  $theme_locs = array();
91
  $child_loc = get_stylesheet_directory();
92
  $parent_loc = get_template_directory();
@@ -107,7 +128,10 @@ class TimberLoader {
107
  return $theme_locs;
108
  }
109
 
110
- function get_locations_user() {
 
 
 
111
  $locs = array();
112
  if (isset(Timber::$locations)) {
113
  if (is_string(Timber::$locations)) {
@@ -123,7 +147,11 @@ class TimberLoader {
123
  return $locs;
124
  }
125
 
126
- function get_locations_caller($caller = false) {
 
 
 
 
127
  $locs = array();
128
  if ($caller && is_string($caller)) {
129
  $caller = trailingslashit($caller);
@@ -138,7 +166,11 @@ class TimberLoader {
138
  return $locs;
139
  }
140
 
141
- function get_locations($caller = false) {
 
 
 
 
142
  //prioirty: user locations, caller (but not theme), child theme, parent theme, caller
143
  $locs = array();
144
  $locs = array_merge($locs, $this->get_locations_user());
@@ -152,7 +184,10 @@ class TimberLoader {
152
  return $locs;
153
  }
154
 
155
- function get_loader() {
 
 
 
156
  $loaders = array();
157
  foreach ($this->locations as $loc) {
158
  $loc = realpath($loc);
@@ -167,7 +202,10 @@ class TimberLoader {
167
  return $loader;
168
  }
169
 
170
- function get_twig() {
 
 
 
171
  $loader_loc = trailingslashit(TIMBER_LOC) . 'Twig/lib/Twig/Autoloader.php';
172
  require_once($loader_loc);
173
  Twig_Autoloader::register();
@@ -188,19 +226,28 @@ class TimberLoader {
188
  return $twig;
189
  }
190
 
191
- private function _get_cache_extension() {
192
- $loader_loc = trailingslashit(TIMBER_LOC) . 'functions/cache/loader.php';
193
- require_once($loader_loc);
194
- TimberCache_Loader::register();
 
 
 
195
 
196
- $key_generator = new \Timber\Cache\KeyGenerator();
197
- $cache_provider = new \Timber\Cache\WPObjectCacheAdapter( $this );
198
- $cache_strategy = new \Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy( $cache_provider, $key_generator );
199
- $cache_extension = new \Asm89\Twig\CacheExtension\Extension($cache_strategy);
200
 
201
- return $cache_extension;
202
- }
203
 
 
 
 
 
 
 
204
  public function get_cache( $key, $group = self::CACHEGROUP, $cache_mode = self::CACHE_USE_DEFAULT ) {
205
  $object_cache = false;
206
 
@@ -224,6 +271,14 @@ class TimberLoader {
224
  return $value;
225
  }
226
 
 
 
 
 
 
 
 
 
227
  public function set_cache( $key, $value, $group = self::CACHEGROUP, $expires = 0, $cache_mode = self::CACHE_USE_DEFAULT ) {
228
  $object_cache = false;
229
 
@@ -248,14 +303,18 @@ class TimberLoader {
248
  return $value;
249
  }
250
 
251
- private function _get_cache_mode( $cache_mode ) {
252
- if ( empty( $cache_mode ) || self::CACHE_USE_DEFAULT === $cache_mode )
253
- $cache_mode = $this->cache_mode;
 
 
 
 
254
 
255
- // Fallback if self::$cache_mode did not get a valid value
256
- if ( !in_array( $cache_mode, self::$cache_modes ) )
257
- $cache_mode = self::CACHE_OBJECT;
258
 
259
- return $cache_mode;
260
- }
261
  }
23
 
24
  var $locations;
25
 
26
+ /**
27
+ * @param bool $caller
28
+ */
29
+ function __construct($caller = false) {
30
  $this->locations = $this->get_locations($caller);
31
  $this->cache_mode = apply_filters( 'timber_cache_mode', $this->cache_mode );
32
  }
33
 
34
+ /**
35
+ * @param string $file
36
+ * @param array $data
37
+ * @param bool $expires
38
+ * @param string $cache_mode
39
+ * @return bool|string
40
+ */
41
+ function render( $file, $data = null, $expires = false, $cache_mode = self::CACHE_USE_DEFAULT ) {
42
  // Different $expires if user is anonymous or logged in
43
  if ( is_array( $expires ) ) {
44
  if ( is_user_logged_in() && isset( $expires[1] ) )
73
  return $output;
74
  }
75
 
76
+ /**
77
+ * @param array $filenames
78
+ * @return bool
79
+ */
80
+ function choose_template($filenames) {
81
  if (is_array($filenames)) {
82
  /* its an array so we have to figure out which one the dev wants */
83
  foreach ($filenames as $filename) {
90
  return $filenames;
91
  }
92
 
93
+ /**
94
+ * @param string $file
95
+ * @return bool
96
+ */
97
+ function template_exists($file) {
98
  foreach ($this->locations as $dir) {
99
  $look_for = trailingslashit($dir) . $file;
100
  if (file_exists($look_for)) {
104
  return false;
105
  }
106
 
107
+ /**
108
+ * @return array
109
+ */
110
+ function get_locations_theme() {
111
  $theme_locs = array();
112
  $child_loc = get_stylesheet_directory();
113
  $parent_loc = get_template_directory();
128
  return $theme_locs;
129
  }
130
 
131
+ /**
132
+ * @return array
133
+ */
134
+ function get_locations_user() {
135
  $locs = array();
136
  if (isset(Timber::$locations)) {
137
  if (is_string(Timber::$locations)) {
147
  return $locs;
148
  }
149
 
150
+ /**
151
+ * @param bool $caller
152
+ * @return array
153
+ */
154
+ function get_locations_caller($caller = false) {
155
  $locs = array();
156
  if ($caller && is_string($caller)) {
157
  $caller = trailingslashit($caller);
166
  return $locs;
167
  }
168
 
169
+ /**
170
+ * @param bool $caller
171
+ * @return array
172
+ */
173
+ function get_locations($caller = false) {
174
  //prioirty: user locations, caller (but not theme), child theme, parent theme, caller
175
  $locs = array();
176
  $locs = array_merge($locs, $this->get_locations_user());
184
  return $locs;
185
  }
186
 
187
+ /**
188
+ * @return Twig_Loader_Chain
189
+ */
190
+ function get_loader() {
191
  $loaders = array();
192
  foreach ($this->locations as $loc) {
193
  $loc = realpath($loc);
202
  return $loader;
203
  }
204
 
205
+ /**
206
+ * @return Twig_Environment
207
+ */
208
+ function get_twig() {
209
  $loader_loc = trailingslashit(TIMBER_LOC) . 'Twig/lib/Twig/Autoloader.php';
210
  require_once($loader_loc);
211
  Twig_Autoloader::register();
226
  return $twig;
227
  }
228
 
229
+ /**
230
+ * @return \Asm89\Twig\CacheExtension\Extension
231
+ */
232
+ private function _get_cache_extension() {
233
+ $loader_loc = trailingslashit(TIMBER_LOC) . 'functions/cache/loader.php';
234
+ require_once($loader_loc);
235
+ TimberCache_Loader::register();
236
 
237
+ $key_generator = new \Timber\Cache\KeyGenerator();
238
+ $cache_provider = new \Timber\Cache\WPObjectCacheAdapter( $this );
239
+ $cache_strategy = new \Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy( $cache_provider, $key_generator );
240
+ $cache_extension = new \Asm89\Twig\CacheExtension\Extension($cache_strategy);
241
 
242
+ return $cache_extension;
243
+ }
244
 
245
+ /**
246
+ * @param string $key
247
+ * @param string $group
248
+ * @param string $cache_mode
249
+ * @return bool
250
+ */
251
  public function get_cache( $key, $group = self::CACHEGROUP, $cache_mode = self::CACHE_USE_DEFAULT ) {
252
  $object_cache = false;
253
 
271
  return $value;
272
  }
273
 
274
+ /**
275
+ * @param string $key
276
+ * @param mixed $value
277
+ * @param string $group
278
+ * @param int $expires
279
+ * @param string $cache_mode
280
+ * @return mixed
281
+ */
282
  public function set_cache( $key, $value, $group = self::CACHEGROUP, $expires = 0, $cache_mode = self::CACHE_USE_DEFAULT ) {
283
  $object_cache = false;
284
 
303
  return $value;
304
  }
305
 
306
+ /**
307
+ * @param string $cache_mode
308
+ * @return string
309
+ */
310
+ private function _get_cache_mode( $cache_mode ) {
311
+ if ( empty( $cache_mode ) || self::CACHE_USE_DEFAULT === $cache_mode )
312
+ $cache_mode = $this->cache_mode;
313
 
314
+ // Fallback if self::$cache_mode did not get a valid value
315
+ if ( !in_array( $cache_mode, self::$cache_modes ) )
316
+ $cache_mode = self::CACHE_OBJECT;
317
 
318
+ return $cache_mode;
319
+ }
320
  }
functions/timber-menu.php CHANGED
@@ -2,15 +2,20 @@
2
 
3
  class TimberMenu extends TimberCore {
4
 
 
 
5
  var $items = null;
6
  var $name = null;
7
  var $ID = null;
8
 
 
 
 
9
  function __construct($slug = 0) {
10
  $locations = get_nav_menu_locations();
11
  if ($slug != 0 && is_numeric($slug)){
12
  $menu_id = $slug;
13
- } else if (count($locations)){
14
  $menu_id = $this->get_menu_id_from_locations($slug, $locations);
15
  } else if ($slug === false){
16
  $menu_id = false;
@@ -25,6 +30,9 @@ class TimberMenu extends TimberCore {
25
  return null;
26
  }
27
 
 
 
 
28
  private function init($menu_id){
29
  $menu = wp_get_nav_menu_items($menu_id);
30
  $menu = self::order_children($menu);
@@ -34,6 +42,11 @@ class TimberMenu extends TimberCore {
34
  $this->ID = $this->term_id;
35
  }
36
 
 
 
 
 
 
37
  private function get_menu_id_from_locations($slug, $locations){
38
  if ($slug === 0){
39
  $slug = $this->get_menu_id_from_terms($slug);
@@ -46,6 +59,10 @@ class TimberMenu extends TimberCore {
46
  }
47
  }
48
 
 
 
 
 
49
  private function get_menu_id_from_terms($slug = 0){
50
  if (!is_numeric($slug) && is_string($slug)){
51
  //we have a string so lets search for that
@@ -67,7 +84,11 @@ class TimberMenu extends TimberCore {
67
  return 0;
68
  }
69
 
70
-
 
 
 
 
71
  function find_parent_item_in_menu($menu_items, $parent_id) {
72
  foreach ($menu_items as &$item) {
73
  if ($item->ID == $parent_id) {
@@ -77,12 +98,16 @@ class TimberMenu extends TimberCore {
77
  return null;
78
  }
79
 
 
 
 
 
80
  function order_children($items) {
81
  $index = array();
82
  $menu = array();
83
  _wp_menu_item_classes_by_context($items);
84
  foreach($items as $item) {
85
- $index[$item->ID] = new TimberMenuItem($item);
86
  }
87
  foreach($index as $item) {
88
  if($item->menu_item_parent && isset($index[$item->menu_item_parent])) {
@@ -94,6 +119,9 @@ class TimberMenu extends TimberCore {
94
  return $menu;
95
  }
96
 
 
 
 
97
  function get_items() {
98
  if (is_array($this->items)) {
99
  return $this->items;
@@ -107,6 +135,9 @@ class TimberMenuItem extends TimberCore {
107
  var $children;
108
  var $has_child_class = false;
109
 
 
 
 
110
  function __construct($data) {
111
  $this->import($data);
112
  $this->import_classes($data);
@@ -117,11 +148,17 @@ class TimberMenuItem extends TimberCore {
117
  $this->add_class('menu-item-'.$this->ID);
118
  }
119
 
 
 
 
120
  function add_class($class_name){
121
  $this->classes[] = $class_name;
122
  $this->class .= ' '.$class_name;
123
  }
124
 
 
 
 
125
  function name() {
126
  if (isset($this->title)){
127
  return $this->title;
@@ -129,18 +166,30 @@ class TimberMenuItem extends TimberCore {
129
  return $this->_name;
130
  }
131
 
 
 
 
132
  function slug() {
133
  return $this->post_name;
134
  }
135
 
 
 
 
136
  function get_link() {
137
  return $this->url;
138
  }
139
 
 
 
 
140
  function get_path() {
141
- return TimberHelper::get_rel_url($this->url);
142
  }
143
 
 
 
 
144
  function add_child($item) {
145
  if (!$this->has_child_class){
146
  $this->add_class('menu-item-has-children');
@@ -152,10 +201,16 @@ class TimberMenuItem extends TimberCore {
152
  $this->children[] = $item;
153
  }
154
 
 
 
 
155
  function import_classes($data){
156
  $this->class = trim(implode(' ', $data->classes));
157
  }
158
 
 
 
 
159
  function get_children() {
160
  if (isset($this->children)) {
161
  return $this->children;
@@ -163,35 +218,56 @@ class TimberMenuItem extends TimberCore {
163
  return false;
164
  }
165
 
 
 
 
166
  function is_external(){
167
  if ($this->type != 'custom'){
168
  return false;
169
  }
170
- return TimberHelper::is_external($this->url);
171
  }
172
 
173
  /* Aliases */
174
 
 
 
 
175
  public function children(){
176
  return $this->get_children();
177
  }
178
 
 
 
 
179
  public function external(){
180
  return $this->is_external();
181
  }
182
 
 
 
 
183
  public function link(){
184
  return $this->get_link();
185
  }
186
 
 
 
 
187
  public function path(){
188
  return $this->get_path();
189
  }
190
 
 
 
 
191
  public function permalink(){
192
  return $this->get_link();
193
  }
194
 
 
 
 
195
  public function get_permalink(){
196
  return $this->get_link();
197
  }
2
 
3
  class TimberMenu extends TimberCore {
4
 
5
+ var $MenuItemClass = 'TimberMenuItem';
6
+
7
  var $items = null;
8
  var $name = null;
9
  var $ID = null;
10
 
11
+ /**
12
+ * @param int $slug
13
+ */
14
  function __construct($slug = 0) {
15
  $locations = get_nav_menu_locations();
16
  if ($slug != 0 && is_numeric($slug)){
17
  $menu_id = $slug;
18
+ } else if (is_array($locations) && count($locations)){
19
  $menu_id = $this->get_menu_id_from_locations($slug, $locations);
20
  } else if ($slug === false){
21
  $menu_id = false;
30
  return null;
31
  }
32
 
33
+ /**
34
+ * @param int $menu_id
35
+ */
36
  private function init($menu_id){
37
  $menu = wp_get_nav_menu_items($menu_id);
38
  $menu = self::order_children($menu);
42
  $this->ID = $this->term_id;
43
  }
44
 
45
+ /**
46
+ * @param string $slug
47
+ * @param array $locations
48
+ * @return mixed
49
+ */
50
  private function get_menu_id_from_locations($slug, $locations){
51
  if ($slug === 0){
52
  $slug = $this->get_menu_id_from_terms($slug);
59
  }
60
  }
61
 
62
+ /**
63
+ * @param int $slug
64
+ * @return int
65
+ */
66
  private function get_menu_id_from_terms($slug = 0){
67
  if (!is_numeric($slug) && is_string($slug)){
68
  //we have a string so lets search for that
84
  return 0;
85
  }
86
 
87
+ /**
88
+ * @param array $menu_items
89
+ * @param int $parent_id
90
+ * @return TimberMenuItem|null
91
+ */
92
  function find_parent_item_in_menu($menu_items, $parent_id) {
93
  foreach ($menu_items as &$item) {
94
  if ($item->ID == $parent_id) {
98
  return null;
99
  }
100
 
101
+ /**
102
+ * @param array $items
103
+ * @return array
104
+ */
105
  function order_children($items) {
106
  $index = array();
107
  $menu = array();
108
  _wp_menu_item_classes_by_context($items);
109
  foreach($items as $item) {
110
+ $index[$item->ID] = new $this->MenuItemClass($item);
111
  }
112
  foreach($index as $item) {
113
  if($item->menu_item_parent && isset($index[$item->menu_item_parent])) {
119
  return $menu;
120
  }
121
 
122
+ /**
123
+ * @return array
124
+ */
125
  function get_items() {
126
  if (is_array($this->items)) {
127
  return $this->items;
135
  var $children;
136
  var $has_child_class = false;
137
 
138
+ /**
139
+ * @param array|object $data
140
+ */
141
  function __construct($data) {
142
  $this->import($data);
143
  $this->import_classes($data);
148
  $this->add_class('menu-item-'.$this->ID);
149
  }
150
 
151
+ /**
152
+ * @param string $class_name
153
+ */
154
  function add_class($class_name){
155
  $this->classes[] = $class_name;
156
  $this->class .= ' '.$class_name;
157
  }
158
 
159
+ /**
160
+ * @return string
161
+ */
162
  function name() {
163
  if (isset($this->title)){
164
  return $this->title;
166
  return $this->_name;
167
  }
168
 
169
+ /**
170
+ * @return string
171
+ */
172
  function slug() {
173
  return $this->post_name;
174
  }
175
 
176
+ /**
177
+ * @return string
178
+ */
179
  function get_link() {
180
  return $this->url;
181
  }
182
 
183
+ /**
184
+ * @return string
185
+ */
186
  function get_path() {
187
+ return TimberURLHelper::get_rel_url($this->url);
188
  }
189
 
190
+ /**
191
+ * @param TimberMenuItem $item
192
+ */
193
  function add_child($item) {
194
  if (!$this->has_child_class){
195
  $this->add_class('menu-item-has-children');
201
  $this->children[] = $item;
202
  }
203
 
204
+ /**
205
+ * @param object $data
206
+ */
207
  function import_classes($data){
208
  $this->class = trim(implode(' ', $data->classes));
209
  }
210
 
211
+ /**
212
+ * @return array|bool
213
+ */
214
  function get_children() {
215
  if (isset($this->children)) {
216
  return $this->children;
218
  return false;
219
  }
220
 
221
+ /**
222
+ * @return bool
223
+ */
224
  function is_external(){
225
  if ($this->type != 'custom'){
226
  return false;
227
  }
228
+ return TimberURLHelper::is_external($this->url);
229
  }
230
 
231
  /* Aliases */
232
 
233
+ /**
234
+ * @return array|bool
235
+ */
236
  public function children(){
237
  return $this->get_children();
238
  }
239
 
240
+ /**
241
+ * @return bool
242
+ */
243
  public function external(){
244
  return $this->is_external();
245
  }
246
 
247
+ /**
248
+ * @return string
249
+ */
250
  public function link(){
251
  return $this->get_link();
252
  }
253
 
254
+ /**
255
+ * @return string
256
+ */
257
  public function path(){
258
  return $this->get_path();
259
  }
260
 
261
+ /**
262
+ * @return string
263
+ */
264
  public function permalink(){
265
  return $this->get_link();
266
  }
267
 
268
+ /**
269
+ * @return string
270
+ */
271
  public function get_permalink(){
272
  return $this->get_link();
273
  }
functions/timber-post.php CHANGED
@@ -15,10 +15,10 @@ class TimberPost extends TimberCore {
15
 
16
 
17
  /**
18
- * If you send the contructor nothing it will try to figure out the current post id based on being inside The_Loop
19
- * @param mixed $pid
20
- * @return \TimberPost TimberPost object -- woo!
21
- */
22
  function __construct($pid = null) {
23
  if ($pid === null && get_the_ID()){
24
  $pid = get_the_ID();
@@ -36,7 +36,10 @@ class TimberPost extends TimberCore {
36
  $this->init($pid);
37
  }
38
 
39
- function init($pid = false) {
 
 
 
40
  if ($pid === false) {
41
  $pid = get_the_ID();
42
  }
@@ -47,9 +50,11 @@ class TimberPost extends TimberCore {
47
  }
48
 
49
  /**
50
- * Get the URL that will edit the current post/object
51
- */
52
- function get_edit_url() {
 
 
53
  if ($this->can_edit()) {
54
  return get_edit_post_link($this->ID);
55
  return '/wp-admin/post.php?post=' . $this->ID . '&action=edit';
@@ -58,12 +63,11 @@ class TimberPost extends TimberCore {
58
  }
59
 
60
  /**
61
- * updates the post_meta of the current object with the given value
62
- *
63
- * @param string $field
64
- * @param mixed $value
65
- * @nodoc
66
- */
67
  function update($field, $value) {
68
  if (isset($this->ID)) {
69
  update_post_meta($this->ID, $field, $value);
@@ -118,10 +122,11 @@ class TimberPost extends TimberCore {
118
 
119
  /**
120
  * get_post_id_by_name($post_name)
121
- * @nodoc
122
- */
123
-
124
- public static function get_post_id_by_name($post_name) {
 
125
  global $wpdb;
126
  $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name);
127
  $result = $wpdb->get_row($query);
@@ -134,11 +139,19 @@ class TimberPost extends TimberCore {
134
 
135
  /**
136
  * ## get a preview of your post, if you have an excerpt it will use that,
137
- * ## otherwise it will pull from the post_content
 
138
  * <p>{{post.get_preview(50)}}</p>
139
  */
140
 
141
- function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) {
 
 
 
 
 
 
 
142
  $text = '';
143
  $trimmed = false;
144
  if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
@@ -149,6 +162,14 @@ class TimberPost extends TimberCore {
149
  $text = $this->post_excerpt;
150
  }
151
  }
 
 
 
 
 
 
 
 
152
  if (!strlen($text)) {
153
  $text = TimberHelper::trim_words($this->get_content(), $len, false);
154
  $trimmed = true;
@@ -185,11 +206,11 @@ class TimberPost extends TimberCore {
185
  return $text;
186
  }
187
 
188
- /**
189
- * gets the post custom and attaches it to the current object
190
- * @param integer $pid a post ID number
191
- * @nodoc
192
- */
193
  function import_custom($pid = false){
194
  if (!$pid){
195
  $pid = $this->ID;
@@ -198,7 +219,11 @@ class TimberPost extends TimberCore {
198
  $this->import($customs);
199
  }
200
 
201
- function get_post_custom($pid) {
 
 
 
 
202
  $customs = apply_filters('timber_post_get_meta_pre', array(), $pid, $this);
203
  $customs = get_post_custom($pid);
204
  if (!is_array($customs) || empty($customs)){
@@ -219,7 +244,10 @@ class TimberPost extends TimberCore {
219
  * <img src="{{post.get_thumbnail.get_src}}" />
220
  */
221
 
222
- function get_thumbnail() {
 
 
 
223
  if (function_exists('get_post_thumbnail_id')) {
224
  $tid = get_post_thumbnail_id($this->ID);
225
  if ($tid) {
@@ -229,7 +257,10 @@ class TimberPost extends TimberCore {
229
  return null;
230
  }
231
 
232
- function get_permalink() {
 
 
 
233
  if (isset($this->permalink)){
234
  return $this->permalink;
235
  }
@@ -237,11 +268,18 @@ class TimberPost extends TimberCore {
237
  return $this->permalink;
238
  }
239
 
240
- function get_link() {
 
 
 
241
  return $this->get_permalink();
242
  }
243
 
244
- function get_next($by_taxonomy = false) {
 
 
 
 
245
  if (!isset($this->_next) || !isset($this->_next[$by_taxonomy])){
246
  global $post;
247
  $this->_next = array();
@@ -263,7 +301,10 @@ class TimberPost extends TimberCore {
263
  return $this->_next[$by_taxonomy];
264
  }
265
 
266
- public function get_pagination(){
 
 
 
267
  global $post, $page, $numpages, $multipage, $more, $pagenow;
268
  $old_global_post = $post;
269
  $post = $this;
@@ -291,7 +332,11 @@ class TimberPost extends TimberCore {
291
  return $ret;
292
  }
293
 
294
- private static function get_wp_link_page($i){
 
 
 
 
295
  $link = _wp_link_page($i);
296
  $link = new SimpleXMLElement($link.'</a>');
297
  if (isset($link['href'])){
@@ -300,11 +345,18 @@ class TimberPost extends TimberCore {
300
  return '';
301
  }
302
 
303
- function get_path() {
304
- return TimberHelper::get_rel_url($this->get_link());
 
 
 
305
  }
306
 
307
- function get_prev($by_taxonomy = false) {
 
 
 
 
308
  if (!isset($this->_prev) || !isset($this->_prev[$by_taxonomy])){
309
  global $post;
310
  $this->_prev = array();
@@ -326,7 +378,10 @@ class TimberPost extends TimberCore {
326
  return $this->_prev[$by_taxonomy];
327
  }
328
 
329
- function get_parent() {
 
 
 
330
  if (!$this->post_parent) {
331
  return false;
332
  }
@@ -338,14 +393,21 @@ class TimberPost extends TimberCore {
338
  * <p class="byline">{{post.get_author.name}}</p>
339
  */
340
 
341
- function get_author() {
 
 
 
342
  if (isset($this->post_author)) {
343
  return new TimberUser($this->post_author);
344
  }
345
  return false;
346
  }
347
 
348
- function get_info($pid) {
 
 
 
 
349
  $post = $this->prepare_post_info($pid);
350
  if (!isset($post->post_status)) {
351
  return null;
@@ -357,11 +419,20 @@ class TimberPost extends TimberCore {
357
  return $post;
358
  }
359
 
360
- function get_display_date($use = 'post_date') {
 
 
 
 
361
  return date(get_option('date_format'), strtotime($this->$use));
362
  }
363
 
364
- function get_children($post_type = 'any', $childPostClass = false) {
 
 
 
 
 
365
  if ($childPostClass == false) {
366
  $childPostClass = $this->PostClass;
367
  }
@@ -382,7 +453,15 @@ class TimberPost extends TimberCore {
382
  * {% endfor %}
383
  */
384
 
385
- function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') {
 
 
 
 
 
 
 
 
386
  $args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order);
387
  if ($ct > 0) {
388
  $args['number'] = $ct;
@@ -405,12 +484,17 @@ class TimberPost extends TimberCore {
405
  * </ul>
406
  */
407
 
408
-
409
- function get_categories() {
 
 
410
  return $this->get_terms('category');
411
  }
412
 
413
- function get_category() {
 
 
 
414
  $cats = $this->get_categories();
415
  if (count($cats) && isset($cats[0])) {
416
  return $cats[0];
@@ -422,7 +506,13 @@ class TimberPost extends TimberCore {
422
  *
423
  */
424
 
425
- function get_terms($tax = '', $merge = true, $TermClass = 'TimberTerm') {
 
 
 
 
 
 
426
  if (is_string($tax)){
427
  if (isset($this->_get_terms) && isset($this->_get_terms[$tax])){
428
  return $this->_get_terms[$tax];
@@ -467,7 +557,12 @@ class TimberPost extends TimberCore {
467
  return $ret;
468
  }
469
 
470
- function has_term($term_name_or_id, $taxonomy = 'all'){
 
 
 
 
 
471
  if ($taxonomy == 'all' || $taxonomy == 'any'){
472
  $taxes = get_object_taxonomies($this->post_type, 'names');
473
  $ret = false;
@@ -482,7 +577,11 @@ class TimberPost extends TimberCore {
482
  return has_term($term_name_or_id, $taxonomy, $this->ID);
483
  }
484
 
485
- function get_image($field) {
 
 
 
 
486
  return new $this->ImageClass($this->$field);
487
  }
488
 
@@ -495,7 +594,10 @@ class TimberPost extends TimberCore {
495
  * </ul>
496
  */
497
 
498
- function get_tags() {
 
 
 
499
  return $this->get_terms('tags');
500
  }
501
 
@@ -504,7 +606,10 @@ class TimberPost extends TimberCore {
504
  * <h1>{{post.get_title}}</h1>
505
  */
506
 
507
- function get_title() {
 
 
 
508
  $title = $this->post_title;
509
  return apply_filters('the_title', $title);
510
  }
@@ -514,7 +619,12 @@ class TimberPost extends TimberCore {
514
  * <div class="article-text">{{post.get_content}}</div>
515
  */
516
 
517
- function get_content($len = 0, $page = 0) {
 
 
 
 
 
518
  if ($len == 0 && $page == 0 && $this->_content){
519
  return $this->_content;
520
  }
@@ -536,11 +646,17 @@ class TimberPost extends TimberCore {
536
  return $content;
537
  }
538
 
539
- public function get_post_type() {
 
 
 
540
  return get_post_type_object($this->post_type);
541
  }
542
 
543
- public function get_comment_count() {
 
 
 
544
  if (isset($this->ID)) {
545
  return get_comments_number($this->ID);
546
  } else {
@@ -548,7 +664,11 @@ class TimberPost extends TimberCore {
548
  }
549
  }
550
 
551
- public function get_field($field_name) {
 
 
 
 
552
  $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this);
553
  if ($value === null){
554
  $value = get_post_meta($this->ID, $field_name);
@@ -560,17 +680,26 @@ class TimberPost extends TimberCore {
560
  return $value;
561
  }
562
 
563
- function import_field($field_name) {
 
 
 
564
  $this->$field_name = $this->get_field($field_name);
565
  }
566
 
567
- function get_format(){
 
 
 
568
  return get_post_format($this->ID);
569
  }
570
 
571
  // Docs
572
 
573
- public function get_method_values(){
 
 
 
574
  $ret = parent::get_method_values();
575
  $ret['author'] = $this->author();
576
  $ret['categories'] = $this->categories();
@@ -595,91 +724,162 @@ class TimberPost extends TimberCore {
595
  }
596
 
597
  // Aliases
598
- public function author() {
 
 
 
599
  return $this->get_author();
600
  }
601
 
602
- public function categories() {
 
 
 
603
  return $this->get_terms('category');
604
  }
605
 
606
- public function category() {
 
 
 
607
  return $this->get_category();
608
  }
609
 
610
- public function children() {
 
 
 
611
  return $this->get_children();
612
  }
613
 
614
- public function comments(){
 
 
 
615
  return $this->get_comments();
616
  }
617
 
618
- public function content($page = 0) {
 
 
 
 
619
  return $this->get_content(0, $page);
620
  }
621
 
622
- public function display_date(){
 
 
 
623
  return date_i18n(get_option('date_format') , strtotime($this->post_date));
624
  }
625
 
626
- public function edit_link(){
 
 
 
627
  return $this->get_edit_url();
628
  }
629
 
630
- public function format(){
 
 
 
631
  return $this->get_format();
632
  }
633
 
634
- public function link() {
 
 
635
  return $this->get_permalink();
636
  }
637
 
638
- public function meta($field_name){
 
 
 
 
639
  return $this->get_field($field_name);
640
  }
641
 
642
- public function next($in_same_cat = false) {
 
 
 
 
643
  return $this->get_next($in_same_cat);
644
  }
645
 
646
- public function pagination(){
 
 
 
647
  return $this->get_pagination();
648
  }
649
 
650
- public function parent(){
 
 
 
651
  return $this->get_parent();
652
  }
653
 
654
- public function path() {
 
 
 
655
  return $this->get_path();
656
  }
657
 
658
- public function permalink() {
 
 
 
659
  return $this->get_permalink();
660
  }
661
 
662
- public function prev($in_same_cat = false) {
 
 
 
 
663
  return $this->get_prev($in_same_cat);
664
  }
665
 
666
- public function terms($tax = '') {
 
 
 
 
667
  return $this->get_terms($tax);
668
  }
669
 
670
- public function tags() {
 
 
 
671
  return $this->get_tags();
672
  }
673
 
674
- public function thumbnail() {
 
 
 
675
  return $this->get_thumbnail();
676
  }
677
 
678
- public function title() {
 
 
 
679
  return $this->get_title();
680
  }
681
 
682
- public function post_class($class='') {
 
 
 
 
683
  $pid = $this->ID;
684
  $class_array = get_post_class($class, $pid);
685
  return implode(' ', $class_array);
15
 
16
 
17
  /**
18
+ * If you send the contructor nothing it will try to figure out the current post id based on being inside The_Loop
19
+ * @param mixed $pid
20
+ * @return \TimberPost TimberPost object -- woo!
21
+ */
22
  function __construct($pid = null) {
23
  if ($pid === null && get_the_ID()){
24
  $pid = get_the_ID();
36
  $this->init($pid);
37
  }
38
 
39
+ /**
40
+ * @param int|bool $pid
41
+ */
42
+ function init($pid = false) {
43
  if ($pid === false) {
44
  $pid = get_the_ID();
45
  }
50
  }
51
 
52
  /**
53
+ * Get the URL that will edit the current post/object
54
+ *
55
+ * @return bool|string
56
+ */
57
+ function get_edit_url() {
58
  if ($this->can_edit()) {
59
  return get_edit_post_link($this->ID);
60
  return '/wp-admin/post.php?post=' . $this->ID . '&action=edit';
63
  }
64
 
65
  /**
66
+ * updates the post_meta of the current object with the given value
67
+ *
68
+ * @param string $field
69
+ * @param mixed $value
70
+ */
 
71
  function update($field, $value) {
72
  if (isset($this->ID)) {
73
  update_post_meta($this->ID, $field, $value);
122
 
123
  /**
124
  * get_post_id_by_name($post_name)
125
+ *
126
+ * @param $post_name
127
+ * @return int
128
+ */
129
+ public static function get_post_id_by_name($post_name) {
130
  global $wpdb;
131
  $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name);
132
  $result = $wpdb->get_row($query);
139
 
140
  /**
141
  * ## get a preview of your post, if you have an excerpt it will use that,
142
+ * ## otherwise it will pull from the post_content.
143
+ * ## If there's a <!-- more --> tag it will use that to mark where to pull through.
144
  * <p>{{post.get_preview(50)}}</p>
145
  */
146
 
147
+ /**
148
+ * @param int $len
149
+ * @param bool $force
150
+ * @param string $readmore
151
+ * @param bool $strip
152
+ * @return string
153
+ */
154
+ function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) {
155
  $text = '';
156
  $trimmed = false;
157
  if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
162
  $text = $this->post_excerpt;
163
  }
164
  }
165
+ if (!strlen($text) && strpos($this->post_content, '<!--more-->') !== false) {
166
+ $pieces = explode('<!--more-->',$this->post_content);
167
+ $text = $pieces[0];
168
+ if ($force) {
169
+ $text = TimberHelper::trim_words($text, $len, false);
170
+ $trimmed = true;
171
+ }
172
+ }
173
  if (!strlen($text)) {
174
  $text = TimberHelper::trim_words($this->get_content(), $len, false);
175
  $trimmed = true;
206
  return $text;
207
  }
208
 
209
+ /**
210
+ * gets the post custom and attaches it to the current object
211
+ * @param bool|int $pid a post ID number
212
+ * @nodoc
213
+ */
214
  function import_custom($pid = false){
215
  if (!$pid){
216
  $pid = $this->ID;
219
  $this->import($customs);
220
  }
221
 
222
+ /**
223
+ * @param int $pid
224
+ * @return array
225
+ */
226
+ function get_post_custom($pid) {
227
  $customs = apply_filters('timber_post_get_meta_pre', array(), $pid, $this);
228
  $customs = get_post_custom($pid);
229
  if (!is_array($customs) || empty($customs)){
244
  * <img src="{{post.get_thumbnail.get_src}}" />
245
  */
246
 
247
+ /**
248
+ * @return null|TimberImage
249
+ */
250
+ function get_thumbnail() {
251
  if (function_exists('get_post_thumbnail_id')) {
252
  $tid = get_post_thumbnail_id($this->ID);
253
  if ($tid) {
257
  return null;
258
  }
259
 
260
+ /**
261
+ * @return string
262
+ */
263
+ function get_permalink() {
264
  if (isset($this->permalink)){
265
  return $this->permalink;
266
  }
268
  return $this->permalink;
269
  }
270
 
271
+ /**
272
+ * @return string
273
+ */
274
+ function get_link() {
275
  return $this->get_permalink();
276
  }
277
 
278
+ /**
279
+ * @param bool $by_taxonomy
280
+ * @return mixed
281
+ */
282
+ function get_next($by_taxonomy = false) {
283
  if (!isset($this->_next) || !isset($this->_next[$by_taxonomy])){
284
  global $post;
285
  $this->_next = array();
301
  return $this->_next[$by_taxonomy];
302
  }
303
 
304
+ /**
305
+ * @return array
306
+ */
307
+ public function get_pagination(){
308
  global $post, $page, $numpages, $multipage, $more, $pagenow;
309
  $old_global_post = $post;
310
  $post = $this;
332
  return $ret;
333
  }
334
 
335
+ /**
336
+ * @param int $i
337
+ * @return string
338
+ */
339
+ private static function get_wp_link_page($i){
340
  $link = _wp_link_page($i);
341
  $link = new SimpleXMLElement($link.'</a>');
342
  if (isset($link['href'])){
345
  return '';
346
  }
347
 
348
+ /**
349
+ * @return string
350
+ */
351
+ function get_path() {
352
+ return TimberURLHelper::get_rel_url($this->get_link());
353
  }
354
 
355
+ /**
356
+ * @param bool $by_taxonomy
357
+ * @return mixed
358
+ */
359
+ function get_prev($by_taxonomy = false) {
360
  if (!isset($this->_prev) || !isset($this->_prev[$by_taxonomy])){
361
  global $post;
362
  $this->_prev = array();
378
  return $this->_prev[$by_taxonomy];
379
  }
380
 
381
+ /**
382
+ * @return bool|TimberPost
383
+ */
384
+ function get_parent() {
385
  if (!$this->post_parent) {
386
  return false;
387
  }
393
  * <p class="byline">{{post.get_author.name}}</p>
394
  */
395
 
396
+ /**
397
+ * @return bool|TimberUser
398
+ */
399
+ function get_author() {
400
  if (isset($this->post_author)) {
401
  return new TimberUser($this->post_author);
402
  }
403
  return false;
404
  }
405
 
406
+ /**
407
+ * @param int $pid
408
+ * @return null|object|WP_Post
409
+ */
410
+ function get_info($pid) {
411
  $post = $this->prepare_post_info($pid);
412
  if (!isset($post->post_status)) {
413
  return null;
419
  return $post;
420
  }
421
 
422
+ /**
423
+ * @param string $use
424
+ * @return bool|string
425
+ */
426
+ function get_display_date($use = 'post_date') {
427
  return date(get_option('date_format'), strtotime($this->$use));
428
  }
429
 
430
+ /**
431
+ * @param string $post_type
432
+ * @param bool $childPostClass
433
+ * @return array
434
+ */
435
+ function get_children($post_type = 'any', $childPostClass = false) {
436
  if ($childPostClass == false) {
437
  $childPostClass = $this->PostClass;
438
  }
453
  * {% endfor %}
454
  */
455
 
456
+ /**
457
+ * @param int $ct
458
+ * @param string $order
459
+ * @param string $type
460
+ * @param string $status
461
+ * @param string $CommentClass
462
+ * @return mixed
463
+ */
464
+ function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') {
465
  $args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order);
466
  if ($ct > 0) {
467
  $args['number'] = $ct;
484
  * </ul>
485
  */
486
 
487
+ /**
488
+ * @return array
489
+ */
490
+ function get_categories() {
491
  return $this->get_terms('category');
492
  }
493
 
494
+ /**
495
+ * @return mixed
496
+ */
497
+ function get_category() {
498
  $cats = $this->get_categories();
499
  if (count($cats) && isset($cats[0])) {
500
  return $cats[0];
506
  *
507
  */
508
 
509
+ /**
510
+ * @param string $tax
511
+ * @param bool $merge
512
+ * @param string $TermClass
513
+ * @return array
514
+ */
515
+ function get_terms($tax = '', $merge = true, $TermClass = 'TimberTerm') {
516
  if (is_string($tax)){
517
  if (isset($this->_get_terms) && isset($this->_get_terms[$tax])){
518
  return $this->_get_terms[$tax];
557
  return $ret;
558
  }
559
 
560
+ /**
561
+ * @param string|int $term_name_or_id
562
+ * @param string $taxonomy
563
+ * @return bool
564
+ */
565
+ function has_term($term_name_or_id, $taxonomy = 'all'){
566
  if ($taxonomy == 'all' || $taxonomy == 'any'){
567
  $taxes = get_object_taxonomies($this->post_type, 'names');
568
  $ret = false;
577
  return has_term($term_name_or_id, $taxonomy, $this->ID);
578
  }
579
 
580
+ /**
581
+ * @param string $field
582
+ * @return TimberImage
583
+ */
584
+ function get_image($field) {
585
  return new $this->ImageClass($this->$field);
586
  }
587
 
594
  * </ul>
595
  */
596
 
597
+ /**
598
+ * @return array
599
+ */
600
+ function get_tags() {
601
  return $this->get_terms('tags');
602
  }
603
 
606
  * <h1>{{post.get_title}}</h1>
607
  */
608
 
609
+ /**
610
+ * @return string
611
+ */
612
+ function get_title() {
613
  $title = $this->post_title;
614
  return apply_filters('the_title', $title);
615
  }
619
  * <div class="article-text">{{post.get_content}}</div>
620
  */
621
 
622
+ /**
623
+ * @param int $len
624
+ * @param int $page
625
+ * @return string
626
+ */
627
+ function get_content($len = 0, $page = 0) {
628
  if ($len == 0 && $page == 0 && $this->_content){
629
  return $this->_content;
630
  }
646
  return $content;
647
  }
648
 
649
+ /**
650
+ * @return mixed
651
+ */
652
+ public function get_post_type() {
653
  return get_post_type_object($this->post_type);
654
  }
655
 
656
+ /**
657
+ * @return int
658
+ */
659
+ public function get_comment_count() {
660
  if (isset($this->ID)) {
661
  return get_comments_number($this->ID);
662
  } else {
664
  }
665
  }
666
 
667
+ /**
668
+ * @param string $field_name
669
+ * @return mixed
670
+ */
671
+ public function get_field($field_name) {
672
  $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this);
673
  if ($value === null){
674
  $value = get_post_meta($this->ID, $field_name);
680
  return $value;
681
  }
682
 
683
+ /**
684
+ * @param string $field_name
685
+ */
686
+ function import_field($field_name) {
687
  $this->$field_name = $this->get_field($field_name);
688
  }
689
 
690
+ /**
691
+ * @return mixed
692
+ */
693
+ function get_format(){
694
  return get_post_format($this->ID);
695
  }
696
 
697
  // Docs
698
 
699
+ /**
700
+ * @return array
701
+ */
702
+ public function get_method_values(){
703
  $ret = parent::get_method_values();
704
  $ret['author'] = $this->author();
705
  $ret['categories'] = $this->categories();
724
  }
725
 
726
  // Aliases
727
+ /**
728
+ * @return bool|TimberUser
729
+ */
730
+ public function author() {
731
  return $this->get_author();
732
  }
733
 
734
+ /**
735
+ * @return array
736
+ */
737
+ public function categories() {
738
  return $this->get_terms('category');
739
  }
740
 
741
+ /**
742
+ * @return mixed
743
+ */
744
+ public function category() {
745
  return $this->get_category();
746
  }
747
 
748
+ /**
749
+ * @return array
750
+ */
751
+ public function children() {
752
  return $this->get_children();
753
  }
754
 
755
+ /**
756
+ * @return mixed
757
+ */
758
+ public function comments(){
759
  return $this->get_comments();
760
  }
761
 
762
+ /**
763
+ * @param int $page
764
+ * @return string
765
+ */
766
+ public function content($page = 0) {
767
  return $this->get_content(0, $page);
768
  }
769
 
770
+ /**
771
+ * @return mixed
772
+ */
773
+ public function display_date(){
774
  return date_i18n(get_option('date_format') , strtotime($this->post_date));
775
  }
776
 
777
+ /**
778
+ * @return bool|string
779
+ */
780
+ public function edit_link(){
781
  return $this->get_edit_url();
782
  }
783
 
784
+ /**
785
+ * @return mixed
786
+ */
787
+ public function format(){
788
  return $this->get_format();
789
  }
790
 
791
+ /**
792
+ * @return string */
793
+ public function link() {
794
  return $this->get_permalink();
795
  }
796
 
797
+ /**
798
+ * @param string $field_name
799
+ * @return mixed
800
+ */
801
+ public function meta($field_name){
802
  return $this->get_field($field_name);
803
  }
804
 
805
+ /**
806
+ * @param bool $in_same_cat
807
+ * @return mixed
808
+ */
809
+ public function next($in_same_cat = false) {
810
  return $this->get_next($in_same_cat);
811
  }
812
 
813
+ /**
814
+ * @return array
815
+ */
816
+ public function pagination(){
817
  return $this->get_pagination();
818
  }
819
 
820
+ /**
821
+ * @return bool|TimberPost
822
+ */
823
+ public function parent(){
824
  return $this->get_parent();
825
  }
826
 
827
+ /**
828
+ * @return string
829
+ */
830
+ public function path() {
831
  return $this->get_path();
832
  }
833
 
834
+ /**
835
+ * @return string
836
+ */
837
+ public function permalink() {
838
  return $this->get_permalink();
839
  }
840
 
841
+ /**
842
+ * @param bool $in_same_cat
843
+ * @return mixed
844
+ */
845
+ public function prev($in_same_cat = false) {
846
  return $this->get_prev($in_same_cat);
847
  }
848
 
849
+ /**
850
+ * @param string $tax
851
+ * @return array
852
+ */
853
+ public function terms($tax = '') {
854
  return $this->get_terms($tax);
855
  }
856
 
857
+ /**
858
+ * @return array
859
+ */
860
+ public function tags() {
861
  return $this->get_tags();
862
  }
863
 
864
+ /**
865
+ * @return null|TimberImage
866
+ */
867
+ public function thumbnail() {
868
  return $this->get_thumbnail();
869
  }
870
 
871
+ /**
872
+ * @return string
873
+ */
874
+ public function title() {
875
  return $this->get_title();
876
  }
877
 
878
+ /**
879
+ * @param string $class
880
+ * @return string
881
+ */
882
+ public function post_class($class='') {
883
  $pid = $this->ID;
884
  $class_array = get_post_class($class, $pid);
885
  return implode(' ', $class_array);
functions/timber-site.php CHANGED
@@ -1,64 +1,87 @@
1
  <?php
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);
22
- $this->ID = $info->blog_id;
23
- $this->name = $this->blogname;
24
- $this->title = $this->blogname;
25
- $this->url = $this->siteurl;
26
- $theme_slug = get_blog_option($info->blog_id, 'stylesheet');
27
- $this->theme = new TimberTheme($theme_slug);
28
- $this->description = get_blog_option($info->blog_id, 'blogdescription');
29
- }
30
 
31
- function init(){
32
- $this->name = get_bloginfo('name');
33
- $this->title = $this->name;
34
- $this->description = get_bloginfo('description');
35
- $this->url = get_bloginfo('url');
36
- $this->language = get_bloginfo('language');
37
- $this->charset = get_bloginfo('charset');
38
- $this->pingback_url = get_bloginfo('pingback_url');
39
- $this->language_attributes = TimberHelper::function_wrapper('language_attributes');
40
- }
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- function __get($field){
43
- if (!isset($this->$field)){
44
- $this->$field = get_blog_option($this->ID, $field);
45
- }
46
- return $this->$field;
47
- }
 
 
 
 
48
 
49
- function get_link(){
50
- return $this->siteurl;
51
- }
 
 
 
 
 
 
 
52
 
53
- function get_url(){
54
- return $this->get_link();
55
- }
 
 
 
56
 
57
- function link(){
58
- return $this->get_link();
59
- }
 
 
 
60
 
61
- function url(){
62
- return $this->get_link();
63
- }
64
- }
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ class TimberSite extends TimberCore {
 
 
 
 
 
 
 
4
 
5
+ /**
6
+ * @param string|int $site_name_or_id
7
+ */
8
+ function __construct($site_name_or_id = null){
9
+ if (is_multisite()){
10
+ $this->init_with_multisite($site_name_or_id);
11
+ } else {
12
+ $this->init();
13
+ }
14
+ }
 
 
 
 
 
 
 
 
15
 
16
+ /**
17
+ * @param string|int $site_name_or_id
18
+ */
19
+ function init_with_multisite($site_name_or_id){
20
+ if ($site_name_or_id === null){
21
+ //this is necessary for some reason, otherwise returns 1 all the time
22
+ if (is_multisite()){
23
+ restore_current_blog();
24
+ $site_name_or_id = get_current_blog_id();
25
+ }
26
+ }
27
+ $info = get_blog_details($site_name_or_id);
28
+ $this->import($info);
29
+ $this->ID = $info->blog_id;
30
+ $this->name = $this->blogname;
31
+ $this->title = $this->blogname;
32
+ $this->url = $this->siteurl;
33
+ $theme_slug = get_blog_option($info->blog_id, 'stylesheet');
34
+ $this->theme = new TimberTheme($theme_slug);
35
+ $this->description = get_blog_option($info->blog_id, 'blogdescription');
36
+ }
37
 
38
+ function init(){
39
+ $this->name = get_bloginfo('name');
40
+ $this->title = $this->name;
41
+ $this->description = get_bloginfo('description');
42
+ $this->url = get_bloginfo('url');
43
+ $this->language = get_bloginfo('language');
44
+ $this->charset = get_bloginfo('charset');
45
+ $this->pingback_url = get_bloginfo('pingback_url');
46
+ $this->language_attributes = TimberHelper::function_wrapper('language_attributes');
47
+ }
48
 
49
+ /**
50
+ * @param string $field
51
+ * @return mixed
52
+ */
53
+ function __get($field){
54
+ if (!isset($this->$field)){
55
+ $this->$field = get_blog_option($this->ID, $field);
56
+ }
57
+ return $this->$field;
58
+ }
59
 
60
+ /**
61
+ * @return string
62
+ */
63
+ function get_link(){
64
+ return $this->siteurl;
65
+ }
66
 
67
+ /**
68
+ * @return string
69
+ */
70
+ function get_url(){
71
+ return $this->get_link();
72
+ }
73
 
74
+ /**
75
+ * @return string
76
+ */
77
+ function link(){
78
+ return $this->get_link();
79
+ }
80
+
81
+ /**
82
+ * @return string
83
+ */
84
+ function url(){
85
+ return $this->get_link();
86
+ }
87
+ }
functions/timber-term-getter.php CHANGED
@@ -2,13 +2,21 @@
2
 
3
  class TimberTermGetter {
4
 
5
- public static function get_term_query_from_query_string($query_string){
 
 
 
 
6
  $args = array();
7
  parse_str($query_string, $args);
8
  $ret = self::get_term_query_from_assoc_array($args);
9
  return $ret;
10
  }
11
 
 
 
 
 
12
  public static function get_term_query_from_string($taxs){
13
  $ret = new stdClass();
14
  $ret->args = array();
@@ -19,6 +27,10 @@ class TimberTermGetter {
19
  return $ret;
20
  }
21
 
 
 
 
 
22
  public static function get_term_query_from_assoc_array($args){
23
  $ret = new stdClass();
24
  $ret->args = $args;
@@ -40,6 +52,10 @@ class TimberTermGetter {
40
  return $ret;
41
  }
42
 
 
 
 
 
43
  public static function get_term_query_from_array($args){
44
  if (is_array($args) && !empty($args)){
45
  //okay its an array with content
@@ -51,6 +67,10 @@ class TimberTermGetter {
51
  }
52
  }
53
 
 
 
 
 
54
  public static function get_term_query_from_array_of_ids($args){
55
  $ret = new stdClass();
56
  $ret->taxonomies = get_taxonomies();
@@ -58,6 +78,10 @@ class TimberTermGetter {
58
  return $ret;
59
  }
60
 
 
 
 
 
61
  public static function get_term_query_from_array_of_strings($args){
62
  $ret = new stdClass();
63
  $ret->taxonomies = self::correct_taxonomy_names($args);
@@ -65,6 +89,10 @@ class TimberTermGetter {
65
  return $ret;
66
  }
67
 
 
 
 
 
68
  private static function correct_taxonomy_names($taxs){
69
  if (is_string($taxs)){
70
  $taxs = array($taxs);
2
 
3
  class TimberTermGetter {
4
 
5
+ /**
6
+ * @param string $query_string
7
+ * @return stdClass
8
+ */
9
+ public static function get_term_query_from_query_string($query_string){
10
  $args = array();
11
  parse_str($query_string, $args);
12
  $ret = self::get_term_query_from_assoc_array($args);
13
  return $ret;
14
  }
15
 
16
+ /**
17
+ * @param string $taxs
18
+ * @return stdClass
19
+ */
20
  public static function get_term_query_from_string($taxs){
21
  $ret = new stdClass();
22
  $ret->args = array();
27
  return $ret;
28
  }
29
 
30
+ /**
31
+ * @param array $args
32
+ * @return stdClass
33
+ */
34
  public static function get_term_query_from_assoc_array($args){
35
  $ret = new stdClass();
36
  $ret->args = $args;
52
  return $ret;
53
  }
54
 
55
+ /**
56
+ * @param array $args
57
+ * @return stdClass
58
+ */
59
  public static function get_term_query_from_array($args){
60
  if (is_array($args) && !empty($args)){
61
  //okay its an array with content
67
  }
68
  }
69
 
70
+ /**
71
+ * @param array $args
72
+ * @return stdClass
73
+ */
74
  public static function get_term_query_from_array_of_ids($args){
75
  $ret = new stdClass();
76
  $ret->taxonomies = get_taxonomies();
78
  return $ret;
79
  }
80
 
81
+ /**
82
+ * @param array $args
83
+ * @return stdClass
84
+ */
85
  public static function get_term_query_from_array_of_strings($args){
86
  $ret = new stdClass();
87
  $ret->taxonomies = self::correct_taxonomy_names($args);
89
  return $ret;
90
  }
91
 
92
+ /**
93
+ * @param string|array $taxs
94
+ * @return array
95
+ */
96
  private static function correct_taxonomy_names($taxs){
97
  if (is_string($taxs)){
98
  $taxs = array($taxs);
functions/timber-term.php CHANGED
@@ -10,7 +10,11 @@ class TimberTerm extends TimberCore {
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
  }
@@ -19,20 +23,29 @@ class TimberTerm extends TimberCore {
19
  $this->init($tid);
20
  }
21
 
22
- function __toString(){
 
 
 
23
  return $this->name;
24
  }
25
 
26
  /* Setup
27
  ===================== */
28
 
29
- private function get_term_from_query() {
 
 
 
30
  global $wp_query;
31
  $qo = $wp_query->queried_object;
32
  return $qo->term_id;
33
  }
34
 
35
- private function init($tid) {
 
 
 
36
  global $wpdb;
37
  $term = $this->get_term($tid);
38
  if (isset($term->id)) {
@@ -52,18 +65,26 @@ class TimberTerm extends TimberCore {
52
  }
53
  }
54
 
55
- private function get_term_meta($tid){
 
 
 
 
56
  $customs = array();
57
  $customs = apply_filters('timber_term_get_meta', $customs, $tid, $this);
58
  return $customs;
59
  }
60
 
61
- private function get_term($tid) {
 
 
 
 
62
  if (is_object($tid) || is_array($tid)) {
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 {
@@ -78,7 +99,11 @@ class TimberTerm extends TimberCore {
78
  return null;
79
  }
80
 
81
- private function get_tid($tid) {
 
 
 
 
82
  global $wpdb;
83
  if (is_numeric($tid)) {
84
  return $tid;
@@ -102,11 +127,18 @@ class TimberTerm extends TimberCore {
102
  /* Public methods
103
  ===================== */
104
 
105
- public function get_edit_url(){
 
 
 
106
  return get_edit_term_link($this->ID, $this->taxonomy);
107
  }
108
 
109
- public function get_meta_field($field_name){
 
 
 
 
110
  if (!isset($this->$field_name)){
111
  $field = '';
112
  $field = apply_filters('timber_term_get_meta_field', $field, $this->ID, $field_name, $this);
@@ -115,18 +147,30 @@ class TimberTerm extends TimberCore {
115
  return $this->$field_name;
116
  }
117
 
118
- public function get_path() {
 
 
 
119
  $link = $this->get_link();
120
- $rel = TimberHelper::get_rel_url($link, true);
121
  return apply_filters('timber_term_path', $rel, $this);
122
  }
123
 
124
- public function get_link() {
 
 
 
125
  $link = get_term_link($this);
126
  return apply_filters('timber_term_link', $link, $this);
127
  }
128
 
129
- public function get_posts($numberposts = 10, $post_type = 'any', $PostClass = '') {
 
 
 
 
 
 
130
  if (!strlen($PostClass)) {
131
  $PostClass = $this->PostClass;
132
  }
@@ -169,7 +213,10 @@ class TimberTerm extends TimberCore {
169
  return Timber::get_posts($args, $PostClass);
170
  }
171
 
172
- public function get_children(){
 
 
 
173
  if (!isset($this->_children)){
174
  $children = get_term_children($this->ID, $this->taxonomy);
175
  foreach($children as &$child){
@@ -183,46 +230,82 @@ class TimberTerm extends TimberCore {
183
  /* Alias
184
  ====================== */
185
 
186
- public function children(){
 
 
 
187
  return $this->get_children();
188
  }
189
 
190
- public function edit_link(){
 
 
 
191
  return $this->get_edit_url();
192
  }
193
 
194
- public function get_url() {
 
 
 
195
  return $this->get_link();
196
  }
197
 
198
- public function link(){
 
 
 
199
  return $this->get_link();
200
  }
201
 
202
- public function meta($field_name){
 
 
 
 
203
  return $this->get_meta_field($field_name);
204
  }
205
 
206
- public function path(){
 
 
 
207
  return $this->get_path();
208
  }
209
 
210
- public function posts($numberposts_or_args = 10, $post_type_or_class = 'any', $post_class = ''){
 
 
 
 
 
 
211
  return $this->get_posts($numberposts_or_args, $post_type_or_class, $post_class);
212
  }
213
 
214
- public function title(){
 
 
 
215
  return $this->name;
216
  }
217
 
218
- public function url(){
 
 
 
219
  return $this->get_url();
220
  }
221
 
222
  /* Deprecated
223
  ===================== */
224
 
225
- function get_page($i) {
 
 
 
 
 
226
  return $this->get_path() . '/page/' . $i;
227
  }
228
 
10
 
11
  public static $representation = 'term';
12
 
13
+ /**
14
+ * @param int $tid
15
+ * @param string $tax
16
+ */
17
+ function __construct($tid = null, $tax='') {
18
  if ($tid === null) {
19
  $tid = $this->get_term_from_query();
20
  }
23
  $this->init($tid);
24
  }
25
 
26
+ /**
27
+ * @return string
28
+ */
29
+ function __toString(){
30
  return $this->name;
31
  }
32
 
33
  /* Setup
34
  ===================== */
35
 
36
+ /**
37
+ * @return mixed
38
+ */
39
+ private function get_term_from_query() {
40
  global $wp_query;
41
  $qo = $wp_query->queried_object;
42
  return $qo->term_id;
43
  }
44
 
45
+ /**
46
+ * @param int $tid
47
+ */
48
+ private function init($tid) {
49
  global $wpdb;
50
  $term = $this->get_term($tid);
51
  if (isset($term->id)) {
65
  }
66
  }
67
 
68
+ /**
69
+ * @param int $tid
70
+ * @return array
71
+ */
72
+ private function get_term_meta($tid){
73
  $customs = array();
74
  $customs = apply_filters('timber_term_get_meta', $customs, $tid, $this);
75
  return $customs;
76
  }
77
 
78
+ /**
79
+ * @param int $tid
80
+ * @return int|null
81
+ */
82
+ private function get_term($tid) {
83
  if (is_object($tid) || is_array($tid)) {
84
  return $tid;
85
  }
86
  $tid = self::get_tid($tid);
87
+
88
  if(isset($this->taxonomy) && strlen($this->taxonomy)) {
89
  return get_term($tid, $this->taxonomy);
90
  } else {
99
  return null;
100
  }
101
 
102
+ /**
103
+ * @param int $tid
104
+ * @return int
105
+ */
106
+ private function get_tid($tid) {
107
  global $wpdb;
108
  if (is_numeric($tid)) {
109
  return $tid;
127
  /* Public methods
128
  ===================== */
129
 
130
+ /**
131
+ * @return string
132
+ */
133
+ public function get_edit_url(){
134
  return get_edit_term_link($this->ID, $this->taxonomy);
135
  }
136
 
137
+ /**
138
+ * @param string $field_name
139
+ * @return string
140
+ */
141
+ public function get_meta_field($field_name){
142
  if (!isset($this->$field_name)){
143
  $field = '';
144
  $field = apply_filters('timber_term_get_meta_field', $field, $this->ID, $field_name, $this);
147
  return $this->$field_name;
148
  }
149
 
150
+ /**
151
+ * @return string
152
+ */
153
+ public function get_path() {
154
  $link = $this->get_link();
155
+ $rel = TimberURLHelper::get_rel_url($link, true);
156
  return apply_filters('timber_term_path', $rel, $this);
157
  }
158
 
159
+ /**
160
+ * @return string
161
+ */
162
+ public function get_link() {
163
  $link = get_term_link($this);
164
  return apply_filters('timber_term_link', $link, $this);
165
  }
166
 
167
+ /**
168
+ * @param int $numberposts
169
+ * @param string $post_type
170
+ * @param string $PostClass
171
+ * @return array|bool|null
172
+ */
173
+ public function get_posts($numberposts = 10, $post_type = 'any', $PostClass = '') {
174
  if (!strlen($PostClass)) {
175
  $PostClass = $this->PostClass;
176
  }
213
  return Timber::get_posts($args, $PostClass);
214
  }
215
 
216
+ /**
217
+ * @return array
218
+ */
219
+ public function get_children(){
220
  if (!isset($this->_children)){
221
  $children = get_term_children($this->ID, $this->taxonomy);
222
  foreach($children as &$child){
230
  /* Alias
231
  ====================== */
232
 
233
+ /**
234
+ * @return array
235
+ */
236
+ public function children(){
237
  return $this->get_children();
238
  }
239
 
240
+ /**
241
+ * @return string
242
+ */
243
+ public function edit_link(){
244
  return $this->get_edit_url();
245
  }
246
 
247
+ /**
248
+ * @return string
249
+ */
250
+ public function get_url() {
251
  return $this->get_link();
252
  }
253
 
254
+ /**
255
+ * @return string
256
+ */
257
+ public function link(){
258
  return $this->get_link();
259
  }
260
 
261
+ /**
262
+ * @param string $field_name
263
+ * @return mixed
264
+ */
265
+ public function meta($field_name){
266
  return $this->get_meta_field($field_name);
267
  }
268
 
269
+ /**
270
+ * @return string
271
+ */
272
+ public function path(){
273
  return $this->get_path();
274
  }
275
 
276
+ /**
277
+ * @param int $numberposts_or_args
278
+ * @param string $post_type_or_class
279
+ * @param string $post_class
280
+ * @return array|bool|null
281
+ */
282
+ public function posts($numberposts_or_args = 10, $post_type_or_class = 'any', $post_class = ''){
283
  return $this->get_posts($numberposts_or_args, $post_type_or_class, $post_class);
284
  }
285
 
286
+ /**
287
+ * @return string
288
+ */
289
+ public function title(){
290
  return $this->name;
291
  }
292
 
293
+ /**
294
+ * @return string
295
+ */
296
+ public function url(){
297
  return $this->get_url();
298
  }
299
 
300
  /* Deprecated
301
  ===================== */
302
 
303
+ /**
304
+ * @deprecated
305
+ * @param int $i
306
+ * @return string
307
+ */
308
+ function get_page($i) {
309
  return $this->get_path() . '/page/' . $i;
310
  }
311
 
functions/timber-theme.php CHANGED
@@ -2,11 +2,17 @@
2
 
3
  class TimberTheme extends TimberCore {
4
 
5
- function __construct($slug = null){
 
 
 
6
  $this->init($slug);
7
  }
8
 
9
- function init($slug = null){
 
 
 
10
  $data = wp_get_theme($slug);
11
  $this->name = $data->get('Name');
12
  $ss = $data->get_stylesheet();
@@ -24,11 +30,19 @@
24
  }
25
  }
26
 
27
- public function theme_mod($name, $default = false){
 
 
 
 
 
28
  return get_theme_mod($name, $default);
29
  }
30
 
31
- public function theme_mods(){
 
 
 
32
  return get_theme_mods();
33
  }
34
 
2
 
3
  class TimberTheme extends TimberCore {
4
 
5
+ /**
6
+ * @param string $slug
7
+ */
8
+ function __construct($slug = null){
9
  $this->init($slug);
10
  }
11
 
12
+ /**
13
+ * @param string $slug
14
+ */
15
+ function init($slug = null){
16
  $data = wp_get_theme($slug);
17
  $this->name = $data->get('Name');
18
  $ss = $data->get_stylesheet();
30
  }
31
  }
32
 
33
+ /**
34
+ * @param string $name
35
+ * @param bool $default
36
+ * @return string
37
+ */
38
+ public function theme_mod($name, $default = false){
39
  return get_theme_mod($name, $default);
40
  }
41
 
42
+ /**
43
+ * @return string
44
+ */
45
+ public function theme_mods(){
46
  return get_theme_mods();
47
  }
48
 
functions/timber-url-helper.php ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class TimberURLHelper {
4
+
5
+ /**
6
+ * @return string
7
+ */
8
+ public static function get_current_url() {
9
+ $pageURL = "http://";
10
+ if (isset($_SERVER['HTTPS']) && $_SERVER["HTTPS"] == "on"){
11
+ $pageURL = "https://";;
12
+ }
13
+ if ($_SERVER["SERVER_PORT"] != "80") {
14
+ $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
15
+ } else {
16
+ $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
17
+ }
18
+ return $pageURL;
19
+ }
20
+
21
+ /**
22
+ * @param string $url
23
+ * @return bool
24
+ */
25
+ public static function is_url($url) {
26
+ if (!is_string($url)){
27
+ return false;
28
+ }
29
+ $url = strtolower($url);
30
+ if (strstr('://', $url)) {
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+
36
+ /**
37
+ * @return string
38
+ */
39
+ public static function get_path_base() {
40
+ $struc = get_option('permalink_structure');
41
+ $struc = explode('/', $struc);
42
+ $p = '/';
43
+ foreach ($struc as $s) {
44
+ if (!strstr($s, '%') && strlen($s)) {
45
+ $p .= $s . '/';
46
+ }
47
+ }
48
+ return $p;
49
+ }
50
+
51
+ /**
52
+ * @param string $url
53
+ * @param bool $force
54
+ * @return string
55
+ */
56
+ public static function get_rel_url($url, $force = false){
57
+ if (!strstr($url, $_SERVER['HTTP_HOST']) && !$force){
58
+ return $url;
59
+ }
60
+ $url_info = parse_url($url);
61
+ $link = $url_info['path'];
62
+ if (isset($url_info['query']) && strlen($url_info['query'])){
63
+ $link .= '?'.$url_info['query'];
64
+ }
65
+ return $link;
66
+ }
67
+
68
+ /**
69
+ * @param string $url
70
+ * @return bool
71
+ */
72
+ public static function is_local($url){
73
+ if (strstr($url, $_SERVER['HTTP_HOST'])){
74
+ return true;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ /**
80
+ * @param string $src
81
+ * @return string
82
+ */
83
+ public static function get_full_path($src) {
84
+ $root = ABSPATH;
85
+ $old_root_path = $root . $src;
86
+ $old_root_path = str_replace('//', '/', $old_root_path);
87
+ return $old_root_path;
88
+ }
89
+
90
+ /**
91
+ * @param string $src
92
+ * @return string
93
+ */
94
+ public static function get_rel_path($src) {
95
+ return str_replace(ABSPATH, '', $src);
96
+ }
97
+
98
+ /**
99
+ * @param string $url
100
+ * @return string
101
+ */
102
+ public static function remove_double_slashes($url){
103
+ $url = str_replace('//', '/', $url);
104
+ if (strstr($url, 'http:') && !strstr($url, 'http://')){
105
+ $url = str_replace('http:/', 'http://', $url);
106
+ }
107
+ return $url;
108
+ }
109
+
110
+ /**
111
+ * @param string $url
112
+ * @param string $path
113
+ * @return string
114
+ */
115
+ public static function prepend_to_url($url, $path){
116
+ if (strstr(strtolower($url), 'http')){
117
+ $url_parts = parse_url($url);
118
+ $url = $url_parts['scheme'].'://'.$url_parts['host'].$path.$url_parts['path'];
119
+ } else {
120
+ $url = $url.$path;
121
+ }
122
+ return self::remove_double_slashes($url);
123
+ }
124
+
125
+ /**
126
+ * @param string $path
127
+ * @return string
128
+ */
129
+ public static function preslashit($path){
130
+ if (strpos($path, '/') != 0) {
131
+ $path = '/' . $path;
132
+ }
133
+ return $path;
134
+ }
135
+
136
+ /**
137
+ * @param string $url
138
+ * @return bool
139
+ */
140
+ public static function is_external($url){
141
+ $has_http = strstr(strtolower($url), 'http');
142
+ $on_domain = strstr($url, $_SERVER['HTTP_HOST']);
143
+ if ($has_http && !$on_domain){
144
+ return true;
145
+ }
146
+ return false;
147
+ }
148
+
149
+ /**
150
+ * @param string $url
151
+ * @param int $timeout
152
+ * @return string|WP_Error
153
+ */
154
+ public static function download_url($url, $timeout = 300) {
155
+ if (!$url) {
156
+ return new WP_Error('http_no_url', __('Invalid URL Provided.'));
157
+ }
158
+
159
+ $tmpfname = wp_tempnam($url);
160
+ if (!$tmpfname) {
161
+ return new WP_Error('http_no_file', __('Could not create Temporary file.'));
162
+ }
163
+
164
+ $response = wp_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
165
+
166
+ if (is_wp_error($response)) {
167
+ unlink($tmpfname);
168
+ return $response;
169
+ }
170
+ if (200 != wp_remote_retrieve_response_code($response)) {
171
+ unlink($tmpfname);
172
+ return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
173
+ }
174
+ return $tmpfname;
175
+ }
176
+
177
+ public static function osort(&$array, $prop) {
178
+ usort($array, function ($a, $b) use ($prop) {
179
+ return $a->$prop > $b->$prop ? 1 : -1;
180
+ });
181
+ }
182
+
183
+ /**
184
+ * @param int $i
185
+ * @return array
186
+ */
187
+ public static function get_params($i = -1) {
188
+ $args = explode('/', trim(strtolower($_SERVER['REQUEST_URI'])));
189
+ $newargs = array();
190
+ foreach ($args as $arg) {
191
+ if (strlen($arg)) {
192
+ $newargs[] = $arg;
193
+ }
194
+ }
195
+ if ($i > -1) {
196
+ if (isset($newargs[$i])) {
197
+ return $newargs[$i];
198
+ }
199
+ }
200
+ return $newargs;
201
+ }
202
+ }
functions/timber-user.php CHANGED
@@ -7,10 +7,16 @@ class TimberUser extends TimberCore {
7
 
8
  public static $representation = 'user';
9
 
 
 
 
10
  function __construct($uid = false) {
11
  $this->init($uid);
12
  }
13
 
 
 
 
14
  function __toString(){
15
  $name = $this->name();
16
  if (strlen($name)){
@@ -22,6 +28,10 @@ class TimberUser extends TimberCore {
22
  return '';
23
  }
24
 
 
 
 
 
25
  function get_meta($field_name){
26
  $value = null;
27
  $value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this);
@@ -32,6 +42,10 @@ class TimberUser extends TimberCore {
32
  return $value;
33
  }
34
 
 
 
 
 
35
  function __set($field, $value){
36
  if ($field == 'name'){
37
  $this->display_name = $value;
@@ -39,6 +53,9 @@ class TimberUser extends TimberCore {
39
  $this->$field = $value;
40
  }
41
 
 
 
 
42
  public function get_link() {
43
  if (!$this->_link){
44
  $this->_link = get_author_posts_url($this->ID);
@@ -46,6 +63,9 @@ class TimberUser extends TimberCore {
46
  return $this->_link;
47
  }
48
 
 
 
 
49
  function init($uid = false) {
50
  if ($uid === false) {
51
  $uid = get_current_user_id();
@@ -60,6 +80,10 @@ class TimberUser extends TimberCore {
60
  }
61
  }
62
 
 
 
 
 
63
  function get_meta_field($field_name){
64
  $value = null;
65
  $value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this);
@@ -70,6 +94,9 @@ class TimberUser extends TimberCore {
70
  return $value;
71
  }
72
 
 
 
 
73
  function get_custom() {
74
  if ($this->ID) {
75
  $um = array();
@@ -95,34 +122,59 @@ class TimberUser extends TimberCore {
95
  $this->import($custom);
96
  }
97
 
 
 
 
98
  function name() {
99
  return $this->display_name;
100
  }
101
 
 
 
 
102
  function get_permalink(){
103
  return $this->get_link();
104
  }
105
 
 
 
 
106
  function permalink() {
107
  return $this->get_link();
108
  }
109
 
 
 
 
110
  function get_path() {
111
  return $this->get_link();
112
  }
113
 
 
 
 
 
114
  function meta($field_name){
115
  return $this->get_meta_field($field_name);
116
  }
117
 
 
 
 
118
  function path() {
119
  return $this->get_path();
120
  }
121
 
 
 
 
122
  function slug() {
123
  return $this->user_nicename;
124
  }
125
 
 
 
 
126
  function link(){
127
  return $this->get_link();
128
  }
7
 
8
  public static $representation = 'user';
9
 
10
+ /**
11
+ * @param int|bool $uid
12
+ */
13
  function __construct($uid = false) {
14
  $this->init($uid);
15
  }
16
 
17
+ /**
18
+ * @return string
19
+ */
20
  function __toString(){
21
  $name = $this->name();
22
  if (strlen($name)){
28
  return '';
29
  }
30
 
31
+ /**
32
+ * @param string $field_name
33
+ * @return null
34
+ */
35
  function get_meta($field_name){
36
  $value = null;
37
  $value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this);
42
  return $value;
43
  }
44
 
45
+ /**
46
+ * @param string $field
47
+ * @param mixed $value
48
+ */
49
  function __set($field, $value){
50
  if ($field == 'name'){
51
  $this->display_name = $value;
53
  $this->$field = $value;
54
  }
55
 
56
+ /**
57
+ * @return string
58
+ */
59
  public function get_link() {
60
  if (!$this->_link){
61
  $this->_link = get_author_posts_url($this->ID);
63
  return $this->_link;
64
  }
65
 
66
+ /**
67
+ * @param int|bool $uid
68
+ */
69
  function init($uid = false) {
70
  if ($uid === false) {
71
  $uid = get_current_user_id();
80
  }
81
  }
82
 
83
+ /**
84
+ * @param string $field_name
85
+ * @return mixed
86
+ */
87
  function get_meta_field($field_name){
88
  $value = null;
89
  $value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this);
94
  return $value;
95
  }
96
 
97
+ /**
98
+ * @return array|null
99
+ */
100
  function get_custom() {
101
  if ($this->ID) {
102
  $um = array();
122
  $this->import($custom);
123
  }
124
 
125
+ /**
126
+ * @return string
127
+ */
128
  function name() {
129
  return $this->display_name;
130
  }
131
 
132
+ /**
133
+ * @return string
134
+ */
135
  function get_permalink(){
136
  return $this->get_link();
137
  }
138
 
139
+ /**
140
+ * @return string
141
+ */
142
  function permalink() {
143
  return $this->get_link();
144
  }
145
 
146
+ /**
147
+ * @return string
148
+ */
149
  function get_path() {
150
  return $this->get_link();
151
  }
152
 
153
+ /**
154
+ * @param string $field_name
155
+ * @return mixed
156
+ */
157
  function meta($field_name){
158
  return $this->get_meta_field($field_name);
159
  }
160
 
161
+ /**
162
+ * @return string
163
+ */
164
  function path() {
165
  return $this->get_path();
166
  }
167
 
168
+ /**
169
+ * @return string
170
+ */
171
  function slug() {
172
  return $this->user_nicename;
173
  }
174
 
175
+ /**
176
+ * @return string
177
+ */
178
  function link(){
179
  return $this->get_link();
180
  }
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.7
5
- Stable tag: 0.17.2
6
- Tested up to: 3.8.1
7
  PHP version: 5.3.0 or greater
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -41,6 +41,14 @@ Timber is great for any WordPress developer who cares about writing good, mainta
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
 
 
 
44
  = 0.17.2 =
45
  * TimberPost::children() now sorts by menu_order, title as WordPress core does (thanks @aduth)
46
  * Fixed an occaisonal warning (thanks @matthewsoares)
@@ -281,4 +289,4 @@ Whatever. It simplifies the silly stuff so that you can focus on building more c
281
  As stated above, we're using it in dozens of sites (and dozens more planned) -- dozens of other developers are using it too. This isn't going anywhere. Twig is the chosen language for other PHP platforms like Symfony, Drupal 8 and Craft. WordPress will eventually adopt Twig too, I promise you that.
282
 
283
  = Support? =
284
- Leave a [GitHub issue](https://github.com/jarednova/timber/issues?state=open) and I'll holler back.
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.7
5
+ Stable tag: 0.18.0
6
+ Tested up to: 3.9
7
  PHP version: 5.3.0 or greater
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
41
 
42
  == Changelog ==
43
 
44
+ = 0.18.0 =
45
+ * BREAKING CHANGE ALERT wp_title no longer appends bloginfo('name') to end of string (thanks @aduth)
46
+ * BREAKING CHANGE ALERT get_preview now respects <!-- more --> tag (thanks @jnweaver)
47
+ * TimberHelper::transient is more reliable (thanks @mgmartel)
48
+ * Secure urls in TimberImage if current page is served over SSL (thanks @mgmartel)
49
+ * Re-wrote most of letterboxing functionality
50
+ * Re-organized Helper functions
51
+
52
  = 0.17.2 =
53
  * TimberPost::children() now sorts by menu_order, title as WordPress core does (thanks @aduth)
54
  * Fixed an occaisonal warning (thanks @matthewsoares)
289
  As stated above, we're using it in dozens of sites (and dozens more planned) -- dozens of other developers are using it too. This isn't going anywhere. Twig is the chosen language for other PHP platforms like Symfony, Drupal 8 and Craft. WordPress will eventually adopt Twig too, I promise you that.
290
 
291
  = Support? =
292
+ Leave a [GitHub issue](https://github.com/jarednova/timber/issues?state=open) and I'll holler back.
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.17.2
8
  Author URI: http://upstatement.com/
9
  */
10
 
@@ -13,6 +13,7 @@ global $timber;
13
 
14
  require_once(__DIR__ . '/functions/functions-twig.php');
15
  require_once(__DIR__ . '/functions/timber-helper.php');
 
16
  require_once(__DIR__ . '/functions/timber-image-helper.php');
17
 
18
  require_once(__DIR__ . '/functions/timber-core.php');
@@ -96,6 +97,11 @@ class Timber {
96
  /* Post Retrieval
97
  ================================ */
98
 
 
 
 
 
 
99
  public static function get_post($query = false, $PostClass = 'TimberPost') {
100
  if (is_int($query)) {
101
  /* its a post id number */
@@ -108,6 +114,12 @@ class Timber {
108
  return $posts;
109
  }
110
 
 
 
 
 
 
 
111
  public static function get_posts($query = false, $PostClass = 'TimberPost'){
112
  if (self::is_post_class_or_class_map($query)) {
113
  $PostClass = $query;
@@ -142,6 +154,10 @@ class Timber {
142
  return self::maybe_set_preview( $posts );
143
  }
144
 
 
 
 
 
145
  public static function get_pids($query = null) {
146
  $posts = get_posts($query);
147
  $pids = array();
@@ -153,11 +169,18 @@ class Timber {
153
  return $pids;
154
  }
155
 
 
 
 
 
156
  public static function get_posts_from_loop($PostClass) {
157
  $results = self::get_pids_from_loop();
158
  return self::handle_post_results($results, $PostClass);
159
  }
160
 
 
 
 
161
  public static function get_pids_from_loop() {
162
  $posts = array();
163
  $i = 0;
@@ -173,6 +196,11 @@ class Timber {
173
  return $posts;
174
  }
175
 
 
 
 
 
 
176
  public static function get_posts_from_slug($slug, $PostClass) {
177
  global $wpdb;
178
  $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $slug);
@@ -193,12 +221,21 @@ class Timber {
193
  return self::handle_post_results($results, $PostClass);
194
  }
195
 
 
 
 
 
 
196
  public static function get_posts_from_wp_query($query = array(), $PostClass = 'TimberPost') {
197
- $start = TimberHelper::start_timer();
198
  $results = get_posts($query);
199
  return self::handle_post_results($results, $PostClass);
200
  }
201
 
 
 
 
 
 
202
  public static function get_posts_from_array_of_ids($query = array(), $PostClass = 'TimberPost') {
203
  if (!is_array($query) || !count($query)) {
204
  return null;
@@ -207,8 +244,12 @@ class Timber {
207
  return self::handle_post_results($results, $PostClass);
208
  }
209
 
 
 
 
 
 
210
  public static function handle_post_results($results, $PostClass = 'TimberPost') {
211
- $start = TimberHelper::start_timer();
212
  $posts = array();
213
  foreach ($results as $rid) {
214
  $PostClassUse = $PostClass;
@@ -233,6 +274,10 @@ class Timber {
233
  return $posts;
234
  }
235
 
 
 
 
 
236
  public function get_pid($query) {
237
  $post = self::get_posts($query);
238
  return $post->ID;
@@ -241,6 +286,10 @@ class Timber {
241
  /* Post Previews
242
  ================================ */
243
 
 
 
 
 
244
  public static function maybe_set_preview( $posts ) {
245
  if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview']
246
  && isset( $_GET['preview_id'] ) && $_GET['preview_id']
@@ -277,10 +326,17 @@ class Timber {
277
  /* Deprecated
278
  ================================ */
279
 
 
 
 
 
280
  public function loop_to_posts($PostClass = 'TimberPost') {
281
  return self::get_posts(false, $PostClass);
282
  }
283
 
 
 
 
284
  public function loop_to_id() {
285
  if (have_posts()) {
286
  the_post();
@@ -294,6 +350,12 @@ class Timber {
294
  /* Term Retrieval
295
  ================================ */
296
 
 
 
 
 
 
 
297
  public static function get_terms($args, $maybe_args = array(), $TermClass = 'TimberTerm'){
298
  if (is_string($maybe_args) && !strstr($maybe_args, '=')){
299
  //the user is sending the $TermClass in the second argument
@@ -333,6 +395,12 @@ class Timber {
333
 
334
  }
335
 
 
 
 
 
 
 
336
  public static function handle_term_query($taxonomies, $args, $TermClass){
337
  if (!isset($args['hide_empty'])){
338
  $args['hide_empty'] = false;
@@ -347,6 +415,10 @@ class Timber {
347
  /* Site Retrieval
348
  ================================ */
349
 
 
 
 
 
350
  public static function get_sites($blog_ids = false){
351
  if (!is_array($blog_ids)){
352
  global $wpdb;
@@ -363,10 +435,13 @@ class Timber {
363
  /* Template Setup and Display
364
  ================================ */
365
 
 
 
 
366
  public static function get_context() {
367
  $data = array();
368
  $data['http_host'] = 'http://' . $_SERVER['HTTP_HOST'];
369
- $data['wp_title'] = get_bloginfo('name');
370
  $data['wp_head'] = TimberHelper::function_wrapper('wp_head');
371
  $data['wp_footer'] = TimberHelper::function_wrapper('wp_footer');
372
  $data['body_class'] = implode(' ', get_body_class());
@@ -387,6 +462,14 @@ class Timber {
387
  return $data;
388
  }
389
 
 
 
 
 
 
 
 
 
390
  public static function compile($filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false) {
391
  $caller = self::get_calling_script_dir();
392
  $loader = new TimberLoader($caller);
@@ -405,6 +488,13 @@ class Timber {
405
  return $output;
406
  }
407
 
 
 
 
 
 
 
 
408
  public static function render($filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT) {
409
  if ($expires === true){
410
  //if this is reading as true; the user probably is using the old $echo param
@@ -422,6 +512,11 @@ class Timber {
422
  /* Sidebar
423
  ================================ */
424
 
 
 
 
 
 
425
  public static function get_sidebar($sidebar = '', $data = array()) {
426
  if ($sidebar == '') {
427
  $sidebar = 'sidebar.php';
@@ -432,6 +527,11 @@ class Timber {
432
  return self::render($sidebar, $data, false);
433
  }
434
 
 
 
 
 
 
435
  public static function get_sidebar_from_php($sidebar = '', $data) {
436
  $caller = self::get_calling_script_dir();
437
  $loader = new TimberLoader();
@@ -456,6 +556,10 @@ class Timber {
456
  /* Widgets
457
  ================================ */
458
 
 
 
 
 
459
  public static function get_widgets($widget_id){
460
  return TimberHelper::function_wrapper('dynamic_sidebar', array($widget_id), true);
461
  }
@@ -476,6 +580,11 @@ class Timber {
476
  }
477
  }
478
 
 
 
 
 
 
479
  public static function add_route($route, $callback, $args = array()) {
480
  global $timber;
481
  if (!isset($timber->router)) {
@@ -508,7 +617,12 @@ class Timber {
508
  });
509
  }
510
 
511
-
 
 
 
 
 
512
  public static function load_template($template, $query = false, $force_header = 0, $tparams = false) {
513
  $template = locate_template($template);
514
  if ($tparams){
@@ -564,6 +678,10 @@ class Timber {
564
  /* Pagination
565
  ================================ */
566
 
 
 
 
 
567
  public static function get_pagination($prefs = array()){
568
  global $wp_query;
569
  global $paged;
@@ -608,11 +726,19 @@ class Timber {
608
  /* Utility
609
  ================================ */
610
 
 
 
 
 
611
  public static function get_calling_script_path($offset = 0) {
612
  $dir = self::get_calling_script_dir($offset);
613
  return str_replace(ABSPATH, '', realpath($dir));
614
  }
615
 
 
 
 
 
616
  public static function get_calling_script_dir($offset = 0) {
617
  $caller = null;
618
  $backtrace = debug_backtrace();
@@ -635,6 +761,10 @@ class Timber {
635
  return null;
636
  }
637
 
 
 
 
 
638
  public static function is_post_class_or_class_map($arg){
639
  if (is_string($arg) && class_exists($arg)) {
640
  return true;
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.18.0
8
  Author URI: http://upstatement.com/
9
  */
10
 
13
 
14
  require_once(__DIR__ . '/functions/functions-twig.php');
15
  require_once(__DIR__ . '/functions/timber-helper.php');
16
+ require_once(__DIR__ . '/functions/timber-url-helper.php');
17
  require_once(__DIR__ . '/functions/timber-image-helper.php');
18
 
19
  require_once(__DIR__ . '/functions/timber-core.php');
97
  /* Post Retrieval
98
  ================================ */
99
 
100
+ /**
101
+ * @param mixed $query
102
+ * @param string $PostClass
103
+ * @return array|bool|null
104
+ */
105
  public static function get_post($query = false, $PostClass = 'TimberPost') {
106
  if (is_int($query)) {
107
  /* its a post id number */
114
  return $posts;
115
  }
116
 
117
+
118
+ /**
119
+ * @param mixed $query
120
+ * @param string $PostClass
121
+ * @return array|bool|null
122
+ */
123
  public static function get_posts($query = false, $PostClass = 'TimberPost'){
124
  if (self::is_post_class_or_class_map($query)) {
125
  $PostClass = $query;
154
  return self::maybe_set_preview( $posts );
155
  }
156
 
157
+ /**
158
+ * @param array|string $query
159
+ * @return array
160
+ */
161
  public static function get_pids($query = null) {
162
  $posts = get_posts($query);
163
  $pids = array();
169
  return $pids;
170
  }
171
 
172
+ /**
173
+ * @param string $PostClass
174
+ * @return array
175
+ */
176
  public static function get_posts_from_loop($PostClass) {
177
  $results = self::get_pids_from_loop();
178
  return self::handle_post_results($results, $PostClass);
179
  }
180
 
181
+ /**
182
+ * @return array
183
+ */
184
  public static function get_pids_from_loop() {
185
  $posts = array();
186
  $i = 0;
196
  return $posts;
197
  }
198
 
199
+ /**
200
+ * @param string $slug
201
+ * @param string $PostClass
202
+ * @return array
203
+ */
204
  public static function get_posts_from_slug($slug, $PostClass) {
205
  global $wpdb;
206
  $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $slug);
221
  return self::handle_post_results($results, $PostClass);
222
  }
223
 
224
+ /**
225
+ * @param array $query
226
+ * @param string $PostClass
227
+ * @return array
228
+ */
229
  public static function get_posts_from_wp_query($query = array(), $PostClass = 'TimberPost') {
 
230
  $results = get_posts($query);
231
  return self::handle_post_results($results, $PostClass);
232
  }
233
 
234
+ /**
235
+ * @param array $query
236
+ * @param string $PostClass
237
+ * @return array|null
238
+ */
239
  public static function get_posts_from_array_of_ids($query = array(), $PostClass = 'TimberPost') {
240
  if (!is_array($query) || !count($query)) {
241
  return null;
244
  return self::handle_post_results($results, $PostClass);
245
  }
246
 
247
+ /**
248
+ * @param array $results
249
+ * @param string $PostClass
250
+ * @return array
251
+ */
252
  public static function handle_post_results($results, $PostClass = 'TimberPost') {
 
253
  $posts = array();
254
  foreach ($results as $rid) {
255
  $PostClassUse = $PostClass;
274
  return $posts;
275
  }
276
 
277
+ /**
278
+ * @param $query
279
+ * @return mixed
280
+ */
281
  public function get_pid($query) {
282
  $post = self::get_posts($query);
283
  return $post->ID;
286
  /* Post Previews
287
  ================================ */
288
 
289
+ /**
290
+ * @param array $posts
291
+ * @return array
292
+ */
293
  public static function maybe_set_preview( $posts ) {
294
  if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview']
295
  && isset( $_GET['preview_id'] ) && $_GET['preview_id']
326
  /* Deprecated
327
  ================================ */
328
 
329
+ /**
330
+ * @param string $PostClass
331
+ * @return bool|null
332
+ */
333
  public function loop_to_posts($PostClass = 'TimberPost') {
334
  return self::get_posts(false, $PostClass);
335
  }
336
 
337
+ /**
338
+ * @return bool|int
339
+ */
340
  public function loop_to_id() {
341
  if (have_posts()) {
342
  the_post();
350
  /* Term Retrieval
351
  ================================ */
352
 
353
+ /**
354
+ * @param string|array $args
355
+ * @param array $maybe_args
356
+ * @param string $TermClass
357
+ * @return mixed
358
+ */
359
  public static function get_terms($args, $maybe_args = array(), $TermClass = 'TimberTerm'){
360
  if (is_string($maybe_args) && !strstr($maybe_args, '=')){
361
  //the user is sending the $TermClass in the second argument
395
 
396
  }
397
 
398
+ /**
399
+ * @param string|array $taxonomies
400
+ * @param string|array $args
401
+ * @param $TermClass
402
+ * @return mixed
403
+ */
404
  public static function handle_term_query($taxonomies, $args, $TermClass){
405
  if (!isset($args['hide_empty'])){
406
  $args['hide_empty'] = false;
415
  /* Site Retrieval
416
  ================================ */
417
 
418
+ /**
419
+ * @param array|bool $blog_ids
420
+ * @return array
421
+ */
422
  public static function get_sites($blog_ids = false){
423
  if (!is_array($blog_ids)){
424
  global $wpdb;
435
  /* Template Setup and Display
436
  ================================ */
437
 
438
+ /**
439
+ * @return array
440
+ */
441
  public static function get_context() {
442
  $data = array();
443
  $data['http_host'] = 'http://' . $_SERVER['HTTP_HOST'];
444
+ $data['wp_title'] = TimberHelper::get_wp_title();
445
  $data['wp_head'] = TimberHelper::function_wrapper('wp_head');
446
  $data['wp_footer'] = TimberHelper::function_wrapper('wp_footer');
447
  $data['body_class'] = implode(' ', get_body_class());
462
  return $data;
463
  }
464
 
465
+ /**
466
+ * @param array $filenames
467
+ * @param array $data
468
+ * @param bool $expires
469
+ * @param string $cache_mode
470
+ * @param bool $via_render
471
+ * @return bool|string
472
+ */
473
  public static function compile($filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false) {
474
  $caller = self::get_calling_script_dir();
475
  $loader = new TimberLoader($caller);
488
  return $output;
489
  }
490
 
491
+ /**
492
+ * @param array $filenames
493
+ * @param array $data
494
+ * @param bool $expires
495
+ * @param string $cache_mode
496
+ * @return bool|string
497
+ */
498
  public static function render($filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT) {
499
  if ($expires === true){
500
  //if this is reading as true; the user probably is using the old $echo param
512
  /* Sidebar
513
  ================================ */
514
 
515
+ /**
516
+ * @param string $sidebar
517
+ * @param array $data
518
+ * @return bool|string
519
+ */
520
  public static function get_sidebar($sidebar = '', $data = array()) {
521
  if ($sidebar == '') {
522
  $sidebar = 'sidebar.php';
527
  return self::render($sidebar, $data, false);
528
  }
529
 
530
+ /**
531
+ * @param string $sidebar
532
+ * @param array $data
533
+ * @return string
534
+ */
535
  public static function get_sidebar_from_php($sidebar = '', $data) {
536
  $caller = self::get_calling_script_dir();
537
  $loader = new TimberLoader();
556
  /* Widgets
557
  ================================ */
558
 
559
+ /**
560
+ * @param int $widget_id
561
+ * @return TimberFunctionWrapper
562
+ */
563
  public static function get_widgets($widget_id){
564
  return TimberHelper::function_wrapper('dynamic_sidebar', array($widget_id), true);
565
  }
580
  }
581
  }
582
 
583
+ /**
584
+ * @param string $route
585
+ * @param callable $callback
586
+ * @param array $args
587
+ */
588
  public static function add_route($route, $callback, $args = array()) {
589
  global $timber;
590
  if (!isset($timber->router)) {
617
  });
618
  }
619
 
620
+ /**
621
+ * @param array $template
622
+ * @param bool $query
623
+ * @param int $force_header
624
+ * @param bool $tparams
625
+ */
626
  public static function load_template($template, $query = false, $force_header = 0, $tparams = false) {
627
  $template = locate_template($template);
628
  if ($tparams){
678
  /* Pagination
679
  ================================ */
680
 
681
+ /**
682
+ * @param array $prefs
683
+ * @return array mixed
684
+ */
685
  public static function get_pagination($prefs = array()){
686
  global $wp_query;
687
  global $paged;
726
  /* Utility
727
  ================================ */
728
 
729
+ /**
730
+ * @param int $offset
731
+ * @return string
732
+ */
733
  public static function get_calling_script_path($offset = 0) {
734
  $dir = self::get_calling_script_dir($offset);
735
  return str_replace(ABSPATH, '', realpath($dir));
736
  }
737
 
738
+ /**
739
+ * @param int $offset
740
+ * @return string|null
741
+ */
742
  public static function get_calling_script_dir($offset = 0) {
743
  $caller = null;
744
  $backtrace = debug_backtrace();
761
  return null;
762
  }
763
 
764
+ /**
765
+ * @param string|array $arg
766
+ * @return bool
767
+ */
768
  public static function is_post_class_or_class_map($arg){
769
  if (is_string($arg) && class_exists($arg)) {
770
  return true;