返回介绍

wpmu_delete_blog()

发布于 2017-09-11 11:18:23 字数 5582 浏览 1103 评论 0 收藏 0

wpmu_delete_blog( int $blog_id,  bool $drop = false )

Delete a site.


description


参数

$blog_id

(int) (Required) Site ID.

$drop

(bool) (Optional) True if site's database tables should be dropped. Default is false.

Default value: false


源代码

File: wp-admin/includes/ms.php

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_site( $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_network = get_network();

	// 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_network->path && $blog->domain == $current_network->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 );
	}

	/**
	 * Fires after the site is deleted from the network.
	 *
	 * @since 4.8.0
	 *
	 * @param int  $blog_id The site ID.
	 * @param bool $drop    True if site's tables should be dropped. Default is false.
	 */
	do_action( 'deleted_blog', $blog_id, $drop );

	if ( $switch )
		restore_current_blog();
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/ms.php: deleted_blog
  • wp-includes/ms-blogs.php: get_network()
  • wp-includes/ms-blogs.php: get_site()
  • wp-includes/functions.php: wp_get_upload_dir()
  • wp-admin/includes/ms.php: delete_blog
  • wp-admin/includes/ms.php: wpmu_drop_tables
  • wp-admin/includes/ms.php: wpmu_delete_blog_upload_dir
  • wp-includes/load.php: get_current_blog_id()
  • wp-includes/functions.php: is_main_site()
  • wp-includes/plugin.php: do_action()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_site_option()
  • wp-includes/option.php: get_option()
  • wp-includes/user.php: get_users()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • wp-includes/ms-blogs.php: switch_to_blog()
  • wp-includes/ms-blogs.php: update_blog_status()
  • wp-includes/ms-blogs.php: restore_current_blog()
  • wp-includes/ms-blogs.php: clean_blog_cache()
  • wp-includes/wp-db.php: wpdb::query()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::tables()
  • Show 17 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文