<!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>[27081] trunk: Properly invalidate the cache for `wp_count_posts()` on insert, trash, or when transitioning `post_status` inside of `_transition_post_status()`.</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/27081">27081</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-02-03 19:41:40 +0000 (Mon, 03 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Properly invalidate the cache for `wp_count_posts()` on insert, trash, or when transitioning `post_status` inside of `_transition_post_status()`. Introduces `_count_posts_cache_key()`. Adds unit tests.

Props mark8barnes, for bringing this to our attention in an initial patch.
Fixes <a href="http://core.trac.wordpress.org/ticket/21879">#21879</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestspostphp">trunk/tests/phpunit/tests/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/post.php (27080 => 27081)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/post.php   2014-02-03 05:30:12 UTC (rev 27080)
+++ trunk/src/wp-includes/post.php      2014-02-03 19:41:40 UTC (rev 27081)
</span><span class="lines">@@ -2077,6 +2077,26 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Return the cache key for wp_count_posts() based on the passed arguments
+ *
+ * @since 3.9.0
+ *
+ * @param string $type Optional. Post type to retrieve count
+ * @param string $perm Optional. 'readable' or empty.
+ * @return string The cache key.
+ */
+function _count_posts_cache_key( $type = 'post', $perm = '' ) {
+       $cache_key = 'posts-' . $type;
+       if ( 'readable' == $perm && is_user_logged_in() ) {
+               $post_type_object = get_post_type_object( $type );
+               if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
+                       $cache_key .= '_' . $perm . '_' . get_current_user_id();
+               }
+       }
+       return $cache_key;
+}
+
+/**
</ins><span class="cx">  * Count number of posts of a post type and if user has permissions to view.
</span><span class="cx">  *
</span><span class="cx">  * This function provides an efficient method of finding the amount of post's
</span><span class="lines">@@ -2101,16 +2121,15 @@
</span><span class="cx">  if ( ! post_type_exists( $type ) )
</span><span class="cx">          return new stdClass;
</span><span class="cx"> 
</span><del>-       $user = wp_get_current_user();
</del><ins>+        $cache_key = _count_posts_cache_key( $type, $perm );
</ins><span class="cx"> 
</span><del>-       $cache_key = 'posts-' . $type;
-
</del><span class="cx">   $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
</span><span class="cx">  if ( 'readable' == $perm && is_user_logged_in() ) {
</span><span class="cx">          $post_type_object = get_post_type_object($type);
</span><del>-               if ( !current_user_can( $post_type_object->cap->read_private_posts ) ) {
-                       $cache_key .= '_' . $perm . '_' . $user->ID;
-                       $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
</del><ins>+                if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
+                       $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
+                               get_current_user_id()
+                       );
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx">  $query .= ' GROUP BY post_status';
</span><span class="lines">@@ -4885,6 +4904,11 @@
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       if ( $new_status !== $old_status ) {
+               wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
+               wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
+       }
+
</ins><span class="cx">   // Always clears the hook in case the post status bounced from future to draft.
</span><span class="cx">  wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) );
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunktestsphpunittestspostphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/post.php (27080 => 27081)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/post.php       2014-02-03 05:30:12 UTC (rev 27080)
+++ trunk/tests/phpunit/tests/post.php  2014-02-03 19:41:40 UTC (rev 27081)
</span><span class="lines">@@ -829,4 +829,40 @@
</span><span class="cx">          $counts->publish = 7;
</span><span class="cx">          return $counts;
</span><span class="cx">  }
</span><ins>+
+       function test_wp_count_posts_insert_invalidation() {
+               $post_ids = $this->factory->post->create_many( 10 );
+               $initial_counts = wp_count_posts();
+
+               $key = array_rand( $post_ids );
+               $_post = get_post( $post_ids[$key], ARRAY_A );
+               $_post['post_status'] = 'draft';
+               wp_insert_post( $_post );
+               $post = get_post( $post_ids[$key] );
+               $this->assertEquals( 'draft', $post->post_status );
+               $this->assertNotEquals( 'publish', $post->post_status );
+
+               $after_draft_counts = wp_count_posts();
+               $this->assertEquals( 1, $after_draft_counts->draft );
+               $this->assertEquals( 9, $after_draft_counts->publish );
+               $this->assertNotEquals( $initial_counts->publish, $after_draft_counts->publish );
+       }
+
+       function test_wp_count_posts_trash_invalidation() {
+               $post_ids = $this->factory->post->create_many( 10 );
+               $initial_counts = wp_count_posts();
+
+               $key = array_rand( $post_ids );
+
+               wp_trash_post( $post_ids[$key] );
+
+               $post = get_post( $post_ids[$key] );
+               $this->assertEquals( 'trash', $post->post_status );
+               $this->assertNotEquals( 'publish', $post->post_status );
+
+               $after_trash_counts = wp_count_posts();
+               $this->assertEquals( 1, $after_trash_counts->trash );
+               $this->assertEquals( 9, $after_trash_counts->publish );
+               $this->assertNotEquals( $initial_counts->publish, $after_trash_counts->publish );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>