Ticket #22400: 22400.general-template-w-unit-tests.diff
File 22400.general-template-w-unit-tests.diff, 11.8 KB (added by , 9 years ago) |
---|
-
tests/phpunit/tests/general-template.php
1 <?php 2 3 class Tests_Get_Archives extends WP_UnitTestCase { 4 5 /* 6 $defaults = array( 7 'type' => 'monthly', 'limit' => '', 8 'format' => 'html', 'before' => '', 9 'after' => '', 'show_post_count' => false, 10 'echo' => 1, 'order' => 'DESC', 11 ); 12 */ 13 14 function setUp() { 15 parent::setUp(); 16 $this->factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) ); 17 } 18 19 function test_wp_get_archives_default() { 20 $expected['default'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></li>"; 21 $this->AssertEquals( $expected['default'], trim( wp_get_archives( array( 'echo' => false ) ) ) ); 22 } 23 24 function test_wp_get_archives_type() { 25 $expected['type'] = "<li><a href='http://example.org/?m=" . date( 'Y' ) . "'>" . date( 'Y' ) . "</a></li>"; 26 $this->AssertEquals( $expected['type'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'yearly' ) ) ) ); 27 } 28 29 function test_wp_get_archives_limit() { 30 $expected['limit'] = <<<EOF 31 <li><a href='http://example.org/?p=26'>Post title 8</a></li> 32 <li><a href='http://example.org/?p=25'>Post title 7</a></li> 33 <li><a href='http://example.org/?p=24'>Post title 6</a></li> 34 <li><a href='http://example.org/?p=23'>Post title 5</a></li> 35 <li><a href='http://example.org/?p=22'>Post title 4</a></li> 36 EOF; 37 $this->AssertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) ); 38 } 39 40 function test_wp_get_archives_format() { 41 $expected['format'] = "<option value='http://example.org/?m=" . date( 'Ym' ) . "'> " . date( 'F Y' ) . " </option>"; 42 $this->AssertEquals( $expected['format'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'option' ) ) ) ); 43 } 44 45 function test_wp_get_archives_before_and_after() { 46 $expected['before_and_after'] = "<div><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></div>"; 47 $this->AssertEquals( $expected['before_and_after'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'custom', 'before' => '<div>', 'after' => '</div>' ) ) ) ); 48 } 49 50 function test_wp_get_archives_show_post_count() { 51 $expected['show_post_count'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a> (8)</li>"; 52 $this->AssertEquals( $expected['show_post_count'], trim( wp_get_archives( array( 'echo' => false, 'show_post_count' => 1 ) ) ) ); 53 } 54 55 function test_wp_get_archives_echo() { 56 $expected['echo'] = "<li><a href='http://example.org/?m=" . date( 'Ym' ) . "'>" . date( 'F Y' ) . "</a></li>"; 57 ob_start(); 58 wp_get_archives( array( 'echo' => true ) ); 59 $actual = ob_get_clean(); 60 $this->AssertEquals( $expected['echo'], trim( $actual ) ); 61 } 62 63 function test_wp_get_archives_order() { 64 $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42' ) ); 65 $date_n = date( "Ym" ); 66 $date_full = date( 'F Y' ); 67 $expected['order_asc'] = <<<EOF 68 <li><a href='http://example.org/?m=201210'>October 2012</a></li> 69 <li><a href='http://example.org/?m=$date_n'>$date_full</a></li> 70 EOF; 71 $this->AssertEquals( $expected['order_asc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'ASC' ) ) ) ); 72 73 $expected['order_desc'] = <<<EOF 74 <li><a href='http://example.org/?m=$date_n'>$date_full</a></li> 75 <li><a href='http://example.org/?m=201210'>October 2012</a></li> 76 EOF; 77 $this->AssertEquals( $expected['order_desc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'DESC' ) ) ) ); 78 } 79 } 80 No newline at end of file -
src/wp-includes/general-template.php
934 934 * @param string|array $args Optional. Override defaults. 935 935 * @return string|null String when retrieving, null when displaying. 936 936 */ 937 function wp_get_archives( $args = '') {937 function wp_get_archives( $args = '' ) { 938 938 global $wpdb, $wp_locale; 939 939 940 940 $defaults = array( … … 945 945 ); 946 946 947 947 $r = wp_parse_args( $args, $defaults ); 948 extract( $r, EXTR_SKIP );949 948 950 if ( '' == $ type)951 $ type= 'monthly';949 if ( '' == $r['type'] ) 950 $r['type'] = 'monthly'; 952 951 953 if ( '' != $ limit) {954 $ limit = absint($limit);955 $ limit = ' LIMIT '.$limit;952 if ( '' != $r['limit'] ) { 953 $r['limit'] = absint( $r['limit'] ); 954 $r['limit'] = ' LIMIT ' . $r['limit']; 956 955 } 957 956 958 $order = strtoupper( $ order);957 $order = strtoupper( $r['order'] ); 959 958 if ( $order !== 'ASC' ) 960 959 $order = 'DESC'; 961 960 … … 973 972 $archive_week_end_date_format = 'Y/m/d'; 974 973 975 974 if ( !$archive_date_format_over_ride ) { 976 $archive_day_date_format = get_option( 'date_format');977 $archive_week_start_date_format = get_option( 'date_format');978 $archive_week_end_date_format = get_option( 'date_format');975 $archive_day_date_format = get_option( 'date_format' ); 976 $archive_week_start_date_format = get_option( 'date_format' ); 977 $archive_week_end_date_format = get_option( 'date_format' ); 979 978 } 980 979 981 980 $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r ); … … 989 988 wp_cache_set( 'last_changed', $last_changed, 'posts' ); 990 989 } 991 990 992 if ( 'monthly' == $type ) { 991 $limit = $r['limit']; 992 993 if ( 'monthly' == $r['type'] ) { 993 994 $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"; 994 995 $key = md5( $query ); 995 996 $key = "wp_get_archives:$key:$last_changed"; … … 998 999 wp_cache_set( $key, $results, 'posts' ); 999 1000 } 1000 1001 if ( $results ) { 1001 $afterafter = $ after;1002 $afterafter = $r['after']; 1002 1003 foreach ( (array) $results as $result ) { 1003 1004 $url = get_month_link( $result->year, $result->month ); 1004 1005 /* translators: 1: month name, 2: 4-digit year */ 1005 $text = sprintf( __('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);1006 if ( $ show_post_count)1007 $ after= ' ('.$result->posts.')' . $afterafter;1008 $output .= get_archives_link( $url, $text, $format, $before, $after);1006 $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year ); 1007 if ( $r['show_post_count'] ) 1008 $r['after'] = ' ('.$result->posts.')' . $afterafter; 1009 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); 1009 1010 } 1010 1011 } 1011 } elseif ( 'yearly' == $type) {1012 } elseif ( 'yearly' == $r['type'] ) { 1012 1013 $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"; 1013 1014 $key = md5( $query ); 1014 1015 $key = "wp_get_archives:$key:$last_changed"; … … 1017 1018 wp_cache_set( $key, $results, 'posts' ); 1018 1019 } 1019 1020 if ( $results ) { 1020 $afterafter = $ after;1021 $afterafter = $r['after']; 1021 1022 foreach ( (array) $results as $result) { 1022 $url = get_year_link( $result->year);1023 $text = sprintf( '%d', $result->year);1024 if ( $show_post_count)1025 $ after= ' ('.$result->posts.')' . $afterafter;1026 $output .= get_archives_link( $url, $text, $format, $before, $after);1023 $url = get_year_link( $result->year ); 1024 $text = sprintf( '%d', $result->year ); 1025 if ( $r['show_post_count'] ) 1026 $r['after'] = ' ('.$result->posts.')' . $afterafter; 1027 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); 1027 1028 } 1028 1029 } 1029 } elseif ( 'daily' == $ type) {1030 } elseif ( 'daily' == $r['type'] ) { 1030 1031 $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"; 1031 1032 $key = md5( $query ); 1032 1033 $key = "wp_get_archives:$key:$last_changed"; … … 1036 1037 wp_cache_set( $key, $results, 'posts' ); 1037 1038 } 1038 1039 if ( $results ) { 1039 $afterafter = $ after;1040 $afterafter = $r['after']; 1040 1041 foreach ( (array) $results as $result ) { 1041 $url = get_day_link( $result->year, $result->month, $result->dayofmonth);1042 $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);1043 $text = mysql2date( $archive_day_date_format, $date);1044 if ( $show_post_count)1045 $after = ' (' .$result->posts.')'.$afterafter;1046 $output .= get_archives_link( $url, $text, $format, $before, $after);1042 $url = get_day_link( $result->year, $result->month, $result->dayofmonth ); 1043 $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth ); 1044 $text = mysql2date( $archive_day_date_format, $date ); 1045 if ( $r['show_post_count'] ) 1046 $after = ' (' . $result->posts . ')' . $afterafter; 1047 $output .= get_archives_link( $url, $text, $format, $before, $after ); 1047 1048 } 1048 1049 } 1049 } elseif ( 'weekly' == $ type) {1050 } elseif ( 'weekly' == $r['type'] ) { 1050 1051 $week = _wp_mysql_week( '`post_date`' ); 1051 1052 $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"; 1052 1053 $key = md5( $query ); … … 1056 1057 wp_cache_set( $key, $results, 'posts' ); 1057 1058 } 1058 1059 $arc_w_last = ''; 1059 $afterafter = $ after;1060 $afterafter = $r['after']; 1060 1061 if ( $results ) { 1061 1062 foreach ( (array) $results as $result ) { 1062 1063 if ( $result->week != $arc_w_last ) { 1063 1064 $arc_year = $result->yr; 1064 1065 $arc_w_last = $result->week; 1065 $arc_week = get_weekstartend( $result->yyyymmdd, get_option('start_of_week'));1066 $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start']);1067 $arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end']);1068 $url = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week);1066 $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) ); 1067 $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] ); 1068 $arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] ); 1069 $url = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week ); 1069 1070 $text = $arc_week_start . $archive_week_separator . $arc_week_end; 1070 if ( $show_post_count)1071 $after = ' (' .$result->posts.')'.$afterafter;1072 $output .= get_archives_link( $url, $text, $format, $before, $after);1071 if ( $r['show_post_count'] ) 1072 $after = ' (' . $result->posts . ')' . $afterafter; 1073 $output .= get_archives_link( $url, $text, $format, $before, $after ); 1073 1074 } 1074 1075 } 1075 1076 } 1076 } elseif ( ( 'postbypost' == $ type ) || ('alpha' == $type) ) {1077 $orderby = ( 'alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';1077 } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) { 1078 $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC '; 1078 1079 $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; 1079 1080 $key = md5( $query ); 1080 1081 $key = "wp_get_archives:$key:$last_changed"; … … 1092 1093 } else { 1093 1094 $text = $result->ID; 1094 1095 } 1095 $output .= get_archives_link( $url, $text, $format, $before, $after);1096 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); 1096 1097 } 1097 1098 } 1098 1099 } 1099 1100 } 1100 if ( $ echo)1101 if ( $r['echo'] ) 1101 1102 echo $output; 1102 1103 else 1103 1104 return $output;