Ticket #22400: wp-get-archives.diff
File wp-get-archives.diff, 12.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/general-template.php
1173 1173 * @param string|array $args Optional. Override defaults. 1174 1174 * @return string|null String when retrieving, null when displaying. 1175 1175 */ 1176 function wp_get_archives( $args = '') {1176 function wp_get_archives( $args = '' ) { 1177 1177 global $wpdb, $wp_locale; 1178 1178 1179 1179 $defaults = array( … … 1184 1184 ); 1185 1185 1186 1186 $r = wp_parse_args( $args, $defaults ); 1187 extract( $r, EXTR_SKIP );1188 1187 1189 if ( '' == $type ) 1190 $type = 'monthly'; 1188 if ( '' == $r['type'] ) { 1189 $r['type'] = 'monthly'; 1190 } 1191 1191 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']; 1195 1195 } 1196 1196 1197 $order = strtoupper( $ order);1198 if ( $order !== 'ASC' ) 1197 $order = strtoupper( $r['order'] ); 1198 if ( $order !== 'ASC' ) { 1199 1199 $order = 'DESC'; 1200 } 1200 1201 1201 1202 // this is what will separate dates on weekly archive links 1202 1203 $archive_week_separator = '–'; … … 1211 1212 $archive_week_start_date_format = 'Y/m/d'; 1212 1213 $archive_week_end_date_format = 'Y/m/d'; 1213 1214 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' ); 1218 1219 } 1219 1220 1220 1221 /** … … 1245 1246 wp_cache_set( 'last_changed', $last_changed, 'posts' ); 1246 1247 } 1247 1248 1248 if ( 'monthly' == $type ) { 1249 $limit = $r['limit']; 1250 1251 if ( 'monthly' == $r['type'] ) { 1249 1252 $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"; 1250 1253 $key = md5( $query ); 1251 1254 $key = "wp_get_archives:$key:$last_changed"; … … 1254 1257 wp_cache_set( $key, $results, 'posts' ); 1255 1258 } 1256 1259 if ( $results ) { 1257 $after after = $after;1260 $after = $r['after']; 1258 1261 foreach ( (array) $results as $result ) { 1259 1262 $url = get_month_link( $result->year, $result->month ); 1260 1263 /* 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 ); 1265 1269 } 1266 1270 } 1267 } elseif ( 'yearly' == $type) {1271 } elseif ( 'yearly' == $r['type'] ) { 1268 1272 $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"; 1269 1273 $key = md5( $query ); 1270 1274 $key = "wp_get_archives:$key:$last_changed"; … … 1273 1277 wp_cache_set( $key, $results, 'posts' ); 1274 1278 } 1275 1279 if ( $results ) { 1276 $after after = $after;1280 $after = $r['after']; 1277 1281 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'] ); 1283 1288 } 1284 1289 } 1285 } elseif ( 'daily' == $ type) {1290 } elseif ( 'daily' == $r['type'] ) { 1286 1291 $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"; 1287 1292 $key = md5( $query ); 1288 1293 $key = "wp_get_archives:$key:$last_changed"; … … 1292 1297 wp_cache_set( $key, $results, 'posts' ); 1293 1298 } 1294 1299 if ( $results ) { 1295 $after after = $after;1300 $after = $r['after']; 1296 1301 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 ); 1303 1309 } 1304 1310 } 1305 } elseif ( 'weekly' == $ type) {1311 } elseif ( 'weekly' == $r['type'] ) { 1306 1312 $week = _wp_mysql_week( '`post_date`' ); 1307 1313 $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"; 1308 1314 $key = md5( $query ); … … 1312 1318 wp_cache_set( $key, $results, 'posts' ); 1313 1319 } 1314 1320 $arc_w_last = ''; 1315 $after after = $after;1321 $after = $r['after']; 1316 1322 if ( $results ) { 1317 1323 foreach ( (array) $results as $result ) { 1318 1324 if ( $result->week != $arc_w_last ) { 1319 1325 $arc_year = $result->yr; 1320 1326 $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 ); 1325 1331 $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 ); 1329 1336 } 1330 1337 } 1331 1338 } 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 '; 1334 1341 $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; 1335 1342 $key = md5( $query ); 1336 1343 $key = "wp_get_archives:$key:$last_changed"; … … 1348 1355 } else { 1349 1356 $text = $result->ID; 1350 1357 } 1351 $output .= get_archives_link( $url, $text, $format, $before, $after);1358 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); 1352 1359 } 1353 1360 } 1354 1361 } 1355 1362 } 1356 if ( $ echo )1363 if ( $r['echo'] ) { 1357 1364 echo $output; 1358 else1365 } else { 1359 1366 return $output; 1367 } 1360 1368 } 1361 1369 1362 1370 /** … … 2509 2517 $r .= join("</li>\n\t<li>", $page_links); 2510 2518 $r .= "</li>\n</ul>\n"; 2511 2519 break; 2512 2520 2513 2521 default : 2514 2522 $r = join("\n", $page_links); 2515 2523 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 */ 10 class 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> 41 EOF; 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> (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> 76 EOF; 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> 82 EOF; 83 $this->assertEquals( $expected['order_desc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'DESC' ) ) ) ); 84 } 85 } 86 No newline at end of file