<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[12715] trunk/wp-admin: Improve get_pending_comments_num()
  to be a little more predictable and revert the erroneous change in [12596]
 .</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12715">12715</a></dd>
<dt>Author</dt> <dd>westi</dd>
<dt>Date</dt> <dd>2010-01-12 22:38:26 +0000 (Tue, 12 Jan 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improve get_pending_comments_num() to be a little more predictable and revert the erroneous change in <a href="http://trac.wordpress.org/changeset/12596">[12596]</a>. See <a href="http://trac.wordpress.org/ticket/11882">#11882</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmineditcommentsphp">trunk/wp-admin/edit-comments.php</a></li>
<li><a href="#trunkwpadminincludescommentphp">trunk/wp-admin/includes/comment.php</a></li>
<li><a href="#trunkwpadminincludestemplatephp">trunk/wp-admin/includes/template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmineditcommentsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/edit-comments.php (12714 => 12715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/edit-comments.php        2010-01-12 21:52:51 UTC (rev 12714)
+++ trunk/wp-admin/edit-comments.php        2010-01-12 22:38:26 UTC (rev 12715)
</span><span class="lines">@@ -244,12 +244,9 @@
</span><span class="cx"> foreach ( $_comments as $_c ) {
</span><span class="cx">         $_comment_post_ids[] = $_c-&gt;comment_post_ID;
</span><span class="cx"> }
</span><del>-$_comment_pending_count_temp = (array) get_pending_comments_num($_comment_post_ids);
-foreach ( (array) $_comment_post_ids as $_cpid )
-        $_comment_pending_count[$_cpid] = isset( $_comment_pending_count_temp[$_cpid] ) ? $_comment_pending_count_temp[$_cpid] : 0;
-if ( empty($_comment_pending_count) )
-        $_comment_pending_count = array();
</del><span class="cx"> 
</span><ins>+$_comment_pending_count = get_pending_comments_num($_comment_post_ids);
+
</ins><span class="cx"> $comments = array_slice($_comments, 0, $comments_per_page);
</span><span class="cx"> $extra_comments = array_slice($_comments, $comments_per_page);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpadminincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/comment.php (12714 => 12715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/comment.php        2010-01-12 21:52:51 UTC (rev 12714)
+++ trunk/wp-admin/includes/comment.php        2010-01-12 22:38:26 UTC (rev 12715)
</span><span class="lines">@@ -109,24 +109,33 @@
</span><span class="cx"> 
</span><span class="cx">         $single = false;
</span><span class="cx">         if ( !is_array($post_id) ) {
</span><del>-                $post_id = (array) $post_id;
</del><ins>+                $post_id_array = (array) $post_id;
</ins><span class="cx">                 $single = true;
</span><ins>+        } else {
+                $post_id_array = $post_id;
</ins><span class="cx">         }
</span><del>-        $post_id = array_map('intval', $post_id);
-        $post_id = &quot;'&quot; . implode(&quot;', '&quot;, $post_id) . &quot;'&quot;;
</del><ins>+        $post_id_array = array_map('intval', $post_id_array);
+        $post_id_in = &quot;'&quot; . implode(&quot;', '&quot;, $post_id_array) . &quot;'&quot;;
</ins><span class="cx"> 
</span><del>-        $pending = $wpdb-&gt;get_results( &quot;SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb-&gt;comments WHERE comment_post_ID IN ( $post_id ) AND comment_approved = '0' GROUP BY comment_post_ID&quot;, ARRAY_N );
</del><ins>+        $pending = $wpdb-&gt;get_results( &quot;SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb-&gt;comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID&quot;, ARRAY_A );
</ins><span class="cx"> 
</span><del>-        if ( empty($pending) )
-                return 0;
</del><ins>+        if ( $single ) {
+                if ( empty($pending) )
+                        return 0;
+                else
+                        return absint($pending[0]['num_comments']);
+        }
+        
+        $pending_keyed = array();
+        
+        // Default to zero pending for all posts in request
+        foreach ( $post_id_array as $id )
+                $pending_keyed[$id] = 0;
</ins><span class="cx"> 
</span><del>-        if ( $single )
-                return $pending[0][1];
</del><ins>+        if ( !empty($pending) )
+                foreach ( $pending as $pend )
+                        $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
</ins><span class="cx"> 
</span><del>-        $pending_keyed = array();
-        foreach ( $pending as $pend )
-                $pending_keyed[$pend[0]] = $pend[1];
-
</del><span class="cx">         return $pending_keyed;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpadminincludestemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/template.php (12714 => 12715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/template.php        2010-01-12 21:52:51 UTC (rev 12714)
+++ trunk/wp-admin/includes/template.php        2010-01-12 22:38:26 UTC (rev 12715)
</span><span class="lines">@@ -2263,10 +2263,10 @@
</span><span class="cx">                         case 'response':
</span><span class="cx">                                 if ( 'single' !== $mode ) {
</span><span class="cx">                                         if ( isset( $_comment_pending_count[$post-&gt;ID] ) ) {
</span><del>-                                                $pending_comments = absint( $_comment_pending_count[$post-&gt;ID] );
</del><ins>+                                                $pending_comments = $_comment_pending_count[$post-&gt;ID];
</ins><span class="cx">                                         } else {
</span><span class="cx">                                                 $_comment_pending_count_temp = get_pending_comments_num( array( $post-&gt;ID ) );
</span><del>-                                                $pending_comments = $_comment_pending_count[$post-&gt;ID] = $_comment_pending_count_temp;
</del><ins>+                                                $pending_comments = $_comment_pending_count[$post-&gt;ID] = $_comment_pending_count_temp[$post-&gt;ID];
</ins><span class="cx">                                         }
</span><span class="cx">                                         if ( $user_can ) {
</span><span class="cx">                                                 $post_link = &quot;&lt;a href='&quot; . get_edit_post_link($post-&gt;ID) . &quot;'&gt;&quot;;
</span></span></pre>
</div>
</div>

</body>
</html>