<!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>[15875] trunk/wp-includes:
  Update network-wide active user and blog counts via a cron job to avoid costly count queries
 .</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15875">15875</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-10-20 20:22:14 +0000 (Wed, 20 Oct 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Update network-wide active user and blog counts via a cron job to avoid costly count queries. see <a href="http://trac.wordpress.org/ticket/15170">#15170</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesmsdefaultfiltersphp">trunk/wp-includes/ms-default-filters.php</a></li>
<li><a href="#trunkwpincludesmsfunctionsphp">trunk/wp-includes/ms-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesmsdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-default-filters.php (15874 => 15875)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-default-filters.php        2010-10-20 19:00:57 UTC (rev 15874)
+++ trunk/wp-includes/ms-default-filters.php        2010-10-20 20:22:14 UTC (rev 15875)
</span><span class="lines">@@ -39,6 +39,8 @@
</span><span class="cx"> add_action( 'private_to_published', 'wpmu_update_blogs_date' );
</span><span class="cx"> add_action( 'publish_phone', 'wpmu_update_blogs_date' );
</span><span class="cx"> add_action( 'publish_post', 'wpmu_update_blogs_date' );
</span><ins>+add_action( 'admin_init', 'wp_schedule_update_network_counts');
+add_action( 'update_network_counts', 'wp_update_network_counts');
</ins><span class="cx"> 
</span><span class="cx"> // Files
</span><span class="cx"> add_filter( 'wp_upload_bits', 'upload_is_file_too_big' );
</span><span class="lines">@@ -57,7 +59,6 @@
</span><span class="cx">         add_filter( 'enable_edit_any_user_configuration', '__return_false' );
</span><span class="cx"> add_filter( 'force_filtered_html_on_import', '__return_true' );
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> // WP_HOME and WP_SITEURL should not have any effect in MS
</span><span class="cx"> remove_filter( 'option_siteurl', '_config_wp_siteurl' );
</span><span class="cx"> remove_filter( 'option_home',    '_config_wp_home'    );
</span></span></pre></div>
<a id="trunkwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-functions.php (15874 => 15875)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-functions.php        2010-10-20 19:00:57 UTC (rev 15874)
+++ trunk/wp-includes/ms-functions.php        2010-10-20 20:22:14 UTC (rev 15875)
</span><span class="lines">@@ -148,8 +148,7 @@
</span><span class="cx"> /**
</span><span class="cx">  * The number of active users in your installation.
</span><span class="cx">  *
</span><del>- * This function also saves the count as a site option,
- * which speeds up future lookups.
</del><ins>+ * The count is cached and updated twice daily. This is not a live count.
</ins><span class="cx">  *
</span><span class="cx">  * @since MU 2.7
</span><span class="cx">  * @uses update_site_option()
</span><span class="lines">@@ -157,25 +156,13 @@
</span><span class="cx">  * @return int
</span><span class="cx">  */
</span><span class="cx"> function get_user_count() {
</span><del>-        global $wpdb;
-
-        $count_ts = get_site_option( 'user_count_ts' );
-        if ( time() - $count_ts &gt; 3600 ) {
-                $count = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT COUNT(ID) as c FROM $wpdb-&gt;users WHERE spam = '0' AND deleted = '0'&quot;) );
-                update_site_option( 'user_count', $count );
-                update_site_option( 'user_count_ts', time() );
-        }
-
-        $count = get_site_option( 'user_count' );
-
-        return $count;
</del><ins>+        return get_site_option( 'user_count' );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * The number of active sites on your installation.
</span><span class="cx">  *
</span><del>- * This function also saves the count as a site option,
- * which speeds up future lookups.
</del><ins>+ * The count is cached and updated twice daily. This is not a live count.
</ins><span class="cx">  *
</span><span class="cx">  * @since MU 1.0
</span><span class="cx">  * @uses update_site_option()
</span><span class="lines">@@ -184,21 +171,7 @@
</span><span class="cx">  * @return int
</span><span class="cx">  */
</span><span class="cx"> function get_blog_count( $id = 0 ) {
</span><del>-        global $wpdb;
-
-        if ( $id == 0 )
-                $id = $wpdb-&gt;siteid;
-
-        $count_ts = get_site_option( 'blog_count_ts' );
-        if ( time() - $count_ts &gt; 3600 ) {
-                $count = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT COUNT(blog_id) as c FROM $wpdb-&gt;blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'&quot;, $id) );
-                update_site_option( 'blog_count', $count );
-                update_site_option( 'blog_count_ts', time() );
-        }
-
-        $count = get_site_option( 'blog_count' );
-
-        return $count;
</del><ins>+        return get_site_option( 'blog_count' );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -1533,4 +1506,32 @@
</span><span class="cx">         return $url;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Schedule update of the network-wide counts for the current network.
+ *
+ * @since 3.1.0
+ */
+function wp_schedule_update_network_counts() {
+        if ( !is_main_site() )
+                return;
+
+        if ( !wp_next_scheduled('update_network_counts') &amp;&amp; !defined('WP_INSTALLING') )
+                wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
+}
+
+/**
+ *  Update the network-wide counts for the current network.
+ *
+ *  @since 3.1.0
+ */
+function wp_update_network_counts() {
+        global $wpdb;
+
+        $count = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT COUNT(blog_id) as c FROM $wpdb-&gt;blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'&quot;, $wpdb-&gt;siteid) );
+        update_site_option( 'blog_count', $count );
+
+        $count = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT COUNT(ID) as c FROM $wpdb-&gt;users WHERE spam = '0' AND deleted = '0'&quot;) );
+        update_site_option( 'user_count', $count );
+}
+
</ins><span class="cx"> ?&gt;
</span></span></pre>
</div>
</div>

</body>
</html>