Timber - Version 0.10.4

Version Description

  • Lots of code cleanup thanks to Jakub
  • Added new function for bloginfo
  • You can now hook into timber_context to filter the $context object
  • Added Timber::get_terms to retrive lists of your blog's terms
  • Added better support for translation
  • Added filter for executing a function, ie {{'my_theme_function'|filter}}
Download this release

Release Info

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

Code changes from version 0.10.3 to 0.10.4

admin/timber-admin.css ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ li[class*="timber"] .wp-menu-image {
2
+ overflow:hidden;
3
+ }
4
+
5
+ li[class*="timber"] .wp-menu-image img {
6
+ position:relative;
7
+ left:-8px;
8
+ top:-36px;
9
+ }
10
+ li[class*="timber"]:hover .wp-menu-image img,
11
+ li[class*="timber"].current .wp-menu-image img {
12
+ top:-3px;
13
+ }
14
+
15
+ .timber-logo {
16
+ max-width:600px;
17
+ }
18
+
19
+ .timber-logo-img {
20
+ width:100%;
21
+ display:block;
22
+ max-width:100%;
23
+ }
admin/timber-admin.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class TimberAdmin
4
+ {
5
+
6
+ function __construct()
7
+ {
8
+ if (!is_admin() || true) {
9
+ return;
10
+ }
11
+ add_action('admin_menu', array(&$this, 'create_menu'));
12
+ add_action('admin_enqueue_scripts', array(&$this, 'load_styles'));
13
+ }
14
+
15
+ function create_menu()
16
+ {
17
+ add_menu_page('Timber', 'Timber', 'administrator', __FILE__, array(&$this, 'create_admin_page'), TIMBER_URL_PATH . 'admin/timber-menu.png');
18
+ }
19
+
20
+ function create_admin_page()
21
+ {
22
+ $data = array();
23
+ $data['theme_dir'] = get_stylesheet_directory();
24
+ $data['home_file']['name'] = 'index.php';
25
+ $data['timber_base'] = TIMBER_URL_PATH;
26
+ $data['home_file']['path'] = trailingslashit(get_stylesheet_directory()) . $data['home_file']['name'];
27
+ $data['home_file']['contents'] = htmlentities(file_get_contents(realpath($data['home_file']['path'])));
28
+ Timber::render('timber-admin.twig', $data);
29
+ }
30
+
31
+ function load_styles()
32
+ {
33
+ wp_enqueue_style('timber-admin-css', TIMBER_URL_PATH . 'admin/timber-admin.css');
34
+ }
35
+
36
+ }
37
+
38
+ new TimberAdmin();
admin/timber-admin.twig ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <h2 class="timber-logo"><img src="{{timber_base}}images/logo/timber-badge-large.jpg" class="timber-logo-img" /></h2>
3
+
4
+ <h3>Getting Started</h3>
5
+ <p>This is a quick getting-started guide to using Timber in your theme. It requires a solid knowledge of HTML and a basic understanding of PHP.</p>
6
+
7
+ <ol class="steps">
8
+ <li>
9
+ <h4>Open your {{home_file}}</h4>
10
+ <p>Using your favorite text editor, open up {{home_file.name}} inside of {{theme_dir}}. If you're working off your server, you'll need to use an FTP program like Cyberduck or Transmit.</p>
11
+ </li>
12
+ <li>
13
+ <h4>You should see this:</h4>
14
+ <pre><code>
15
+ {{home_file.contents}}
16
+ </code></pre>
17
+ </ol>
18
+
19
+ </div>
admin/timber-menu.png ADDED
Binary file
admin/timber-menu.psd ADDED
Binary file
functions/functions-php-helper.php CHANGED
@@ -2,76 +2,84 @@
2
  /*
3
  * THIS FILE IS DEPRECATED AND WILL BE REMOVED AT SOME POINT */
4
 
5
- class PHPHelper {
 
6
 
7
- function is_array_assoc($arr){
8
- if (!is_array($arr)){
9
- return false;
10
- }
11
- return (bool)count(array_filter(array_keys($arr), 'is_string'));
12
- }
 
13
 
14
- function is_true($property){
15
- if (isset($property)){
16
- if ($property == 'true' || $property == 1 || $property == '1' || $property == true){
17
- return true;
18
- }
19
- }
20
- return false;
21
- }
 
22
 
23
- function array_truncate($array, $len){
24
- if (sizeof($array) > $len) {
25
- $array = array_splice($array, 0, $len);
26
- }
27
- return $array;
28
- }
 
29
 
30
- function array_to_object($array) {
31
- $obj = new stdClass;
32
- foreach($array as $k => $v) {
33
- if(is_array($v)) {
34
- $obj->{$k} = array_to_object($v); //RECURSION
35
- } else {
36
- $obj->{$k} = $v;
37
- }
38
- }
39
- return $obj;
40
- }
 
41
 
42
- function get_object_index_by_property($array, $key, $value){
43
- if (is_array($array)){
44
- $i = 0;
45
- foreach($array as $arr){
46
- if ($arr->$key == $value || $arr[$key] == $value){
47
- return $i;
48
- }
49
- $i++;
50
- }
51
- }
52
- return false;
53
- }
 
54
 
55
- function get_object_by_property($array, $key, $value){
56
- if (is_array($array)){
57
- foreach($array as $arr){
58
- if ($arr->$key == $value){
59
- return $arr;
60
- }
61
- }
62
- } else {
63
- echo $array;
64
- echo 'not an array'.$key.' = '.$value;
65
- }
66
- }
 
67
 
68
- function iseven($i){
69
- return ($i % 2) == 0;
70
- }
 
71
 
72
- function isodd($i){
73
- return ($i % 2) != 0;
74
- }
 
75
 
76
- }
77
- ?>
2
  /*
3
  * THIS FILE IS DEPRECATED AND WILL BE REMOVED AT SOME POINT */
4
 
5
+ class PHPHelper
6
+ {
7
 
8
+ public static function is_array_assoc($arr)
9
+ {
10
+ if (!is_array($arr)) {
11
+ return false;
12
+ }
13
+ return (bool)count(array_filter(array_keys($arr), 'is_string'));
14
+ }
15
 
16
+ public static function is_true($property)
17
+ {
18
+ if (isset($property)) {
19
+ if ($property == 'true' || $property == 1 || $property == '1' || $property == true) {
20
+ return true;
21
+ }
22
+ }
23
+ return false;
24
+ }
25
 
26
+ public static function array_truncate($array, $len)
27
+ {
28
+ if (sizeof($array) > $len) {
29
+ $array = array_splice($array, 0, $len);
30
+ }
31
+ return $array;
32
+ }
33
 
34
+ public static function array_to_object($array)
35
+ {
36
+ $obj = new stdClass;
37
+ foreach ($array as $k => $v) {
38
+ if (is_array($v)) {
39
+ $obj->{$k} = self::array_to_object($v); //RECURSION
40
+ } else {
41
+ $obj->{$k} = $v;
42
+ }
43
+ }
44
+ return $obj;
45
+ }
46
 
47
+ public static function get_object_index_by_property($array, $key, $value)
48
+ {
49
+ if (is_array($array)) {
50
+ $i = 0;
51
+ foreach ($array as $arr) {
52
+ if ($arr->$key == $value || $arr[$key] == $value) {
53
+ return $i;
54
+ }
55
+ $i++;
56
+ }
57
+ }
58
+ return false;
59
+ }
60
 
61
+ public static function get_object_by_property($array, $key, $value)
62
+ {
63
+ if (is_array($array)) {
64
+ foreach ($array as $arr) {
65
+ if ($arr->$key == $value) {
66
+ return $arr;
67
+ }
68
+ }
69
+ } else {
70
+ throw new Exception('$array is not an array, given value: ' . $array);
71
+ }
72
+ return null;
73
+ }
74
 
75
+ public static function iseven($i)
76
+ {
77
+ return ($i % 2) == 0;
78
+ }
79
 
80
+ public static function isodd($i)
81
+ {
82
+ return ($i % 2) != 0;
83
+ }
84
 
85
+ }
 
functions/functions-post-master.php CHANGED
@@ -1,279 +1,298 @@
1
  <?php
2
- class PostMaster {
3
 
4
- function __construct(){
5
 
6
- }
7
-
8
- function loop_to_array($limit = 99999){
9
- $posts = array();
10
- $i = 0;
11
- ob_start();
12
- while ( have_posts() && $i < $limit ) {
13
- the_post();
14
- $posts[] = PostMaster::get_post_info(get_the_ID());
15
- $i++;
16
- }
17
- ob_end_clean();
18
- return $posts;
19
- }
20
 
21
- function loop_to_ids($limit = 99999){
22
- $posts = array();
23
- $i = 0;
24
- ob_start();
25
- while ( have_posts() && $i < $limit ) {
26
- the_post();
27
- $posts[] = get_the_ID();
28
- $i++;
29
- }
30
- wp_reset_query();
31
- ob_end_clean();
32
- return $posts;
33
- }
34
 
35
- function loop_to_id(){
36
- if (have_posts()){
37
- the_post();
38
- wp_reset_query();
39
- return get_the_ID();
40
- }
41
- return false;
42
- }
 
 
 
 
 
 
43
 
44
- function loop_to_post(){
45
- if (have_posts()){
46
- the_post();
47
- return PostMaster::get_post_info(get_the_ID());
48
- }
49
- return false;
50
- }
 
 
51
 
52
- function get_post_id_by_name($post_name){
53
- global $wpdb;
54
- $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name'";
55
- $result = $wpdb->get_row($query);
56
- return $result->ID;
57
- }
 
 
58
 
59
- function get_post_id_by_name_and_type($post_name, $post_type){
60
- global $wpdb;
61
- $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type'";
62
- $result = $wpdb->get_row($query);
63
- return $result->ID;
64
- }
 
65
 
66
- function check_post_id($pid){
67
- if (is_numeric($pid) && $pid === 0){
68
- $pid = get_the_ID();
69
- return $pid;
70
- }
71
- if (!is_numeric($pid) && is_string($pid)){
72
- $pid = self::get_post_id_by_name($pid);
73
- return $pid;
74
- }
75
- if (!$pid){
76
- return;
77
- }
78
- return $pid;
79
- }
80
 
81
- function get_posts_by_meta($key, $value){
82
- global $wpdb;
83
- $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value'";
84
- $results = $wpdb->get_results($query);
85
- $pids = array();
86
- foreach($results as $result){
87
- if (get_post($result->post_id)){
88
- $pids[] = $result->post_id;
89
- }
90
- }
91
- if (count($pids)){
92
- return $pids;
93
- }
94
- return 0;
95
- }
96
 
97
- function get_post_by_meta($key, $value){
98
- global $wpdb;
99
- $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value' ORDER BY post_id";
100
- $result = $wpdb->get_row($query);
101
- if ($result && get_post($result->post_id)){
102
- return $result->post_id;
103
- }
104
- return 0;
105
- }
 
 
 
 
 
 
 
106
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- function get_posts_info($query, $extras = null){
109
- if (is_array($query) && !WPHelper::is_array_assoc($query)){
110
- $results = $query;
111
- } else {
112
- $results = get_posts($query);
113
- }
114
- $ret = array();
115
- foreach($results as $result){
116
- $ret[] = self::get_post_info($result, $extras);
117
- }
118
- return $ret;
119
- }
120
 
121
- function get_teaser($pid){
122
- $post = self::prepare_post_info($pid);
123
- $pos = strpos($post->post_content, '<!--more');
124
- if ($pos > 0){
125
- return trim(substr($post->post_content, 0, $pos));
126
- }
127
- }
 
 
 
 
 
 
128
 
129
- function prepare_post_info($pid){
130
- if (is_string($pid) || is_numeric($pid) || !$pid->post_title){
131
- $pid = self::check_post_id($pid);
132
- return get_post($pid);
133
- } else {
134
- return $pid;
135
- }
136
- throw new Exception('Could not find post '.$pid);
137
- return false;
138
- }
139
 
140
- function get_post_custom($post, $pid){
141
- $customs = get_post_custom($pid);
142
- foreach($customs as $key => $value){
143
- $v = $value[0];
144
- $post->$key = $v;
145
- if (is_serialized($v)){
146
- if (gettype(unserialize($v)) == 'array'){
147
- $post->$key = unserialize($v);
148
- }
149
- }
150
- }
151
- }
152
 
153
- function get_path($url){
154
- $url_info = parse_url($url);
155
- return $url_info['path'];
156
- }
 
 
 
 
 
 
 
 
 
157
 
158
- function get_post_info($pid, $extras = null){
159
- $post = self::prepare_post_info($pid);
160
- $post->title = $post->post_title;
161
- $post->body = wpautop($post->post_content);
162
- $post->teaser = self::get_teaser($post);
163
- $post->slug = $post->post_name;
164
- $post->custom = get_post_custom($post->ID);
165
- $post->permalink = get_permalink($post->ID);
166
- $post->author = get_userdata($post->post_author);
167
- $post->path = self::get_path($post->permalink);
168
- $post->thumb_src = self::get_post_thumbnail_src($post->ID);
169
- $post->display_date = date(get_option('date_format'), strtotime($post->post_date));
170
- if ($post->_thumbnail_id){
171
- $post->thumb_src = self::get_image_path($post->_thumbnail_id);
172
- }
173
- if ($post->custom){
174
- foreach($post->custom as $key => $value){
175
- $v = $value[0];
176
- $post->$key = $v;
177
- if (is_serialized($v)){
178
- if (gettype(unserialize($v)) == 'array'){
179
- $post->$key = unserialize($v);
180
- }
181
- }
182
- }
183
- }
184
- $post->status = $post->post_status;
185
- if (is_array($extras)){
186
- foreach($extras as $extra){
187
- if ($extra == 'post_type_info'){
188
- $post->post_type_info = get_post_type_object($post->post_type);
189
- }
190
- if ($extra == 'children'){
191
- $post->children = get_children('post_parent='.$post->ID.'&post_type='.$post->post_type);
192
- }
193
- if ($extra == 'terms'){
194
- $post->terms = self::get_post_terms($post->ID);
195
- }
196
- }
197
- }
198
- return $post;
199
- }
200
 
201
- function get_post_terms($pid){
202
- global $wpdb;
203
- $query = "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_ID = '$pid'";
204
- $results = $wpdb->get_col($query);
205
- $taxes = array();
206
- foreach($results as $result){
207
- $query = "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$result'";
208
- $tax = $wpdb->get_row($query);
209
- if (!$taxes[$tax->taxonomy]){
210
- $taxes[$tax->taxonomy] = array();
211
- }
212
- $taxes[$tax->taxonomy][] = get_term($tax->term_id, $tax->taxonomy);
213
- }
214
- return $taxes;
215
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
- function set_post_parent($child_ids, $parent_id){
220
- if (!is_array($child_ids)){
221
- $child_ids = array($child_ids);
222
- }
223
- global $wpdb;
224
- foreach($child_ids as $child_id){
225
- $wpdb->query("UPDATE $wpdb->posts SET post_parent = $parent_id WHERE ID = $child_id");
226
- }
227
- }
228
 
229
- function update_post_meta($pids, $field, $value){
230
- if (!is_array($pids)){
231
- $pids = array($pids);
232
- }
233
- foreach($pids as $pid){
234
- update_post_meta($pid, $field, $value);
235
- }
236
- }
237
-
 
238
 
239
-
 
 
 
 
 
 
 
 
240
 
241
- function get_related_posts_on_field($arr, $field){
242
- $ret = array();
243
- $upload_info = wp_upload_dir();
244
- $upload_path = str_replace($_SERVER['HTTP_HOST'], '', $upload_info['url']);
245
- $upload_path = str_replace($upload_info['subdir'], '', $upload_path);
246
- $upload_path = str_replace('http://', '', $upload_path);
247
- $upload_path .= '/';
248
- foreach($arr as $post){
249
- $related_id = $post->$field;
250
- if ($related_id){
251
- $related = self::get_post_info($related_id);
252
- $post->$field = $related;
253
- $post->$field->path = $upload_path . $post->$field->_wp_attached_file;
254
- $ret[] = $post;
255
- }
256
- }
257
- return $ret;
258
- }
259
 
260
- function get_image_path($iid){
261
- $size = 'full';
262
- $src = wp_get_attachment_image_src($iid, $size);
263
- $src = $src[0];
264
- return self::get_path($src);
265
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
- function get_post_thumbnail_src($pid){
268
- $src = false;
269
- if (function_exists('get_post_thumbnail_id')){
270
- $tid = get_post_thumbnail_id($pid);
271
- $size = 'full';
272
- $src = wp_get_attachment_image_src($tid, $size);
273
- $src = $src[0];
274
- }
275
- return $src;
276
- }
277
-
278
- }
279
- ?>
 
 
 
 
 
 
 
 
1
  <?php
2
+ class PostMaster {
3
 
4
+ function __construct(){
5
 
6
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ function loop_to_array($limit = 99999)
9
+ {
10
+ $posts = array();
11
+ $i = 0;
12
+ ob_start();
13
+ while (have_posts() && $i < $limit) {
14
+ the_post();
15
+ $posts[] = PostMaster::get_post_info(get_the_ID());
16
+ $i++;
17
+ }
18
+ ob_end_clean();
19
+ return $posts;
20
+ }
21
 
22
+ function loop_to_ids($limit = 99999)
23
+ {
24
+ $posts = array();
25
+ $i = 0;
26
+ ob_start();
27
+ while (have_posts() && $i < $limit) {
28
+ the_post();
29
+ $posts[] = get_the_ID();
30
+ $i++;
31
+ }
32
+ wp_reset_query();
33
+ ob_end_clean();
34
+ return $posts;
35
+ }
36
 
37
+ function loop_to_id()
38
+ {
39
+ if (have_posts()) {
40
+ the_post();
41
+ wp_reset_query();
42
+ return get_the_ID();
43
+ }
44
+ return false;
45
+ }
46
 
47
+ function loop_to_post()
48
+ {
49
+ if (have_posts()) {
50
+ the_post();
51
+ return PostMaster::get_post_info(get_the_ID());
52
+ }
53
+ return false;
54
+ }
55
 
56
+ function get_post_id_by_name($post_name)
57
+ {
58
+ global $wpdb;
59
+ $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name'";
60
+ $result = $wpdb->get_row($query);
61
+ return $result->ID;
62
+ }
63
 
64
+ function get_post_id_by_name_and_type($post_name, $post_type)
65
+ {
66
+ global $wpdb;
67
+ $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type'";
68
+ $result = $wpdb->get_row($query);
69
+ return $result->ID;
70
+ }
 
 
 
 
 
 
 
71
 
72
+ function check_post_id($pid)
73
+ {
74
+ if (is_numeric($pid) && $pid === 0) {
75
+ $pid = get_the_ID();
76
+ return $pid;
77
+ }
78
+ if (!is_numeric($pid) && is_string($pid)) {
79
+ $pid = self::get_post_id_by_name($pid);
80
+ return $pid;
81
+ }
82
+ if (!$pid) {
83
+ return null;
84
+ }
85
+ return $pid;
86
+ }
87
 
88
+ function get_posts_by_meta($key, $value)
89
+ {
90
+ global $wpdb;
91
+ $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value'";
92
+ $results = $wpdb->get_results($query);
93
+ $pids = array();
94
+ foreach ($results as $result) {
95
+ if (get_post($result->post_id)) {
96
+ $pids[] = $result->post_id;
97
+ }
98
+ }
99
+ if (count($pids)) {
100
+ return $pids;
101
+ }
102
+ return 0;
103
+ }
104
 
105
+ function get_post_by_meta($key, $value)
106
+ {
107
+ global $wpdb;
108
+ $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value' ORDER BY post_id";
109
+ $result = $wpdb->get_row($query);
110
+ if ($result && get_post($result->post_id)) {
111
+ return $result->post_id;
112
+ }
113
+ return 0;
114
+ }
115
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
+ function get_posts_info($query, $extras = null)
118
+ {
119
+ if (is_array($query) && !WPHelper::is_array_assoc($query)) {
120
+ $results = $query;
121
+ } else {
122
+ $results = get_posts($query);
123
+ }
124
+ $ret = array();
125
+ foreach ($results as $result) {
126
+ $ret[] = self::get_post_info($result, $extras);
127
+ }
128
+ return $ret;
129
+ }
130
 
131
+ function get_teaser($pid)
132
+ {
133
+ $post = self::prepare_post_info($pid);
134
+ $pos = strpos($post->post_content, '<!--more');
135
+ if ($pos > 0) {
136
+ return trim(substr($post->post_content, 0, $pos));
137
+ }
138
+ return null;
139
+ }
 
140
 
141
+ function prepare_post_info($pid)
142
+ {
143
+ if (is_string($pid) || is_numeric($pid) || !$pid->post_title) {
144
+ $pid = self::check_post_id($pid);
145
+ if (!$pid) {
146
+ throw new Exception('Could not find post ' . $pid);
147
+ }
148
+ return get_post($pid);
149
+ } else {
150
+ return $pid;
151
+ }
152
+ }
153
 
154
+ function get_post_custom($post, $pid)
155
+ {
156
+ $customs = get_post_custom($pid);
157
+ foreach ($customs as $key => $value) {
158
+ $v = $value[0];
159
+ $post->$key = $v;
160
+ if (is_serialized($v)) {
161
+ if (gettype(unserialize($v)) == 'array') {
162
+ $post->$key = unserialize($v);
163
+ }
164
+ }
165
+ }
166
+ }
167
 
168
+ function get_path($url)
169
+ {
170
+ $url_info = parse_url($url);
171
+ return $url_info['path'];
172
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
+ function get_post_info($pid, $extras = null)
175
+ {
176
+ $post = self::prepare_post_info($pid);
177
+ $post->title = $post->post_title;
178
+ $post->body = wpautop($post->post_content);
179
+ $post->teaser = self::get_teaser($post);
180
+ $post->slug = $post->post_name;
181
+ $post->custom = get_post_custom($post->ID);
182
+ $post->permalink = get_permalink($post->ID);
183
+ $post->author = get_userdata($post->post_author);
184
+ $post->path = self::get_path($post->permalink);
185
+ $post->thumb_src = self::get_post_thumbnail_src($post->ID);
186
+ $post->display_date = date(get_option('date_format'), strtotime($post->post_date));
187
+ if ($post->_thumbnail_id) {
188
+ $post->thumb_src = self::get_image_path($post->_thumbnail_id);
189
+ }
190
+ if ($post->custom) {
191
+ foreach ($post->custom as $key => $value) {
192
+ $v = $value[0];
193
+ $post->$key = $v;
194
+ if (is_serialized($v)) {
195
+ if (gettype(unserialize($v)) == 'array') {
196
+ $post->$key = unserialize($v);
197
+ }
198
+ }
199
+ }
200
+ }
201
+ $post->status = $post->post_status;
202
+ if (is_array($extras)) {
203
+ foreach ($extras as $extra) {
204
+ if ($extra == 'post_type_info') {
205
+ $post->post_type_info = get_post_type_object($post->post_type);
206
+ }
207
+ if ($extra == 'children') {
208
+ $post->children = get_children('post_parent=' . $post->ID . '&post_type=' . $post->post_type);
209
+ }
210
+ if ($extra == 'terms') {
211
+ $post->terms = self::get_post_terms($post->ID);
212
+ }
213
+ }
214
+ }
215
+ return $post;
216
+ }
217
 
218
+ function get_post_terms($pid)
219
+ {
220
+ global $wpdb;
221
+ $query = "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_ID = '$pid'";
222
+ $results = $wpdb->get_col($query);
223
+ $taxes = array();
224
+ foreach ($results as $result) {
225
+ $query = "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$result'";
226
+ $tax = $wpdb->get_row($query);
227
+ if (!$taxes[$tax->taxonomy]) {
228
+ $taxes[$tax->taxonomy] = array();
229
+ }
230
+ $taxes[$tax->taxonomy][] = get_term($tax->term_id, $tax->taxonomy);
231
+ }
232
+ return $taxes;
233
+ }
234
 
 
 
 
 
 
 
 
 
 
235
 
236
+ function set_post_parent($child_ids, $parent_id)
237
+ {
238
+ if (!is_array($child_ids)) {
239
+ $child_ids = array($child_ids);
240
+ }
241
+ global $wpdb;
242
+ foreach ($child_ids as $child_id) {
243
+ $wpdb->query("UPDATE $wpdb->posts SET post_parent = $parent_id WHERE ID = $child_id");
244
+ }
245
+ }
246
 
247
+ function update_post_meta($pids, $field, $value)
248
+ {
249
+ if (!is_array($pids)) {
250
+ $pids = array($pids);
251
+ }
252
+ foreach ($pids as $pid) {
253
+ update_post_meta($pid, $field, $value);
254
+ }
255
+ }
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
+ function get_related_posts_on_field($arr, $field)
259
+ {
260
+ $ret = array();
261
+ $upload_info = wp_upload_dir();
262
+ $upload_path = str_replace($_SERVER['HTTP_HOST'], '', $upload_info['url']);
263
+ $upload_path = str_replace($upload_info['subdir'], '', $upload_path);
264
+ $upload_path = str_replace('http://', '', $upload_path);
265
+ $upload_path .= '/';
266
+ foreach ($arr as $post) {
267
+ $related_id = $post->$field;
268
+ if ($related_id) {
269
+ $related = self::get_post_info($related_id);
270
+ $post->$field = $related;
271
+ $post->$field->path = $upload_path . $post->$field->_wp_attached_file;
272
+ $ret[] = $post;
273
+ }
274
+ }
275
+ return $ret;
276
+ }
277
 
278
+ public static function get_image_path($iid)
279
+ {
280
+ $size = 'full';
281
+ $src = wp_get_attachment_image_src($iid, $size);
282
+ $src = $src[0];
283
+ return self::get_path($src);
284
+ }
285
+
286
+ function get_post_thumbnail_src($pid)
287
+ {
288
+ $src = false;
289
+ if (function_exists('get_post_thumbnail_id')) {
290
+ $tid = get_post_thumbnail_id($pid);
291
+ $size = 'full';
292
+ $src = wp_get_attachment_image_src($tid, $size);
293
+ $src = $src[0];
294
+ }
295
+ return $src;
296
+ }
297
+
298
+ }
functions/functions-twig.php CHANGED
@@ -1,456 +1,515 @@
1
- <?php
2
-
3
- class TimberTwig {
4
-
5
- function __construct(){
6
- add_action('twig_apply_filters', array(&$this, 'add_twig_filters'));
7
- }
8
-
9
- function add_twig_filters($twig){
10
- $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
11
- $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
12
- $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
13
- $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
14
- $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
15
- $twig->addFilter('docs', new Twig_Filter_function('twig_object_docs'));
16
-
17
- $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
18
- $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
19
- $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
20
- $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
21
- $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
22
- $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
23
-
24
- $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
25
- $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
26
- $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
27
-
28
- $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
29
- $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
30
- $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
31
- $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
32
- return $twig;
33
- }
34
-
35
- function add_dir_name_to_locations($locs){
36
- $locs = array_filter($locs);
37
- foreach($locs as &$loc){
38
- $loc = trailingslashit($loc).trailingslashit(self::$dir_name);
39
- }
40
- return $locs;
41
- }
42
-
43
- function template_exists($file, $dirs){
44
- if (is_string($dirs)){
45
- $dirs = array($dirs);
46
- }
47
- foreach($dirs as $dir){
48
- $look_for = trailingslashit($dir).trailingslashit(self::$dir_name).$file;
49
- if (file_exists($look_for)){
50
- return true;
51
- }
52
- }
53
- return false;
54
- }
55
-
56
- function twig_choose_template($filenames, $dirs){
57
- if(is_array($filenames)){
58
- /* its an array so we have to figure out which one the dev wants */
59
- foreach($filenames as $filename){
60
- if (self::template_exists($filename, $dirs)){
61
- return $filename;
62
- }
63
- }
64
- return false;
65
- } else {
66
- /* its a single, but we still need to figure out if it exists, default to index.html */
67
- // if (!twig_template_exists($filenames, $dirs)){
68
- // $filenames = 'index.html';
69
- // }
70
- }
71
- return $filenames;
72
- }
73
-
74
-
75
- function get_twig($loader){
76
-
77
- $loader_loc = TIMBER_LOC.'/Twig/lib/Twig/Autoloader.php';
78
- require_once($loader_loc);
79
- $reg = Twig_Autoloader::register();
80
-
81
- $params = array('debug' => false, 'autoescape' => false);
82
- if (Timber::$cache){
83
- $params['cache'] = TIMBER_LOC.'/twig-cache';
84
- }
85
- $twig = new Twig_Environment($loader, $params);
86
- $twig->addExtension(new Twig_Extension_Debug());
87
- $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
88
- $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
89
- $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
90
- $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
91
- $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
92
- $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
93
- $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
94
- $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
95
- $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
96
- $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
97
- $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
98
-
99
- $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
100
- $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
101
- $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
102
-
103
- $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
104
- $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
105
- $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
106
- $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
107
-
108
- $twig = apply_filters('get_twig', $twig);
109
- return $twig;
110
- }
111
- }
112
-
113
- function twig_shortcodes($text){
114
- return do_shortcode($text);
115
- //apply_filters('the_content', ($text));
116
- }
117
-
118
- function twig_get_class($this){
119
- return get_class($this);
120
- }
121
-
122
- function twig_get_type($this){
123
- return gettype($this);
124
- }
125
-
126
- function wp_resize_external($src, $w, $h){
127
- $upload = wp_upload_dir();
128
- $dir = $upload['path'];
129
- $file = parse_url($src);
130
- $path_parts = pathinfo($file['path']);
131
- $basename = $path_parts['filename'];
132
- $newbase = $basename.'-r-'.$w.'x'.$h;
133
- $ext = $path_parts['extension'];
134
-
135
- $new_root_path = $dir.'/'.$newbase.'.'.$ext;
136
-
137
- $new_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $new_root_path);
138
- if (strpos($new_path, '/') != 0)
139
- {
140
- $new_path = '/'.$new_path;
141
- }
142
- $ret = array('new_root_path' => $new_root_path, 'old_root_path' => $dir.'/'.$basename.'.'.$ext, 'new_path' => $new_path);
143
-
144
- if (file_exists($new_root_path)){
145
- return $ret;
146
- }
147
- $image = WPHelper::sideload_image($src);
148
- return $ret;
149
- }
150
-
151
- function hexrgb($hexstr) {
152
- $int = hexdec($hexstr);
153
-
154
- return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
155
- }
156
-
157
- function wp_resize_letterbox($src, $w, $h, $color = '#000000'){
158
- $old_file = WPHelper::get_full_path($src);
159
- $new_file = WPHelper::get_letterbox_file_path($src, $w, $h);
160
- $new_file_rel = WPHelper::get_letterbox_file_rel($src, $w, $h);
161
- $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
162
- if (file_exists($new_file_boxed)){
163
- $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
164
- return $new_file_rel;
165
- }
166
-
167
- $bg = imagecreatetruecolor($w, $h);
168
- $c = hexrgb($color);
169
-
170
- $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
171
- imagefill($bg, 0, 0, $white);
172
-
173
- $image = wp_get_image_editor($old_file);
174
- if ( ! is_wp_error( $image ) ) {
175
- $current_size = $image->get_size();
176
- $ow = $current_size['width'];
177
- $oh = $current_size['height'];
178
- $new_aspect = $w/$h;
179
- $old_aspect = $ow/$oh;
180
- if ($new_aspect > $old_aspect){
181
- //taller than goal
182
- $h_scale = $h / $oh;
183
- $owt = $ow * $h_scale;
184
- $y = 0;
185
- $x = $w/2 - $owt/2;
186
- $oht = $h;
187
- $image->crop(0, 0, $ow, $oh, $owt, $oht);
188
- } else {
189
- $w_scale = $w / $ow;
190
- $oht = $oh * $w_scale;
191
- $x = 0;
192
- $y = $h/2 - $oht/2;
193
- $owt = $w;
194
- $image->crop(0, 0, $ow, $oh, $owt, $oht);
195
- }
196
- $image->save($new_file);
197
- $func = 'imagecreatefromjpeg';
198
- $ext = pathinfo($new_file, PATHINFO_EXTENSION);
199
- if ($ext == 'gif'){
200
- $func = 'imagecreatefromgif';
201
- } else if ($ext == 'png'){
202
- $func = 'imagecreatefrompng';
203
- }
204
- $image = $func($new_file);
205
- imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
206
- $new_file = str_replace('-lb-', '-lbox-', $new_file);
207
- imagejpeg($bg, $new_file);
208
- return WPHelper::get_rel_path($new_file);
209
- }
210
- }
211
-
212
- function wp_resize($src, $w, $h = 0){
213
- $root = $_SERVER['DOCUMENT_ROOT'];
214
- if (strstr($src, 'http')){
215
- //Its a URL so we need to fetch it
216
- $external = wp_resize_external($src, $w, $h);
217
- $old_root_path = $external['old_root_path'];
218
- $new_root_path = $external['new_root_path'];
219
- $new_path = $external['new_path'];
220
- } else {
221
- //oh good, its in the uploads folder!
222
- $path_parts = pathinfo($src);
223
- $basename = $path_parts['filename'];
224
- $ext = $path_parts['extension'];
225
- $dir = $path_parts['dirname'];
226
- $newbase = $basename.'-r-'.$w.'x'.$h;
227
- $new_path = $dir.'/'.$newbase.'.'.$ext;
228
- $new_root_path = $root.$new_path;
229
- $old_root_path = $root.$src;
230
-
231
- $old_root_path = str_replace('//', '/', $old_root_path);
232
- $new_root_path = str_replace('//', '/', $new_root_path);
233
-
234
- if (file_exists($new_root_path)){
235
- return $new_path;
236
- }
237
- }
238
- $image = wp_get_image_editor($old_root_path);
239
- if ( ! is_wp_error( $image ) ) {
240
- $current_size = $image->get_size();
241
- $ow = $current_size['width'];
242
- $oh = $current_size['height'];
243
- if ($h){
244
- $new_aspect = $w/$h;
245
- $old_aspect = $ow/$oh;
246
-
247
- if ($new_aspect > $old_aspect){
248
- //cropping a vertical photo horitzonally
249
- $oht = $ow/$new_aspect;
250
- $oy = ($oh - $oht) / 6;
251
- $image->crop(0, $oy, $ow, $oht, $w, $h);
252
- } else {
253
- $owt = $oh * $new_aspect;
254
- $ox = $ow/2 - $owt/2;
255
- $image->crop($ox, 0, $owt, $oh, $w, $h);
256
- }
257
- } else {
258
- $image->resize($w, $w);
259
- }
260
- // $image->
261
- $image->save($new_root_path);
262
- return $new_path;
263
- } else {
264
- return $src;
265
- }
266
- return $src;
267
- }
268
-
269
- function twig_twitterify($ret) {
270
- $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
271
- $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
272
- $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
273
- $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
274
- $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
275
- $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
276
- $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
277
- return $ret;
278
- }
279
-
280
- function twig_time_ago($from, $to = null) {
281
- $to = (($to === null) ? (time()) : ($to));
282
- $to = ((is_int($to)) ? ($to) : (strtotime($to)));
283
- $from = ((is_int($from)) ? ($from) : (strtotime($from)));
284
-
285
- $units = array(
286
- "year" => 29030400, // seconds in a year (12 months)
287
- "month" => 2419200, // seconds in a month (4 weeks)
288
- "week" => 604800, // seconds in a week (7 days)
289
- "day" => 86400, // seconds in a day (24 hours)
290
- "hour" => 3600, // seconds in an hour (60 minutes)
291
- "minute" => 60, // seconds in a minute (60 seconds)
292
- "second" => 1 // 1 second
293
- );
294
-
295
- $diff = abs($from - $to);
296
- $suffix = (($from > $to) ? ("from now") : ("ago"));
297
- $output = '';
298
- foreach($units as $unit => $mult) {
299
- if ($diff >= $mult) {
300
- $and = (($mult != 1) ? ("") : ("and "));
301
- $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));
302
- $diff -= intval($diff / $mult) * $mult;
303
- break;
304
- }
305
- }
306
- $output .= " ".$suffix;
307
- $output = substr($output, strlen(", "));
308
- return $output;
309
  }
 
 
310
 
311
- function twig_wp_sidebar($arg){
312
- get_sidebar($arg);
313
- }
314
-
315
- function twig_wp_title(){
316
- return wp_title('|', false, 'right');
317
- }
318
-
319
- function twig_body_class($body_classes){
320
- ob_start();
321
- if (is_array($body_classes)){
322
- $body_classes = explode(' ', $body_classes);
323
- }
324
- body_class($body_classes);
325
- $return = ob_get_contents();
326
- ob_end_clean();
327
- return $return;
328
- }
329
-
330
- function render_twig_string($string, $data = array()){
331
- $loader = new Twig_Loader_String();
332
- $twig = new Twig_Environment($loader);
333
- return $twig->render($string, $data);
334
- }
335
-
336
- function get_calling_script_dir($backtrace){
337
- $caller = $backtrace[0]['file'];
338
- $pathinfo = pathinfo($caller);
339
- $dir = $pathinfo['dirname'];
340
- return $dir.'/';
341
- }
342
-
343
- //deprecated
344
- function render_twig($filenames, $data = array(), $echo = true){
345
- $caller = Timber::get_calling_script_dir();
346
- $loader = new TimberLoader($caller);
347
- $file = $loader->choose_template($filenames);
348
- $output = '';
349
- if (strlen($file)){
350
- $output = $loader->render($file, $data);
351
- }
352
- if ($echo){
353
- echo $output;
354
- }
355
- return $output;
356
- }
357
-
358
- function twig_get_src_from_attachment_id($aid){
359
- $src = PostMaster::get_image_path($aid);
360
- return $src;
361
- }
362
-
363
- function twig_get_path($url){
364
- $url = parse_url($url);
365
- return $url['path'];
366
- }
367
-
368
- function twig_make_excerpt($text, $length = 55){
369
- return wp_trim_words($text, $length);
370
- }
371
-
372
- function twig_invoke($method, $obj){
373
- $product = '';
374
- $totalParams = $method->getNumberOfParameters();
375
- $reqParams = $method->getNumberOfRequiredParameters();
376
- if (!$method->getNumberOfParameters()){
377
- //zero parameters, easy street
378
- $product = $method->invoke($obj);
379
- //$product = $method->getName();
380
- } else if ($method->getNumberOfRequiredParameters()){
381
- //there are required parametres
382
- //$product = $method->getName();
383
- } else if ($totalParams && !$reqParams) {
384
- //all params are optional
385
- $pass = array();
386
- $product = $pass;
387
- if ($method->getName() == 'get_preview'){
388
- $function = $method->getName();
389
- // try {
390
- // $product = $obj->$function();
391
- // } catch($e){
392
- // $product = 'error with '.$method->getName();
393
- // }
394
- }
395
-
396
- //$product = $method->invokeArgs($obj, $pass);
397
- //$product = $args;
398
- } else {
399
- $product = '?????';
400
- }
401
- return $product;
402
- }
403
-
404
- function twig_print_r($arr){
405
- return print_r($arr, true);
406
- }
407
-
408
- function twig_print_a($arr){
409
- return '<pre>'.twig_object_docs($arr, true).'</pre>';
410
- }
411
-
412
- function twig_object_docs($obj){
413
- $reflector = new ReflectionClass($obj);
414
- $methods = $reflector->getMethods();
415
- $rets = array();
416
- $rep = $reflector->getProperty('representation')->getValue();
417
- foreach($methods as $method){
418
- if ($method->isPublic()){
419
- $comments = $method->getDocComment();
420
- $comments = str_replace('/**', '', $comments);
421
- //$comments = preg_replace('(\/)(\*)(\*)\r', '', $comments);
422
- $info = new stdClass();
423
- $info->comments = $comments;
424
- $info->returns = twig_invoke($method, $obj);
425
- $info->params = $method->getParameters();
426
- //if (strlen($comments) && !strstr($comments, '@nodoc')){
427
- //$rets[$rep.'.'.$method->name] = $comments;
428
- //$rets[$rep.'.'.$method->name] = $info->returns;
429
- $rets[$method->name] = $info->returns;
430
- //}
431
- }
432
- }
433
- foreach($obj as $key=>$value){
434
- $rets[$key] = $value;
435
- }
436
- ksort($rets);
437
-
438
- return '<pre>'.(print_r($rets, true)).'</pre>';
439
- }
440
-
441
- function twig_img_to_jpg($src){
442
- $output = str_replace('.png', '.jpg', $src);
443
- if (file_exists($_SERVER['DOCUMENT_ROOT'].$output)){
444
- return $output;
445
- }
446
- $image = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$src);
447
- $w = imagesx($image);
448
- $h = imagesy($image);
449
- $bg = imagecreatetruecolor($w, $h);
450
- imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
451
- imagealphablending($bg, TRUE);
452
- imagecopy($bg, $image, 0, 0, 0, 0, $w, $h);
453
- imagejpeg($bg, '/'.$_SERVER['DOCUMENT_ROOT'].$output, 90);
454
- imagedestroy($image);
455
- return $output;
456
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class TimberTwig
4
+ {
5
+
6
+ function __construct()
7
+ {
8
+ add_action('twig_apply_filters', array(&$this, 'add_twig_filters'));
9
+ }
10
+
11
+ /**
12
+ * @param Twig_Environment $twig
13
+ * @return Twig_Environment
14
+ */
15
+ function add_twig_filters($twig)
16
+ {
17
+ $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
18
+ $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
19
+ $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
20
+ $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
21
+ $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
22
+ $twig->addFilter('docs', new Twig_Filter_function('twig_object_docs'));
23
+
24
+ $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
25
+ $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
26
+ $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
27
+ $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
28
+ $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
29
+ $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
30
+ $twig->addFilter('function', new Twig_Filter_Function(array(&$this, 'exec_function')));
31
+
32
+ $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
33
+ $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
34
+ $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
35
+ $twig->addFilter('pretags', new Twig_Filter_Function(array(&$this, 'twig_pretags')));
36
+ $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
37
+ $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
38
+ $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
39
+ $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
40
+
41
+ $twig->addFunction('bloginfo', new Twig_SimpleFunction('bloginfo', function($show = '', $filter = 'raw'){
42
+ return get_bloginfo($show, $filter);
43
+ }));
44
+ $twig->addFunction('__', new Twig_SimpleFunction('__', function($text, $domain = 'default'){
45
+ return __($text, $domain);
46
+ }));
47
+
48
+ return $twig;
49
+ }
50
+
51
+ function exec_function($function_name){
52
+ return call_user_func(trim($function_name));
53
+ }
54
+
55
+ function twig_pretags( $content ) {
56
+ return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , array(&$this, 'convert_pre_entities'), $content );
57
+ }
58
+ function convert_pre_entities( $matches ) {
59
+ return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
60
+ }
61
+
62
+
63
+ function add_dir_name_to_locations($locs)
64
+ {
65
+ $locs = array_filter($locs);
66
+ foreach ($locs as &$loc) {
67
+ $loc = trailingslashit($loc) . trailingslashit(self::$dir_name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  }
69
+ return $locs;
70
+ }
71
 
72
+ function template_exists($file, $dirs)
73
+ {
74
+ if (is_string($dirs)) {
75
+ $dirs = array($dirs);
76
+ }
77
+ foreach ($dirs as $dir) {
78
+ $look_for = trailingslashit($dir) . trailingslashit(self::$dir_name) . $file;
79
+ if (file_exists($look_for)) {
80
+ return true;
81
+ }
82
+ }
83
+ return false;
84
+ }
85
+
86
+ function twig_choose_template($filenames, $dirs)
87
+ {
88
+ if (is_array($filenames)) {
89
+ /* its an array so we have to figure out which one the dev wants */
90
+ foreach ($filenames as $filename) {
91
+ if (self::template_exists($filename, $dirs)) {
92
+ return $filename;
93
+ }
94
+ }
95
+ return false;
96
+ } else {
97
+ /* its a single, but we still need to figure out if it exists, default to index.html */
98
+ // if (!twig_template_exists($filenames, $dirs)){
99
+ // $filenames = 'index.html';
100
+ // }
101
+ }
102
+ return $filenames;
103
+ }
104
+
105
+
106
+ function get_twig($loader)
107
+ {
108
+
109
+ $loader_loc = TIMBER_LOC . '/Twig/lib/Twig/Autoloader.php';
110
+ require_once($loader_loc);
111
+ Twig_Autoloader::register();
112
+
113
+ $params = array('debug' => WP_DEBUG, 'autoescape' => false);
114
+ if (Timber::$cache) {
115
+ $params['cache'] = TIMBER_LOC . '/twig-cache';
116
+ }
117
+ $twig = new Twig_Environment($loader, $params);
118
+ $twig->addExtension(new Twig_Extension_Debug());
119
+ $twig->addFilter('resize', new Twig_Filter_Function('wp_resize'));
120
+ $twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
121
+ $twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
122
+ $twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
123
+ $twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
124
+ $twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
125
+ $twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
126
+ $twig->addFilter('tojpg', new Twig_Filter_Function('twig_img_to_jpg'));
127
+ $twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
128
+ $twig->addFilter('twitterify', new Twig_Filter_Function('twig_twitterify'));
129
+ $twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
130
+
131
+ $twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
132
+ $twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
133
+ $twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
134
+
135
+ $twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
136
+ $twig->addFilter('wp_title', new Twig_Filter_Function('twig_wp_title'));
137
+ $twig->addFilter('wp_sidebar', new Twig_Filter_Function('twig_wp_sidebar'));
138
+ $twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
139
+
140
+ $twig = apply_filters('get_twig', $twig);
141
+ return $twig;
142
+ }
143
+ }
144
+
145
+ function twig_shortcodes($text)
146
+ {
147
+ return do_shortcode($text);
148
+ //apply_filters('the_content', ($text));
149
+ }
150
+
151
+ function twig_get_class($this)
152
+ {
153
+ return get_class($this);
154
+ }
155
+
156
+ function twig_get_type($this)
157
+ {
158
+ return gettype($this);
159
+ }
160
+
161
+ function wp_resize_external($src, $w, $h)
162
+ {
163
+ $upload = wp_upload_dir();
164
+ $dir = $upload['path'];
165
+ $file = parse_url($src);
166
+ $path_parts = pathinfo($file['path']);
167
+ $basename = $path_parts['filename'];
168
+ $newbase = $basename . '-r-' . $w . 'x' . $h;
169
+ $ext = $path_parts['extension'];
170
+
171
+ $new_root_path = $dir . '/' . $newbase . '.' . $ext;
172
+
173
+ $new_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $new_root_path);
174
+ if (strpos($new_path, '/') != 0) {
175
+ $new_path = '/' . $new_path;
176
+ }
177
+ $ret = array('new_root_path' => $new_root_path, 'old_root_path' => $dir . '/' . $basename . '.' . $ext, 'new_path' => $new_path);
178
+
179
+ if (file_exists($new_root_path)) {
180
+ return $ret;
181
+ }
182
+ $image = WPHelper::sideload_image($src);
183
+ return $ret;
184
+ }
185
+
186
+ function hexrgb($hexstr)
187
+ {
188
+ $int = hexdec($hexstr);
189
+
190
+ return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int);
191
+ }
192
+
193
+ function wp_resize_letterbox($src, $w, $h, $color = '#000000')
194
+ {
195
+ $old_file = WPHelper::get_full_path($src);
196
+ $new_file = WPHelper::get_letterbox_file_path($src, $w, $h);
197
+ $new_file_rel = WPHelper::get_letterbox_file_rel($src, $w, $h);
198
+ $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
199
+ if (file_exists($new_file_boxed)) {
200
+ $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
201
+ return $new_file_rel;
202
+ }
203
+
204
+ $bg = imagecreatetruecolor($w, $h);
205
+ $c = hexrgb($color);
206
+
207
+ $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
208
+ imagefill($bg, 0, 0, $white);
209
+
210
+ $image = wp_get_image_editor($old_file);
211
+ if (!is_wp_error($image)) {
212
+ $current_size = $image->get_size();
213
+ $ow = $current_size['width'];
214
+ $oh = $current_size['height'];
215
+ $new_aspect = $w / $h;
216
+ $old_aspect = $ow / $oh;
217
+ if ($new_aspect > $old_aspect) {
218
+ //taller than goal
219
+ $h_scale = $h / $oh;
220
+ $owt = $ow * $h_scale;
221
+ $y = 0;
222
+ $x = $w / 2 - $owt / 2;
223
+ $oht = $h;
224
+ $image->crop(0, 0, $ow, $oh, $owt, $oht);
225
+ } else {
226
+ $w_scale = $w / $ow;
227
+ $oht = $oh * $w_scale;
228
+ $x = 0;
229
+ $y = $h / 2 - $oht / 2;
230
+ $owt = $w;
231
+ $image->crop(0, 0, $ow, $oh, $owt, $oht);
232
+ }
233
+ $image->save($new_file);
234
+ $func = 'imagecreatefromjpeg';
235
+ $ext = pathinfo($new_file, PATHINFO_EXTENSION);
236
+ if ($ext == 'gif') {
237
+ $func = 'imagecreatefromgif';
238
+ } else if ($ext == 'png') {
239
+ $func = 'imagecreatefrompng';
240
+ }
241
+ $image = $func($new_file);
242
+ imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
243
+ $new_file = str_replace('-lb-', '-lbox-', $new_file);
244
+ imagejpeg($bg, $new_file);
245
+ return WPHelper::get_rel_path($new_file);
246
+ }
247
+ return null;
248
+ }
249
+
250
+ function wp_resize($src, $w, $h = 0)
251
+ {
252
+ $root = $_SERVER['DOCUMENT_ROOT'];
253
+ if (strstr($src, 'http')) {
254
+ //Its a URL so we need to fetch it
255
+ $external = wp_resize_external($src, $w, $h);
256
+ $old_root_path = $external['old_root_path'];
257
+ $new_root_path = $external['new_root_path'];
258
+ $new_path = $external['new_path'];
259
+ } else {
260
+ //oh good, its in the uploads folder!
261
+ $path_parts = pathinfo($src);
262
+ $basename = $path_parts['filename'];
263
+ $ext = $path_parts['extension'];
264
+ $dir = $path_parts['dirname'];
265
+ $newbase = $basename . '-r-' . $w . 'x' . $h;
266
+ $new_path = $dir . '/' . $newbase . '.' . $ext;
267
+ $new_root_path = $root . $new_path;
268
+ $old_root_path = $root . $src;
269
+
270
+ $old_root_path = str_replace('//', '/', $old_root_path);
271
+ $new_root_path = str_replace('//', '/', $new_root_path);
272
+
273
+ if (file_exists($new_root_path)) {
274
+ return $new_path;
275
+ }
276
+ }
277
+ $image = wp_get_image_editor($old_root_path);
278
+ if (!is_wp_error($image)) {
279
+ $current_size = $image->get_size();
280
+ $ow = $current_size['width'];
281
+ $oh = $current_size['height'];
282
+ if ($h) {
283
+ $new_aspect = $w / $h;
284
+ $old_aspect = $ow / $oh;
285
+
286
+ if ($new_aspect > $old_aspect) {
287
+ //cropping a vertical photo horitzonally
288
+ $oht = $ow / $new_aspect;
289
+ $oy = ($oh - $oht) / 6;
290
+ $image->crop(0, $oy, $ow, $oht, $w, $h);
291
+ } else {
292
+ $owt = $oh * $new_aspect;
293
+ $ox = $ow / 2 - $owt / 2;
294
+ $image->crop($ox, 0, $owt, $oh, $w, $h);
295
+ }
296
+ } else {
297
+ $image->resize($w, $w);
298
+ }
299
+ // $image->
300
+ $image->save($new_root_path);
301
+ return $new_path;
302
+ } else {
303
+ return $src;
304
+ }
305
+ }
306
+
307
+ function twig_twitterify($ret)
308
+ {
309
+ $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
310
+ $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
311
+ $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
312
+ $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
313
+ $ret = preg_replace($pattern, '<a href="mailto:\\1">\\1</a>', $ret);
314
+ $ret = preg_replace("/\B@(\w+)/", " <a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
315
+ $ret = preg_replace("/\B#(\w+)/", " <a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
316
+ return $ret;
317
+ }
318
+
319
+ function twig_time_ago($from, $to = null)
320
+ {
321
+ $to = (($to === null) ? (time()) : ($to));
322
+ $to = ((is_int($to)) ? ($to) : (strtotime($to)));
323
+ $from = ((is_int($from)) ? ($from) : (strtotime($from)));
324
+
325
+ $units = array(
326
+ "year" => 29030400, // seconds in a year (12 months)
327
+ "month" => 2419200, // seconds in a month (4 weeks)
328
+ "week" => 604800, // seconds in a week (7 days)
329
+ "day" => 86400, // seconds in a day (24 hours)
330
+ "hour" => 3600, // seconds in an hour (60 minutes)
331
+ "minute" => 60, // seconds in a minute (60 seconds)
332
+ "second" => 1 // 1 second
333
+ );
334
+
335
+ $diff = abs($from - $to);
336
+ $suffix = (($from > $to) ? ("from now") : ("ago"));
337
+ $output = '';
338
+ foreach ($units as $unit => $mult) {
339
+ if ($diff >= $mult) {
340
+ $and = (($mult != 1) ? ("") : ("and "));
341
+ $output .= ", " . $and . intval($diff / $mult) . " " . $unit . ((intval($diff / $mult) == 1) ? ("") : ("s"));
342
+ $diff -= intval($diff / $mult) * $mult;
343
+ break;
344
+ }
345
+ }
346
+ $output .= " " . $suffix;
347
+ $output = substr($output, strlen(", "));
348
+ return $output;
349
+ }
350
+
351
+ function twig_wp_sidebar($arg)
352
+ {
353
+ get_sidebar($arg);
354
+ }
355
+
356
+ function twig_wp_title()
357
+ {
358
+ return wp_title('|', false, 'right');
359
+ }
360
+
361
+ function twig_body_class($body_classes)
362
+ {
363
+ ob_start();
364
+ if (is_array($body_classes)) {
365
+ $body_classes = explode(' ', $body_classes);
366
+ }
367
+ body_class($body_classes);
368
+ $return = ob_get_contents();
369
+ ob_end_clean();
370
+ return $return;
371
+ }
372
+
373
+ function render_twig_string($string, $data = array())
374
+ {
375
+ $loader = new Twig_Loader_String();
376
+ $twig = new Twig_Environment($loader);
377
+ return $twig->render($string, $data);
378
+ }
379
+
380
+ function get_calling_script_dir($backtrace)
381
+ {
382
+ $caller = $backtrace[0]['file'];
383
+ $pathinfo = pathinfo($caller);
384
+ $dir = $pathinfo['dirname'];
385
+ return $dir . '/';
386
+ }
387
+
388
+ //deprecated
389
+ function render_twig($filenames, $data = array(), $echo = true)
390
+ {
391
+ $caller = Timber::get_calling_script_dir();
392
+ $loader = new TimberLoader($caller);
393
+ $file = $loader->choose_template($filenames);
394
+ $output = '';
395
+ if (strlen($file)) {
396
+ $output = $loader->render($file, $data);
397
+ }
398
+ if ($echo) {
399
+ echo $output;
400
+ }
401
+ return $output;
402
+ }
403
+
404
+ function twig_get_src_from_attachment_id($aid)
405
+ {
406
+ return WPHelper::get_image_path($aid);
407
+ }
408
+
409
+
410
+
411
+ function twig_get_path($url)
412
+ {
413
+ $url = parse_url($url);
414
+ return $url['path'];
415
+ }
416
+
417
+ function twig_make_excerpt($text, $length = 55)
418
+ {
419
+ return wp_trim_words($text, $length);
420
+ }
421
+
422
+ function twig_invoke($method, $obj)
423
+ {
424
+ $product = '';
425
+ $totalParams = $method->getNumberOfParameters();
426
+ $reqParams = $method->getNumberOfRequiredParameters();
427
+ if (!$method->getNumberOfParameters()) {
428
+ //zero parameters, easy street
429
+ $product = $method->invoke($obj);
430
+ //$product = $method->getName();
431
+ } else if ($method->getNumberOfRequiredParameters()) {
432
+ //there are required parametres
433
+ //$product = $method->getName();
434
+ } else if ($totalParams && !$reqParams) {
435
+ //all params are optional
436
+ $pass = array();
437
+ $product = $pass;
438
+ if ($method->getName() == 'get_preview') {
439
+ $function = $method->getName();
440
+ // try {
441
+ // $product = $obj->$function();
442
+ // } catch($e){
443
+ // $product = 'error with '.$method->getName();
444
+ // }
445
+ }
446
+
447
+ //$product = $method->invokeArgs($obj, $pass);
448
+ //$product = $args;
449
+ } else {
450
+ $product = '?????';
451
+ }
452
+ return $product;
453
+ }
454
+
455
+ function twig_print_r($arr)
456
+ {
457
+ return print_r($arr, true);
458
+ }
459
+
460
+ function twig_print_a($arr)
461
+ {
462
+ return '<pre>' . twig_object_docs($arr, true) . '</pre>';
463
+ }
464
+
465
+ function twig_object_docs($obj)
466
+ {
467
+ if (!class_exists(get_class($obj))){
468
+ return false;
469
+ }
470
+ $reflector = new ReflectionClass($obj);
471
+ $methods = $reflector->getMethods();
472
+ $rets = array();
473
+ $rep = $reflector->getProperty('representation')->getValue();
474
+ foreach ($methods as $method) {
475
+ if ($method->isPublic()) {
476
+ $comments = $method->getDocComment();
477
+ $comments = str_replace('/**', '', $comments);
478
+ //$comments = preg_replace('(\/)(\*)(\*)\r', '', $comments);
479
+ $info = new stdClass();
480
+ $info->comments = $comments;
481
+ $info->returns = twig_invoke($method, $obj);
482
+ $info->params = $method->getParameters();
483
+ //if (strlen($comments) && !strstr($comments, '@nodoc')){
484
+ //$rets[$rep.'.'.$method->name] = $comments;
485
+ //$rets[$rep.'.'.$method->name] = $info->returns;
486
+ $rets[$method->name] = $info->returns;
487
+ //}
488
+ }
489
+ }
490
+ foreach ($obj as $key => $value) {
491
+ $rets[$key] = $value;
492
+ }
493
+ ksort($rets);
494
+
495
+ return '<pre>' . (print_r($rets, true)) . '</pre>';
496
+ }
497
+
498
+ function twig_img_to_jpg($src)
499
+ {
500
+ $output = str_replace('.png', '.jpg', $src);
501
+ $oldpath = $_SERVER['DOCUMENT_ROOT'] . $src;
502
+ $newpath = $_SERVER['DOCUMENT_ROOT'] . $output;
503
+ if (file_exists($newpath)) {
504
+ return $output;
505
+ }
506
+ //make it!
507
+ $image = wp_get_image_editor($oldpath);
508
+ if (!is_wp_error($image)){
509
+ $image->save($newpath);
510
+ return $output;
511
+ }
512
+ return $src;
513
+ }
514
+
515
+ new TimberTwig();
functions/functions-word-query.php ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class WordQuery {
4
+
5
+ public function get_posts_multisite($query){
6
+ add_action('posts_clauses', array(&$this, 'query_info'));
7
+ }
8
+
9
+ public function query_info($query){
10
+ echo '----QUERY INFO----';
11
+ print_r($query);
12
+ }
13
+
14
+ public function get_posts_multisite_old($query){
15
+ if (is_string($query)){
16
+ parse_str($query, $query);
17
+ }
18
+ if (!isset($query['blogs'])){
19
+ return get_posts($query);
20
+ }
21
+ $ids = self::get_blog_ids_from_names($query['blogs']);
22
+ $results = array();
23
+ foreach($ids as $id){
24
+ switch_to_blog($id);
25
+ $results[] = get_posts($query);
26
+ }
27
+ return $results;
28
+ }
29
+
30
+ function get_blog_ids_from_names($blog_names){
31
+ if (is_string($blog_names)){
32
+ $blog_names = explode(',', $blog_names);
33
+ }
34
+ $ids = array();
35
+ foreach($blog_names as $blog_name){
36
+ if (is_numeric($blog_name)){
37
+ $ids[] = $blog_name;
38
+ } else (is_string($blog_name)) {
39
+ $ids[] = get_id_from_blogname($blog_name);
40
+ }
41
+ }
42
+ return $ids;
43
+ }
44
+
45
+ }
functions/functions-wp-helper.php CHANGED
@@ -1,375 +1,412 @@
1
  <?php
2
 
3
- class WPHelper {
4
-
5
- function is_array_assoc($arr){
6
- if (!is_array($arr)){
7
- return false;
8
- }
9
- return (bool)count(array_filter(array_keys($arr), 'is_string'));
10
- }
11
-
12
- function ob_function($function, $args = array(null)){
13
- ob_start();
14
- call_user_func_array($function, $args);
15
- //call_user_func($function);
16
- $data = ob_get_contents();
17
- ob_end_clean();
18
- return $data;
19
- }
20
-
21
- function is_url($url){
22
- $url = strtolower($url);
23
- if (strstr('://', $url)){
24
- return true;
25
- }
26
- return false;
27
- }
28
-
29
- function resize_letterbox($src, $w, $h){
30
-
31
- }
32
-
33
- function get_path_base(){
34
- $struc = get_option('permalink_structure');
35
- $struc = explode('/', $struc);
36
- $p = '/';
37
- foreach($struc as $s){
38
- if (!strstr($s, '%') && strlen($s)){
39
- $p .= $s.'/';
40
- }
41
- }
42
- return $p;
43
- }
44
-
45
- function get_full_path($src){
46
- $root = $_SERVER['DOCUMENT_ROOT'];
47
- $old_root_path = $root.$src;
48
- $old_root_path = str_replace('//', '/', $old_root_path);
49
- return $old_root_path;
50
- }
51
-
52
- function get_rel_path($src){
53
- return str_replace($_SERVER['DOCUMENT_ROOT'], '', $src);
54
- }
55
-
56
- function get_letterbox_file_rel($src, $w, $h){
57
- $path_parts = pathinfo($src);
58
- $basename = $path_parts['filename'];
59
- $ext = $path_parts['extension'];
60
- $dir = $path_parts['dirname'];
61
- $newbase = $basename.'-lb-'.$w.'x'.$h;
62
- $new_path = $dir.'/'.$newbase.'.'.$ext;
63
- return $new_path;
64
- }
65
-
66
- function get_letterbox_file_path($src, $w, $h){
67
- $root = $_SERVER['DOCUMENT_ROOT'];
68
- $path_parts = pathinfo($src);
69
- $basename = $path_parts['filename'];
70
- $ext = $path_parts['extension'];
71
- $dir = $path_parts['dirname'];
72
- $newbase = $basename.'-lb-'.$w.'x'.$h;
73
- $new_path = $dir.'/'.$newbase.'.'.$ext;
74
- $new_root_path = $root.$new_path;
75
- $new_root_path = str_replace('//', '/', $new_root_path);
76
- return $new_root_path;
77
- }
78
-
79
- function download_url( $url, $timeout = 300 ) {
80
- //WARNING: The file is not automatically deleted, The script must unlink() the file.
81
- if ( ! $url )
82
- return new WP_Error('http_no_url', __('Invalid URL Provided.'));
83
-
84
- $tmpfname = wp_tempnam($url);
85
- if ( ! $tmpfname )
86
- return new WP_Error('http_no_file', __('Could not create Temporary file.'));
87
-
88
- $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
89
-
90
- if ( is_wp_error( $response ) ) {
91
- unlink( $tmpfname );
92
- return $response;
93
- }
94
-
95
- if ( 200 != wp_remote_retrieve_response_code( $response ) ){
96
- unlink( $tmpfname );
97
- return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
98
- }
99
-
100
- return $tmpfname;
101
- }
102
-
103
- function sideload_image($file){
104
- error_log('sideload_image');
105
- require_once($_SERVER['DOCUMENT_ROOT'].'/wp-admin/includes/file.php');
106
- require_once($_SERVER['DOCUMENT_ROOT'].'/wp-admin/includes/media.php');
107
- if (empty($file)){
108
- error_log('returnning');
109
- return null;
110
- }
111
- // Download file to temp location
112
- $tmp = download_url($file);
113
-
114
- error_log('about to do preg match');
115
- // Set variables for storage
116
- // fix file filename for query strings
117
- preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
118
- $file_array['name'] = basename($matches[0]);
119
- $file_array['tmp_name'] = $tmp;
120
- // If error storing temporarily, unlink
121
- if ( is_wp_error( $tmp ) ) {
122
- error_log('theres an error');
123
- @unlink($file_array['tmp_name']);
124
- $file_array['tmp_name'] = '';
125
- }
126
- error_log('continuing on');
127
- // do the validation and storage stuff
128
- $file = wp_upload_bits($file_array['name'], null, file_get_contents($file_array['tmp_name']));
129
-
130
- return $file;
131
- }
132
-
133
- function osort(&$array, $prop) {
134
- usort($array, function($a, $b) use ($prop) {
135
- return $a->$prop > $b->$prop ? 1 : -1;
136
- });
137
- }
138
-
139
-
140
- function error_log($arg){
141
- if (is_object($arg) || is_array($arg)){
142
- $arg = print_r($arg, true);
143
- }
144
- error_log($arg);
145
- }
146
-
147
- function get_params($i = -1){
148
- $args = explode('/', trim(strtolower($_SERVER['REQUEST_URI'])));
149
- $newargs = array();
150
- foreach($args as $arg){
151
- if (strlen($arg)){
152
- $newargs[] = $arg;
153
- }
154
- }
155
- if ($i > -1){
156
- if (isset($newargs[$i])){
157
- return $newargs[$i];
158
- }
159
- }
160
- return $newargs;
161
- }
162
-
163
- function get_json($url){
164
- $data = self::get_curl($url);
165
- return json_decode($data);
166
- }
167
-
168
- function get_curl($url) {
169
- $ch = curl_init();
170
- curl_setopt($ch,CURLOPT_URL,$url);
171
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
172
- curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
173
- $content = curl_exec($ch);
174
- curl_close($ch);
175
- return $content;
176
- }
177
-
178
- function get_wp_title(){
179
- return wp_title('|', false, 'right');
180
- }
181
-
182
- function force_update_option($option, $value){
183
- global $wpdb;
184
- $wpdb->query("UPDATE $wpdb->options SET option_value = '$value' WHERE option_name = '$option'");
185
- }
186
-
187
- function get_current_url(){
188
- $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
189
- if ($_SERVER["SERVER_PORT"] != "80"){
190
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
191
- } else {
192
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
193
- }
194
- return $pageURL;
195
- }
196
-
197
- function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br' ) {
198
- if ( null === $more )
199
- $more = __( '&hellip;' );
200
- $original_text = $text;
201
- $allowed_tag_string = '';
202
- foreach(explode(' ', $allowed_tags) as $tag){
203
- $allowed_tag_string .= '<'.$tag.'>';
204
- }
205
- $text = strip_tags($text, $allowed_tag_string);
206
- /* translators: If your word count is based on single characters (East Asian characters),
207
- enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
208
- if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
209
- $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
210
- preg_match_all( '/./u', $text, $words_array );
211
- $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
212
- $sep = '';
213
- } else {
214
- $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
215
- $sep = ' ';
216
- }
217
- if ( count( $words_array ) > $num_words ) {
218
- array_pop( $words_array );
219
- $text = implode( $sep, $words_array );
220
- $text = $text . $more;
221
- } else {
222
- $text = implode( $sep, $words_array );
223
- }
224
- $text = self::close_tags($text);
225
- return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
226
- }
227
-
228
- function trim_text($input, $length, $strip_html = true, $ellipses = '') {
229
- //strip tags, if desired
230
- if ($strip_html) {
231
- $input = strip_tags($input);
232
- }
233
-
234
- //no need to trim, already shorter than trim length
235
- if (strlen($input) <= $length) {
236
- return $input;
237
- }
238
-
239
- //find last space within length
240
- $last_space = strrpos(substr($input, 0, $length), ' ');
241
- $trimmed_text = substr($input, 0, $last_space);
242
-
243
- //add ellipses (...)
244
- if ($ellipses) {
245
- $trimmed_text .= $ellipses;
246
- }
247
- return $trimmed_text;
248
- }
249
-
250
- function close_tags($html) {
251
- #put all opened tags into an array
252
- preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
253
- $openedtags = $result[1];
254
- #put all closed tags into an array
255
- preg_match_all('#</([a-z]+)>#iU', $html, $result);
256
- $closedtags = $result[1];
257
- $len_opened = count($openedtags);
258
- # all tags are closed
259
- if (count($closedtags) == $len_opened) {
260
- return $html;
261
- }
262
- $openedtags = array_reverse($openedtags);
263
- # close tags
264
- for ($i=0; $i < $len_opened; $i++) {
265
- if (!in_array($openedtags[$i], $closedtags)){
266
- $html .= '</'.$openedtags[$i].'>';
267
- } else {
268
- unset($closedtags[array_search($openedtags[$i], $closedtags)]);
269
- }
270
- }
271
- return $html;
272
- }
273
-
274
- function get_posts_by_meta($key, $value){
275
- global $wpdb;
276
- $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value'";
277
- $results = $wpdb->get_results($query);
278
- $pids = array();
279
- foreach($results as $result){
280
- if (get_post($result->post_id)){
281
- $pids[] = $result->post_id;
282
- }
283
- }
284
- if (count($pids)){
285
- return $pids;
286
- }
287
- return 0;
288
- }
289
-
290
- function get_post_by_meta($key, $value){
291
- global $wpdb;
292
- $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value' ORDER BY post_id";
293
- $result = $wpdb->get_row($query);
294
- if ($result && get_post($result->post_id)){
295
- return $result->post_id;
296
- }
297
- return 0;
298
- }
299
-
300
- /* this $args thing is a fucking mess, fix at some point:
301
-
302
- http://codex.wordpress.org/Function_Reference/comment_form */
303
-
304
- function get_comment_form( $post_id = null, $args = array()) {
305
- ob_start();
306
- comment_form( $args, $post_id );
307
- $ret = ob_get_contents();
308
- ob_end_clean();
309
- return $ret;
310
- }
311
-
312
- function is_true($property){
313
- if (isset($property)){
314
- if ($property == 'true' || $property == 1 || $property == '1' || $property == true){
315
- return true;
316
- }
317
- }
318
- return false;
319
- }
320
-
321
-
322
- function array_to_object($array) {
323
- $obj = new stdClass;
324
- foreach($array as $k => $v) {
325
- if(is_array($v)) {
326
- $obj->{$k} = array_to_object($v); //RECURSION
327
- } else {
328
- $obj->{$k} = $v;
329
- }
330
- }
331
- return $obj;
332
- }
333
-
334
- function get_object_index_by_property($array, $key, $value){
335
- if (is_array($array)){
336
- $i = 0;
337
- foreach($array as $arr){
338
- if ($arr->$key == $value || $arr[$key] == $value){
339
- return $i;
340
- }
341
- $i++;
342
- }
343
- }
344
- return false;
345
- }
346
-
347
- function get_object_by_property($array, $key, $value){
348
- if (is_array($array)){
349
- foreach($array as $arr){
350
- if ($arr->$key == $value){
351
- return $arr;
352
- }
353
- }
354
- } else {
355
- echo $array;
356
- echo 'not an array'.$key.' = '.$value;
357
- }
358
- }
359
-
360
- function array_truncate($array, $len){
361
- if (sizeof($array) > $len) {
362
- $array = array_splice($array, 0, $len);
363
- }
364
- return $array;
365
- }
366
-
367
- function iseven($i){
368
- return ($i % 2) == 0;
369
- }
370
-
371
- function isodd($i){
372
- return ($i % 2) != 0;
373
- }
374
-
375
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ class WPHelper
4
+ {
5
+
6
+ public static function is_array_assoc($arr)
7
+ {
8
+ if (!is_array($arr)) {
9
+ return false;
10
+ }
11
+ return (bool)count(array_filter(array_keys($arr), 'is_string'));
12
+ }
13
+
14
+ public static function ob_function($function, $args = array(null))
15
+ {
16
+ ob_start();
17
+ call_user_func_array($function, $args);
18
+ $data = ob_get_contents();
19
+ ob_end_clean();
20
+ return $data;
21
+ }
22
+
23
+ public static function is_url($url)
24
+ {
25
+ $url = strtolower($url);
26
+ if (strstr('://', $url)) {
27
+ return true;
28
+ }
29
+ return false;
30
+ }
31
+
32
+ public static function resize_letterbox($src, $w, $h)
33
+ {
34
+
35
+ }
36
+
37
+ public static function get_path_base()
38
+ {
39
+ $struc = get_option('permalink_structure');
40
+ $struc = explode('/', $struc);
41
+ $p = '/';
42
+ foreach ($struc as $s) {
43
+ if (!strstr($s, '%') && strlen($s)) {
44
+ $p .= $s . '/';
45
+ }
46
+ }
47
+ return $p;
48
+ }
49
+
50
+ public static function get_full_path($src)
51
+ {
52
+ $root = $_SERVER['DOCUMENT_ROOT'];
53
+ $old_root_path = $root . $src;
54
+ $old_root_path = str_replace('//', '/', $old_root_path);
55
+ return $old_root_path;
56
+ }
57
+
58
+ public static function get_rel_path($src)
59
+ {
60
+ return str_replace($_SERVER['DOCUMENT_ROOT'], '', $src);
61
+ }
62
+
63
+ public static function get_letterbox_file_rel($src, $w, $h)
64
+ {
65
+ $path_parts = pathinfo($src);
66
+ $basename = $path_parts['filename'];
67
+ $ext = $path_parts['extension'];
68
+ $dir = $path_parts['dirname'];
69
+ $newbase = $basename . '-lb-' . $w . 'x' . $h;
70
+ $new_path = $dir . '/' . $newbase . '.' . $ext;
71
+ return $new_path;
72
+ }
73
+
74
+ public static function get_letterbox_file_path($src, $w, $h)
75
+ {
76
+ $root = $_SERVER['DOCUMENT_ROOT'];
77
+ $path_parts = pathinfo($src);
78
+ $basename = $path_parts['filename'];
79
+ $ext = $path_parts['extension'];
80
+ $dir = $path_parts['dirname'];
81
+ $newbase = $basename . '-lb-' . $w . 'x' . $h;
82
+ $new_path = $dir . '/' . $newbase . '.' . $ext;
83
+ $new_root_path = $root . $new_path;
84
+ $new_root_path = str_replace('//', '/', $new_root_path);
85
+ return $new_root_path;
86
+ }
87
+
88
+ public static function download_url($url, $timeout = 300)
89
+ {
90
+ //WARNING: The file is not automatically deleted, The script must unlink() the file.
91
+ if (!$url) {
92
+ return new WP_Error('http_no_url', __('Invalid URL Provided.'));
93
+ }
94
+
95
+ $tmpfname = wp_tempnam($url);
96
+ if (!$tmpfname) {
97
+ return new WP_Error('http_no_file', __('Could not create Temporary file.'));
98
+ }
99
+
100
+ $response = wp_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
101
+
102
+ if (is_wp_error($response)) {
103
+ unlink($tmpfname);
104
+ return $response;
105
+ }
106
+
107
+ if (200 != wp_remote_retrieve_response_code($response)) {
108
+ unlink($tmpfname);
109
+ return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
110
+ }
111
+
112
+ return $tmpfname;
113
+ }
114
+
115
+ public static function sideload_image($file)
116
+ {
117
+ require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/file.php');
118
+ require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/media.php');
119
+ if (empty($file)) {
120
+ error_log('returnning');
121
+ return null;
122
+ }
123
+ // Download file to temp location
124
+ $tmp = download_url($file);
125
+ preg_match('/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches);
126
+ $file_array['name'] = basename($matches[0]);
127
+ $file_array['tmp_name'] = $tmp;
128
+ // If error storing temporarily, unlink
129
+ if (is_wp_error($tmp)) {
130
+ error_log('theres an error');
131
+ @unlink($file_array['tmp_name']);
132
+ $file_array['tmp_name'] = '';
133
+ }
134
+ error_log('continuing on');
135
+ // do the validation and storage stuff
136
+ $file = wp_upload_bits($file_array['name'], null, file_get_contents($file_array['tmp_name']));
137
+
138
+ return $file;
139
+ }
140
+
141
+ public static function osort(&$array, $prop)
142
+ {
143
+ usort($array, function ($a, $b) use ($prop) {
144
+ return $a->$prop > $b->$prop ? 1 : -1;
145
+ });
146
+ }
147
+
148
+ public static function error_log($arg)
149
+ {
150
+ if (is_object($arg) || is_array($arg)) {
151
+ $arg = print_r($arg, true);
152
+ }
153
+ error_log($arg);
154
+ }
155
+
156
+ public static function get_params($i = -1)
157
+ {
158
+ $args = explode('/', trim(strtolower($_SERVER['REQUEST_URI'])));
159
+ $newargs = array();
160
+ foreach ($args as $arg) {
161
+ if (strlen($arg)) {
162
+ $newargs[] = $arg;
163
+ }
164
+ }
165
+ if ($i > -1) {
166
+ if (isset($newargs[$i])) {
167
+ return $newargs[$i];
168
+ }
169
+ }
170
+ return $newargs;
171
+ }
172
+
173
+ public static function get_json($url)
174
+ {
175
+ $data = self::get_curl($url);
176
+ return json_decode($data);
177
+ }
178
+
179
+ public static function get_curl($url)
180
+ {
181
+ $ch = curl_init();
182
+ curl_setopt($ch, CURLOPT_URL, $url);
183
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
184
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
185
+ $content = curl_exec($ch);
186
+ curl_close($ch);
187
+ return $content;
188
+ }
189
+
190
+ public static function get_wp_title()
191
+ {
192
+ return wp_title('|', false, 'right');
193
+ }
194
+
195
+ public static function force_update_option($option, $value)
196
+ {
197
+ global $wpdb;
198
+ $wpdb->query("UPDATE $wpdb->options SET option_value = '$value' WHERE option_name = '$option'");
199
+ }
200
+
201
+ public static function get_current_url()
202
+ {
203
+ $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
204
+ if ($_SERVER["SERVER_PORT"] != "80") {
205
+ $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
206
+ } else {
207
+ $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
208
+ }
209
+ return $pageURL;
210
+ }
211
+
212
+ public static function trim_words($text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br')
213
+ {
214
+ if (null === $more) {
215
+ $more = __('&hellip;');
216
+ }
217
+ $original_text = $text;
218
+ $allowed_tag_string = '';
219
+ foreach (explode(' ', $allowed_tags) as $tag) {
220
+ $allowed_tag_string .= '<' . $tag . '>';
221
+ }
222
+ $text = strip_tags($text, $allowed_tag_string);
223
+ /* translators: If your word count is based on single characters (East Asian characters),
224
+ enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
225
+ if ('characters' == _x('words', 'word count: words or characters?') && preg_match('/^utf\-?8$/i', get_option('blog_charset'))) {
226
+ $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');
227
+ preg_match_all('/./u', $text, $words_array);
228
+ $words_array = array_slice($words_array[0], 0, $num_words + 1);
229
+ $sep = '';
230
+ } else {
231
+ $words_array = preg_split("/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY);
232
+ $sep = ' ';
233
+ }
234
+ if (count($words_array) > $num_words) {
235
+ array_pop($words_array);
236
+ $text = implode($sep, $words_array);
237
+ $text = $text . $more;
238
+ } else {
239
+ $text = implode($sep, $words_array);
240
+ }
241
+ $text = self::close_tags($text);
242
+ return apply_filters('wp_trim_words', $text, $num_words, $more, $original_text);
243
+ }
244
+
245
+ public static function trim_text($input, $length, $strip_html = true, $ellipses = '')
246
+ {
247
+ //strip tags, if desired
248
+ if ($strip_html) {
249
+ $input = strip_tags($input);
250
+ }
251
+
252
+ //no need to trim, already shorter than trim length
253
+ if (strlen($input) <= $length) {
254
+ return $input;
255
+ }
256
+
257
+ //find last space within length
258
+ $last_space = strrpos(substr($input, 0, $length), ' ');
259
+ $trimmed_text = substr($input, 0, $last_space);
260
+
261
+ //add ellipses (...)
262
+ if ($ellipses) {
263
+ $trimmed_text .= $ellipses;
264
+ }
265
+ return $trimmed_text;
266
+ }
267
+
268
+ public static function close_tags($html)
269
+ {
270
+ #put all opened tags into an array
271
+ preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
272
+ $openedtags = $result[1];
273
+ #put all closed tags into an array
274
+ preg_match_all('#</([a-z]+)>#iU', $html, $result);
275
+ $closedtags = $result[1];
276
+ $len_opened = count($openedtags);
277
+ # all tags are closed
278
+ if (count($closedtags) == $len_opened) {
279
+ return $html;
280
+ }
281
+ $openedtags = array_reverse($openedtags);
282
+ # close tags
283
+ for ($i = 0; $i < $len_opened; $i++) {
284
+ if (!in_array($openedtags[$i], $closedtags)) {
285
+ $html .= '</' . $openedtags[$i] . '>';
286
+ } else {
287
+ unset($closedtags[array_search($openedtags[$i], $closedtags)]);
288
+ }
289
+ }
290
+ return $html;
291
+ }
292
+
293
+ public static function get_posts_by_meta($key, $value)
294
+ {
295
+ global $wpdb;
296
+ $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value'";
297
+ $results = $wpdb->get_results($query);
298
+ $pids = array();
299
+ foreach ($results as $result) {
300
+ if (get_post($result->post_id)) {
301
+ $pids[] = $result->post_id;
302
+ }
303
+ }
304
+ if (count($pids)) {
305
+ return $pids;
306
+ }
307
+ return 0;
308
+ }
309
+
310
+ public static function get_post_by_meta($key, $value)
311
+ {
312
+ global $wpdb;
313
+ $query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$key' AND meta_value = '$value' ORDER BY post_id";
314
+ $result = $wpdb->get_row($query);
315
+ if ($result && get_post($result->post_id)) {
316
+ return $result->post_id;
317
+ }
318
+ return 0;
319
+ }
320
+
321
+ /* this $args thing is a fucking mess, fix at some point:
322
+
323
+ http://codex.wordpress.org/Function_Reference/comment_form */
324
+
325
+ public static function get_comment_form($post_id = null, $args = array())
326
+ {
327
+ ob_start();
328
+ comment_form($args, $post_id);
329
+ $ret = ob_get_contents();
330
+ ob_end_clean();
331
+ return $ret;
332
+ }
333
+
334
+ public static function is_true($property)
335
+ {
336
+ if (isset($property)) {
337
+ if ($property == 'true' || $property == 1 || $property == '1' || $property == true) {
338
+ return true;
339
+ }
340
+ }
341
+ return false;
342
+ }
343
+
344
+
345
+ public static function array_to_object($array)
346
+ {
347
+ $obj = new stdClass;
348
+ foreach ($array as $k => $v) {
349
+ if (is_array($v)) {
350
+ $obj->{$k} = self::array_to_object($v); //RECURSION
351
+ } else {
352
+ $obj->{$k} = $v;
353
+ }
354
+ }
355
+ return $obj;
356
+ }
357
+
358
+ public static function get_object_index_by_property($array, $key, $value)
359
+ {
360
+ if (is_array($array)) {
361
+ $i = 0;
362
+ foreach ($array as $arr) {
363
+ if ($arr->$key == $value || $arr[$key] == $value) {
364
+ return $i;
365
+ }
366
+ $i++;
367
+ }
368
+ }
369
+ return false;
370
+ }
371
+
372
+ public static function get_object_by_property($array, $key, $value)
373
+ {
374
+ if (is_array($array)) {
375
+ foreach ($array as $arr) {
376
+ if ($arr->$key == $value) {
377
+ return $arr;
378
+ }
379
+ }
380
+ } else {
381
+ throw new Exception('$array is not an array, given value: ' . $array);
382
+ }
383
+ return null;
384
+ }
385
+
386
+ public static function get_image_path($iid)
387
+ {
388
+ $size = 'full';
389
+ $src = wp_get_attachment_image_src($iid, $size);
390
+ $src = $src[0];
391
+ return self::get_path($src);
392
+ }
393
+
394
+ public static function array_truncate($array, $len)
395
+ {
396
+ if (sizeof($array) > $len) {
397
+ $array = array_splice($array, 0, $len);
398
+ }
399
+ return $array;
400
+ }
401
+
402
+ public static function iseven($i)
403
+ {
404
+ return ($i % 2) == 0;
405
+ }
406
+
407
+ public static function isodd($i)
408
+ {
409
+ return ($i % 2) != 0;
410
+ }
411
+
412
+ }
objects/timber-comment.php CHANGED
@@ -1,39 +1,45 @@
1
  <?php
2
-
3
- class TimberComment extends TimberCore {
4
-
5
- function __construct($cid){
6
- $this->init($cid);
7
- }
8
-
9
- /* core definition */
10
-
11
- function author(){
12
- if ($this->user_id){
13
- return new TimberUser($this->user_id);
14
- }
15
- $fakeUser = new stdClass();
16
- $fakeUser->name = 'Anonymous';
17
- if ($this->comment_author){
18
- $fakeUser->name = $this->comment_author;
19
- }
20
- return $fakeUser;
21
- }
22
-
23
- function date(){
24
- return $this->comment_date;
25
- }
26
-
27
- function content(){
28
- return $this->comment_content;
29
- }
30
-
31
- function init($cid){
32
- $comment_data = $cid;
33
- if (is_integer($cid)){
34
- $comment_data = get_comment($cid);
35
- }
36
- $this->import($comment_data);
37
- }
38
-
39
- }
 
 
 
 
 
 
1
  <?php
2
+
3
+ class TimberComment extends TimberCore
4
+ {
5
+
6
+ function __construct($cid)
7
+ {
8
+ $this->init($cid);
9
+ }
10
+
11
+ /* core definition */
12
+
13
+ function author()
14
+ {
15
+ if ($this->user_id) {
16
+ return new TimberUser($this->user_id);
17
+ }
18
+ $fakeUser = new stdClass();
19
+ $fakeUser->name = 'Anonymous';
20
+ if ($this->comment_author) {
21
+ $fakeUser->name = $this->comment_author;
22
+ }
23
+ return $fakeUser;
24
+ }
25
+
26
+ function date()
27
+ {
28
+ return $this->comment_date;
29
+ }
30
+
31
+ function content()
32
+ {
33
+ return $this->comment_content;
34
+ }
35
+
36
+ function init($cid)
37
+ {
38
+ $comment_data = $cid;
39
+ if (is_integer($cid)) {
40
+ $comment_data = get_comment($cid);
41
+ }
42
+ $this->import($comment_data);
43
+ }
44
+
45
+ }
objects/timber-core.php CHANGED
@@ -1,37 +1,41 @@
1
  <?php
2
-
3
- class TimberCore {
4
 
5
- function import($info){
6
- if (is_object($info)){
7
- $info = get_object_vars($info);
8
- }
9
- if (is_array($info)){
10
- foreach($info as $key=>$value){
11
- $this->$key = $value;
12
- }
13
- }
14
- }
15
 
16
- function url_to_path($url = ''){
17
- if (!strlen($url) && $this->url){
18
- $url = $this->url;
19
- }
20
- $url_info = parse_url($url);
21
- return $url_info['path'];
22
- }
 
 
 
 
23
 
24
- function can_edit(){
25
- if (isset($this->_can_edit)){
26
- return $this->_can_edit;
27
- }
28
- $this->_can_edit = false;
29
- if (!function_exists('current_user_can')){
30
- return false;
31
- }
32
- if (current_user_can('edit_post', $this->ID)){
33
- $this->_can_edit = true;
34
- }
35
- return $this->_can_edit;
36
- }
37
- }
 
 
 
 
 
 
 
 
 
 
1
  <?php
 
 
2
 
3
+ class TimberCore
4
+ {
 
 
 
 
 
 
 
 
5
 
6
+ function import($info)
7
+ {
8
+ if (is_object($info)) {
9
+ $info = get_object_vars($info);
10
+ }
11
+ if (is_array($info)) {
12
+ foreach ($info as $key => $value) {
13
+ $this->$key = $value;
14
+ }
15
+ }
16
+ }
17
 
18
+ function url_to_path($url = '')
19
+ {
20
+ if (!strlen($url) && $this->url) {
21
+ $url = $this->url;
22
+ }
23
+ $url_info = parse_url($url);
24
+ return $url_info['path'];
25
+ }
26
+
27
+ function can_edit()
28
+ {
29
+ if (isset($this->_can_edit)) {
30
+ return $this->_can_edit;
31
+ }
32
+ $this->_can_edit = false;
33
+ if (!function_exists('current_user_can')) {
34
+ return false;
35
+ }
36
+ if (current_user_can('edit_post', $this->ID)) {
37
+ $this->_can_edit = true;
38
+ }
39
+ return $this->_can_edit;
40
+ }
41
+ }
objects/timber-image.php CHANGED
@@ -1,99 +1,115 @@
1
  <?php
2
-
3
- class TimberImage extends TimberCore {
4
 
5
- var $_can_edit;
6
- var $abs_url;
7
- var $PostClass = 'TimberPost';
8
 
9
- function __construct($iid){
10
- $this->init($iid);
11
- }
12
 
13
- function get_src(){
14
- if (isset($this->abs_url)){
15
- return $this->abs_url;
16
- }
17
- if (!isset($this->file) && isset($this->_wp_attached_file)){
18
- $this->file = $this->_wp_attached_file;
19
- }
20
- if (isset($this->file)){
21
- $dir = wp_upload_dir();
22
- $base = $this->url_to_path($dir["baseurl"]);
23
- return trailingslashit($base).$this->file;
24
- }
25
- return false;
26
- }
27
 
28
- function get_path(){
29
- if (strlen($this->abs_url)){
30
- return $this->abs_url;
31
- }
32
- return get_permalink($this->ID);
33
- }
34
 
35
- function get_parent(){
36
- if (!$this->post_parent){
37
- return false;
38
- }
39
- return new $this->PostClass($this->post_parent);
40
- }
 
 
 
 
 
 
 
 
 
41
 
42
- function init($iid){
43
-
44
- if (!is_numeric($iid) && is_string($iid)){
45
- if (strstr($iid, '://')){
46
- $this->init_with_url($iid);
47
- return;
48
- } else if (strstr(strtolower($iid), '.jpg')){
49
- $this->init_with_url($iid);
50
- }
51
- }
52
- $image_info = $iid;
53
- if(is_numeric($iid)){
54
- $image_info = wp_get_attachment_metadata($iid);
55
- if (!is_array($image_info)){
56
- $image_info = array();
57
- }
58
- $image_custom = get_post_custom($iid);
59
- $basic = get_post($iid);
60
- $this->caption = $basic->post_excerpt;
61
- $image_info = array_merge($image_info, $image_custom, get_object_vars($basic));
62
 
 
 
 
 
 
 
 
63
 
64
- } else if (is_array($image_info) && isset($image_info['image'])){
65
- $image_info = $image_info['image'];
66
- } else if (is_object($image_info)){
67
- $image_info = get_object_vars($image_info);
68
- }
69
- $this->import($image_info);
70
- if (isset($image_info['id'])){
71
- $this->ID = $image_info['id'];
72
- } else if (is_numeric($iid)){
73
- $this->ID = $iid;
74
- }
75
- if (isset($this->ID)){
76
- $custom = get_post_custom($this->ID);
77
- foreach($custom as $key=>$value){
78
- $this->$key = $value[0];
79
- }
80
- } else {
81
- error_log('iid='.$iid);
82
- }
83
- }
84
 
85
- function init_with_url($url){
86
- $this->abs_url = $url;
87
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- /* deprecated */
90
- function get_url(){
91
- return $this->get_src();
92
- }
93
 
94
- /* Alias */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- function src(){
97
- return $this->get_src();
98
- }
99
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
 
 
2
 
3
+ class TimberImage extends TimberCore
4
+ {
 
5
 
6
+ var $_can_edit;
7
+ var $abs_url;
8
+ var $PostClass = 'TimberPost';
9
 
10
+ function __construct($iid)
11
+ {
12
+ $this->init($iid);
13
+ }
 
 
 
 
 
 
 
 
 
 
14
 
15
+ function __toString(){
16
+ if ($this->get_src()){
17
+ return $this->get_src();
18
+ }
19
+ return '';
20
+ }
21
 
22
+ function get_src()
23
+ {
24
+ if (isset($this->abs_url)) {
25
+ return $this->abs_url;
26
+ }
27
+ if (!isset($this->file) && isset($this->_wp_attached_file)) {
28
+ $this->file = $this->_wp_attached_file;
29
+ }
30
+ if (isset($this->file)) {
31
+ $dir = wp_upload_dir();
32
+ $base = $this->url_to_path($dir["baseurl"]);
33
+ return trailingslashit($base) . $this->file;
34
+ }
35
+ return false;
36
+ }
37
 
38
+ function get_path()
39
+ {
40
+ if (strlen($this->abs_url)) {
41
+ return $this->abs_url;
42
+ }
43
+ return get_permalink($this->ID);
44
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ function get_parent()
47
+ {
48
+ if (!$this->post_parent) {
49
+ return false;
50
+ }
51
+ return new $this->PostClass($this->post_parent);
52
+ }
53
 
54
+ function init($iid)
55
+ {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ if (!is_numeric($iid) && is_string($iid)) {
58
+ if (strstr($iid, '://')) {
59
+ $this->init_with_url($iid);
60
+ return;
61
+ } else if (strstr(strtolower($iid), '.jpg')) {
62
+ $this->init_with_url($iid);
63
+ }
64
+ }
65
+ $image_info = $iid;
66
+ if (is_numeric($iid)) {
67
+ $image_info = wp_get_attachment_metadata($iid);
68
+ if (!is_array($image_info)) {
69
+ $image_info = array();
70
+ }
71
+ $image_custom = get_post_custom($iid);
72
+ $basic = get_post($iid);
73
+ $this->caption = $basic->post_excerpt;
74
+ $image_info = array_merge($image_info, $image_custom, get_object_vars($basic));
75
 
 
 
 
 
76
 
77
+ } else if (is_array($image_info) && isset($image_info['image'])) {
78
+ $image_info = $image_info['image'];
79
+ } else if (is_object($image_info)) {
80
+ $image_info = get_object_vars($image_info);
81
+ }
82
+ $this->import($image_info);
83
+ if (isset($image_info['id'])) {
84
+ $this->ID = $image_info['id'];
85
+ } else if (is_numeric($iid)) {
86
+ $this->ID = $iid;
87
+ }
88
+ if (isset($this->ID)) {
89
+ $custom = get_post_custom($this->ID);
90
+ foreach ($custom as $key => $value) {
91
+ $this->$key = $value[0];
92
+ }
93
+ } else {
94
+ error_log('Not able to init in TimberImage with iid=' . $iid);
95
+ }
96
+ }
97
 
98
+ function init_with_url($url)
99
+ {
100
+ $this->abs_url = $url;
101
+ }
102
+
103
+ /* deprecated */
104
+ function get_url()
105
+ {
106
+ return $this->get_src();
107
+ }
108
+
109
+ /* Alias */
110
+
111
+ function src()
112
+ {
113
+ return $this->get_src();
114
+ }
115
+ }
objects/timber-loader.php CHANGED
@@ -1,133 +1,145 @@
1
  <?php
2
 
3
- class TimberLoader {
 
4
 
5
- var $locations;
6
 
7
- function __construct($caller = false){
8
- $this->locations = $this->get_locations($caller);
9
- }
 
10
 
11
- function render($file, $data = null){
12
- $twig = $this->get_twig();
13
- return $twig->render($file, $data);
14
- }
 
15
 
16
- function choose_template($filenames){
17
- if(is_array($filenames)){
18
- /* its an array so we have to figure out which one the dev wants */
19
- foreach($filenames as $filename){
20
- if ($this->template_exists($filename)){
21
- return $filename;
22
- }
23
- }
24
- return false;
25
- }
26
- return $filenames;
27
- }
 
28
 
29
- function template_exists($file){
30
- foreach($this->locations as $dir){
31
- $look_for = trailingslashit($dir).$file;
32
- if (file_exists($look_for)){
33
- return true;
34
- }
35
- }
36
- return false;
37
- }
 
38
 
39
- function get_locations_theme(){
40
- $theme_locs = array();
41
- $child_loc = get_stylesheet_directory();
42
- $parent_loc = get_template_directory();
43
- $theme_locs[] = $child_loc;
44
- if ($child_loc != $parent_loc){
45
- $theme_locs[] = $parent_loc;
46
- }
47
- //add the template directory to each path
48
- foreach($theme_locs as $tl){
49
- $theme_locs[] = trailingslashit($tl).trailingslashit(Timber::$dirname);
50
- }
51
- //now make sure theres a trailing slash on everything
52
- foreach($theme_locs as &$tl){
53
- $tl = trailingslashit($tl);
54
- }
55
- return $theme_locs;
56
- }
 
57
 
58
- function get_locations_user(){
59
- $locs = array();
60
- if (isset(Timber::$locations)){
61
- if (is_string(Timber::$locations)){
62
- Timber::$locations = array(Timber::$locations);
63
- }
64
- foreach(Timber::$locations as $tloc){
65
- $tloc = realpath($tloc);
66
- if (is_dir($tloc)){
67
- $locs[] = $tloc;
68
- }
69
- }
70
- }
71
- return $locs;
72
- }
 
73
 
74
- function get_locations_caller($caller = false){
75
- $locs = array();
76
- if ($caller && is_string($caller)){
77
- $caller = trailingslashit($caller);
78
- if (is_dir($caller)){
79
- $locs[] = $caller;
80
- }
81
- $caller_sub = $caller.trailingslashit(Timber::$dirname);
82
- if (is_dir($caller_sub)){
83
- $locs[] = $caller_sub;
84
- }
85
- }
86
- return $locs;
87
- }
 
88
 
89
- function get_locations($caller = false){
90
- //prioirty: user locations, caller, theme
91
- $locs = array();
92
- $locs = array_merge($locs, $this->get_locations_user());
93
- $locs = array_merge($locs, $this->get_locations_caller($caller));
94
- $locs = array_merge($locs, $this->get_locations_theme());
95
- $locs = array_unique($locs);
96
- return $locs;
97
- }
 
98
 
99
- function get_loader(){
100
- $loaders = array();
101
- foreach($this->locations as $loc){
102
- $loc = realpath($loc);
103
- if (is_dir($loc)){
104
- $loc = realpath($loc);
105
- $loaders[] = new Twig_Loader_Filesystem($loc);
106
- } else {
107
- //error_log($loc.' is not a directory');
108
- }
109
- }
110
- $loader = new Twig_Loader_Chain($loaders);
111
- return $loader;
112
- }
 
113
 
114
- function get_twig(){
115
- $loader_loc = trailingslashit(TIMBER_LOC).'Twig/lib/Twig/Autoloader.php';
116
- require_once($loader_loc);
117
- $reg = Twig_Autoloader::register();
118
-
119
- $loader = $this->get_loader();
120
- $params = array('debug' => false, 'autoescape' => false);
121
- if (Timber::$cache){
122
- $params['cache'] = TIMBER_LOC.'/twig-cache';
123
- }
124
- $twig = new Twig_Environment($loader, $params);
125
- $twig->addExtension(new Twig_Extension_Debug());
126
- $twig = apply_filters('twig_apply_filters', $twig);
127
- return $twig;
128
- }
129
 
130
- function get_file($filenames){
 
 
 
 
 
 
 
 
 
131
 
132
- }
 
 
 
133
  }
1
  <?php
2
 
3
+ class TimberLoader
4
+ {
5
 
6
+ var $locations;
7
 
8
+ function __construct($caller = false)
9
+ {
10
+ $this->locations = $this->get_locations($caller);
11
+ }
12
 
13
+ function render($file, $data = null)
14
+ {
15
+ $twig = $this->get_twig();
16
+ return $twig->render($file, $data);
17
+ }
18
 
19
+ function choose_template($filenames)
20
+ {
21
+ if (is_array($filenames)) {
22
+ /* its an array so we have to figure out which one the dev wants */
23
+ foreach ($filenames as $filename) {
24
+ if ($this->template_exists($filename)) {
25
+ return $filename;
26
+ }
27
+ }
28
+ return false;
29
+ }
30
+ return $filenames;
31
+ }
32
 
33
+ function template_exists($file)
34
+ {
35
+ foreach ($this->locations as $dir) {
36
+ $look_for = trailingslashit($dir) . $file;
37
+ if (file_exists($look_for)) {
38
+ return true;
39
+ }
40
+ }
41
+ return false;
42
+ }
43
 
44
+ function get_locations_theme()
45
+ {
46
+ $theme_locs = array();
47
+ $child_loc = get_stylesheet_directory();
48
+ $parent_loc = get_template_directory();
49
+ $theme_locs[] = $child_loc;
50
+ if ($child_loc != $parent_loc) {
51
+ $theme_locs[] = $parent_loc;
52
+ }
53
+ //add the template directory to each path
54
+ foreach ($theme_locs as $tl) {
55
+ $theme_locs[] = trailingslashit($tl) . trailingslashit(Timber::$dirname);
56
+ }
57
+ //now make sure theres a trailing slash on everything
58
+ foreach ($theme_locs as &$tl) {
59
+ $tl = trailingslashit($tl);
60
+ }
61
+ return $theme_locs;
62
+ }
63
 
64
+ function get_locations_user()
65
+ {
66
+ $locs = array();
67
+ if (isset(Timber::$locations)) {
68
+ if (is_string(Timber::$locations)) {
69
+ Timber::$locations = array(Timber::$locations);
70
+ }
71
+ foreach (Timber::$locations as $tloc) {
72
+ $tloc = realpath($tloc);
73
+ if (is_dir($tloc)) {
74
+ $locs[] = $tloc;
75
+ }
76
+ }
77
+ }
78
+ return $locs;
79
+ }
80
 
81
+ function get_locations_caller($caller = false)
82
+ {
83
+ $locs = array();
84
+ if ($caller && is_string($caller)) {
85
+ $caller = trailingslashit($caller);
86
+ if (is_dir($caller)) {
87
+ $locs[] = $caller;
88
+ }
89
+ $caller_sub = $caller . trailingslashit(Timber::$dirname);
90
+ if (is_dir($caller_sub)) {
91
+ $locs[] = $caller_sub;
92
+ }
93
+ }
94
+ return $locs;
95
+ }
96
 
97
+ function get_locations($caller = false)
98
+ {
99
+ //prioirty: user locations, caller, theme
100
+ $locs = array();
101
+ $locs = array_merge($locs, $this->get_locations_user());
102
+ $locs = array_merge($locs, $this->get_locations_caller($caller));
103
+ $locs = array_merge($locs, $this->get_locations_theme());
104
+ $locs = array_unique($locs);
105
+ return $locs;
106
+ }
107
 
108
+ function get_loader()
109
+ {
110
+ $loaders = array();
111
+ foreach ($this->locations as $loc) {
112
+ $loc = realpath($loc);
113
+ if (is_dir($loc)) {
114
+ $loc = realpath($loc);
115
+ $loaders[] = new Twig_Loader_Filesystem($loc);
116
+ } else {
117
+ //error_log($loc.' is not a directory');
118
+ }
119
+ }
120
+ $loader = new Twig_Loader_Chain($loaders);
121
+ return $loader;
122
+ }
123
 
124
+ function get_twig()
125
+ {
126
+ $loader_loc = trailingslashit(TIMBER_LOC) . 'Twig/lib/Twig/Autoloader.php';
127
+ require_once($loader_loc);
128
+ Twig_Autoloader::register();
 
 
 
 
 
 
 
 
 
 
129
 
130
+ $loader = $this->get_loader();
131
+ $params = array('debug' => WP_DEBUG, 'autoescape' => false);
132
+ if (Timber::$cache) {
133
+ $params['cache'] = TIMBER_LOC . '/twig-cache';
134
+ }
135
+ $twig = new Twig_Environment($loader, $params);
136
+ $twig->addExtension(new Twig_Extension_Debug());
137
+ $twig = apply_filters('twig_apply_filters', $twig);
138
+ return $twig;
139
+ }
140
 
141
+ function get_file($filenames)
142
+ {
143
+
144
+ }
145
  }
objects/timber-menu.php CHANGED
@@ -1,89 +1,102 @@
1
  <?php
2
-
3
- class TimberMenu extends TimberCore {
4
 
5
- var $items = null;
 
6
 
7
- function __construct($slug){
8
- //$menu = wp_get_nav_menu_object($slug);
9
- $locations = get_nav_menu_locations();
10
- if (isset($locations[$slug])){
11
- $menu = wp_get_nav_menu_object($locations[$slug]);
12
- $menu = wp_get_nav_menu_items($menu);
13
- $menu = self::order_children($menu);
14
- $this->items = $menu;
15
- }
16
- return null;
17
- }
18
 
19
- function find_parent_item_in_menu($menu_items, $parent_id){
20
- foreach($menu_items as &$item){
21
- if ($item->ID == $parent_id){
22
- return $item;
23
- }
24
- }
25
- return null;
26
- }
 
 
 
 
27
 
28
- function order_children($items){
29
- $menu = array();
30
- foreach($items as $item){
31
- if($item->menu_item_parent == 0){
32
- $menu[] = new TimberMenuItem($item);
33
- continue;
34
- }
35
- $parent_id = $item->menu_item_parent;
36
- $parent = self::find_parent_item_in_menu($menu, $parent_id);
37
- if ($parent){
38
- $parent->add_child(new TimberMenuItem($item));
39
- }
40
- }
41
- return $menu;
42
- }
43
 
44
- function get_items(){
45
- if (is_array($this->items)){
46
- return $this->items;
47
- }
48
- return array();
49
- }
50
- }
 
 
 
 
 
 
 
 
 
51
 
52
- class TimberMenuItem extends TimberCore {
 
 
 
 
 
 
 
53
 
54
- var $children;
 
55
 
56
- function __construct($data){
57
- $this->import($data);
58
- }
59
 
60
- function get_link(){
61
- return $this->get_path();
62
- }
 
63
 
64
- function name(){
65
- return $this->post_title;
66
- }
 
67
 
68
- function slug(){
69
- return $this->post_name;
70
- }
 
71
 
72
- function get_path(){
73
- return $this->url_to_path($this->url);
74
- }
 
75
 
76
- function add_child($item){
77
- if (!isset($this->children)){
78
- $this->children = array();
79
- }
80
- $this->children[] = $item;
81
- }
82
 
83
- function get_children(){
84
- if (isset($this->children)){
85
- return $this->children;
86
- }
87
- return false;
88
- }
89
- }
 
 
 
 
 
 
 
 
 
1
  <?php
 
 
2
 
3
+ class TimberMenu extends TimberCore
4
+ {
5
 
6
+ var $items = null;
 
 
 
 
 
 
 
 
 
 
7
 
8
+ function __construct($slug)
9
+ {
10
+ //$menu = wp_get_nav_menu_object($slug);
11
+ $locations = get_nav_menu_locations();
12
+ if (isset($locations[$slug])) {
13
+ $menu = wp_get_nav_menu_object($locations[$slug]);
14
+ $menu = wp_get_nav_menu_items($menu);
15
+ $menu = self::order_children($menu);
16
+ $this->items = $menu;
17
+ }
18
+ return null;
19
+ }
20
 
21
+ function find_parent_item_in_menu($menu_items, $parent_id)
22
+ {
23
+ foreach ($menu_items as &$item) {
24
+ if ($item->ID == $parent_id) {
25
+ return $item;
26
+ }
27
+ }
28
+ return null;
29
+ }
 
 
 
 
 
 
30
 
31
+ function order_children($items)
32
+ {
33
+ $menu = array();
34
+ foreach ($items as $item) {
35
+ if ($item->menu_item_parent == 0) {
36
+ $menu[] = new TimberMenuItem($item);
37
+ continue;
38
+ }
39
+ $parent_id = $item->menu_item_parent;
40
+ $parent = self::find_parent_item_in_menu($menu, $parent_id);
41
+ if ($parent) {
42
+ $parent->add_child(new TimberMenuItem($item));
43
+ }
44
+ }
45
+ return $menu;
46
+ }
47
 
48
+ function get_items()
49
+ {
50
+ if (is_array($this->items)) {
51
+ return $this->items;
52
+ }
53
+ return array();
54
+ }
55
+ }
56
 
57
+ class TimberMenuItem extends TimberCore
58
+ {
59
 
60
+ var $children;
 
 
61
 
62
+ function __construct($data)
63
+ {
64
+ $this->import($data);
65
+ }
66
 
67
+ function get_link()
68
+ {
69
+ return $this->get_path();
70
+ }
71
 
72
+ function name()
73
+ {
74
+ return $this->post_title;
75
+ }
76
 
77
+ function slug()
78
+ {
79
+ return $this->post_name;
80
+ }
81
 
82
+ function get_path()
83
+ {
84
+ return $this->url_to_path($this->url);
85
+ }
 
 
86
 
87
+ function add_child($item)
88
+ {
89
+ if (!isset($this->children)) {
90
+ $this->children = array();
91
+ }
92
+ $this->children[] = $item;
93
+ }
94
+
95
+ function get_children()
96
+ {
97
+ if (isset($this->children)) {
98
+ return $this->children;
99
+ }
100
+ return false;
101
+ }
102
+ }
objects/timber-page.php CHANGED
@@ -1,19 +1,9 @@
1
  <?php
2
 
3
- class TimberPage {
4
-
5
- function __construct($path, $file){
6
 
7
- }
8
-
9
- }
10
 
11
- Timber::add_route('/articles/page/$p', function($page){
12
- if ($page > 1){
13
- $query['paged'] = $page;
14
- }
15
- });
16
 
17
- Timber::add_route('/blog/', function($page){
18
-
19
- });
1
  <?php
2
 
3
+ class TimberPage extends TimberPost {
 
 
4
 
5
+ function __construct($path, $file){
 
 
6
 
7
+ }
 
 
 
 
8
 
9
+ }
 
 
objects/timber-post.php CHANGED
@@ -1,456 +1,503 @@
1
  <?php
2
-
3
- class TimberPost extends TimberCore {
4
-
5
- var $ImageClass = 'TimberImage';
6
- var $PostClass = 'TimberPost';
7
- var $_can_edit;
8
-
9
- public static $representation = 'post';
10
-
11
- /**
12
- * If you send the contructor nothing it will try to figure out the current post id based on being inside The_Loop
13
- * @param mixed $pid
14
- * @return a TimberPost object -- woo!
15
- */
16
- function __construct($pid = null){
17
- if ($pid === null && have_posts()){
18
- ob_start();
19
- the_post();
20
- $pid = get_the_ID();
21
- $this->ID = $pid;
22
- ob_end_clean();
23
- }
24
- if (is_numeric($pid)){
25
- $this->ID = $pid;
26
- }
27
- $this->init($pid);
28
- return $this;
29
- }
30
-
31
- function init($pid = false){
32
- if ($pid === false){
33
- $pid = get_the_ID();
34
- }
35
- $this->import_info($pid);
36
- }
37
-
38
- /**
39
- * Get the URL that will edit the current post/object
40
- */
41
- function get_edit_url(){
42
- if ($this->can_edit()){
43
- return '/wp-admin/post.php?post='.$this->ID.'&action=edit';
44
- }
45
- return false;
46
- }
47
-
48
- /**
49
- * updates the post_meta of the current object with the given value
50
- *
51
- * @param string $field
52
- * @param mixed $value
53
- * @nodoc
54
- */
55
- function update($field, $value){
56
- if (isset($this->ID)) {
57
- update_post_meta($this->ID, $field, $value);
58
- $this->$field = $value;
59
- }
60
- }
61
-
62
-
63
- /**
64
- * takes a mix of integer (post ID), string (post slug), or object to return a WordPress post object from WP's built-in get_post() function
65
- *
66
- * @param mixed $pid
67
- * @return WP_Post on success
68
- */
69
- private function prepare_post_info($pid = 0){
70
- if (is_string($pid) || is_numeric($pid) || (is_object($pid) && !isset($pid->post_title)) || $pid === 0){
71
- $pid = self::check_post_id($pid);
72
- $post = get_post($pid);
73
- if ($post){
74
- return $post;
75
- } else {
76
- $post = get_page($pid);
77
- return $post;
78
- }
79
- }
80
- return $pid;
81
- }
82
-
83
-
84
- /**
85
- * helps you find the post id regardless of whetehr you send a string or whatever
86
- *
87
- * @param mixed $pid;
88
- * @return integer ID number of a post
89
- */
90
- private function check_post_id($pid){
91
- if (is_numeric($pid) && $pid === 0){
92
- $pid = get_the_ID();
93
- return $pid;
94
- }
95
- if (!is_numeric($pid) && is_string($pid)){
96
- $pid = self::get_post_id_by_name($pid);
97
- return $pid;
98
- }
99
- if (!$pid){
100
- return;
101
- }
102
- return $pid;
103
- }
104
-
105
-
106
- /**
107
- * get_post_id_by_name($post_name)
108
- * @nodoc
109
- */
110
-
111
- function get_post_id_by_name($post_name){
112
- global $wpdb;
113
- $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name'";
114
- $result = $wpdb->get_row($query);
115
- return $result->ID;
116
- }
117
-
118
-
119
- /**
120
- * ## get a preview of your post, if you have an excerpt it will use that,
121
- * ## otherwise it will pull from the post_content
122
- * <p>{{post.get_preview(50)}}</p>
123
- */
124
-
125
- function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true){
126
- $text = '';
127
- $trimmed = false;
128
- if (isset($this->post_excerpt) && strlen($this->post_excerpt)){
129
- if ($force){
130
- $text = WPHelper::trim_words($this->post_excerpt, $len);
131
- $trimmed = true;
132
- } else {
133
- $text = $this->post_excerpt;
134
- }
135
- }
136
- if (!strlen($text)){
137
- $text = WPHelper::trim_words($this->get_content(), $len, false);
138
- $trimmed = true;
139
- }
140
- if (!strlen(trim($text))){
141
- return $text;
142
- }
143
- if ($strip){
144
- $text = trim(strip_tags($text));
145
- }
146
- if (strlen($text)){
147
- $text = trim($text);
148
- $last = $text[strlen($text)-1];
149
- if ($last != '.' && $trimmed){
150
- $text .= ' &hellip; ';
151
- }
152
- $text .= ' <a href="'.$this->get_path().'" class="read-more">'.$readmore.'</a>';
153
- }
154
- return $text;
155
- }
156
-
157
- /**
158
- * gets the post custom and attaches it to the current object
159
- * @param integer $pid a post ID number
160
- * @nodoc
161
- */
162
- function import_custom($pid){
163
- $customs = get_post_custom($pid);
164
- foreach($customs as $key => $value){
165
- $v = $value[0];
166
- $this->$key = $v;
167
- if (is_serialized($v)){
168
- if (gettype(unserialize($v)) == 'array'){
169
- $this->$key = unserialize($v);
170
- }
171
- }
172
- }
173
- }
174
-
175
- /**
176
- * ## get the featured image as a TimberImage
177
- * <img src="{{post.get_thumbnail.get_src}}" />
178
- */
179
-
180
- function get_thumbnail(){
181
- if (function_exists('get_post_thumbnail_id')){
182
- $tid = get_post_thumbnail_id($this->ID);
183
- if ($tid){
184
- return new $this->ImageClass($tid);
185
- }
186
- }
187
- return null;
188
- }
189
-
190
- function get_permalink(){
191
- return get_permalink( $this->ID );
192
- }
193
-
194
- function get_link(){
195
- if (isset($this->path)){
196
- return $this->path;
197
- }
198
- }
199
-
200
- function import_info($pid){
201
- $post_info = $this->get_info($pid);
202
- $this->import($post_info);
203
- }
204
-
205
- function get_parent(){
206
- if (!$this->post_parent){
207
- return false;
208
- }
209
- return new $this->PostClass($this->post_parent);
210
- }
211
-
212
- /**
213
- * ## Gets a User object from the author of the post
214
- * <p class="byline">{{post.get_author.name}}</p>
215
- */
216
-
217
- function get_author(){
218
- if (isset($this->post_author)){
219
- return new TimberUser($this->post_author);
220
- }
221
- return false;
222
- }
223
-
224
- function get_info($pid){
225
- global $wp_rewrite;
226
- $post = $this->prepare_post_info($pid);
227
- if (!isset($post->post_title)){
228
- return;
229
- }
230
- $post->slug = $post->post_name;
231
- $post->display_date = date(get_option('date_format'), strtotime($post->post_date));
232
- $this->import_custom($post->ID);
233
- $post->status = $post->post_status;
234
- if (!isset($wp_rewrite)){
235
- return $post;
236
- } else {
237
- $post->permalink = get_permalink($post->ID);
238
- $post->path = $this->url_to_path($post->permalink);
239
- }
240
- return $post;
241
- }
242
-
243
- function get_display_date($use = 'post_date'){
244
- return date(get_option('date_format'), strtotime($this->$use));
245
- }
246
-
247
- function get_children($post_type = 'any', $childPostClass = false){
248
- if ($childPostClass == false){
249
- $childPostClass = $this->PostClass;
250
- }
251
- if ($post_type == 'parent'){
252
- $post_type = $this->post_type;
253
- }
254
- $children = get_children('post_parent='.$this->ID.'&post_type='.$post_type);
255
- foreach($children as &$child){
256
- $child = new $childPostClass($child->ID);
257
- }
258
- $children = array_values($children);
259
- return $children;
260
- }
261
-
262
- /**
263
- * {% for comment in post.get_comments %}
264
- * <p>{{comment.content}}</p>
265
- * {% endfor %}
266
- */
267
-
268
- function get_comments($ct = 0, $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment'){
269
- $args = array('post_id' => $this->ID, 'status' => $status);
270
- if ($ct > 0){
271
- $args['number'] = $ct;
272
- }
273
- $comments = get_comments($args);
274
- foreach($comments as &$comment){
275
- $comment = new $CommentClass($comment);
276
- }
277
- return $comments;
278
- }
279
-
280
- /**
281
- * <ul class="categories">
282
- * {% for cateogry in post.get_categories %}
283
- * <li>{{category.name}}</li>
284
- * {% endfor %}
285
- * </ul>
286
- */
287
-
288
-
289
- function get_categories(){
290
- return $this->get_terms('category');
291
- }
292
-
293
- function get_category(){
294
- $cats = $this->get_categories();
295
- if (count($cats) && isset($cats[0])){
296
- return $cats[0];
297
- }
298
- return null;
299
- }
300
-
301
- /** # get terms is good
302
- *
303
- */
304
-
305
- function get_terms($tax = '', $merge = true){
306
- if (!strlen($tax) || $tax == 'all' || $tax == 'any'){
307
- $taxs = get_object_taxonomies($this->post_type);
308
- } else {
309
- $taxs = array($tax);
310
- }
311
- $ret = array();
312
- foreach($taxs as $tax){
313
- if ($tax == 'tags' || $tax == 'tag'){
314
- $tax = 'post_tag';
315
- } else if ($tax == 'categories'){
316
- $tax = 'category';
317
- }
318
- $terms = wp_get_post_terms($this->ID, $tax);
319
- foreach($terms as &$term){
320
- $term = new TimberTerm($term->term_id);
321
- }
322
- if ($merge){
323
- $ret = array_merge($ret, $terms);
324
- } else if (count($terms)){
325
- $ret[$tax] = $terms;
326
- }
327
- }
328
- return $ret;
329
- }
330
-
331
- function get_image($field){
332
- return new $ImageClass($this->$field);
333
- }
334
-
335
- /**
336
- * ## Gets an array of tags for you to use
337
- * <ul class="tags">
338
- * {% for tag in post.tags %}
339
- * <li>{{tag.name}}</li>
340
- * {% endfor %}
341
- * </ul>
342
- */
343
-
344
- function get_tags(){
345
- $tags = get_the_tags($this->ID);
346
- if (is_array($tags)){
347
- $tags = array_values($tags);
348
- } else {
349
- $tags = array();
350
- }
351
- return $tags;
352
- }
353
-
354
- /**
355
- * ## Outputs the title with filters applied
356
- * <h1>{{post.get_title}}</h1>
357
- */
358
-
359
- function get_title(){
360
- $title = $this->post_title;
361
- return apply_filters('the_title', $title);
362
- }
363
-
364
- /**
365
- * ## Displays the content of the post with filters, shortcodes and wpautop applied
366
- * <div class="article-text">{{post.get_content}}</div>
367
- */
368
-
369
- function get_content($len = 0, $page = 0){
370
- $content = $this->post_content;
371
- if ($len){
372
- wp_trim_words($content, $len);
373
- }
374
- if ($page){
375
- $contents = explode('<!--nextpage-->', $content);
376
- $page--;
377
- if (count($contents) > $page){
378
- $content = $contents[$page];
379
- }
380
- }
381
- return apply_filters('the_content', ($content));
382
- }
383
-
384
- function get_post_type(){
385
- return get_post_type_object($this->post_type);
386
- }
387
-
388
- function get_comment_count(){
389
- if (isset($this->ID)){
390
- return get_comments_number($this->ID);
391
- } else {
392
- return 0;
393
- }
394
- }
395
-
396
- //This is for integration with Elliot Condon's wonderful ACF
397
- function get_field($field_name){
398
- return get_field($field_name, $this->ID);
399
- }
400
-
401
- function import_field($field_name){
402
- $this->$field_name = $this->get_field($field_name);
403
- }
404
-
405
- //Aliases
406
- function author(){
407
- return $this->get_author();
408
- }
409
-
410
- function categories(){
411
- return $this->get_terms('category');
412
- }
413
-
414
- function category(){
415
- return $this->get_category();
416
- }
417
-
418
- function children(){
419
- return $this->get_children();
420
- }
421
-
422
- function content(){
423
- return $this->get_content();
424
- }
425
-
426
- function link(){
427
- return $this->get_link();
428
- }
429
-
430
- function permalink(){
431
- return $this->get_permalink();
432
- }
433
-
434
- function terms($tax = ''){
435
- return $this->get_terms($tax);
436
- }
437
-
438
- function tags(){
439
- return $this->get_tags();
440
- }
441
-
442
- function thumbnail(){
443
- return $this->get_thumbnail();
444
- }
445
-
446
- function title(){
447
- return $this->get_title();
448
- }
449
-
450
- //Deprecated
451
- function get_path(){
452
- return $this->get_link();
453
- }
454
-
455
-
456
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+
3
+ class TimberPost extends TimberCore
4
+ {
5
+
6
+ var $ImageClass = 'TimberImage';
7
+ var $PostClass = 'TimberPost';
8
+ var $_can_edit;
9
+
10
+ public static $representation = 'post';
11
+
12
+ /**
13
+ * If you send the contructor nothing it will try to figure out the current post id based on being inside The_Loop
14
+ * @param mixed $pid
15
+ * @return \TimberPost TimberPost object -- woo!
16
+ */
17
+ function __construct($pid = null)
18
+ {
19
+ if ($pid === null && have_posts()) {
20
+ ob_start();
21
+ the_post();
22
+ $pid = get_the_ID();
23
+ $this->ID = $pid;
24
+ ob_end_clean();
25
+ }
26
+ if (is_numeric($pid)) {
27
+ $this->ID = $pid;
28
+ }
29
+ $this->init($pid);
30
+ return $this;
31
+ }
32
+
33
+ function init($pid = false)
34
+ {
35
+ if ($pid === false) {
36
+ $pid = get_the_ID();
37
+ }
38
+ $this->import_info($pid);
39
+ }
40
+
41
+ /**
42
+ * Get the URL that will edit the current post/object
43
+ */
44
+ function get_edit_url()
45
+ {
46
+ if ($this->can_edit()) {
47
+ return '/wp-admin/post.php?post=' . $this->ID . '&action=edit';
48
+ }
49
+ return false;
50
+ }
51
+
52
+ /**
53
+ * updates the post_meta of the current object with the given value
54
+ *
55
+ * @param string $field
56
+ * @param mixed $value
57
+ * @nodoc
58
+ */
59
+ function update($field, $value)
60
+ {
61
+ if (isset($this->ID)) {
62
+ update_post_meta($this->ID, $field, $value);
63
+ $this->$field = $value;
64
+ }
65
+ }
66
+
67
+
68
+ /**
69
+ * takes a mix of integer (post ID), string (post slug), or object to return a WordPress post object from WP's built-in get_post() function
70
+ *
71
+ * @param mixed $pid
72
+ * @return WP_Post on success
73
+ */
74
+ private function prepare_post_info($pid = 0)
75
+ {
76
+ if (is_string($pid) || is_numeric($pid) || (is_object($pid) && !isset($pid->post_title)) || $pid === 0) {
77
+ $pid = self::check_post_id($pid);
78
+ $post = get_post($pid);
79
+ if ($post) {
80
+ return $post;
81
+ } else {
82
+ $post = get_page($pid);
83
+ return $post;
84
+ }
85
+ }
86
+ return $pid;
87
+ }
88
+
89
+
90
+ /**
91
+ * helps you find the post id regardless of whetehr you send a string or whatever
92
+ *
93
+ * @param mixed $pid;
94
+ * @return integer ID number of a post
95
+ */
96
+ private function check_post_id($pid)
97
+ {
98
+ if (is_numeric($pid) && $pid === 0) {
99
+ $pid = get_the_ID();
100
+ return $pid;
101
+ }
102
+ if (!is_numeric($pid) && is_string($pid)) {
103
+ $pid = self::get_post_id_by_name($pid);
104
+ return $pid;
105
+ }
106
+ if (!$pid) {
107
+ return null;
108
+ }
109
+ return $pid;
110
+ }
111
+
112
+
113
+ /**
114
+ * get_post_id_by_name($post_name)
115
+ * @nodoc
116
+ */
117
+
118
+ function get_post_id_by_name($post_name)
119
+ {
120
+ global $wpdb;
121
+ $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$post_name'";
122
+ $result = $wpdb->get_row($query);
123
+ return $result->ID;
124
+ }
125
+
126
+
127
+ /**
128
+ * ## get a preview of your post, if you have an excerpt it will use that,
129
+ * ## otherwise it will pull from the post_content
130
+ * <p>{{post.get_preview(50)}}</p>
131
+ */
132
+
133
+ function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true)
134
+ {
135
+ $text = '';
136
+ $trimmed = false;
137
+ if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
138
+ if ($force) {
139
+ $text = WPHelper::trim_words($this->post_excerpt, $len);
140
+ $trimmed = true;
141
+ } else {
142
+ $text = $this->post_excerpt;
143
+ }
144
+ }
145
+ if (!strlen($text)) {
146
+ $text = WPHelper::trim_words($this->get_content(), $len, false);
147
+ $trimmed = true;
148
+ }
149
+ if (!strlen(trim($text))) {
150
+ return $text;
151
+ }
152
+ if ($strip) {
153
+ $text = trim(strip_tags($text));
154
+ }
155
+ if (strlen($text)) {
156
+ $text = trim($text);
157
+ $last = $text[strlen($text) - 1];
158
+ if ($last != '.' && $trimmed) {
159
+ $text .= ' &hellip; ';
160
+ }
161
+ $text .= ' <a href="' . $this->get_path() . '" class="read-more">' . $readmore . '</a>';
162
+ }
163
+ return $text;
164
+ }
165
+
166
+ /**
167
+ * gets the post custom and attaches it to the current object
168
+ * @param integer $pid a post ID number
169
+ * @nodoc
170
+ */
171
+ function import_custom($pid)
172
+ {
173
+ $customs = get_post_custom($pid);
174
+ foreach ($customs as $key => $value) {
175
+ $v = $value[0];
176
+ $this->$key = $v;
177
+ if (is_serialized($v)) {
178
+ if (gettype(unserialize($v)) == 'array') {
179
+ $this->$key = unserialize($v);
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ /**
186
+ * ## get the featured image as a TimberImage
187
+ * <img src="{{post.get_thumbnail.get_src}}" />
188
+ */
189
+
190
+ function get_thumbnail()
191
+ {
192
+ if (function_exists('get_post_thumbnail_id')) {
193
+ $tid = get_post_thumbnail_id($this->ID);
194
+ if ($tid) {
195
+ return new $this->ImageClass($tid);
196
+ }
197
+ }
198
+ return null;
199
+ }
200
+
201
+ function get_permalink()
202
+ {
203
+ return get_permalink($this->ID);
204
+ }
205
+
206
+ function get_link()
207
+ {
208
+ if (isset($this->path)) {
209
+ return $this->path;
210
+ }
211
+ return null;
212
+ }
213
+
214
+ function import_info($pid)
215
+ {
216
+ $post_info = $this->get_info($pid);
217
+ $this->import($post_info);
218
+ }
219
+
220
+ function get_parent()
221
+ {
222
+ if (!$this->post_parent) {
223
+ return false;
224
+ }
225
+ return new $this->PostClass($this->post_parent);
226
+ }
227
+
228
+ /**
229
+ * ## Gets a User object from the author of the post
230
+ * <p class="byline">{{post.get_author.name}}</p>
231
+ */
232
+
233
+ function get_author()
234
+ {
235
+ if (isset($this->post_author)) {
236
+ return new TimberUser($this->post_author);
237
+ }
238
+ return false;
239
+ }
240
+
241
+ function get_info($pid)
242
+ {
243
+ global $wp_rewrite;
244
+ $post = $this->prepare_post_info($pid);
245
+ if (!isset($post->post_title)) {
246
+ return null;
247
+ }
248
+ $post->slug = $post->post_name;
249
+ $post->display_date = date(get_option('date_format'), strtotime($post->post_date));
250
+ $this->import_custom($post->ID);
251
+ $post->status = $post->post_status;
252
+ if (!isset($wp_rewrite)) {
253
+ return $post;
254
+ } else {
255
+ $post->permalink = get_permalink($post->ID);
256
+ $post->path = $this->url_to_path($post->permalink);
257
+ }
258
+ return $post;
259
+ }
260
+
261
+ function get_display_date($use = 'post_date')
262
+ {
263
+ return date(get_option('date_format'), strtotime($this->$use));
264
+ }
265
+
266
+ function get_children($post_type = 'any', $childPostClass = false)
267
+ {
268
+ if ($childPostClass == false) {
269
+ $childPostClass = $this->PostClass;
270
+ }
271
+ if ($post_type == 'parent') {
272
+ $post_type = $this->post_type;
273
+ }
274
+ $children = get_children('post_parent=' . $this->ID . '&post_type=' . $post_type);
275
+ foreach ($children as &$child) {
276
+ $child = new $childPostClass($child->ID);
277
+ }
278
+ $children = array_values($children);
279
+ return $children;
280
+ }
281
+
282
+ /**
283
+ * {% for comment in post.get_comments %}
284
+ * <p>{{comment.content}}</p>
285
+ * {% endfor %}
286
+ */
287
+
288
+ function get_comments($ct = 0, $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment')
289
+ {
290
+ $args = array('post_id' => $this->ID, 'status' => $status);
291
+ if ($ct > 0) {
292
+ $args['number'] = $ct;
293
+ }
294
+ $comments = get_comments($args);
295
+ foreach ($comments as &$comment) {
296
+ $comment = new $CommentClass($comment);
297
+ }
298
+ return $comments;
299
+ }
300
+
301
+ /**
302
+ * <ul class="categories">
303
+ * {% for cateogry in post.get_categories %}
304
+ * <li>{{category.name}}</li>
305
+ * {% endfor %}
306
+ * </ul>
307
+ */
308
+
309
+
310
+ function get_categories()
311
+ {
312
+ return $this->get_terms('category');
313
+ }
314
+
315
+ function get_category()
316
+ {
317
+ $cats = $this->get_categories();
318
+ if (count($cats) && isset($cats[0])) {
319
+ return $cats[0];
320
+ }
321
+ return null;
322
+ }
323
+
324
+ /** # get terms is good
325
+ *
326
+ */
327
+
328
+ function get_terms($tax = '', $merge = true)
329
+ {
330
+ if (!strlen($tax) || $tax == 'all' || $tax == 'any') {
331
+ $taxs = get_object_taxonomies($this->post_type);
332
+ } else {
333
+ $taxs = array($tax);
334
+ }
335
+ $ret = array();
336
+ foreach ($taxs as $tax) {
337
+ if ($tax == 'tags' || $tax == 'tag') {
338
+ $tax = 'post_tag';
339
+ } else if ($tax == 'categories') {
340
+ $tax = 'category';
341
+ }
342
+ $terms = wp_get_post_terms($this->ID, $tax);
343
+ foreach ($terms as &$term) {
344
+ $term = new TimberTerm($term->term_id);
345
+ }
346
+ if ($merge) {
347
+ $ret = array_merge($ret, $terms);
348
+ } else if (count($terms)) {
349
+ $ret[$tax] = $terms;
350
+ }
351
+ }
352
+ return $ret;
353
+ }
354
+
355
+ function get_image($field)
356
+ {
357
+ return new $this->ImageClass($this->$field);
358
+ }
359
+
360
+ /**
361
+ * ## Gets an array of tags for you to use
362
+ * <ul class="tags">
363
+ * {% for tag in post.tags %}
364
+ * <li>{{tag.name}}</li>
365
+ * {% endfor %}
366
+ * </ul>
367
+ */
368
+
369
+ function get_tags()
370
+ {
371
+ $tags = get_the_tags($this->ID);
372
+ if (is_array($tags)) {
373
+ $tags = array_values($tags);
374
+ } else {
375
+ $tags = array();
376
+ }
377
+ return $tags;
378
+ }
379
+
380
+ /**
381
+ * ## Outputs the title with filters applied
382
+ * <h1>{{post.get_title}}</h1>
383
+ */
384
+
385
+ function get_title()
386
+ {
387
+ $title = $this->post_title;
388
+ return apply_filters('the_title', $title);
389
+ }
390
+
391
+ /**
392
+ * ## Displays the content of the post with filters, shortcodes and wpautop applied
393
+ * <div class="article-text">{{post.get_content}}</div>
394
+ */
395
+
396
+ function get_content($len = 0, $page = 0)
397
+ {
398
+ $content = $this->post_content;
399
+ if ($len) {
400
+ wp_trim_words($content, $len);
401
+ }
402
+ if ($page) {
403
+ $contents = explode('<!--nextpage-->', $content);
404
+ $page--;
405
+ if (count($contents) > $page) {
406
+ $content = $contents[$page];
407
+ }
408
+ }
409
+ return apply_filters('the_content', ($content));
410
+ }
411
+
412
+ function get_post_type()
413
+ {
414
+ return get_post_type_object($this->post_type);
415
+ }
416
+
417
+ function get_comment_count()
418
+ {
419
+ if (isset($this->ID)) {
420
+ return get_comments_number($this->ID);
421
+ } else {
422
+ return 0;
423
+ }
424
+ }
425
+
426
+ //This is for integration with Elliot Condon's wonderful ACF
427
+ function get_field($field_name)
428
+ {
429
+ return $this->get_field($field_name, $this->ID);
430
+ }
431
+
432
+ function import_field($field_name)
433
+ {
434
+ $this->$field_name = $this->get_field($field_name);
435
+ }
436
+
437
+ //Aliases
438
+ function author()
439
+ {
440
+ return $this->get_author();
441
+ }
442
+
443
+ function categories()
444
+ {
445
+ return $this->get_terms('category');
446
+ }
447
+
448
+ function category()
449
+ {
450
+ return $this->get_category();
451
+ }
452
+
453
+ function children()
454
+ {
455
+ return $this->get_children();
456
+ }
457
+
458
+ function comments(){
459
+ return $this->get_comments();
460
+ }
461
+
462
+ function content()
463
+ {
464
+ return $this->get_content();
465
+ }
466
+
467
+ function link()
468
+ {
469
+ return $this->get_link();
470
+ }
471
+
472
+ function permalink()
473
+ {
474
+ return $this->get_permalink();
475
+ }
476
+
477
+ function terms($tax = '')
478
+ {
479
+ return $this->get_terms($tax);
480
+ }
481
+
482
+ function tags()
483
+ {
484
+ return $this->get_tags();
485
+ }
486
+
487
+ function thumbnail()
488
+ {
489
+ return $this->get_thumbnail();
490
+ }
491
+
492
+ function title()
493
+ {
494
+ return $this->get_title();
495
+ }
496
+
497
+ //Deprecated
498
+ function get_path()
499
+ {
500
+ return $this->get_link();
501
+ }
502
+
503
+ }
objects/timber-term-getter.php ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
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();
15
+ if (is_string($taxs)){
16
+ $taxs = array($taxs);
17
+ }
18
+ $ret->taxonomies = self::correct_taxonomy_names($taxs);
19
+ return $ret;
20
+ }
21
+
22
+ public static function get_term_query_from_assoc_array($args){
23
+ $ret = new stdClass();
24
+ $ret->args = $args;
25
+ if (isset($ret->args['tax'])){
26
+ $ret->taxonomies = $ret->args['tax'];
27
+ } else if (isset($ret->args['taxonomies'])){
28
+ $ret->taxonomies = $ret->args['taxonomies'];
29
+ } else if (isset($ret->args['taxs'])){
30
+ $ret->taxonomies = $ret->args['taxs'];
31
+ } else if (isset($ret->args['taxonomy'])){
32
+ $ret->taxonomies = $ret->args['taxonomy'];
33
+ }
34
+ if (isset($ret->taxonomies)){
35
+ if (is_string($ret->taxonomies)){
36
+ $ret->taxonomies = array($ret->taxonomies);
37
+ }
38
+ $ret->taxonomies = self::correct_taxonomy_names($ret->taxonomies);
39
+ }
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
46
+ if (is_int($args[0])){
47
+ return self::get_term_query_from_array_of_ids($args);
48
+ } else if (is_string($args[0])){
49
+ return self::get_term_query_from_array_of_strings($args);
50
+ }
51
+ }
52
+ }
53
+
54
+ public static function get_term_query_from_array_of_ids($args){
55
+ $ret = new stdClass();
56
+ $ret->taxonomies = get_taxonomies();
57
+ $ret->args['include'] = $args;
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);
64
+ $ret->args = array();
65
+ return $ret;
66
+ }
67
+
68
+ private static function correct_taxonomy_names($taxs){
69
+ if (is_string($taxs)){
70
+ $taxs = array($taxs);
71
+ }
72
+ foreach($taxs as &$tax){
73
+ if ($tax == 'tags' || $tax == 'tag'){
74
+ $tax = 'post_tag';
75
+ } else if ($tax == 'categories'){
76
+ $tax = 'category';
77
+ }
78
+ }
79
+ return $taxs;
80
+ }
81
+
82
+ }
objects/timber-term.php CHANGED
@@ -1,125 +1,151 @@
1
  <?php
2
-
3
- class TimberTerm extends TimberCore {
4
-
5
- var $taxonomy;
6
- var $PostClass;
7
-
8
- function __construct($tid = null){
9
- if ($tid === null){
10
- $tid = $this->get_term_from_query();
11
- }
12
- $this->init($tid);
13
- }
14
-
15
- function get_term_from_query(){
16
- global $wp_query;
17
- $qo = $wp_query->queried_object;
18
- return $qo->term_id;
19
- }
20
-
21
- function get_page($i){
22
- return $this->get_path().'/page/'.$i;
23
- }
24
-
25
- function init($tid){
26
- global $wpdb;
27
- $term = $this->get_term($tid);
28
- if (isset($term->id)){
29
- $term->ID = $term->id;
30
- } else if (isset($term->term_id)){
31
- $term->ID = $term->term_id;
32
- } else {
33
- //echo 'bad call';
34
- WPHelper::error_log(debug_backtrace());
35
- }
36
- if (function_exists('get_fields')){
37
- //lets get whatever we can from advanced custom fields;
38
- //IF you have the wonderful ACF installed
39
- $searcher = $term->taxonomy."_".$term->ID; // save to a specific category
40
- $fields = array();
41
- $fds = get_fields($searcher);
42
- if (is_array($fds)){
43
- foreach($fds as $key=>$value){
44
- $key = preg_replace('/_/', '', $key, 1);
45
- $key = str_replace($searcher, '', $key);
46
- $key = preg_replace('/_/', '', $key, 1);
47
- $field = get_field($key, $searcher);
48
- $fields[$key] = $field;
49
- }
50
- }
51
- $this->import($fields);
52
-
53
- }
54
- $this->import($term);
55
- }
56
-
57
- function get_term($tid){
58
- if (is_object($tid) || is_array($tid)){
59
- return $tid;
60
- }
61
- $tid = self::get_tid($tid);
62
- global $wpdb;
63
- $query = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id = '$tid'";
64
- $tax = $wpdb->get_row($query);
65
- if (isset($tax) && isset($tax->taxonomy)){
66
- if ($tax->taxonomy){
67
- $term = get_term($tid, $tax->taxonomy);
68
- return $term;
69
- }
70
- }
71
- return null;
72
- }
73
-
74
- function get_tid($tid){
75
- global $wpdb;
76
- if (is_numeric($tid)){
77
- return $tid;
78
- }
79
- if (gettype($tid) == 'object'){
80
- $tid = $tid->term_id;
81
- }
82
- if (is_numeric($tid)){
83
- $query = "SELECT * FROM $wpdb->terms WHERE term_id = '$tid'";
84
- } else {
85
- $query = "SELECT * FROM $wpdb->terms WHERE slug = '$tid'";
86
- }
87
-
88
- $result = $wpdb->get_row($query);
89
- if (isset($result->term_id)){
90
- $result->ID = $result->term_id;
91
- return $result->ID;
92
- }
93
- return 0;
94
- }
95
-
96
- function get_path(){
97
- $p = WPHelper::get_path_base();
98
- return $p.$this->get_url();
99
- }
100
-
101
- function get_url(){
102
- $base = $this->taxonomy;
103
- if ($base == 'post_tag'){
104
- $base = 'tag';
105
- }
106
- return $base.'/'.$this->slug;
107
- }
108
-
109
- function get_posts($numberposts = 10, $post_type = 'any', $PostClass = ''){
110
- if (!strlen($PostClass)){
111
- $PostClass = $this->PostClass;
112
- }
113
- $args = array(
114
- 'numberposts' => $numberposts,
115
- 'tax_query' => array(array(
116
- 'field' => 'id',
117
- 'terms' => $this->ID,
118
- 'taxonomy' => $this->taxonomy,
119
- )),
120
- 'post_type' => $post_type
121
- );
122
- return Timber::get_posts($args, $PostClass);
123
- }
124
-
125
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+
3
+ class TimberTerm extends TimberCore {
4
+
5
+ var $taxonomy;
6
+ var $PostClass;
7
+
8
+ function __construct($tid = null) {
9
+ if ($tid === null) {
10
+ $tid = $this->get_term_from_query();
11
+ }
12
+ $this->init($tid);
13
+ }
14
+
15
+ function __toString(){
16
+ return $this->name;
17
+ }
18
+
19
+ function get_term_from_query()
20
+ {
21
+ global $wp_query;
22
+ $qo = $wp_query->queried_object;
23
+ return $qo->term_id;
24
+ }
25
+
26
+ function get_page($i)
27
+ {
28
+ return $this->get_path() . '/page/' . $i;
29
+ }
30
+
31
+ function init($tid)
32
+ {
33
+ global $wpdb;
34
+ $term = $this->get_term($tid);
35
+ if (isset($term->id)) {
36
+ $term->ID = $term->id;
37
+ } else if (isset($term->term_id)) {
38
+ $term->ID = $term->term_id;
39
+ } else {
40
+ //echo 'bad call';
41
+ WPHelper::error_log(debug_backtrace());
42
+ }
43
+ if (function_exists('get_fields')) {
44
+ //lets get whatever we can from advanced custom fields;
45
+ //IF you have the wonderful ACF installed
46
+ $searcher = $term->taxonomy . "_" . $term->ID; // save to a specific category
47
+ $fields = array();
48
+ $fds = get_fields($searcher);
49
+ if (is_array($fds)) {
50
+ foreach ($fds as $key => $value) {
51
+ $key = preg_replace('/_/', '', $key, 1);
52
+ $key = str_replace($searcher, '', $key);
53
+ $key = preg_replace('/_/', '', $key, 1);
54
+ $field = get_field($key, $searcher);
55
+ $fields[$key] = $field;
56
+ }
57
+ }
58
+ $this->import($fields);
59
+
60
+ }
61
+ $this->import($term);
62
+ }
63
+
64
+ function get_term($tid)
65
+ {
66
+ if (is_object($tid) || is_array($tid)) {
67
+ return $tid;
68
+ }
69
+ $tid = self::get_tid($tid);
70
+ global $wpdb;
71
+ $query = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id = '$tid'";
72
+ $tax = $wpdb->get_row($query);
73
+ if (isset($tax) && isset($tax->taxonomy)) {
74
+ if ($tax->taxonomy) {
75
+ $term = get_term($tid, $tax->taxonomy);
76
+ return $term;
77
+ }
78
+ }
79
+ return null;
80
+ }
81
+
82
+ function get_tid($tid)
83
+ {
84
+ global $wpdb;
85
+ if (is_numeric($tid)) {
86
+ return $tid;
87
+ }
88
+ if (gettype($tid) == 'object') {
89
+ $tid = $tid->term_id;
90
+ }
91
+ if (is_numeric($tid)) {
92
+ $query = "SELECT * FROM $wpdb->terms WHERE term_id = '$tid'";
93
+ } else {
94
+ $query = "SELECT * FROM $wpdb->terms WHERE slug = '$tid'";
95
+ }
96
+
97
+ $result = $wpdb->get_row($query);
98
+ if (isset($result->term_id)) {
99
+ $result->ID = $result->term_id;
100
+ return $result->ID;
101
+ }
102
+ return 0;
103
+ }
104
+
105
+ function get_path()
106
+ {
107
+ $p = WPHelper::get_path_base();
108
+ return $p . $this->get_url();
109
+ }
110
+
111
+ function get_link(){
112
+ return $this->get_path();
113
+ }
114
+
115
+ function get_url()
116
+ {
117
+ $base = $this->taxonomy;
118
+ if ($base == 'post_tag') {
119
+ $base = 'tag';
120
+ }
121
+ return $base . '/' . $this->slug;
122
+ }
123
+
124
+ function get_posts($numberposts = 10, $post_type = 'any', $PostClass = '') {
125
+ if (!strlen($PostClass)) {
126
+ $PostClass = $this->PostClass;
127
+ }
128
+ $args = array(
129
+ 'numberposts' => $numberposts,
130
+ 'tax_query' => array(array(
131
+ 'field' => 'id',
132
+ 'terms' => $this->ID,
133
+ 'taxonomy' => $this->taxonomy,
134
+ )),
135
+ 'post_type' => $post_type
136
+ );
137
+ return Timber::get_posts($args, $PostClass);
138
+ }
139
+
140
+ /* Alias
141
+ ====================== */
142
+
143
+ public function url(){
144
+ return $this->get_url();
145
+ }
146
+
147
+ public function link(){
148
+ return $this->get_link();
149
+ }
150
+
151
+ }
objects/timber-user.php CHANGED
@@ -1,67 +1,78 @@
1
  <?php
2
-
3
- class TimberUser extends TimberCore {
4
 
5
- function __construct($uid = false){
6
- $this->init($uid);
7
- }
8
 
9
- public function get_link(){
10
- $p = WPHelper::get_path_base();
11
- return $p.'author/'.$this->slug();
12
- }
13
 
14
- function init($uid = false){
15
- if (!$uid){
16
- $uid = get_current_user_id();
17
- }
18
- if (function_exists('get_userdata')){
19
- $data = get_userdata($uid);
20
- if (is_object($data) && isset($data)){
21
- $this->import($data->data);
22
- }
23
- }
24
 
25
- $this->ID = $uid;
26
- $this->import_custom();
27
- }
 
 
 
 
 
 
 
 
28
 
29
- function get_custom(){
30
- if ($this->ID){
31
- $um = get_user_meta($this->ID);
32
- $custom = new stdClass();
33
- foreach($um as $key => $value){
34
- $v = $value[0];
35
- $custom->$key = $v;
36
- if (is_serialized($v)){
37
- if (gettype(unserialize($v)) == 'array'){
38
- $custom->$key = unserialize($v);
39
- }
40
- }
41
- }
42
- return $custom;
43
- }
44
- }
45
 
46
- function import_custom(){
47
- $custom = $this->get_custom();
48
- $this->import($custom);
49
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
-
52
- function name(){
53
- return $this->display_name;
54
- }
 
55
 
56
- function get_path(){
57
- return $this->get_link();
58
- }
59
 
60
- function path(){
61
- return $this->get_path();
62
- }
 
63
 
64
- function slug(){
65
- return $this->user_nicename;
66
- }
67
- }
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
 
 
2
 
3
+ class TimberUser extends TimberCore
4
+ {
 
5
 
6
+ function __construct($uid = false)
7
+ {
8
+ $this->init($uid);
9
+ }
10
 
11
+ public function get_link()
12
+ {
13
+ $p = WPHelper::get_path_base();
14
+ return $p . 'author/' . $this->slug();
15
+ }
 
 
 
 
 
16
 
17
+ function init($uid = false)
18
+ {
19
+ if (!$uid) {
20
+ $uid = get_current_user_id();
21
+ }
22
+ if (function_exists('get_userdata')) {
23
+ $data = get_userdata($uid);
24
+ if (is_object($data) && isset($data)) {
25
+ $this->import($data->data);
26
+ }
27
+ }
28
 
29
+ $this->ID = $uid;
30
+ $this->import_custom();
31
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ function get_custom()
34
+ {
35
+ if ($this->ID) {
36
+ $um = get_user_meta($this->ID);
37
+ $custom = new stdClass();
38
+ foreach ($um as $key => $value) {
39
+ $v = $value[0];
40
+ $custom->$key = $v;
41
+ if (is_serialized($v)) {
42
+ if (gettype(unserialize($v)) == 'array') {
43
+ $custom->$key = unserialize($v);
44
+ }
45
+ }
46
+ }
47
+ return $custom;
48
+ }
49
+ return null;
50
+ }
51
 
52
+ function import_custom()
53
+ {
54
+ $custom = $this->get_custom();
55
+ $this->import($custom);
56
+ }
57
 
 
 
 
58
 
59
+ function name()
60
+ {
61
+ return $this->display_name;
62
+ }
63
 
64
+ function get_path()
65
+ {
66
+ return $this->get_link();
67
+ }
68
+
69
+ function path()
70
+ {
71
+ return $this->get_path();
72
+ }
73
+
74
+ function slug()
75
+ {
76
+ return $this->user_nicename;
77
+ }
78
+ }
readme.txt CHANGED
@@ -2,7 +2,7 @@
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.5
5
- Stable tag: 0.10.3
6
  Tested up to: 3.6
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -32,6 +32,14 @@ Timber is great for any WordPress developer who cares about writing good, mainta
32
 
33
  == Changelog ==
34
 
 
 
 
 
 
 
 
 
35
  = 0.10.3 =
36
  * Corrected error with sidebar retrieval
37
  * language_attributes are now avaiable as part of Timber::get_context(); payload.
2
  Contributors: jarednova
3
  Tags: template engine, templates, twig
4
  Requires at least: 3.5
5
+ Stable tag: 0.10.4
6
  Tested up to: 3.6
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
32
 
33
  == Changelog ==
34
 
35
+ = 0.10.4 =
36
+ * Lots of code cleanup thanks to [Jakub](http://github.com/hsz)
37
+ * Added new function for bloginfo
38
+ * You can now hook into timber_context to filter the $context object
39
+ * Added Timber::get_terms to retrive lists of your blog's terms
40
+ * Added better support for translation
41
+ * Added filter for executing a function, ie {{'my_theme_function'|filter}}
42
+
43
  = 0.10.3 =
44
  * Corrected error with sidebar retrieval
45
  * language_attributes are now avaiable as part of Timber::get_context(); payload.
tests/timber-post-test.php DELETED
@@ -1,26 +0,0 @@
1
- <?php
2
-
3
- require_once('../objects/timber-post.php');
4
-
5
- class TimberPostTest extends PHPUnit_testCase {
6
- var $post;
7
-
8
- function TimberPostTest($query){
9
- $this->PHPUnit_TestCase($query);
10
- }
11
-
12
- function setUp(){
13
- $this->post = new TimberPost();
14
- }
15
-
16
- function tearDown(){
17
- unset($this->post);
18
- }
19
-
20
- function test_get_thumbnail(){
21
- $image = $this->post->get_thumbnail();
22
- $this->assertTrue(is_string($image->get_src));
23
- }
24
-
25
-
26
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
timber-starter-theme/404.php CHANGED
@@ -2,12 +2,11 @@
2
  /**
3
  * The template for displaying 404 pages (Not Found)
4
  *
5
- * Methods for PostMaster and WPHelper can be found in the /functions sub-directory
6
  *
7
- * @package WordPress
8
- * @subpackage Timber
9
- * @since Timber 0.1
10
  */
11
 
12
- Timber::render('404.twig');
13
- ?>
2
  /**
3
  * The template for displaying 404 pages (Not Found)
4
  *
5
+ * Methods for WPHelper can be found in the /functions sub-directory
6
  *
7
+ * @package WordPress
8
+ * @subpackage Timber
9
+ * @since Timber 0.1
10
  */
11
 
12
+ Timber::render('404.twig');
 
timber-starter-theme/author.php CHANGED
@@ -4,18 +4,17 @@
4
  *
5
  * Methods for WPHelper can be found in the /functions sub-directory
6
  *
7
- * @package WordPress
8
- * @subpackage Timber
9
- * @since Timber 0.1
10
  */
11
- global $wp_query;
12
 
13
- $data = Timber::get_context();
14
- $data['posts'] = Timber::get_posts();
15
 
16
- $author = new TimberUser($wp_query->query_vars['author']);
17
- $data['author'] = $author;
18
- $data['title'] = 'Author Archives: '.$author->name();
19
 
20
- Timber::render(array('author.twig', 'archive.twig'), $data);
21
- ?>
4
  *
5
  * Methods for WPHelper can be found in the /functions sub-directory
6
  *
7
+ * @package WordPress
8
+ * @subpackage Timber
9
+ * @since Timber 0.1
10
  */
11
+ global $wp_query;
12
 
13
+ $data = Timber::get_context();
14
+ $data['posts'] = Timber::get_posts();
15
 
16
+ $author = new TimberUser($wp_query->query_vars['author']);
17
+ $data['author'] = $author;
18
+ $data['title'] = 'Author Archives: ' . $author->name();
19
 
20
+ Timber::render(array('author.twig', 'archive.twig'), $data);
 
timber-starter-theme/functions.php CHANGED
@@ -1,8 +1,22 @@
1
  <?php
2
 
 
 
 
3
  add_filter('get_twig', 'add_to_twig');
 
 
 
 
 
4
  define('THEME_URL', get_template_directory_uri());
5
 
 
 
 
 
 
 
6
  function add_to_twig($twig){
7
  /* this is where you can add your own fuctions to twig */
8
  $twig->addExtension(new Twig_Extension_StringLoader());
@@ -14,24 +28,13 @@
14
  }
15
 
16
  function load_styles(){
17
-
18
  wp_register_style( 'screen', THEME_URL.'/style.css', '', '', 'screen' );
19
  wp_enqueue_style( 'screen' );
20
  }
21
 
22
- function osort(&$array, $prop) {
23
- usort($array, function($a, $b) use ($prop) {
24
- return $a->$prop > $b->$prop ? 1 : -1;
25
- });
26
- }
27
-
28
- register_activation_hook(__FILE__, 'my_activation');
29
-
30
- add_action('wp_enqueue_scripts', 'load_scripts');
31
- add_action('wp_enqueue_scripts', 'load_styles');
32
 
33
- add_theme_support('post-formats');
34
- add_theme_support('post-thumbnails');
35
 
36
 
37
 
1
  <?php
2
 
3
+ add_theme_support('post-formats');
4
+ add_theme_support('post-thumbnails');
5
+
6
  add_filter('get_twig', 'add_to_twig');
7
+ add_filter('timber_context', 'add_to_context');
8
+
9
+ add_action('wp_enqueue_scripts', 'load_scripts');
10
+ add_action('wp_enqueue_scripts', 'load_styles');
11
+
12
  define('THEME_URL', get_template_directory_uri());
13
 
14
+ function add_to_context($data){
15
+ /* this is where you can add your own data to Timber's context object */
16
+ $data['foo'] = 'bar';
17
+ return $data;
18
+ }
19
+
20
  function add_to_twig($twig){
21
  /* this is where you can add your own fuctions to twig */
22
  $twig->addExtension(new Twig_Extension_StringLoader());
28
  }
29
 
30
  function load_styles(){
 
31
  wp_register_style( 'screen', THEME_URL.'/style.css', '', '', 'screen' );
32
  wp_enqueue_style( 'screen' );
33
  }
34
 
35
+
 
 
 
 
 
 
 
 
 
36
 
37
+
 
38
 
39
 
40
 
timber-starter-theme/js/site.js CHANGED
@@ -1,7 +1,5 @@
 
1
 
2
- jQuery(document).ready(function($) {
3
-
4
- // Your JavaScript goes here
5
-
6
- });
7
 
 
1
+ jQuery(document).ready(function($) {
2
 
3
+ // Your JavaScript goes here
 
 
 
 
4
 
5
+ });
timber-starter-theme/page.php CHANGED
@@ -16,14 +16,12 @@
16
  *
17
  * Methods for WPHelper can be found in the /functions sub-directory
18
  *
19
- * @package WordPress
20
- * @subpackage Timber
21
- * @since Timber 0.1
22
  */
23
- ?>
24
- <?php
25
-
26
- $context = Timber::get_context();
27
- $post = new TimberPost();
28
- $context['post'] = $post;
29
- Timber::render(array('page-'.$post->post_name.'.twig', 'page.twig'), $context);
16
  *
17
  * Methods for WPHelper can be found in the /functions sub-directory
18
  *
19
+ * @package WordPress
20
+ * @subpackage Timber
21
+ * @since Timber 0.1
22
  */
23
+
24
+ $context = Timber::get_context();
25
+ $post = new TimberPost();
26
+ $context['post'] = $post;
27
+ Timber::render(array('page-' . $post->post_name . '.twig', 'page.twig'), $context);
 
 
timber-starter-theme/sidebar.php CHANGED
@@ -3,9 +3,8 @@
3
  * The Template for displaying all single posts
4
  *
5
  *
6
- * @package WordPress
7
- * @subpackage Timber
8
  */
9
 
10
- Timber::render(array('sidebar.twig'), $data);
11
- ?>
3
  * The Template for displaying all single posts
4
  *
5
  *
6
+ * @package WordPress
7
+ * @subpackage Timber
8
  */
9
 
10
+ Timber::render(array('sidebar.twig'), $data);
 
timber-starter-theme/single.php CHANGED
@@ -4,16 +4,15 @@
4
  *
5
  * Methods for WPHelper can be found in the /functions sub-directory
6
  *
7
- * @package WordPress
8
- * @subpackage Timber
9
- * @since Timber 0.1
10
  */
11
 
12
- $context = Timber::get_context();
13
- $post = new TimberPost();
14
- $context['post'] = $post;
15
- $context['wp_title'] .= ' - '.$post->post_title;
16
- $context['comment_form'] = WPHelper::get_comment_form($pid);
17
 
18
- Timber::render(array('single-'.$post->post_type.'.twig','single.twig'), $context);
19
- ?>
4
  *
5
  * Methods for WPHelper can be found in the /functions sub-directory
6
  *
7
+ * @package WordPress
8
+ * @subpackage Timber
9
+ * @since Timber 0.1
10
  */
11
 
12
+ $context = Timber::get_context();
13
+ $post = new TimberPost();
14
+ $context['post'] = $post;
15
+ $context['wp_title'] .= ' - ' . $post->post_title;
16
+ $context['comment_form'] = WPHelper::get_comment_form($pid);
17
 
18
+ Timber::render(array('single-' . $post->post_type . '.twig', 'single.twig'), $context);
 
timber-starter-theme/views/html-header.twig CHANGED
@@ -1,13 +1,15 @@
1
  <!doctype html>
2
- <!--[if lt IE 7]><html class="no-js ie ie6 lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
3
- <!--[if IE 7]><html class="no-js ie ie7 lt-ie9 lt-ie8" lang="en"> <![endif]-->
4
- <!--[if IE 8]><html class="no-js ie ie8 lt-ie9" lang="en"> <![endif]-->
5
  <!--[if gt IE 8]><!--><html class="no-js" {{language_attributes}}> <!--<![endif]-->
6
  <head>
7
- <title>{{wp_title}}</title>
8
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
9
- <meta charset="UTF-8" />
10
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 
 
11
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
12
- <link rel="pingback" href="/xmlrpc.php" />
13
  {{wp_head}}
1
  <!doctype html>
2
+ <!--[if lt IE 7]><html class="no-js ie ie6 lt-ie9 lt-ie8 lt-ie7" lang="{{language_attributes}}"> <![endif]-->
3
+ <!--[if IE 7]><html class="no-js ie ie7 lt-ie9 lt-ie8" lang="{{language_attributes}}"> <![endif]-->
4
+ <!--[if IE 8]><html class="no-js ie ie8 lt-ie9" lang="{{language_attributes}}"> <![endif]-->
5
  <!--[if gt IE 8]><!--><html class="no-js" {{language_attributes}}> <!--<![endif]-->
6
  <head>
7
+ <meta charset="{{ bloginfo('charset') }}" />
8
+ <title>{{wp_title}}</title>
9
+ <meta name="description" content="{{ bloginfo('description') }}">
10
+ <link rel="stylesheet" href="{{ stylesheet_uri }}" type="text/css" media="screen" />
11
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
12
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
13
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
14
+ <link rel="pingback" href="{{ bloginfo('pingback_url') }}" />
15
  {{wp_head}}
timber.php CHANGED
@@ -3,424 +3,443 @@
3
  Plugin Name: Timber
4
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
5
  Author: Jared Novack + Upstatement
6
- Version: 0.10.3
7
  Author URI: http://timber.upstatement.com/
8
  */
9
 
10
  global $wp_version;
11
  global $timber;
12
- $exit_msg = 'Timber reqiures WordPress 3.0 or newer';
13
- if (version_compare($wp_version, '3.0', '<')){
14
- exit ($exit_msg);
15
- }
16
 
17
- require_once(__DIR__.'/functions/functions-twig.php');
18
- require_once(__DIR__.'/functions/functions-post-master.php');
19
- require_once(__DIR__.'/functions/functions-php-helper.php');
20
- require_once(__DIR__.'/functions/functions-wp-helper.php');
21
 
22
- require_once(__DIR__.'/objects/timber-core.php');
23
- require_once(__DIR__.'/objects/timber-post.php');
24
- require_once(__DIR__.'/objects/timber-comment.php');
25
- require_once(__DIR__.'/objects/timber-user.php');
26
- require_once(__DIR__.'/objects/timber-term.php');
27
- require_once(__DIR__.'/objects/timber-image.php');
28
- require_once(__DIR__.'/objects/timber-menu.php');
 
29
 
30
- require_once(__DIR__.'/objects/timber-loader.php');
31
- //require_once(__DIR__.'/admin/timber-admin.php');
32
 
33
 
34
  /** Usage:
35
- *
36
- * $posts = Timber::get_posts();
37
- * $posts = Timber::get_posts('post_type = article')
38
- * $posts = Timber::get_posts(array('post_type' => 'article', 'category_name' => 'sports')); // uses wp_query format
39
- * $posts = Timber::get_posts(array(23,24,35,67), 'InkwellArticle');
40
- *
41
- * $context = Timber::get_context(); // returns wp favorites!
42
- *
43
- * Timber::render('index.twig', $context);
44
- */
45
-
46
  class Timber {
47
 
48
- public static $locations;
49
- public static $dirname = 'views';
50
- public static $cache = false;
51
-
52
- var $router;
53
-
54
- function __construct(){
55
- $this->init_constants();
56
- $timber_twig = new TimberTwig();
57
- add_action('init', array(&$this, 'init_routes'));
58
- }
59
-
60
- public function get_post($query = false, $PostClass = 'TimberPost'){
61
- if (is_int($query)){
62
- /* its a post id number */
63
- $query = array($query);
64
- }
65
- $posts = self::get_posts($query, $PostClass);
66
- if (count($posts) && is_array($posts)){
67
- return $posts[0];
68
- }
69
- return $posts;
70
- }
71
-
72
- public function get_posts($query = false, $PostClass = 'TimberPost'){
73
- if (self::is_post_class_or_class_map($query)){
74
- $PostClass = $query;
75
- $query = false;
76
- }
77
-
78
- if (WPHelper::is_array_assoc($query) || (is_string($query) && strstr($query, '='))) {
79
- // we have a regularly formed WP query string or array to use
80
- return self::get_posts_from_wp_query($query, $PostClass);
81
- } else if (is_string($query) && !is_integer($query)){
82
- // we have what could be a post name to pull out
83
- return self::get_posts_from_slug($query, $PostClass);
84
- } else if (is_array($query) && count($query) && (is_integer($query[0]) || is_string($query[0]))){
85
- // we have a list of pids (post IDs) to extract from
86
- return self::get_posts_from_array_of_ids($query, $PostClass);
87
-
88
- } else if(is_array($query) && count($query) && isset($query[0]) && is_object($query[0])){
89
- // maybe its an array of post objects that already have data
90
- return self::handle_post_results($query, $PostClass);
91
-
92
- } else if (have_posts()){
93
- //lets just use the default WordPress current query
94
- return self::get_posts_from_loop($PostClass);
95
-
96
- } else if (!$query){
97
- //okay, everything failed lets just return some posts so that the user has something to work with
98
- //this turns out to cause all kinds of awful behavior
99
- //return self::get_posts_from_wp_query(array(), $PostClass);
100
- return null;
101
-
102
- } else {
103
- error_log('I have failed you! in timber.php::94');
104
- WPHelper::error_log($query);
105
- }
106
- return $query;
107
- }
108
-
109
- // TODO: new interface for loop_to_ids
110
- public function get_pids($query = false) {
111
- $posts = get_posts($query);
112
- $pids = array();
113
- foreach($posts as $post){
114
- if ($post->ID){
115
- $pids[] = $post->ID;
116
- }
117
- }
118
- return $pids;
119
- }
120
-
121
- /* Experimental */
122
- public function get_pagination(){
123
- global $wp_query;
124
- global $paged;
125
- $data = array();
126
- $data['pages'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
127
- $paged = 1;
128
- if (isset($wp_query->query_vars['paged'])){
129
- $paged = $wp_query->query_vars['paged'];
130
- }
131
- $data['base'] = get_pagenum_link(0);
132
- $data['paged'] = $paged;
133
- if ($paged < $data['pages']){
134
- $data['next'] = array('link' => next_posts(0, false), 'path' => next_posts(0, false));
135
- }
136
- if ($paged > 1){
137
- $data['prev'] = array('link' => previous_posts(false), 'path' => previous_posts(false));
138
- }
139
- return $data;
140
- }
141
-
142
- function get_posts_from_loop($PostClass){
143
- $results = self::get_pids_from_loop();
144
- return self::handle_post_results($results, $PostClass);
145
- }
146
-
147
- function get_posts_from_slug($slug, $PostClass){
148
- global $wpdb;
149
- $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'";
150
- if (strstr($slug, '#')){
151
- //we have a post_type directive here
152
- $q = explode('#', $slug);
153
- $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$q[1]' AND post_type = '$q[0]'";
154
- }
155
- $results = $wpdb->get_col($query);
156
- return self::handle_post_results($results, $PostClass);
157
- }
158
-
159
- function get_posts_from_wp_query($query = array(), $PostClass = 'TimberPost'){
160
- $results = get_posts($query);
161
- return self::handle_post_results($results, $PostClass);
162
- }
163
-
164
- function get_posts_from_array_of_ids($query = array(), $PostClass = 'TimberPost'){
165
- if (!is_array($query) || !count($query)){
166
- return null;
167
- }
168
- global $wpdb;
169
- $query_list = implode(', ', $query);
170
- $results = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE ID IN ($query_list)");
171
- $results = array_intersect($query, $results);
172
- return self::handle_post_results($results, $PostClass);
173
- }
174
-
175
- function handle_post_results($results, $PostClass = 'TimberPost'){
176
- $posts = array();
177
- foreach($results as $rid){
178
- $PostClassUse = $PostClass;
179
- if (is_array($PostClass)){
180
- $post_type = get_post_type($rid);
181
- $PostClassUse = 'TimberPost';
182
- if (isset($PostClass[$post_type])){
183
- $PostClassUse = $PostClass[$post_type];
184
- } else {
185
- if (is_array($PostClass)){
186
- error_log($post_type.' not found in '.print_r($PostClass, true));
187
- } else {
188
- error_log($post_type.' not found in '.$PostClass);
189
- }
190
- }
191
- }
192
- $post = new $PostClassUse($rid);
193
- if (isset($post->post_title)){
194
- $posts[] = $post;
195
- }
196
- }
197
- return $posts;
198
- }
199
-
200
- public function get_sidebar($sidebar = '', $data = array()){
201
- if ($sidebar == ''){
202
- $sidebar = 'sidebar.php';
203
- }
204
- if (strstr(strtolower($sidebar), '.php')){
205
- return self::get_sidebar_from_php($sidebar, $data);
206
- }
207
- return self::render($sidebar, $data, false);
208
- }
209
-
210
- function get_sidebar_from_php($sidebar = '', $data){
211
- $context = $data;
212
- $caller = self::get_calling_script_dir();
213
- $loader = new TimberLoader();
214
- $uris = $loader->get_locations($caller);
215
- ob_start();
216
- $found = false;
217
- foreach($uris as $uri){
218
- if (file_exists(trailingslashit($uri).$sidebar)){
219
- include(trailingslashit($uri).$sidebar);
220
- $found = true;
221
- break;
222
- }
223
- }
224
- if (!$found){
225
- error_log('error loading your sidebar, check to make sure the file exists');
226
- }
227
- $ret = ob_get_contents();
228
- ob_end_clean();
229
- return $ret;
230
- }
231
-
232
- //this function is deprecated in favor of:
233
- //Timber::get_posts(false, $PostClass);
234
- function loop_to_posts($PostClass = 'TimberPost'){
235
- return self::get_posts(false, $PostClass);
236
- }
237
-
238
-
239
- // TODO: new interface for loop_to_id
240
- function get_pid() {
241
-
242
- }
243
-
244
- // shortcut function for common wordpress things
245
- function get_wp_context() {
246
-
247
- }
248
-
249
- /* ----
250
-
251
- "private"
252
-
253
- */
254
-
255
- // returns ids of posts from current query
256
- function get_pids_from_loop(){
257
- $posts = array();
258
- $i = 0;
259
- ob_start();
260
- while ( have_posts() && $i < 99999 ) {
261
- the_post();
262
- $posts[] = get_the_ID();
263
- $i++;
264
- }
265
- //why is this here? seems to only cause pain.
266
- //wp_reset_query();
267
- ob_end_clean();
268
- return $posts;
269
- }
270
-
271
- function loop_to_id(){
272
- if (have_posts()){
273
- the_post();
274
- wp_reset_query();
275
- return get_the_ID();
276
- }
277
- return false;
278
- }
279
-
280
- function get_calling_script_path(){
281
- $dir = self::get_calling_script_dir();
282
- return str_replace($_SERVER['DOCUMENT_ROOT'], '', $dir);
283
- }
284
-
285
- function get_calling_script_dir(){
286
- $backtrace = debug_backtrace();
287
- foreach($backtrace as $trace){
288
- if ($trace['file'] != __FILE__){
289
- $caller = $trace['file'];
290
- break;
291
- }
292
- }
293
- $pathinfo = pathinfo($caller);
294
- $dir = $pathinfo['dirname'];
295
- return $dir;
296
- }
297
-
298
- function render($filenames, $data = array(), $echo = true){
299
- $caller = self::get_calling_script_dir();
300
- $loader = new TimberLoader($caller);
301
- $file = $loader->choose_template($filenames);
302
- $output = '';
303
- if (strlen($file)){
304
- $output = $loader->render($file, $data);
305
- }
306
- if ($echo){
307
- echo $output;
308
- }
309
- return $output;
310
- }
311
-
312
- // TODO: move into wp shortcut function
313
- function get_context(){
314
- $data = array();
315
- $data['http_host'] = 'http://'.$_SERVER['HTTP_HOST'];
316
- $data['wp_title'] = get_bloginfo('name');
317
- $data['wp_head'] = self::get_wp_head();
318
- $data['wp_footer'] = self::get_wp_footer();
319
- $data['body_class'] = implode(' ', get_body_class());
320
- $data['bloginfo'] = array();
321
- $data['bloginfo']['name'] = get_bloginfo('name');
322
- $data['bloginfo']['description'] = get_bloginfo('description');
323
- $data['bloginfo']['admin_email'] = get_bloginfo('admin_email');
324
- if (function_exists('wp_nav_menu')){
325
- $data['wp_nav_menu'] = wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' , 'echo' => false) );
326
- }
327
- $data['language_attributes'] = WPHelper::ob_function('language_attributes');
328
- return $data;
329
- }
330
-
331
- function is_post_class_or_class_map($arg){
332
- if (is_string($arg) && class_exists($arg)){
333
- return true;
334
- }
335
- if (is_array($arg)){
336
- foreach($arg as $item){
337
- if (is_string($item) && class_exists($item)){
338
- return true;
339
- }
340
- }
341
- }
342
- return false;
343
- }
344
-
345
- function init_constants(){
346
- $timber_loc = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', realpath(__DIR__));
347
- $plugin_url_path = str_replace($_SERVER['HTTP_HOST'], '', plugins_url());
348
- $plugin_url_path = str_replace('https://', '', $plugin_url_path);
349
- $plugin_url_path = str_replace('http://', '', $plugin_url_path);
350
-
351
- $timber_dirs = dirname(__FILE__);
352
- $timber_dirs = explode('/', $timber_dirs);
353
- $timber_dirname = array_pop($timber_dirs);
354
-
355
- define("TIMBER", $timber_loc);
356
- define("TIMBER_URL_PATH", trailingslashit($plugin_url_path).trailingslashit($timber_dirname));
357
-
358
- define("TIMBER_URL", 'http://'.$_SERVER["HTTP_HOST"].TIMBER);
359
- define("TIMBER_LOC", realpath(__DIR__));
360
- }
361
-
362
- /* Routes */
363
- /* ==================== */
364
-
365
- function init_routes(){
366
- global $timber;
367
- if (isset($timber->router)){
368
- $route = $timber->router->matchCurrentRequest();
369
- if ($route){
370
- $callback = $route->getTarget();
371
- $params = $route->getParameters();
372
- $callback($params);
373
- }
374
- }
375
- }
376
-
377
- function add_route($route, $callback){
378
- global $timber;
379
- if (!isset($timber->router)){
380
- require_once('router/Router.php');
381
- require_once('router/Route.php');
382
- $timber->router = new Router();
383
- $timber->router->setBasePath('/');
384
- }
385
- $timber->router->map($route, $callback);
386
- }
387
-
388
- function load_template($template, $query = false){
389
- if ($query){
390
- global $wp_query;
391
- $wp_query = new WP_Query($query);
392
- }
393
- $template = locate_template($template);
394
- $GLOBALS['timber_template'] = $template;
395
- add_action('send_headers', function(){
396
- header('HTTP/1.1 200 OK');
397
- });
398
- add_action('wp_loaded', function($template){
399
- if (isset($GLOBALS['timber_template'])){
400
- load_template($GLOBALS['timber_template']);
401
- die;
402
- }
403
- }, 10, 1);
404
- }
405
-
406
-
407
- // TODO: move into wp shortcut function
408
- function get_wp_footer(){
409
- ob_start();
410
- wp_footer();
411
- $ret = ob_get_contents();
412
- ob_end_clean();
413
- return $ret;
414
- }
415
-
416
- // TODO: move into wp shortcut function
417
- function get_wp_head(){
418
- ob_start();
419
- wp_head();
420
- $ret = ob_get_contents();
421
- ob_end_clean();
422
- return $ret;
423
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  }
425
 
426
  $timber = new Timber();
3
  Plugin Name: Timber
4
  Description: The WordPress Timber Library allows you to write themes using the power Twig templates
5
  Author: Jared Novack + Upstatement
6
+ Version: 0.10.4
7
  Author URI: http://timber.upstatement.com/
8
  */
9
 
10
  global $wp_version;
11
  global $timber;
 
 
 
 
12
 
13
+ require_once(__DIR__ . '/functions/functions-twig.php');
14
+ require_once(__DIR__ . '/functions/functions-post-master.php');
15
+ require_once(__DIR__ . '/functions/functions-php-helper.php');
16
+ require_once(__DIR__ . '/functions/functions-wp-helper.php');
17
 
18
+ require_once(__DIR__ . '/objects/timber-core.php');
19
+ require_once(__DIR__ . '/objects/timber-post.php');
20
+ require_once(__DIR__ . '/objects/timber-comment.php');
21
+ require_once(__DIR__ . '/objects/timber-user.php');
22
+ require_once(__DIR__ . '/objects/timber-term.php');
23
+ require_once(__DIR__ . '/objects/timber-term-getter.php');
24
+ require_once(__DIR__ . '/objects/timber-image.php');
25
+ require_once(__DIR__ . '/objects/timber-menu.php');
26
 
27
+ require_once(__DIR__ . '/objects/timber-loader.php');
 
28
 
29
 
30
  /** Usage:
31
+ *
32
+ * $posts = Timber::get_posts();
33
+ * $posts = Timber::get_posts('post_type = article')
34
+ * $posts = Timber::get_posts(array('post_type' => 'article', 'category_name' => 'sports')); // uses wp_query format
35
+ * $posts = Timber::get_posts(array(23,24,35,67), 'InkwellArticle');
36
+ *
37
+ * $context = Timber::get_context(); // returns wp favorites!
38
+ *
39
+ * Timber::render('index.twig', $context);
40
+ */
41
+
42
  class Timber {
43
 
44
+ public static $locations;
45
+ public static $dirname = 'views';
46
+ public static $cache = false;
47
+
48
+ protected $router;
49
+
50
+ public function __construct(){
51
+ $this->init_constants();
52
+ add_action('init', array(&$this, 'init_routes'));
53
+ }
54
+
55
+ protected function init_constants() {
56
+ $timber_loc = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', realpath(__DIR__));
57
+ $plugin_url_path = str_replace($_SERVER['HTTP_HOST'], '', plugins_url());
58
+ $plugin_url_path = str_replace('https://', '', $plugin_url_path);
59
+ $plugin_url_path = str_replace('http://', '', $plugin_url_path);
60
+ $timber_dirs = dirname(__FILE__);
61
+ $timber_dirs = explode('/', $timber_dirs);
62
+ $timber_dirname = array_pop($timber_dirs);
63
+ define("TIMBER", $timber_loc);
64
+ define("TIMBER_URL_PATH", trailingslashit($plugin_url_path) . trailingslashit($timber_dirname));
65
+ define("TIMBER_URL", 'http://' . $_SERVER["HTTP_HOST"] . TIMBER);
66
+ define("TIMBER_LOC", realpath(__DIR__));
67
+ }
68
+
69
+ /* Post Retrieval
70
+ ================================ */
71
+
72
+ public static function get_post($query = false, $PostClass = 'TimberPost') {
73
+ if (is_int($query)) {
74
+ /* its a post id number */
75
+ $query = array($query);
76
+ }
77
+ $posts = self::get_posts($query, $PostClass);
78
+ if (count($posts) && is_array($posts)) {
79
+ return $posts[0];
80
+ }
81
+ return $posts;
82
+ }
83
+
84
+ public static function get_posts($query = false, $PostClass = 'TimberPost'){
85
+ if (self::is_post_class_or_class_map($query)) {
86
+ $PostClass = $query;
87
+ $query = false;
88
+ }
89
+ if (WPHelper::is_array_assoc($query) || (is_string($query) && strstr($query, '='))) {
90
+ // we have a regularly formed WP query string or array to use
91
+ return self::get_posts_from_wp_query($query, $PostClass);
92
+ } else if (is_string($query) && !is_integer($query)) {
93
+ // we have what could be a post name to pull out
94
+ return self::get_posts_from_slug($query, $PostClass);
95
+ } else if (is_array($query) && count($query) && (is_integer($query[0]) || is_string($query[0]))) {
96
+ // we have a list of pids (post IDs) to extract from
97
+ return self::get_posts_from_array_of_ids($query, $PostClass);
98
+ } else if (is_array($query) && count($query) && isset($query[0]) && is_object($query[0])) {
99
+ // maybe its an array of post objects that already have data
100
+ return self::handle_post_results($query, $PostClass);
101
+ } else if (have_posts()) {
102
+ //lets just use the default WordPress current query
103
+ return self::get_posts_from_loop($PostClass);
104
+ } else if (!$query) {
105
+ //okay, everything failed lets just return some posts so that the user has something to work with
106
+ //this turns out to cause all kinds of awful behavior
107
+ //return self::get_posts_from_wp_query(array(), $PostClass);
108
+ return null;
109
+ } else {
110
+ error_log('I have failed you! in timber.php::94');
111
+ WPHelper::error_log($query);
112
+ }
113
+ return $query;
114
+ }
115
+
116
+ public function get_pids($query = null) {
117
+ $posts = get_posts($query);
118
+ $pids = array();
119
+ foreach ($posts as $post) {
120
+ if ($post->ID) {
121
+ $pids[] = $post->ID;
122
+ }
123
+ }
124
+ return $pids;
125
+ }
126
+
127
+ public static function get_posts_from_loop($PostClass) {
128
+ $results = self::get_pids_from_loop();
129
+ return self::handle_post_results($results, $PostClass);
130
+ }
131
+
132
+ public static function get_pids_from_loop() {
133
+ $posts = array();
134
+ $i = 0;
135
+ ob_start();
136
+ while (have_posts() && $i < 99999) {
137
+ the_post();
138
+ $posts[] = get_the_ID();
139
+ $i++;
140
+ }
141
+ //why is this here? seems to only cause pain.
142
+ //wp_reset_query();
143
+ ob_end_clean();
144
+ return $posts;
145
+ }
146
+
147
+ public static function get_posts_from_slug($slug, $PostClass) {
148
+ global $wpdb;
149
+ $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'";
150
+ if (strstr($slug, '#')) {
151
+ //we have a post_type directive here
152
+ $q = explode('#', $slug);
153
+ $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '$q[1]' AND post_type = '$q[0]'";
154
+ }
155
+ $results = $wpdb->get_col($query);
156
+ return self::handle_post_results($results, $PostClass);
157
+ }
158
+
159
+ public static function get_posts_from_wp_query($query = array(), $PostClass = 'TimberPost') {
160
+ $results = get_posts($query);
161
+ return self::handle_post_results($results, $PostClass);
162
+ }
163
+
164
+ public static function get_posts_from_array_of_ids($query = array(), $PostClass = 'TimberPost') {
165
+ if (!is_array($query) || !count($query)) {
166
+ return null;
167
+ }
168
+ global $wpdb;
169
+ $query_list = implode(', ', $query);
170
+ $results = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE ID IN ($query_list)");
171
+ $results = array_intersect($query, $results);
172
+ return self::handle_post_results($results, $PostClass);
173
+ }
174
+
175
+ public static function handle_post_results($results, $PostClass = 'TimberPost') {
176
+ $posts = array();
177
+ foreach ($results as $rid) {
178
+ $PostClassUse = $PostClass;
179
+ if (is_array($PostClass)) {
180
+ $post_type = get_post_type($rid);
181
+ $PostClassUse = 'TimberPost';
182
+ if (isset($PostClass[$post_type])) {
183
+ $PostClassUse = $PostClass[$post_type];
184
+ } else {
185
+ if (is_array($PostClass)) {
186
+ error_log($post_type . ' not found in ' . print_r($PostClass, true));
187
+ } else {
188
+ error_log($post_type . ' not found in ' . $PostClass);
189
+ }
190
+ }
191
+ }
192
+ $post = new $PostClassUse($rid);
193
+ if (isset($post->post_title)) {
194
+ $posts[] = $post;
195
+ }
196
+ }
197
+ return $posts;
198
+ }
199
+
200
+ public function get_pid($query) {
201
+ $post = self::get_posts($query);
202
+ return $post->ID;
203
+ }
204
+
205
+
206
+ /* Deprecated
207
+ ================================ */
208
+
209
+ public function loop_to_posts($PostClass = 'TimberPost') {
210
+ return self::get_posts(false, $PostClass);
211
+ }
212
+
213
+ public function loop_to_id() {
214
+ if (have_posts()) {
215
+ the_post();
216
+ wp_reset_query();
217
+ return get_the_ID();
218
+ }
219
+ return false;
220
+ }
221
+
222
+
223
+ /* Term Retrieval
224
+ ================================ */
225
+
226
+ public static function get_terms($args, $TermClass = 'TimberTerm'){
227
+ if (is_string($args) && strstr($args, '=')){
228
+ //a string and a query string!
229
+ $parsed = TimberTermGetter::get_term_query_from_query_string($args);
230
+ return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
231
+ } else if (is_string($args)){
232
+ //its just a string with a single taxonomy
233
+ $parsed = TimberTermGetter::get_term_query_from_string($args);
234
+ return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
235
+ } else if (is_array($args) && WPHelper::is_array_assoc($args)){
236
+ //its an associative array, like a good ole query
237
+ $parsed = TimberTermGetter::get_term_query_from_assoc_array($args);
238
+ return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
239
+ } else if (is_array($args)){
240
+ //its just an array of strings or IDs (hopefully)
241
+ $parsed = TimberTermGetter::get_term_query_from_array($args);
242
+ return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
243
+ } else {
244
+ //no clue, what you talkin' bout?
245
+ }
246
+
247
+ }
248
+
249
+ public static function handle_term_query($taxonomies, $args, $TermClass){
250
+
251
+ $terms = get_terms($taxonomies, $args);
252
+ foreach($terms as &$term){
253
+ $term = new TimberTerm($term->term_id);
254
+ }
255
+ return $terms;
256
+ }
257
+
258
+
259
+
260
+
261
+ /* Template Setup and Display
262
+ ================================ */
263
+
264
+ public static function get_context() {
265
+ $data = array();
266
+ $data['http_host'] = 'http://' . $_SERVER['HTTP_HOST'];
267
+ $data['wp_title'] = get_bloginfo('name');
268
+ $data['wp_head'] = WPHelper::ob_function('wp_head');
269
+ $data['wp_footer'] = WPHelper::ob_function('wp_footer');
270
+ $data['body_class'] = implode(' ', get_body_class());
271
+ if (function_exists('wp_nav_menu')) {
272
+ $data['wp_nav_menu'] = wp_nav_menu(array('container_class' => 'menu-header', 'theme_location' => 'primary', 'echo' => false, 'menu_class' => 'nav-menu'));
273
+ }
274
+ $data['theme_dir'] = str_replace($_SERVER['DOCUMENT_ROOT'], '', get_stylesheet_directory());
275
+ $data['language_attributes'] = WPHelper::ob_function('language_attributes');
276
+ $data['stylesheet_uri'] = get_stylesheet_uri();
277
+ $data = apply_filters('timber_context', $data);
278
+ return $data;
279
+ }
280
+
281
+ public static function render($filenames, $data = array(), $echo = true) {
282
+ $caller = self::get_calling_script_dir();
283
+ $loader = new TimberLoader($caller);
284
+ $file = $loader->choose_template($filenames);
285
+ $output = '';
286
+ if (strlen($file)) {
287
+ $output = $loader->render($file, $data);
288
+ }
289
+ if ($echo) {
290
+ echo $output;
291
+ }
292
+ return $output;
293
+ }
294
+
295
+
296
+ /* Sidebar
297
+ ================================ */
298
+
299
+ public static function get_sidebar($sidebar = '', $data = array()) {
300
+ if ($sidebar == '') {
301
+ $sidebar = 'sidebar.php';
302
+ }
303
+ if (strstr(strtolower($sidebar), '.php')) {
304
+ return self::get_sidebar_from_php($sidebar, $data);
305
+ }
306
+ return self::render($sidebar, $data, false);
307
+ }
308
+
309
+ public static function get_sidebar_from_php($sidebar = '', $data) {
310
+ $caller = self::get_calling_script_dir();
311
+ $loader = new TimberLoader();
312
+ $uris = $loader->get_locations($caller);
313
+ ob_start();
314
+ $found = false;
315
+ foreach ($uris as $uri) {
316
+ if (file_exists(trailingslashit($uri) . $sidebar)) {
317
+ include(trailingslashit($uri) . $sidebar);
318
+ $found = true;
319
+ break;
320
+ }
321
+ }
322
+ if (!$found) {
323
+ error_log('error loading your sidebar, check to make sure the file exists');
324
+ }
325
+ $ret = ob_get_contents();
326
+ error_log($ret);
327
+ ob_end_clean();
328
+ return $ret;
329
+ }
330
+
331
+
332
+ /* Routes
333
+ ================================ */
334
+
335
+ public function init_routes() {
336
+ global $timber;
337
+ if (isset($timber->router)) {
338
+ $route = $timber->router->matchCurrentRequest();
339
+ if ($route) {
340
+ $callback = $route->getTarget();
341
+ $params = $route->getParameters();
342
+ $callback($params);
343
+ }
344
+ }
345
+ }
346
+
347
+ public static function add_route($route, $callback) {
348
+ global $timber;
349
+ if (!isset($timber->router)) {
350
+ require_once('router/Router.php');
351
+ require_once('router/Route.php');
352
+ $timber->router = new Router();
353
+ $timber->router->setBasePath('/');
354
+ }
355
+ $timber->router->map($route, $callback);
356
+ }
357
+
358
+ public function load_template($template, $query = false) {
359
+ if ($query) {
360
+ global $wp_query;
361
+ $wp_query = new WP_Query($query);
362
+ }
363
+ $template = locate_template($template);
364
+ $GLOBALS['timber_template'] = $template;
365
+ add_action('send_headers', function () {
366
+ header('HTTP/1.1 200 OK');
367
+ });
368
+ add_action('wp_loaded', function ($template) {
369
+ if (isset($GLOBALS['timber_template'])) {
370
+ load_template($GLOBALS['timber_template']);
371
+ die;
372
+ }
373
+ }, 10, 1);
374
+ }
375
+
376
+ /* Pagination
377
+ ================================ */
378
+
379
+ public function get_pagination(){
380
+ global $wp_query;
381
+ global $paged;
382
+ $data = array();
383
+ $data['pages'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
384
+ $paged = 1;
385
+ if (isset($wp_query->query_vars['paged'])) {
386
+ $paged = $wp_query->query_vars['paged'];
387
+ }
388
+ $data['base'] = get_pagenum_link(0);
389
+ $data['paged'] = $paged;
390
+ if ($paged < $data['pages']) {
391
+ $data['next'] = array('link' => next_posts(0, false), 'path' => next_posts(0, false));
392
+ }
393
+ if ($paged > 1) {
394
+ $data['prev'] = array('link' => previous_posts(false), 'path' => previous_posts(false));
395
+ }
396
+ return $data;
397
+ }
398
+
399
+ /* Utility
400
+ ================================ */
401
+
402
+ public function get_calling_script_path($offset = 0) {
403
+ $dir = self::get_calling_script_dir($offset);
404
+ return str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath($dir));
405
+ }
406
+
407
+ public static function get_calling_script_dir($offset = 0) {
408
+ $caller = null;
409
+ $backtrace = debug_backtrace();
410
+ $i = 0;
411
+ foreach ($backtrace as $trace) {
412
+ if ($trace['file'] != __FILE__) {
413
+ $caller = $trace['file'];
414
+ break;
415
+ }
416
+ $i++;
417
+ }
418
+ if ($offset){
419
+ $caller = $backtrace[$i + $offset]['file'];
420
+ }
421
+ if ($caller !== null) {
422
+ $pathinfo = pathinfo($caller);
423
+ $dir = $pathinfo['dirname'];
424
+ return $dir;
425
+ }
426
+ return null;
427
+ }
428
+
429
+ public static function is_post_class_or_class_map($arg){
430
+ if (is_string($arg) && class_exists($arg)) {
431
+ return true;
432
+ }
433
+ if (is_array($arg)) {
434
+ foreach ($arg as $item) {
435
+ if (is_string($item) && class_exists($item)) {
436
+ return true;
437
+ }
438
+ }
439
+ }
440
+ return false;
441
+ }
442
+
443
  }
444
 
445
  $timber = new Timber();