mirror of
https://github.com/WordPress/WordPress.git
synced 2025-01-05 16:08:12 +01:00
Eliminate use of extract()
in wp_get_archives()
.
Adds unit tests: `tests/functions/getArchives.php`. All other unit tests pass. Props MikeHansenMe, wonderboymusic. See #22400. Built from https://develop.svn.wordpress.org/trunk@28379 git-svn-id: http://core.svn.wordpress.org/trunk@28207 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
62b6306739
commit
b110c7f887
@ -1184,19 +1184,20 @@ function wp_get_archives($args = '') {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$r = wp_parse_args( $args, $defaults );
|
$r = wp_parse_args( $args, $defaults );
|
||||||
extract( $r, EXTR_SKIP );
|
|
||||||
|
|
||||||
if ( '' == $type )
|
if ( '' == $r['type'] ) {
|
||||||
$type = 'monthly';
|
$r['type'] = 'monthly';
|
||||||
|
|
||||||
if ( '' != $limit ) {
|
|
||||||
$limit = absint($limit);
|
|
||||||
$limit = ' LIMIT '.$limit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = strtoupper( $order );
|
if ( '' != $r['limit'] ) {
|
||||||
if ( $order !== 'ASC' )
|
$r['limit'] = absint( $r['limit'] );
|
||||||
|
$r['limit'] = ' LIMIT ' . $r['limit'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$order = strtoupper( $r['order'] );
|
||||||
|
if ( $order !== 'ASC' ) {
|
||||||
$order = 'DESC';
|
$order = 'DESC';
|
||||||
|
}
|
||||||
|
|
||||||
// this is what will separate dates on weekly archive links
|
// this is what will separate dates on weekly archive links
|
||||||
$archive_week_separator = '–';
|
$archive_week_separator = '–';
|
||||||
@ -1245,7 +1246,9 @@ function wp_get_archives($args = '') {
|
|||||||
wp_cache_set( 'last_changed', $last_changed, 'posts' );
|
wp_cache_set( 'last_changed', $last_changed, 'posts' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'monthly' == $type ) {
|
$limit = $r['limit'];
|
||||||
|
|
||||||
|
if ( 'monthly' == $r['type'] ) {
|
||||||
$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";
|
$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";
|
||||||
$key = md5( $query );
|
$key = md5( $query );
|
||||||
$key = "wp_get_archives:$key:$last_changed";
|
$key = "wp_get_archives:$key:$last_changed";
|
||||||
@ -1254,17 +1257,18 @@ function wp_get_archives($args = '') {
|
|||||||
wp_cache_set( $key, $results, 'posts' );
|
wp_cache_set( $key, $results, 'posts' );
|
||||||
}
|
}
|
||||||
if ( $results ) {
|
if ( $results ) {
|
||||||
$afterafter = $after;
|
$after = $r['after'];
|
||||||
foreach ( (array) $results as $result ) {
|
foreach ( (array) $results as $result ) {
|
||||||
$url = get_month_link( $result->year, $result->month );
|
$url = get_month_link( $result->year, $result->month );
|
||||||
/* translators: 1: month name, 2: 4-digit year */
|
/* translators: 1: month name, 2: 4-digit year */
|
||||||
$text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
|
$text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
|
||||||
if ( $show_post_count )
|
if ( $r['show_post_count'] ) {
|
||||||
$after = ' ('.$result->posts.')' . $afterafter;
|
$after = ' ('.$result->posts.')' . $after;
|
||||||
$output .= get_archives_link($url, $text, $format, $before, $after);
|
}
|
||||||
|
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ('yearly' == $type) {
|
} elseif ( 'yearly' == $r['type'] ) {
|
||||||
$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";
|
$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";
|
||||||
$key = md5( $query );
|
$key = md5( $query );
|
||||||
$key = "wp_get_archives:$key:$last_changed";
|
$key = "wp_get_archives:$key:$last_changed";
|
||||||
@ -1273,16 +1277,17 @@ function wp_get_archives($args = '') {
|
|||||||
wp_cache_set( $key, $results, 'posts' );
|
wp_cache_set( $key, $results, 'posts' );
|
||||||
}
|
}
|
||||||
if ( $results ) {
|
if ( $results ) {
|
||||||
$afterafter = $after;
|
$after = $r['after'];
|
||||||
foreach ( (array) $results as $result) {
|
foreach ( (array) $results as $result) {
|
||||||
$url = get_year_link( $result->year );
|
$url = get_year_link( $result->year );
|
||||||
$text = sprintf( '%d', $result->year );
|
$text = sprintf( '%d', $result->year );
|
||||||
if ($show_post_count)
|
if ( $r['show_post_count'] ) {
|
||||||
$after = ' ('.$result->posts.')' . $afterafter;
|
$r['after'] = ' ('.$result->posts.')' . $after;
|
||||||
$output .= get_archives_link($url, $text, $format, $before, $after);
|
}
|
||||||
|
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ( 'daily' == $type ) {
|
} elseif ( 'daily' == $r['type'] ) {
|
||||||
$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";
|
$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";
|
||||||
$key = md5( $query );
|
$key = md5( $query );
|
||||||
$key = "wp_get_archives:$key:$last_changed";
|
$key = "wp_get_archives:$key:$last_changed";
|
||||||
@ -1292,17 +1297,18 @@ function wp_get_archives($args = '') {
|
|||||||
wp_cache_set( $key, $results, 'posts' );
|
wp_cache_set( $key, $results, 'posts' );
|
||||||
}
|
}
|
||||||
if ( $results ) {
|
if ( $results ) {
|
||||||
$afterafter = $after;
|
$after = $r['after'];
|
||||||
foreach ( (array) $results as $result ) {
|
foreach ( (array) $results as $result ) {
|
||||||
$url = get_day_link( $result->year, $result->month, $result->dayofmonth );
|
$url = get_day_link( $result->year, $result->month, $result->dayofmonth );
|
||||||
$date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
|
$date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
|
||||||
$text = mysql2date( $archive_day_date_format, $date );
|
$text = mysql2date( $archive_day_date_format, $date );
|
||||||
if ($show_post_count)
|
if ( $r['show_post_count'] ) {
|
||||||
$after = ' ('.$result->posts.')'.$afterafter;
|
$after = ' (' . $result->posts . ')' . $after;
|
||||||
$output .= get_archives_link($url, $text, $format, $before, $after);
|
}
|
||||||
|
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ( 'weekly' == $type ) {
|
} elseif ( 'weekly' == $r['type'] ) {
|
||||||
$week = _wp_mysql_week( '`post_date`' );
|
$week = _wp_mysql_week( '`post_date`' );
|
||||||
$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";
|
$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";
|
||||||
$key = md5( $query );
|
$key = md5( $query );
|
||||||
@ -1312,7 +1318,7 @@ function wp_get_archives($args = '') {
|
|||||||
wp_cache_set( $key, $results, 'posts' );
|
wp_cache_set( $key, $results, 'posts' );
|
||||||
}
|
}
|
||||||
$arc_w_last = '';
|
$arc_w_last = '';
|
||||||
$afterafter = $after;
|
$after = $r['after'];
|
||||||
if ( $results ) {
|
if ( $results ) {
|
||||||
foreach ( (array) $results as $result ) {
|
foreach ( (array) $results as $result ) {
|
||||||
if ( $result->week != $arc_w_last ) {
|
if ( $result->week != $arc_w_last ) {
|
||||||
@ -1323,14 +1329,15 @@ function wp_get_archives($args = '') {
|
|||||||
$arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] );
|
$arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] );
|
||||||
$url = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week );
|
$url = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week );
|
||||||
$text = $arc_week_start . $archive_week_separator . $arc_week_end;
|
$text = $arc_week_start . $archive_week_separator . $arc_week_end;
|
||||||
if ($show_post_count)
|
if ( $r['show_post_count'] ) {
|
||||||
$after = ' ('.$result->posts.')'.$afterafter;
|
$after = ' (' . $result->posts . ')' . $after;
|
||||||
$output .= get_archives_link($url, $text, $format, $before, $after);
|
}
|
||||||
|
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
|
} elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
|
||||||
$orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
|
$orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC ';
|
||||||
$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
|
$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
|
||||||
$key = md5( $query );
|
$key = md5( $query );
|
||||||
$key = "wp_get_archives:$key:$last_changed";
|
$key = "wp_get_archives:$key:$last_changed";
|
||||||
@ -1348,16 +1355,17 @@ function wp_get_archives($args = '') {
|
|||||||
} else {
|
} else {
|
||||||
$text = $result->ID;
|
$text = $result->ID;
|
||||||
}
|
}
|
||||||
$output .= get_archives_link($url, $text, $format, $before, $after);
|
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( $echo )
|
if ( $r['echo'] ) {
|
||||||
echo $output;
|
echo $output;
|
||||||
else
|
} else {
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get number of days since the start of the week.
|
* Get number of days since the start of the week.
|
||||||
|
Loading…
Reference in New Issue
Block a user