* Register blog-id-cache group as global

* Introduce clean_blog_cache() so we can run it independently of refresh_blog_details() which assumes the blog still exists and get_blog_details() can be called.
* Don't db escape cache keys in get_blog_id_from_url()
* prepare() the query in get_blog_id_from_url()
* Return 0 for all failures in get_blog_id_from_url()
* clean_blog_cache() after dropping tables in wpmu_delete_blog() to make sure the cache is for real cleaned.



git-svn-id: https://develop.svn.wordpress.org/trunk@22092 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2012-10-01 18:03:23 +00:00
parent 0d5f429651
commit 42cfdfb614
4 changed files with 39 additions and 24 deletions

View File

@ -55,14 +55,13 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
global $wpdb, $current_site;
$switch = false;
if ( $blog_id != $wpdb->blogid ) {
if ( get_current_blog_id() != $blog_id ) {
$switch = true;
switch_to_blog( $blog_id );
$blog = get_blog_details( $blog_id );
} else {
$blog = $GLOBALS['current_blog'];
}
$blog = get_blog_details( $blog_id );
do_action( 'delete_blog', $blog_id, $drop );
$users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
@ -81,7 +80,6 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
$drop = false;
if ( $drop ) {
$drop_tables = apply_filters( 'wpmu_drop_tables', $wpdb->tables( 'blog' ) );
foreach ( (array) $drop_tables as $table ) {
@ -122,6 +120,8 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
if ( $dir != $top_dir)
@rmdir( $dir );
}
clean_blog_cache( $blog );
}
if ( $switch )

View File

@ -409,7 +409,7 @@ function wp_start_object_cache() {
wp_cache_init();
if ( function_exists( 'wp_cache_add_global_groups' ) ) {
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache' ) );
wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
}
}

View File

@ -253,12 +253,7 @@ function refresh_blog_details( $blog_id ) {
$blog_id = (int) $blog_id;
$details = get_blog_details( $blog_id, false );
wp_cache_delete( $blog_id , 'blog-details' );
wp_cache_delete( $blog_id . 'short' , 'blog-details' );
wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
wp_cache_delete( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' );
clean_blog_cache( $details );
do_action( 'refresh_blog_details', $blog_id );
}
@ -316,6 +311,26 @@ function update_blog_details( $blog_id, $details = array() ) {
return true;
}
/**
* Clean the blog cache
*
* @since 3.5.0
*
* @param stdClass $blog The blog details as returned from get_blog_details()
*/
function clean_blog_cache( $blog ) {
$blog_id = $blog->blog_id;
$domain_path_key = md5( $blog->domain . $blog->path );
wp_cache_delete( $blog_id , 'blog-details' );
wp_cache_delete( $blog_id . 'short' , 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-lookup' );
wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-id-cache' );
}
/**
* Retrieve option value for a given blog id based on name of option.
*
@ -492,7 +507,7 @@ function switch_to_blog( $new_blog, $deprecated = null ) {
if ( is_array( $global_groups ) )
wp_cache_add_global_groups( $global_groups );
else
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
}
}
@ -553,7 +568,7 @@ function restore_current_blog() {
if ( is_array( $global_groups ) )
wp_cache_add_global_groups( $global_groups );
else
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
}
}

View File

@ -332,27 +332,27 @@ function get_blog_permalink( $blog_id, $post_id ) {
*
* @param string $domain
* @param string $path Optional. Not required for subdomain installations.
* @return int
* @return int 0 if no blog found, otherwise the ID of the matching blog
*/
function get_blog_id_from_url( $domain, $path = '/' ) {
global $wpdb;
$domain = strtolower( $wpdb->escape( $domain ) );
$path = strtolower( $wpdb->escape( $path ) );
$domain = strtolower( $domain );
$path = strtolower( $path );
$id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
if ( $id == -1 ) { // blog does not exist
if ( $id == -1 ) // blog does not exist
return 0;
} elseif ( $id ) {
elseif ( $id )
return (int) $id;
}
$id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs WHERE domain = '$domain' and path = '$path' /* get_blog_id_from_url */" );
$id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
if ( ! $id ) {
wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
return false;
return 0;
}
wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
return $id;