WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/13/2014 04:28:56 AM (7 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r28326 r28379  
    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
     
    11851185
    11861186    $r = wp_parse_args( $args, $defaults );
    1187     extract( $r, EXTR_SKIP );
    1188 
    1189     if ( '' == $type )
    1190         $type = 'monthly';
    1191 
    1192     if ( '' != $limit ) {
    1193         $limit = absint($limit);
    1194         $limit = ' LIMIT '.$limit;
    1195     }
    1196 
    1197     $order = strtoupper( $order );
    1198     if ( $order !== 'ASC' )
     1187
     1188    if ( '' == $r['type'] ) {
     1189        $r['type'] = 'monthly';
     1190    }
     1191
     1192    if ( '' != $r['limit'] ) {
     1193        $r['limit'] = absint( $r['limit'] );
     1194        $r['limit'] = ' LIMIT ' . $r['limit'];
     1195    }
     1196
     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
     
    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
     
    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 );
     
    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 );
     
    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 );
     
    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";
     
    13131319        }
    13141320        $arc_w_last = '';
    1315         $afterafter = $after;
     1321        $after = $r['after'];
    13161322        if ( $results ) {
    13171323                foreach ( (array) $results as $result ) {
     
    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 );
     
    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
     
    25102518            $r .= "</li>\n</ul>\n";
    25112519            break;
    2512        
     2520
    25132521        default :
    25142522            $r = join("\n", $page_links);
Note: See TracChangeset for help on using the changeset viewer.