Make WordPress Core

Ticket #22400: wp-get-archives.diff

File wp-get-archives.diff, 12.2 KB (added by wonderboymusic, 9 years ago)
  • src/wp-includes/general-template.php

     
    11731173 * @param string|array $args Optional. Override defaults.
    11741174 * @return string|null String when retrieving, null when displaying.
    11751175 */
    1176 function wp_get_archives($args = '') {
     1176function wp_get_archives( $args = '' ) {
    11771177        global $wpdb, $wp_locale;
    11781178
    11791179        $defaults = array(
     
    11841184        );
    11851185
    11861186        $r = wp_parse_args( $args, $defaults );
    1187         extract( $r, EXTR_SKIP );
    11881187
    1189         if ( '' == $type )
    1190                 $type = 'monthly';
     1188        if ( '' == $r['type'] ) {
     1189                $r['type'] = 'monthly';
     1190        }
    11911191
    1192         if ( '' != $limit ) {
    1193                 $limit = absint($limit);
    1194                 $limit = ' LIMIT '.$limit;
     1192        if ( '' != $r['limit'] ) {
     1193                $r['limit'] = absint( $r['limit'] );
     1194                $r['limit'] = ' LIMIT ' . $r['limit'];
    11951195        }
    11961196
    1197         $order = strtoupper( $order );
    1198         if ( $order !== 'ASC' )
     1197        $order = strtoupper( $r['order'] );
     1198        if ( $order !== 'ASC' ) {
    11991199                $order = 'DESC';
     1200        }
    12001201
    12011202        // this is what will separate dates on weekly archive links
    12021203        $archive_week_separator = '–';
     
    12111212        $archive_week_start_date_format = 'Y/m/d';
    12121213        $archive_week_end_date_format   = 'Y/m/d';
    12131214
    1214         if ( !$archive_date_format_over_ride ) {
    1215                 $archive_day_date_format = get_option('date_format');
    1216                 $archive_week_start_date_format = get_option('date_format');
    1217                 $archive_week_end_date_format = get_option('date_format');
     1215        if ( ! $archive_date_format_over_ride ) {
     1216                $archive_day_date_format = get_option( 'date_format' );
     1217                $archive_week_start_date_format = get_option( 'date_format' );
     1218                $archive_week_end_date_format = get_option( 'date_format' );
    12181219        }
    12191220
    12201221        /**
     
    12451246                wp_cache_set( 'last_changed', $last_changed, 'posts' );
    12461247        }
    12471248
    1248         if ( 'monthly' == $type ) {
     1249        $limit = $r['limit'];
     1250
     1251        if ( 'monthly' == $r['type'] ) {
    12491252                $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
    12501253                $key = md5( $query );
    12511254                $key = "wp_get_archives:$key:$last_changed";
     
    12541257                        wp_cache_set( $key, $results, 'posts' );
    12551258                }
    12561259                if ( $results ) {
    1257                         $afterafter = $after;
     1260                        $after = $r['after'];
    12581261                        foreach ( (array) $results as $result ) {
    12591262                                $url = get_month_link( $result->year, $result->month );
    12601263                                /* translators: 1: month name, 2: 4-digit year */
    1261                                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
    1262                                 if ( $show_post_count )
    1263                                         $after = ' ('.$result->posts.')' . $afterafter;
    1264                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1264                                $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
     1265                                if ( $r['show_post_count'] ) {
     1266                                        $after = ' ('.$result->posts.')' . $after;
     1267                                }
     1268                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
    12651269                        }
    12661270                }
    1267         } elseif ('yearly' == $type) {
     1271        } elseif ( 'yearly' == $r['type'] ) {
    12681272                $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";
    12691273                $key = md5( $query );
    12701274                $key = "wp_get_archives:$key:$last_changed";
     
    12731277                        wp_cache_set( $key, $results, 'posts' );
    12741278                }
    12751279                if ( $results ) {
    1276                         $afterafter = $after;
     1280                        $after = $r['after'];
    12771281                        foreach ( (array) $results as $result) {
    1278                                 $url = get_year_link($result->year);
    1279                                 $text = sprintf('%d', $result->year);
    1280                                 if ($show_post_count)
    1281                                         $after = ' ('.$result->posts.')' . $afterafter;
    1282                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1282                                $url = get_year_link( $result->year );
     1283                                $text = sprintf( '%d', $result->year );
     1284                                if ( $r['show_post_count'] ) {
     1285                                        $r['after'] = ' ('.$result->posts.')' . $after;
     1286                                }
     1287                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    12831288                        }
    12841289                }
    1285         } elseif ( 'daily' == $type ) {
     1290        } elseif ( 'daily' == $r['type'] ) {
    12861291                $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";
    12871292                $key = md5( $query );
    12881293                $key = "wp_get_archives:$key:$last_changed";
     
    12921297                        wp_cache_set( $key, $results, 'posts' );
    12931298                }
    12941299                if ( $results ) {
    1295                         $afterafter = $after;
     1300                        $after = $r['after'];
    12961301                        foreach ( (array) $results as $result ) {
    1297                                 $url    = get_day_link($result->year, $result->month, $result->dayofmonth);
    1298                                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
    1299                                 $text = mysql2date($archive_day_date_format, $date);
    1300                                 if ($show_post_count)
    1301                                         $after = ' ('.$result->posts.')'.$afterafter;
    1302                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1302                                $url    = get_day_link( $result->year, $result->month, $result->dayofmonth );
     1303                                $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
     1304                                $text = mysql2date( $archive_day_date_format, $date );
     1305                                if ( $r['show_post_count'] ) {
     1306                                        $after = ' (' . $result->posts . ')' . $after;
     1307                                }
     1308                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
    13031309                        }
    13041310                }
    1305         } elseif ( 'weekly' == $type ) {
     1311        } elseif ( 'weekly' == $r['type'] ) {
    13061312                $week = _wp_mysql_week( '`post_date`' );
    13071313                $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
    13081314                $key = md5( $query );
     
    13121318                        wp_cache_set( $key, $results, 'posts' );
    13131319                }
    13141320                $arc_w_last = '';
    1315                 $afterafter = $after;
     1321                $after = $r['after'];
    13161322                if ( $results ) {
    13171323                                foreach ( (array) $results as $result ) {
    13181324                                        if ( $result->week != $arc_w_last ) {
    13191325                                                $arc_year = $result->yr;
    13201326                                                $arc_w_last = $result->week;
    1321                                                 $arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week'));
    1322                                                 $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
    1323                                                 $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
    1324                                                 $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week);
     1327                                                $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
     1328                                                $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] );
     1329                                                $arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] );
     1330                                                $url  = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week );
    13251331                                                $text = $arc_week_start . $archive_week_separator . $arc_week_end;
    1326                                                 if ($show_post_count)
    1327                                                         $after = ' ('.$result->posts.')'.$afterafter;
    1328                                                 $output .= get_archives_link($url, $text, $format, $before, $after);
     1332                                                if ( $r['show_post_count'] ) {
     1333                                                        $after = ' (' . $result->posts . ')' . $after;
     1334                                                }
     1335                                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
    13291336                                        }
    13301337                                }
    13311338                }
    1332         } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    1333                 $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
     1339        } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
     1340                $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC ';
    13341341                $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    13351342                $key = md5( $query );
    13361343                $key = "wp_get_archives:$key:$last_changed";
     
    13481355                                        } else {
    13491356                                                $text = $result->ID;
    13501357                                        }
    1351                                         $output .= get_archives_link($url, $text, $format, $before, $after);
     1358                                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    13521359                                }
    13531360                        }
    13541361                }
    13551362        }
    1356         if ( $echo )
     1363        if ( $r['echo'] ) {
    13571364                echo $output;
    1358         else
     1365        } else {
    13591366                return $output;
     1367        }
    13601368}
    13611369
    13621370/**
     
    25092517                        $r .= join("</li>\n\t<li>", $page_links);
    25102518                        $r .= "</li>\n</ul>\n";
    25112519                        break;
    2512                
     2520
    25132521                default :
    25142522                        $r = join("\n", $page_links);
    25152523                        break;
  • tests/phpunit/tests/functions/getArchives.php

     
     1<?php
     2/*
     3$defaults = array(
     4        'type' => 'monthly', 'limit' => '',
     5        'format' => 'html', 'before' => '',
     6        'after' => '', 'show_post_count' => false,
     7        'echo' => 1, 'order' => 'DESC',
     8);
     9*/
     10class Tests_Get_Archives extends WP_UnitTestCase {
     11        protected $post_ids;
     12        protected $month_url;
     13
     14        function setUp() {
     15                parent::setUp();
     16
     17                $this->month_url = get_month_link( date( 'Y' ), date( 'm' ) );
     18                $this->year_url = get_year_link( date( 'Y' ) );
     19                $this->post_ids = $this->factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
     20        }
     21
     22        function test_wp_get_archives_default() {
     23                $expected['default'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . "</a></li>";
     24                $this->assertEquals( $expected['default'], trim( wp_get_archives( array( 'echo' => false ) ) ) );
     25        }
     26
     27        function test_wp_get_archives_type() {
     28                $expected['type'] = "<li><a href='" . $this->year_url . "'>" . date( 'Y' ) . "</a></li>";
     29                $this->assertEquals( $expected['type'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'yearly' ) ) ) );
     30        }
     31
     32        function test_wp_get_archives_limit() {
     33                $ids = array_slice( array_reverse( $this->post_ids ), 0, 5 );
     34
     35                $expected['limit'] = <<<EOF
     36<li><a href='http://example.org/?p={$ids[0]}'>Post title 8</a></li>
     37        <li><a href='http://example.org/?p={$ids[1]}'>Post title 7</a></li>
     38        <li><a href='http://example.org/?p={$ids[2]}'>Post title 6</a></li>
     39        <li><a href='http://example.org/?p={$ids[3]}'>Post title 5</a></li>
     40        <li><a href='http://example.org/?p={$ids[4]}'>Post title 4</a></li>
     41EOF;
     42                $this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
     43        }
     44
     45        function test_wp_get_archives_format() {
     46                $expected['format'] = "<option value='" . $this->month_url . "'> " . date( 'F Y' ) . ' </option>';
     47                $this->assertEquals( $expected['format'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'option' ) ) ) );
     48        }
     49
     50        function test_wp_get_archives_before_and_after() {
     51                $expected['before_and_after'] = "<div><a href='" . $this->month_url . "'>" . date( 'F Y' ) . '</a></div>';
     52                $this->assertEquals( $expected['before_and_after'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'custom', 'before' => '<div>', 'after' => '</div>' ) ) ) );
     53        }
     54
     55        function test_wp_get_archives_show_post_count() {
     56                $expected['show_post_count'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . "</a>&nbsp;(8)</li>";
     57                $this->assertEquals( $expected['show_post_count'], trim( wp_get_archives( array( 'echo' => false, 'show_post_count' => 1 ) ) ) );
     58        }
     59
     60        function test_wp_get_archives_echo() {
     61                $expected['echo'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . '</a></li>';
     62                ob_start();
     63                wp_get_archives( array( 'echo' => true ) );
     64                $actual = ob_get_clean();
     65                $this->assertEquals( $expected['echo'], trim( $actual ) );
     66        }
     67
     68        function test_wp_get_archives_order() {
     69                $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42' ) );
     70
     71                $date_full = date( 'F Y' );
     72                $oct_url = get_month_link( 2012, 10 );
     73                $expected['order_asc'] = <<<EOF
     74<li><a href='{$oct_url}'>October 2012</a></li>
     75        <li><a href='{$this->month_url}'>$date_full</a></li>
     76EOF;
     77                $this->assertEquals( $expected['order_asc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'ASC' ) ) ) );
     78
     79                $expected['order_desc'] = <<<EOF
     80<li><a href='{$this->month_url}'>$date_full</a></li>
     81        <li><a href='{$oct_url}'>October 2012</a></li>
     82EOF;
     83                $this->assertEquals( $expected['order_desc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'DESC' ) ) ) );
     84        }
     85}
     86 No newline at end of file