1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The TimberArchives class is used to generate a menu based on the date archives of your posts. The [Nieman Foundation News site](http://nieman.harvard.edu/news/) has an example of how the output can be used in a real site ([screenshot](https://cloud.githubusercontent.com/assets/1298086/9610076/3cdca596-50a5-11e5-82fd-acb74c09c482.png)). |
4
|
|
|
* @example |
5
|
|
|
* ```php |
6
|
|
|
* $context['archives'] = new TimberArchives( $args ); |
7
|
|
|
* ``` |
8
|
|
|
* ```twig |
9
|
|
|
* <ul> |
10
|
|
|
* {% for item in archives.items %} |
11
|
|
|
* <li><a href="{{item.link}}">{{item.name}}</a></li> |
12
|
|
|
* {% for child in item.children %} |
13
|
|
|
* <li class="child"><a href="{{child.link}}">{{child.name}}</a></li> |
14
|
|
|
* {% endfor %} |
15
|
|
|
* {% endfor %} |
16
|
|
|
* </ul> |
17
|
|
|
* ``` |
18
|
|
|
* ```html |
19
|
|
|
* <ul> |
20
|
|
|
* <li>2015</li> |
21
|
|
|
* <li class="child">May</li> |
22
|
|
|
* <li class="child">April</li> |
23
|
|
|
* <li class="child">March</li> |
24
|
|
|
* <li class="child">February</li> |
25
|
|
|
* <li class="child">January</li> |
26
|
|
|
* <li>2014</li> |
27
|
|
|
* <li class="child">December</li> |
28
|
|
|
* <li class="child">November</li> |
29
|
|
|
* <li class="child">October</li> |
30
|
|
|
* </ul> |
31
|
|
|
* ``` |
32
|
|
|
*/ |
33
|
|
|
class TimberArchives extends TimberCore { |
34
|
|
|
|
35
|
|
|
public $base = ''; |
36
|
|
|
/** |
37
|
|
|
* @api |
38
|
|
|
* @var array the items of the archives to iterate through and markup for your page |
39
|
|
|
*/ |
40
|
|
|
public $items; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @api |
44
|
|
|
* @param $args array of arguments { |
45
|
|
|
* @type bool show_year => false |
46
|
|
|
* @type string |
47
|
|
|
* @type string type => 'monthly-nested' |
48
|
|
|
* @type int limit => -1 |
49
|
|
|
* @type bool show_post_count => false |
50
|
|
|
* @type string order => 'DESC' |
51
|
|
|
* @type string post_type => 'post' |
52
|
|
|
* @type bool show_year => false |
53
|
|
|
* @type bool nested => false |
54
|
|
|
* } |
55
|
|
|
* @param string $base any additional paths that need to be prepended to the URLs that are generated, for example: "tags" |
56
|
|
|
*/ |
57
|
|
|
function __construct( $args = null, $base = '' ) { |
|
|
|
|
58
|
|
|
$this->init($args, $base); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @internal |
63
|
|
|
* @param array|string $args |
|
|
|
|
64
|
|
|
* @param string $base |
65
|
|
|
*/ |
66
|
|
|
function init( $args = null, $base = '' ) { |
|
|
|
|
67
|
|
|
$this->base = $base; |
68
|
|
|
$this->items = $this->get_items($args); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @internal |
73
|
|
|
* @param string $url |
74
|
|
|
* @param string $text |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function get_archives_link( $url, $text ) { |
78
|
|
|
$ret = array(); |
79
|
|
|
$ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text); |
80
|
|
|
$ret['url'] = $ret['link'] = esc_url(TimberURLHelper::prepend_to_url($url, $this->base)); |
81
|
|
|
return $ret; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @internal |
86
|
|
|
* @param array $args |
87
|
|
|
* @param string $last_changed |
88
|
|
|
* @param string $join |
89
|
|
|
* @param string $where |
90
|
|
|
* @param string $order |
91
|
|
|
* @param string $limit |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function get_items_yearly( $args, $last_changed, $join, $where, $order, $limit ) { |
|
|
|
|
95
|
|
|
global $wpdb; |
96
|
|
|
$output = array(); |
97
|
|
|
$query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM {$wpdb->posts} $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit"; |
98
|
|
|
$key = md5($query); |
99
|
|
|
$key = "wp_get_archives:$key:$last_changed"; |
100
|
|
View Code Duplication |
if (!$results = wp_cache_get($key, 'posts')) { |
|
|
|
|
101
|
|
|
$results = $wpdb->get_results($query); |
102
|
|
|
wp_cache_set($key, $results, 'posts'); |
103
|
|
|
} |
104
|
|
|
if ($results) { |
105
|
|
|
foreach ( (array)$results as $result ) { |
106
|
|
|
$url = get_year_link( $result->year ); |
107
|
|
|
$text = sprintf('%d', $result->year); |
108
|
|
|
$output[] = $this->get_archives_link($url, $text); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $output; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @internal |
116
|
|
|
* @param array|string $args |
117
|
|
|
* @param string $last_changed |
118
|
|
|
* @param string $join |
119
|
|
|
* @param string $where |
120
|
|
|
* @param string $order |
121
|
|
|
* @param int $limit |
122
|
|
|
* @param bool $nested |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function get_items_monthly( $args, $last_changed, $join, $where, $order, $limit = 1000, $nested = true ) { |
126
|
|
|
global $wpdb, $wp_locale; |
127
|
|
|
$output = array(); |
128
|
|
|
$defaults = array( |
129
|
|
|
'show_year' => false, |
130
|
|
|
); |
131
|
|
|
$r = wp_parse_args($args, $defaults); |
132
|
|
|
|
133
|
|
|
$show_year = $r['show_year']; |
134
|
|
|
//will need to specify which year we're looking for |
135
|
|
|
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts " |
136
|
|
|
. "FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) " |
137
|
|
|
. "ORDER BY post_date $order $limit"; |
138
|
|
|
$key = md5($query); |
139
|
|
|
$key = "wp_get_archives:$key:$last_changed"; |
140
|
|
View Code Duplication |
if (!$results = wp_cache_get($key, 'posts')) { |
|
|
|
|
141
|
|
|
$results = $wpdb->get_results($query); |
142
|
|
|
wp_cache_set($key, $results, 'posts'); |
143
|
|
|
} |
144
|
|
|
if ($results) { |
145
|
|
|
foreach ((array)$results as $result) { |
146
|
|
|
$url = get_month_link($result->year, $result->month); |
147
|
|
|
if ($show_year && !$nested) { |
148
|
|
|
$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year); |
149
|
|
|
} else { |
150
|
|
|
$text = sprintf(__('%1$s'), $wp_locale->get_month($result->month)); |
151
|
|
|
} |
152
|
|
|
if ($nested) { |
153
|
|
|
$output[$result->year][] = $this->get_archives_link($url, $text); |
154
|
|
|
} else { |
155
|
|
|
$output[] = $this->get_archives_link($url, $text); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
if ($nested) { |
160
|
|
|
$out2 = array(); |
161
|
|
|
foreach ($output as $year => $months) { |
162
|
|
|
$out2[] = array('name' => $year, 'children' => $months); |
163
|
|
|
} |
164
|
|
|
return $out2; |
165
|
|
|
} |
166
|
|
|
return $output; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @api |
171
|
|
|
* @param array|string $args |
|
|
|
|
172
|
|
|
* @return array|string |
173
|
|
|
*/ |
174
|
|
|
function get_items( $args = null ) { |
|
|
|
|
175
|
|
|
global $wpdb; |
176
|
|
|
|
177
|
|
|
$defaults = array( |
178
|
|
|
'type' => 'monthly-nested', |
179
|
|
|
'limit' => '', |
180
|
|
|
'show_post_count' => false, |
181
|
|
|
'order' => 'DESC', |
182
|
|
|
'post_type' => 'post', |
183
|
|
|
'show_year' => false, |
184
|
|
|
'nested' => false |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$args = wp_parse_args($args, $defaults); |
188
|
|
|
$post_type = $args['post_type']; |
189
|
|
|
$order = $args['order']; |
190
|
|
|
$nested = $args['nested']; |
191
|
|
|
$type = $args['type']; |
192
|
|
|
$limit = ''; |
193
|
|
|
if ( $type == 'yearlymonthly' || $type == 'yearmonth' ) { |
194
|
|
|
$type = 'monthly-nested'; |
195
|
|
|
} |
196
|
|
|
if ( $type == 'monthly-nested' ) { |
197
|
|
|
$nested = true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ( !empty($args['limit']) ) { |
201
|
|
|
$limit = absint($limit); |
202
|
|
|
$limit = ' LIMIT ' . $limit; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$order = strtoupper($order); |
206
|
|
|
if ( $order !== 'ASC' ) { |
207
|
|
|
$order = 'DESC'; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// this is what will separate dates on weekly archive links |
211
|
|
|
$archive_week_separator = '–'; |
212
|
|
|
|
213
|
|
|
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride |
214
|
|
|
$archive_date_format_over_ride = 0; |
215
|
|
|
|
216
|
|
|
// options for daily archive (only if you over-ride the general date format) |
217
|
|
|
$archive_day_date_format = 'Y/m/d'; |
218
|
|
|
|
219
|
|
|
// options for weekly archive (only if you over-ride the general date format) |
220
|
|
|
$archive_week_start_date_format = 'Y/m/d'; |
221
|
|
|
$archive_week_end_date_format = 'Y/m/d'; |
222
|
|
|
|
223
|
|
|
if (!$archive_date_format_over_ride) { |
224
|
|
|
$archive_day_date_format = get_option('date_format'); |
225
|
|
|
$archive_week_start_date_format = get_option('date_format'); |
226
|
|
|
$archive_week_end_date_format = get_option('date_format'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$where = $wpdb->prepare('WHERE post_type = "%s" AND post_status = "publish"', $post_type); |
230
|
|
|
$where = apply_filters('getarchives_where', $where, $args); |
231
|
|
|
$join = apply_filters('getarchives_join', '', $args); |
232
|
|
|
|
233
|
|
|
$output = array(); |
234
|
|
|
$last_changed = wp_cache_get('last_changed', 'posts'); |
235
|
|
|
if (!$last_changed) { |
236
|
|
|
$last_changed = microtime(); |
237
|
|
|
wp_cache_set('last_changed', $last_changed, 'posts'); |
238
|
|
|
} |
239
|
|
|
if ( 'monthly' == $type ) { |
240
|
|
|
$output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit, $nested); |
241
|
|
|
} elseif ( 'yearly' == $type ) { |
242
|
|
|
$output = $this->get_items_yearly($args, $last_changed, $join, $where, $order, $limit); |
243
|
|
|
} elseif ( 'monthly-nested' == $type ) { |
244
|
|
|
$output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit); |
245
|
|
|
} elseif ( 'daily' == $type ) { |
246
|
|
|
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit"; |
247
|
|
|
$key = md5($query); |
248
|
|
|
$key = "wp_get_archives:$key:$last_changed"; |
249
|
|
|
if (!$results = wp_cache_get($key, 'posts')) { |
250
|
|
|
$results = $wpdb->get_results($query); |
251
|
|
|
$cache = array(); |
252
|
|
|
$cache[$key] = $results; |
253
|
|
|
wp_cache_set($key, $results, 'posts'); |
254
|
|
|
} |
255
|
|
|
if ( $results ) { |
256
|
|
|
foreach ( (array)$results as $result ) { |
257
|
|
|
$url = get_day_link($result->year, $result->month, $result->dayofmonth); |
258
|
|
|
$date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth); |
259
|
|
|
$text = mysql2date($archive_day_date_format, $date); |
260
|
|
|
$output[] = $this->get_archives_link($url, $text); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} elseif ( 'weekly' == $type ) { |
264
|
|
|
$week = _wp_mysql_week('`post_date`'); |
265
|
|
|
$query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, " |
266
|
|
|
. "count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit"; |
267
|
|
|
$key = md5($query); |
268
|
|
|
$key = "wp_get_archives:$key:$last_changed"; |
269
|
|
View Code Duplication |
if (!$results = wp_cache_get($key, 'posts')) { |
|
|
|
|
270
|
|
|
$results = $wpdb->get_results($query); |
271
|
|
|
wp_cache_set($key, $results, 'posts'); |
272
|
|
|
} |
273
|
|
|
$arc_w_last = ''; |
274
|
|
|
if ( $results ) { |
275
|
|
|
foreach ( (array)$results as $result ) { |
276
|
|
|
if ( $result->week != $arc_w_last ) { |
277
|
|
|
$arc_year = $result->yr; |
278
|
|
|
$arc_w_last = $result->week; |
279
|
|
|
$arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week')); |
280
|
|
|
$arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']); |
281
|
|
|
$arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']); |
282
|
|
|
$url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week); |
283
|
|
|
$text = $arc_week_start . $archive_week_separator . $arc_week_end; |
284
|
|
|
$output[] = $this->get_archives_link($url, $text); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} elseif ( 'postbypost' == $type || 'alpha' == $type ) { |
289
|
|
|
$orderby = 'alpha' == $type ? 'post_title ASC ' : 'post_date DESC '; |
290
|
|
|
$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; |
291
|
|
|
$key = md5($query); |
292
|
|
|
$key = "wp_get_archives:$key:$last_changed"; |
293
|
|
View Code Duplication |
if ( !$results = wp_cache_get($key, 'posts') ) { |
|
|
|
|
294
|
|
|
$results = $wpdb->get_results($query); |
295
|
|
|
wp_cache_set($key, $results, 'posts'); |
296
|
|
|
} |
297
|
|
|
if ( $results ) { |
298
|
|
|
foreach ( (array)$results as $result ) { |
299
|
|
|
if ($result->post_date != '0000-00-00 00:00:00') { |
300
|
|
|
$url = get_permalink($result); |
301
|
|
|
if ($result->post_title) { |
302
|
|
|
/** This filter is documented in wp-includes/post-template.php */ |
303
|
|
|
$text = strip_tags(apply_filters('the_title', $result->post_title, $result->ID)); |
304
|
|
|
} else { |
305
|
|
|
$text = $result->ID; |
306
|
|
|
} |
307
|
|
|
$output[] = $this->get_archives_link($url, $text); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
return $output; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.