<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[28379] trunk: Eliminate use of `extract()` in `wp_get_archives()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28379">28379</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-05-13 04:28:56 +0000 (Tue, 13 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Eliminate use of `extract()` in `wp_get_archives()`. 

Adds unit tests: `tests/functions/getArchives.php`. 
All other unit tests pass.

Props MikeHansenMe, wonderboymusic.
See <a href="http://core.trac.wordpress.org/ticket/22400">#22400</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesgeneraltemplatephp">trunk/src/wp-includes/general-template.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsfunctionsgetArchivesphp">trunk/tests/phpunit/tests/functions/getArchives.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/general-template.php (28378 => 28379)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/general-template.php       2014-05-13 04:28:30 UTC (rev 28378)
+++ trunk/src/wp-includes/general-template.php  2014-05-13 04:28:56 UTC (rev 28379)
</span><span class="lines">@@ -1173,7 +1173,7 @@
</span><span class="cx">  * @param string|array $args Optional. Override defaults.
</span><span class="cx">  * @return string|null String when retrieving, null when displaying.
</span><span class="cx">  */
</span><del>-function wp_get_archives($args = '') {
</del><ins>+function wp_get_archives( $args = '' ) {
</ins><span class="cx">   global $wpdb, $wp_locale;
</span><span class="cx"> 
</span><span class="cx">  $defaults = array(
</span><span class="lines">@@ -1184,19 +1184,20 @@
</span><span class="cx">  );
</span><span class="cx"> 
</span><span class="cx">  $r = wp_parse_args( $args, $defaults );
</span><del>-       extract( $r, EXTR_SKIP );
</del><span class="cx"> 
</span><del>-       if ( '' == $type )
-               $type = 'monthly';
</del><ins>+        if ( '' == $r['type'] ) {
+               $r['type'] = 'monthly';
+       }
</ins><span class="cx"> 
</span><del>-       if ( '' != $limit ) {
-               $limit = absint($limit);
-               $limit = ' LIMIT '.$limit;
</del><ins>+        if ( '' != $r['limit'] ) {
+               $r['limit'] = absint( $r['limit'] );
+               $r['limit'] = ' LIMIT ' . $r['limit'];
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       $order = strtoupper( $order );
-       if ( $order !== 'ASC' )
</del><ins>+        $order = strtoupper( $r['order'] );
+       if ( $order !== 'ASC' ) {
</ins><span class="cx">           $order = 'DESC';
</span><ins>+       }
</ins><span class="cx"> 
</span><span class="cx">  // this is what will separate dates on weekly archive links
</span><span class="cx">  $archive_week_separator = '&#8211;';
</span><span class="lines">@@ -1211,10 +1212,10 @@
</span><span class="cx">  $archive_week_start_date_format = 'Y/m/d';
</span><span class="cx">  $archive_week_end_date_format   = 'Y/m/d';
</span><span class="cx"> 
</span><del>-       if ( !$archive_date_format_over_ride ) {
-               $archive_day_date_format = get_option('date_format');
-               $archive_week_start_date_format = get_option('date_format');
-               $archive_week_end_date_format = get_option('date_format');
</del><ins>+        if ( ! $archive_date_format_over_ride ) {
+               $archive_day_date_format = get_option( 'date_format' );
+               $archive_week_start_date_format = get_option( 'date_format' );
+               $archive_week_end_date_format = get_option( 'date_format' );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -1245,7 +1246,9 @@
</span><span class="cx">          wp_cache_set( 'last_changed', $last_changed, 'posts' );
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       if ( 'monthly' == $type ) {
</del><ins>+        $limit = $r['limit'];
+
+       if ( 'monthly' == $r['type'] ) {
</ins><span class="cx">           $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";
</span><span class="cx">          $key = md5( $query );
</span><span class="cx">          $key = "wp_get_archives:$key:$last_changed";
</span><span class="lines">@@ -1254,17 +1257,18 @@
</span><span class="cx">                  wp_cache_set( $key, $results, 'posts' );
</span><span class="cx">          }
</span><span class="cx">          if ( $results ) {
</span><del>-                       $afterafter = $after;
</del><ins>+                        $after = $r['after'];
</ins><span class="cx">                   foreach ( (array) $results as $result ) {
</span><span class="cx">                          $url = get_month_link( $result->year, $result->month );
</span><span class="cx">                          /* translators: 1: month name, 2: 4-digit year */
</span><del>-                               $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
-                               if ( $show_post_count )
-                                       $after = '&nbsp;('.$result->posts.')' . $afterafter;
-                               $output .= get_archives_link($url, $text, $format, $before, $after);
</del><ins>+                                $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
+                               if ( $r['show_post_count'] ) {
+                                       $after = '&nbsp;('.$result->posts.')' . $after;
+                               }
+                               $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><del>-       } elseif ('yearly' == $type) {
</del><ins>+        } elseif ( 'yearly' == $r['type'] ) {
</ins><span class="cx">           $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";
</span><span class="cx">          $key = md5( $query );
</span><span class="cx">          $key = "wp_get_archives:$key:$last_changed";
</span><span class="lines">@@ -1273,16 +1277,17 @@
</span><span class="cx">                  wp_cache_set( $key, $results, 'posts' );
</span><span class="cx">          }
</span><span class="cx">          if ( $results ) {
</span><del>-                       $afterafter = $after;
</del><ins>+                        $after = $r['after'];
</ins><span class="cx">                   foreach ( (array) $results as $result) {
</span><del>-                               $url = get_year_link($result->year);
-                               $text = sprintf('%d', $result->year);
-                               if ($show_post_count)
-                                       $after = '&nbsp;('.$result->posts.')' . $afterafter;
-                               $output .= get_archives_link($url, $text, $format, $before, $after);
</del><ins>+                                $url = get_year_link( $result->year );
+                               $text = sprintf( '%d', $result->year );
+                               if ( $r['show_post_count'] ) {
+                                       $r['after'] = '&nbsp;('.$result->posts.')' . $after;
+                               }
+                               $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><del>-       } elseif ( 'daily' == $type ) {
</del><ins>+        } elseif ( 'daily' == $r['type'] ) {
</ins><span class="cx">           $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";
</span><span class="cx">          $key = md5( $query );
</span><span class="cx">          $key = "wp_get_archives:$key:$last_changed";
</span><span class="lines">@@ -1292,17 +1297,18 @@
</span><span class="cx">                  wp_cache_set( $key, $results, 'posts' );
</span><span class="cx">          }
</span><span class="cx">          if ( $results ) {
</span><del>-                       $afterafter = $after;
</del><ins>+                        $after = $r['after'];
</ins><span class="cx">                   foreach ( (array) $results as $result ) {
</span><del>-                               $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);
-                               $text = mysql2date($archive_day_date_format, $date);
-                               if ($show_post_count)
-                                       $after = '&nbsp;('.$result->posts.')'.$afterafter;
-                               $output .= get_archives_link($url, $text, $format, $before, $after);
</del><ins>+                                $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 );
+                               $text = mysql2date( $archive_day_date_format, $date );
+                               if ( $r['show_post_count'] ) {
+                                       $after = '&nbsp;(' . $result->posts . ')' . $after;
+                               }
+                               $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><del>-       } elseif ( 'weekly' == $type ) {
</del><ins>+        } elseif ( 'weekly' == $r['type'] ) {
</ins><span class="cx">           $week = _wp_mysql_week( '`post_date`' );
</span><span class="cx">          $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";
</span><span class="cx">          $key = md5( $query );
</span><span class="lines">@@ -1312,25 +1318,26 @@
</span><span class="cx">                  wp_cache_set( $key, $results, 'posts' );
</span><span class="cx">          }
</span><span class="cx">          $arc_w_last = '';
</span><del>-               $afterafter = $after;
</del><ins>+                $after = $r['after'];
</ins><span class="cx">           if ( $results ) {
</span><span class="cx">                          foreach ( (array) $results as $result ) {
</span><span class="cx">                                  if ( $result->week != $arc_w_last ) {
</span><span class="cx">                                          $arc_year = $result->yr;
</span><span class="cx">                                          $arc_w_last = $result->week;
</span><del>-                                               $arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week'));
-                                               $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
-                                               $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, '&amp;', '=', $result->week);
</del><ins>+                                                $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
+                                               $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] );
+                                               $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, '&amp;', '=', $result->week );
</ins><span class="cx">                                           $text = $arc_week_start . $archive_week_separator . $arc_week_end;
</span><del>-                                               if ($show_post_count)
-                                                       $after = '&nbsp;('.$result->posts.')'.$afterafter;
-                                               $output .= get_archives_link($url, $text, $format, $before, $after);
</del><ins>+                                                if ( $r['show_post_count'] ) {
+                                                       $after = '&nbsp;(' . $result->posts . ')' . $after;
+                                               }
+                                               $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $after );
</ins><span class="cx">                                   }
</span><span class="cx">                          }
</span><span class="cx">          }
</span><del>-       } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
-               $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
</del><ins>+        } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
+               $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC ';
</ins><span class="cx">           $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
</span><span class="cx">          $key = md5( $query );
</span><span class="cx">          $key = "wp_get_archives:$key:$last_changed";
</span><span class="lines">@@ -1348,15 +1355,16 @@
</span><span class="cx">                                  } else {
</span><span class="cx">                                          $text = $result->ID;
</span><span class="cx">                                  }
</span><del>-                                       $output .= get_archives_link($url, $text, $format, $before, $after);
</del><ins>+                                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
</ins><span class="cx">                           }
</span><span class="cx">                  }
</span><span class="cx">          }
</span><span class="cx">  }
</span><del>-       if ( $echo )
</del><ins>+        if ( $r['echo'] ) {
</ins><span class="cx">           echo $output;
</span><del>-       else
</del><ins>+        } else {
</ins><span class="cx">           return $output;
</span><ins>+       }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -2509,7 +2517,7 @@
</span><span class="cx">                  $r .= join("</li>\n\t<li>", $page_links);
</span><span class="cx">                  $r .= "</li>\n</ul>\n";
</span><span class="cx">                  break;
</span><del>-               
</del><ins>+
</ins><span class="cx">           default :
</span><span class="cx">                  $r = join("\n", $page_links);
</span><span class="cx">                  break;
</span></span></pre></div>
<a id="trunktestsphpunittestsfunctionsgetArchivesphp"></a>
<div class="addfile"><h4>Added: trunk/tests/phpunit/tests/functions/getArchives.php (0 => 28379)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/functions/getArchives.php                              (rev 0)
+++ trunk/tests/phpunit/tests/functions/getArchives.php 2014-05-13 04:28:56 UTC (rev 28379)
</span><span class="lines">@@ -0,0 +1,85 @@
</span><ins>+<?php
+/*
+$defaults = array(
+       'type' => 'monthly', 'limit' => '',
+       'format' => 'html', 'before' => '',
+       'after' => '', 'show_post_count' => false,
+       'echo' => 1, 'order' => 'DESC',
+);
+*/
+class Tests_Get_Archives extends WP_UnitTestCase {
+       protected $post_ids;
+       protected $month_url;
+
+       function setUp() {
+               parent::setUp();
+
+               $this->month_url = get_month_link( date( 'Y' ), date( 'm' ) );
+               $this->year_url = get_year_link( date( 'Y' ) );
+               $this->post_ids = $this->factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
+       }
+
+       function test_wp_get_archives_default() {
+               $expected['default'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . "</a></li>";
+               $this->assertEquals( $expected['default'], trim( wp_get_archives( array( 'echo' => false ) ) ) );
+       }
+
+       function test_wp_get_archives_type() {
+               $expected['type'] = "<li><a href='" . $this->year_url . "'>" . date( 'Y' ) . "</a></li>";
+               $this->assertEquals( $expected['type'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'yearly' ) ) ) );
+       }
+
+       function test_wp_get_archives_limit() {
+               $ids = array_slice( array_reverse( $this->post_ids ), 0, 5 );
+
+               $expected['limit'] = <<<EOF
+<li><a href='http://example.org/?p={$ids[0]}'>Post title 8</a></li>
+       <li><a href='http://example.org/?p={$ids[1]}'>Post title 7</a></li>
+       <li><a href='http://example.org/?p={$ids[2]}'>Post title 6</a></li>
+       <li><a href='http://example.org/?p={$ids[3]}'>Post title 5</a></li>
+       <li><a href='http://example.org/?p={$ids[4]}'>Post title 4</a></li>
+EOF;
+               $this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
+       }
+
+       function test_wp_get_archives_format() {
+               $expected['format'] = "<option value='" . $this->month_url . "'> " . date( 'F Y' ) . ' </option>';
+               $this->assertEquals( $expected['format'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'option' ) ) ) );
+       }
+
+       function test_wp_get_archives_before_and_after() {
+               $expected['before_and_after'] = "<div><a href='" . $this->month_url . "'>" . date( 'F Y' ) . '</a></div>';
+               $this->assertEquals( $expected['before_and_after'], trim( wp_get_archives( array( 'echo' => false, 'format' => 'custom', 'before' => '<div>', 'after' => '</div>' ) ) ) );
+       }
+
+       function test_wp_get_archives_show_post_count() {
+               $expected['show_post_count'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . "</a>&nbsp;(8)</li>";
+               $this->assertEquals( $expected['show_post_count'], trim( wp_get_archives( array( 'echo' => false, 'show_post_count' => 1 ) ) ) );
+       }
+
+       function test_wp_get_archives_echo() {
+               $expected['echo'] = "<li><a href='" . $this->month_url . "'>" . date( 'F Y' ) . '</a></li>';
+               ob_start();
+               wp_get_archives( array( 'echo' => true ) );
+               $actual = ob_get_clean();
+               $this->assertEquals( $expected['echo'], trim( $actual ) );
+       }
+
+       function test_wp_get_archives_order() {
+               $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42' ) );
+
+               $date_full = date( 'F Y' );
+               $oct_url = get_month_link( 2012, 10 );
+               $expected['order_asc'] = <<<EOF
+<li><a href='{$oct_url}'>October 2012</a></li>
+       <li><a href='{$this->month_url}'>$date_full</a></li>
+EOF;
+               $this->assertEquals( $expected['order_asc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'ASC' ) ) ) );
+
+               $expected['order_desc'] = <<<EOF
+<li><a href='{$this->month_url}'>$date_full</a></li>
+       <li><a href='{$oct_url}'>October 2012</a></li>
+EOF;
+               $this->assertEquals( $expected['order_desc'], trim( wp_get_archives( array( 'echo' => false, 'order' => 'DESC' ) ) ) );
+       }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>