/** * Clear the blog details cache. * * @since MU * * @param int $blog_id Optional. Blog ID. Defaults to current blog. */ function refresh_blog_details($blog_id = 0) { $blog_id = (int) $blog_id; if (!$blog_id) { $blog_id = get_current_blog_id(); } $details = get_blog_details($blog_id, false); if (!$details) { // Make sure clean_blog_cache() gets the blog ID // when the blog has been previously cached as // non-existent. $details = (object) array('blog_id' => $blog_id, 'domain' => null, 'path' => null); } clean_blog_cache($details); /** * Fires after the blog details cache is cleared. * * @since 3.4.0 * * @param int $blog_id Blog ID. */ do_action('refresh_blog_details', $blog_id); }
/** * Delete a site. * * @since 3.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $blog_id Site ID. * @param bool $drop True if site's database tables should be dropped. Default is false. */ function wpmu_delete_blog($blog_id, $drop = false) { global $wpdb; $switch = false; if (get_current_blog_id() != $blog_id) { $switch = true; switch_to_blog($blog_id); } $blog = get_blog_details($blog_id); /** * Fires before a site is deleted. * * @since MU * * @param int $blog_id The site ID. * @param bool $drop True if site's table should be dropped. Default is false. */ do_action('delete_blog', $blog_id, $drop); $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids')); // Remove users from this blog. if (!empty($users)) { foreach ($users as $user_id) { remove_user_from_blog($user_id, $blog_id); } } update_blog_status($blog_id, 'deleted', 1); $current_site = get_current_site(); // If a full blog object is not available, do not destroy anything. if ($drop && !$blog) { $drop = false; } // Don't destroy the initial, main, or root blog. if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) { $drop = false; } $upload_path = trim(get_option('upload_path')); // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable. if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) { $drop = false; } if ($drop) { $uploads = wp_get_upload_dir(); $tables = $wpdb->tables('blog'); /** * Filters the tables to drop when the site is deleted. * * @since MU * * @param array $tables The site tables to be dropped. * @param int $blog_id The ID of the site to drop tables for. */ $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id); foreach ((array) $drop_tables as $table) { $wpdb->query("DROP TABLE IF EXISTS `{$table}`"); } $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id)); /** * Filters the upload base directory to delete when the site is deleted. * * @since MU * * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir() * @param int $blog_id The site ID. */ $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id); $dir = rtrim($dir, DIRECTORY_SEPARATOR); $top_dir = $dir; $stack = array($dir); $index = 0; while ($index < count($stack)) { // Get indexed directory from stack $dir = $stack[$index]; $dh = @opendir($dir); if ($dh) { while (($file = @readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) { $stack[] = $dir . DIRECTORY_SEPARATOR . $file; } elseif (@is_file($dir . DIRECTORY_SEPARATOR . $file)) { @unlink($dir . DIRECTORY_SEPARATOR . $file); } } @closedir($dh); } $index++; } $stack = array_reverse($stack); // Last added dirs are deepest foreach ((array) $stack as $dir) { if ($dir != $top_dir) { @rmdir($dir); } } clean_blog_cache($blog); } if ($switch) { restore_current_blog(); } }
/** * Clear the blog details cache. * * @since MU * * @param int $blog_id Blog ID */ function refresh_blog_details($blog_id) { $blog_id = (int) $blog_id; $details = get_blog_details($blog_id, false); if (!$details) { // Make sure clean_blog_cache() gets the blog ID // when the blog has been previously cached as // non-existent. $details = (object) array('blog_id' => $blog_id, 'domain' => null, 'path' => null); } clean_blog_cache($details); do_action('refresh_blog_details', $blog_id); }
/** * Delete a blog * * @since 3.0.0 * * @param int $blog_id Blog ID * @param bool $drop True if blog's table should be dropped. Default is false. * @return void */ function wpmu_delete_blog($blog_id, $drop = false) { global $wpdb, $current_site; $switch = false; if (get_current_blog_id() != $blog_id) { $switch = true; switch_to_blog($blog_id); } $blog = get_blog_details($blog_id); do_action('delete_blog', $blog_id, $drop); $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids')); // Remove users from this blog. if (!empty($users)) { foreach ($users as $user_id) { remove_user_from_blog($user_id, $blog_id); } } update_blog_status($blog_id, 'deleted', 1); // Don't destroy the initial, main, or root blog. if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) { $drop = false; } if ($drop) { $drop_tables = apply_filters('wpmu_drop_tables', $wpdb->tables('blog')); foreach ((array) $drop_tables as $table) { $wpdb->query("DROP TABLE IF EXISTS `{$table}`"); } $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id)); $uploads = wp_upload_dir(); $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id); $dir = rtrim($dir, DIRECTORY_SEPARATOR); $top_dir = $dir; $stack = array($dir); $index = 0; while ($index < count($stack)) { # Get indexed directory from stack $dir = $stack[$index]; $dh = @opendir($dir); if ($dh) { while (($file = @readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) { $stack[] = $dir . DIRECTORY_SEPARATOR . $file; } else { if (@is_file($dir . DIRECTORY_SEPARATOR . $file)) { @unlink($dir . DIRECTORY_SEPARATOR . $file); } } } @closedir($dh); } $index++; } $stack = array_reverse($stack); // Last added dirs are deepest foreach ((array) $stack as $dir) { if ($dir != $top_dir) { @rmdir($dir); } } clean_blog_cache($blog); } if ($switch) { restore_current_blog(); } }
/** * Clear the blog details cache. * * @since MU * * @param int $blog_id Blog ID */ function refresh_blog_details($blog_id) { $blog_id = (int) $blog_id; $details = get_blog_details($blog_id, false); clean_blog_cache($details); do_action('refresh_blog_details', $blog_id); }