• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.52
/src/Archives.php
1
<?php
2

3
namespace Timber;
4

5
/**
6
 * Class Archive
7
 *
8
 * The `Timber\Archives` class is used to generate a menu based on the date archives of your posts.
9
 *
10
 * The [Nieman Foundation News site](http://nieman.harvard.edu/news/) has an example of how the
11
 * output can be used in a real site ([screenshot](https://cloud.githubusercontent.com/assets/1298086/9610076/3cdca596-50a5-11e5-82fd-acb74c09c482.png)).
12
 *
13
 * @api
14
 * @example
15
 * ```php
16
 * $context['archives'] = new Timber\Archives( $args );
17
 * ```
18
 * ```twig
19
 * <ul>
20
 * {% for item in archives.items %}
21
 *     <li><a href="{{item.link}}">{{item.name}}</a></li>
22
 *     {% for child in item.children %}
23
 *         <li class="child"><a href="{{child.link}}">{{child.name}}</a></li>
24
 *     {% endfor %}
25
 * {% endfor %}
26
 * </ul>
27
 * ```
28
 * ```html
29
 * <ul>
30
 *     <li>2015</li>
31
 *     <li class="child">May</li>
32
 *     <li class="child">April</li>
33
 *     <li class="child">March</li>
34
 *     <li class="child">February</li>
35
 *     <li class="child">January</li>
36
 *     <li>2014</li>
37
 *     <li class="child">December</li>
38
 *     <li class="child">November</li>
39
 *     <li class="child">October</li>
40
 * </ul>
41
 * ```
42
 */
43
class Archives extends Core
44
{
45
    /**
46
     * @var array Preserves arguments sent with the constructor for possible later use when
47
     * displaying items.
48
     */
49
    protected $args;
50

51
    /**
52
     * URL prefix.
53
     *
54
     * @api
55
     * @var string
56
     */
57
    public $base = '';
58

59
    /**
60
     * @api
61
     * @var array The items of the archives to iterate through and markup for your page.
62
     */
63
    public $items;
64

65
    /**
66
     * Build an Archives menu
67
     *
68
     * @api
69
     * @param array  $args {
70
     *      Optional. Array of arguments.
71
     *
72
     *      @type bool   $show_year => false
73
     *      @type string
74
     *      @type string $type => 'monthly-nested'
75
     *      @type int    $limit => -1
76
     *      @type bool   $show_post_count => false
77
     *      @type string $order => 'DESC'
78
     *      @type string $post_type => 'post'
79
     *      @type bool   $show_year => false
80
     *      @type bool   $nested => false
81
     * }
82
     * @param string $base Any additional paths that need to be prepended to the URLs that are
83
     *                     generated, for example: "tags". Default ''.
84
     */
85
    public function __construct($args = null, $base = '')
86
    {
87
        $this->init($args, $base);
8✔
88
    }
89

90
    /**
91
     * Initialize the Archives
92
     *
93
     * @internal
94
     * @param array|string $args
95
     * @param string       $base
96
     */
97
    public function init($args = null, $base = '')
98
    {
99
        $this->base = $base;
8✔
100
        $this->items = $this->items($args);
8✔
101
        $this->args = $args;
8✔
102
    }
103

104
    /**
105
     * @internal
106
     * @param string $url
107
     * @param string $text
108
     * @param int    $post_count
109
     * @return mixed
110
     */
111
    protected function get_archives_link($url, $text, $post_count = 0)
112
    {
113
        $ret = [];
8✔
114
        $ret['text'] = $ret['title'] = $ret['name'] = \wptexturize($text);
8✔
115
        $ret['url'] = $ret['link'] = \esc_url(URLHelper::prepend_to_url($url, $this->base));
8✔
116
        if ($post_count) {
8✔
117
            $ret['post_count'] = (int) $post_count;
7✔
118
        }
119
        return $ret;
8✔
120
    }
121

122
    /**
123
     * @internal
124
     * @param array  $args
125
     * @param string $last_changed
126
     * @param string $join
127
     * @param string $where
128
     * @param string $order
129
     * @param string $limit
130
     * @return array
131
     */
132
    protected function get_items_yearly($args, $last_changed, $join, $where, $order, $limit)
133
    {
134
        global $wpdb;
135
        $output = [];
1✔
136
        $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";
1✔
137
        $key = \md5($query);
1✔
138
        $key = "wp_get_archives:$key:$last_changed";
1✔
139
        if (!$results = \wp_cache_get($key, 'posts')) {
1✔
140
            $results = $wpdb->get_results($query);
1✔
141
            \wp_cache_set($key, $results, 'posts');
1✔
142
        }
143
        if ($results) {
1✔
144
            foreach ((array) $results as $result) {
1✔
145
                $url = \get_year_link($result->year);
1✔
146
                $text = \sprintf('%d', $result->year);
1✔
147
                $output[] = $this->get_archives_link($url, $text, $result->posts);
1✔
148
            }
149
        }
150
        return $output;
1✔
151
    }
152

153
    /**
154
     * @internal
155
     * @param array|string $args
156
     * @param string $last_changed
157
     * @param string $join
158
     * @param string $where
159
     * @param string $order
160
     * @param string $limit
161
     * @param bool $nested
162
     * @return array
163
     */
164
    protected function get_items_monthly($args, $last_changed, $join, $where, $order, $limit = '', $nested = true)
165
    {
166
        global $wpdb, $wp_locale;
167
        $output = [];
4✔
168
        $defaults = [
4✔
169
            'show_year' => false,
4✔
170
        ];
4✔
171
        $r = \wp_parse_args($args, $defaults);
4✔
172

173
        $show_year = $r['show_year'];
4✔
174
        //will need to specify which year we're looking for
175
        $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts "
4✔
176
            . "FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) "
4✔
177
            . "ORDER BY post_date $order $limit";
4✔
178
        $key = \md5($query);
4✔
179
        $key = "wp_get_archives:$key:$last_changed";
4✔
180
        if (!$results = \wp_cache_get($key, 'posts')) {
4✔
181
            $results = $wpdb->get_results($query);
4✔
182
            \wp_cache_set($key, $results, 'posts');
4✔
183
        }
184
        if ($results) {
4✔
185
            foreach ((array) $results as $result) {
4✔
186
                $url = \get_month_link($result->year, $result->month);
4✔
187
                if ($show_year && !$nested) {
4✔
188
                    $text = \sprintf(\__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
1✔
189
                } else {
190
                    $text = $wp_locale->get_month($result->month);
4✔
191
                }
192
                if ($nested) {
4✔
193
                    $output[$result->year][] = $this->get_archives_link($url, $text, $result->posts);
2✔
194
                } else {
195
                    $output[] = $this->get_archives_link($url, $text, $result->posts);
3✔
196
                }
197
            }
198
        }
199
        if ($nested) {
4✔
200
            $out2 = [];
2✔
201
            foreach ($output as $year => $months) {
2✔
202
                $out2[] = [
2✔
203
                    'name' => $year,
2✔
204
                    'children' => $months,
2✔
205
                    'post_count' => \array_sum(\array_column($months, 'post_count')),
2✔
206
                ];
2✔
207
            }
208
            return $out2;
2✔
209
        }
210
        return $output;
3✔
211
    }
212

213
    /**
214
     * Gets archive items.
215
     *
216
     * @api
217
     * @deprecated 2.0.0, use `{{ archives.items }}` instead.
218
     * @see \Timber\Archives::items()
219
     * @return array|string
220
     */
221
    public function get_items($args = null)
222
    {
223
        Helper::warn('{{ archives.get_items }} is deprecated. Use {{ archives.items }} instead.');
×
224

225
        return $this->items($args);
×
226
    }
227

228
    /**
229
     * @api
230
     * @param array|string $args Optional. Array of arguments.
231
     * @return array|string
232
     */
233
    public function items($args = null)
234
    {
235
        global $wpdb;
236

237
        $defaults = [
8✔
238
            'type' => 'monthly-nested',
8✔
239
            'limit' => '',
8✔
240
            'show_post_count' => false,
8✔
241
            'order' => 'DESC',
8✔
242
            'post_type' => 'post',
8✔
243
            'show_year' => false,
8✔
244
            'nested' => false,
8✔
245
        ];
8✔
246

247
        if ($args === null) {
8✔
248
            $args = $this->args;
1✔
249
        }
250

251
        $args = \wp_parse_args($args, $defaults);
8✔
252
        $post_type = $args['post_type'];
8✔
253
        $order = $args['order'];
8✔
254
        $nested = $args['nested'];
8✔
255
        $type = $args['type'];
8✔
256
        $limit = '';
8✔
257
        if ($type == 'yearlymonthly' || $type == 'yearmonth') {
8✔
258
            $type = 'monthly-nested';
1✔
259
        }
260
        if ($type == 'monthly-nested') {
8✔
261
            $nested = true;
2✔
262
        }
263

264
        if (!empty($args['limit'])) {
8✔
265
            $limit = \absint($args['limit']);
1✔
266
            $limit = ' LIMIT ' . $limit;
1✔
267
        }
268
        $order = \strtoupper($order);
8✔
269
        if ($order !== 'ASC') {
8✔
270
            $order = 'DESC';
8✔
271
        }
272

273
        // this is what will separate dates on weekly archive links
274
        $archive_week_separator = '&#8211;';
8✔
275

276
        // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
277
        $archive_date_format_over_ride = 0;
8✔
278

279
        // options for daily archive (only if you over-ride the general date format)
280
        $archive_day_date_format = 'Y/m/d';
8✔
281

282
        // options for weekly archive (only if you over-ride the general date format)
283
        $archive_week_start_date_format = 'Y/m/d';
8✔
284
        $archive_week_end_date_format = 'Y/m/d';
8✔
285

286
        if (!$archive_date_format_over_ride) {
8✔
287
            $archive_day_date_format = \get_option('date_format');
8✔
288
            $archive_week_start_date_format = \get_option('date_format');
8✔
289
            $archive_week_end_date_format = \get_option('date_format');
8✔
290
        }
291

292
        $where = $wpdb->prepare('WHERE post_type = "%s" AND post_status = "publish"', $post_type);
8✔
293

294
        /**
295
         * @link https://developer.wordpress.org/reference/hooks/getarchives_where/
296
         */
297
        $where = \apply_filters('getarchives_where', $where, $args);
8✔
298

299
        /**
300
         * @link https://developer.wordpress.org/reference/hooks/getarchives_join/
301
         */
302
        $join = \apply_filters('getarchives_join', '', $args);
8✔
303

304
        $output = [];
8✔
305
        $last_changed = \wp_cache_get('last_changed', 'posts');
8✔
306
        if (!$last_changed) {
8✔
307
            $last_changed = \microtime();
×
308
            \wp_cache_set('last_changed', $last_changed, 'posts');
×
309
        }
310
        if ('monthly' == $type) {
8✔
311
            $output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit, $nested);
3✔
312
        } elseif ('yearly' == $type) {
6✔
313
            $output = $this->get_items_yearly($args, $last_changed, $join, $where, $order, $limit);
1✔
314
        } elseif ('monthly-nested' == $type) {
5✔
315
            $output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit);
2✔
316
        } elseif ('daily' == $type) {
3✔
317
            $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";
1✔
318
            $key = \md5($query);
1✔
319
            $key = "wp_get_archives:$key:$last_changed";
1✔
320
            if (!$results = \wp_cache_get($key, 'posts')) {
1✔
321
                $results = $wpdb->get_results($query);
1✔
322
                $cache = [];
1✔
323
                $cache[$key] = $results;
1✔
324
                \wp_cache_set($key, $results, 'posts');
1✔
325
            }
326
            if ($results) {
1✔
327
                foreach ((array) $results as $result) {
1✔
328
                    $url = \get_day_link($result->year, $result->month, $result->dayofmonth);
1✔
329
                    $date = \sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
1✔
330
                    $text = \mysql2date($archive_day_date_format, $date);
1✔
331
                    $output[] = $this->get_archives_link($url, $text, $result->posts);
1✔
332
                }
333
            }
334
        } elseif ('weekly' == $type) {
2✔
335
            $week = \_wp_mysql_week('`post_date`');
1✔
336
            $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, "
1✔
337
                . "count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
1✔
338
            $key = \md5($query);
1✔
339
            $key = "wp_get_archives:$key:$last_changed";
1✔
340
            if (!$results = \wp_cache_get($key, 'posts')) {
1✔
341
                $results = $wpdb->get_results($query);
1✔
342
                \wp_cache_set($key, $results, 'posts');
1✔
343
            }
344
            $arc_w_last = '';
1✔
345
            if ($results) {
1✔
346
                foreach ((array) $results as $result) {
1✔
347
                    if ($result->week != $arc_w_last) {
1✔
348
                        $arc_year = $result->yr;
1✔
349
                        $arc_w_last = $result->week;
1✔
350
                        $arc_week = \get_weekstartend($result->yyyymmdd, \get_option('start_of_week'));
1✔
351
                        $arc_week_start = \date_i18n($archive_week_start_date_format, $arc_week['start']);
1✔
352
                        $arc_week_end = \date_i18n($archive_week_end_date_format, $arc_week['end']);
1✔
353
                        $url = \sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', \home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week);
1✔
354
                        $text = $arc_week_start . $archive_week_separator . $arc_week_end;
1✔
355
                        $output[] = $this->get_archives_link($url, $text, $result->posts);
1✔
356
                    }
357
                }
358
            }
359
        } elseif ('postbypost' == $type || 'alpha' == $type) {
1✔
360
            $orderby = 'alpha' == $type ? 'post_title ASC ' : 'post_date DESC ';
1✔
361
            $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
1✔
362
            $key = \md5($query);
1✔
363
            $key = "wp_get_archives:$key:$last_changed";
1✔
364
            if (!$results = \wp_cache_get($key, 'posts')) {
1✔
365
                $results = $wpdb->get_results($query);
1✔
366
                \wp_cache_set($key, $results, 'posts');
1✔
367
            }
368
            if ($results) {
1✔
369
                foreach ((array) $results as $result) {
1✔
370
                    if ($result->post_date != '0000-00-00 00:00:00') {
1✔
371
                        $url = \get_permalink($result);
1✔
372
                        $text = $result->ID;
1✔
373
                        if ($result->post_title) {
1✔
374
                            /** This filter is documented in wp-includes/post-template.php */
375
                            $text = \strip_tags(\apply_filters('the_title', $result->post_title, $result->ID));
1✔
376
                        }
377
                        $output[] = $this->get_archives_link($url, $text);
1✔
378
                    }
379
                }
380
            }
381
        }
382
        return $output;
8✔
383
    }
384
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc