Live network counts of users and sites for small networks.

props adamsilverstein, jeremyfelt.
fixes #22917.

Built from https://develop.svn.wordpress.org/trunk@25621


git-svn-id: http://core.svn.wordpress.org/trunk@25538 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-09-25 16:21:09 +00:00
parent a0010703bb
commit 0e959f06dc
2 changed files with 83 additions and 0 deletions

View File

@ -37,8 +37,15 @@ add_filter( 'term_id_filter', 'global_terms', 10, 2 );
add_action( 'publish_post', 'update_posts_count' );
add_action( 'delete_post', '_update_blog_date_on_post_delete' );
add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
// Counts
add_action( 'admin_init', 'wp_schedule_update_network_counts');
add_action( 'update_network_counts', 'wp_update_network_counts');
foreach ( array( 'user_register', 'deleted_user', 'wpmu_new_user', 'make_spam_user', 'make_ham_user' ) as $action )
add_action( $action, 'wp_maybe_update_network_user_counts' );
foreach ( array( 'make_spam_blog', 'make_ham_blog', 'archive_blog', 'unarchive_blog', 'make_delete_blog', 'make_undelete_blog' ) as $action )
add_action( $action, 'wp_maybe_update_network_site_counts' );
unset( $action );
// Files
add_filter( 'wp_upload_bits', 'upload_is_file_too_big' );

View File

@ -1102,6 +1102,9 @@ function insert_blog($domain, $path, $site_id) {
$blog_id = $wpdb->insert_id;
refresh_blog_details( $blog_id );
wp_maybe_update_network_site_counts();
return $blog_id;
}
@ -1876,10 +1879,83 @@ function wp_schedule_update_network_counts() {
* @since 3.1.0
*/
function wp_update_network_counts() {
wp_update_network_user_counts();
wp_update_network_site_counts();
}
/**
* Update the count of sites for the current network.
*
* If enabled through the 'enable_live_network_counts' filter, update the sites count
* on a network when a site is created or its status is updated.
*
* @since 3.7.0
*
* @uses wp_update_network_site_counts()
*/
function wp_maybe_update_network_site_counts() {
$is_small_network = ! wp_is_large_network( 'sites' );
/**
* Filter the decision to update network user and site counts in real time.
*
* @since 3.7.0
*
* @param bool $small_network Based on wp_is_large_network( $context ).
* @param string $context Context. Either 'users' or 'sites'.
*/
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
return;
wp_update_network_site_counts();
}
/**
* Update the network-wide users count.
*
* If enabled through the 'enable_live_network_counts' filter, update the users count
* on a network when a user is created or its status is updated.
*
* @since 3.7.0
*
* @uses wp_update_network_user_counts()
*/
function wp_maybe_update_network_user_counts() {
$is_small_network = ! wp_is_large_network( 'users' );
/**
* Filter the decision to update network user and site counts in real time.
*
* @since 3.7.0
*
* @param bool $small_network Based on wp_is_large_network( $context ).
* @param string $context Context. Either 'users' or 'sites'.
*/
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
return;
wp_update_network_user_counts();
}
/**
* Update the network-wide site count.
*
* @since 3.7.0
*/
function wp_update_network_site_counts() {
global $wpdb;
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
update_site_option( 'blog_count', $count );
}
/**
* Update the network-wide user count.
*
* @since 3.7.0
*/
function wp_update_network_user_counts() {
global $wpdb;
$count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
update_site_option( 'user_count', $count );