返回介绍

populate_network()

发布于 2017-09-11 09:55:03 字数 11806 浏览 827 评论 0 收藏 0

populate_network( int $network_id = 1,  string $domain = '',  string $email = '',  string $site_name = '',  string $path = '/',  bool $subdomain_install = false )

Populate network settings.


description


参数

$network_id

(int) (Optional) ID of network to populate.

Default value: 1

$domain

(string) (Optional) The domain name for the network (eg. "example.com").

Default value: ''

$email

(string) (Optional) Email address for the network administrator.

Default value: ''

$site_name

(string) (Optional) The name of the network.

Default value: ''

$path

(string) (Optional) The path to append to the network's domain name.

Default value: '/'

$subdomain_install

(bool) (Optional) Whether the network is a subdomain install or a subdirectory install. Default false, meaning the network is a subdirectory install.

Default value: false


返回值

(bool|WP_Error) True on success, or WP_Error on warning (with the install otherwise successful, so the error code must be checked) or failure.


源代码

File: wp-admin/includes/schema.php

function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
	global $wpdb, $current_site, $wp_db_version, $wp_rewrite;

	$errors = new WP_Error();
	if ( '' == $domain )
		$errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
	if ( '' == $site_name )
		$errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) );

	// Check for network collision.
	if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) )
		$errors->add( 'siteid_exists', __( 'The network already exists.' ) );

	if ( ! is_email( $email ) )
		$errors->add( 'invalid_email', __( 'You must provide a valid email address.' ) );

	if ( $errors->get_error_code() )
		return $errors;

	// If a user with the provided email does not exist, default to the current user as the new network admin.
	$site_user = get_user_by( 'email', $email );
	if ( false === $site_user ) {
		$site_user = wp_get_current_user();
	}

	// Set up site tables.
	$template = get_option( 'template' );
	$stylesheet = get_option( 'stylesheet' );
	$allowed_themes = array( $stylesheet => true );

	if ( $template != $stylesheet ) {
		$allowed_themes[ $template ] = true;
	}

	if ( WP_DEFAULT_THEME != $stylesheet && WP_DEFAULT_THEME != $template ) {
		$allowed_themes[ WP_DEFAULT_THEME ] = true;
	}

	// If WP_DEFAULT_THEME doesn't exist, also whitelist the latest core default theme.
	if ( ! wp_get_theme( WP_DEFAULT_THEME )->exists() ) {
		if ( $core_default = WP_Theme::get_core_default_theme() ) {
			$allowed_themes[ $core_default->get_stylesheet() ] = true;
		}
	}

	if ( 1 == $network_id ) {
		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path ) );
		$network_id = $wpdb->insert_id;
	} else {
		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path, 'id' => $network_id ) );
	}

	wp_cache_delete( 'networks_have_paths', 'site-options' );

	if ( !is_multisite() ) {
		$site_admins = array( $site_user->user_login );
		$users = get_users( array(
			'fields' => array( 'user_login' ),
			'role'   => 'administrator',
		) );
		if ( $users ) {
			foreach ( $users as $user ) {
				$site_admins[] = $user->user_login;
			}

			$site_admins = array_unique( $site_admins );
		}
	} else {
		$site_admins = get_site_option( 'site_admins' );
	}

	/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
	$welcome_email = __( 'Howdy USERNAME,

Your new SITE_NAME site has been successfully set up at:
BLOG_URL

You can log in to the administrator account with the following information:

Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLwp-login.php

We hope you enjoy your new site. Thanks!

--The Team @ SITE_NAME' );

	$misc_exts = array(
		// Images.
		'jpg', 'jpeg', 'png', 'gif',
		// Video.
		'mov', 'avi', 'mpg', '3gp', '3g2',
		// "audio".
		'midi', 'mid',
		// Miscellaneous.
		'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key',
	);
	$audio_exts = wp_get_audio_extensions();
	$video_exts = wp_get_video_extensions();
	$upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) );

	$sitemeta = array(
		'site_name' => $site_name,
		'admin_email' => $email,
		'admin_user_id' => $site_user->ID,
		'registration' => 'none',
		'upload_filetypes' => implode( ' ', $upload_filetypes ),
		'blog_upload_space' => 100,
		'fileupload_maxk' => 1500,
		'site_admins' => $site_admins,
		'allowedthemes' => $allowed_themes,
		'illegal_names' => array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
		'wpmu_upgrade_site' => $wp_db_version,
		'welcome_email' => $welcome_email,
		/* translators: %s: site link */
		'first_post' => __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' ),
		// @todo - network admins should have a method of editing the network siteurl (used for cookie hash)
		'siteurl' => get_option( 'siteurl' ) . '/',
		'add_new_users' => '0',
		'upload_space_check_disabled' => is_multisite() ? get_site_option( 'upload_space_check_disabled' ) : '1',
		'subdomain_install' => intval( $subdomain_install ),
		'global_terms_enabled' => global_terms_enabled() ? '1' : '0',
		'ms_files_rewriting' => is_multisite() ? get_site_option( 'ms_files_rewriting' ) : '0',
		'initial_db_version' => get_option( 'initial_db_version' ),
		'active_sitewide_plugins' => array(),
		'WPLANG' => get_locale(),
	);
	if ( ! $subdomain_install )
		$sitemeta['illegal_names'][] = 'blog';

	/**
	 * Filters meta for a network on creation.
	 *
	 * @since 3.7.0
	 *
	 * @param array $sitemeta   Associative array of network meta keys and values to be inserted.
	 * @param int   $network_id ID of network to populate.
	 */
	$sitemeta = apply_filters( 'populate_network_meta', $sitemeta, $network_id );

	$insert = '';
	foreach ( $sitemeta as $meta_key => $meta_value ) {
		if ( is_array( $meta_value ) )
			$meta_value = serialize( $meta_value );
		if ( !empty( $insert ) )
			$insert .= ', ';
		$insert .= $wpdb->prepare( "( %d, %s, %s)", $network_id, $meta_key, $meta_value );
	}
	$wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert );

	/*
	 * When upgrading from single to multisite, assume the current site will
	 * become the main site of the network. When using populate_network()
	 * to create another network in an existing multisite environment, skip
	 * these steps since the main site of the new network has not yet been
	 * created.
	 */
	if ( ! is_multisite() ) {
		$current_site = new stdClass;
		$current_site->domain = $domain;
		$current_site->path = $path;
		$current_site->site_name = ucfirst( $domain );
		$wpdb->insert( $wpdb->blogs, array( 'site_id' => $network_id, 'blog_id' => 1, 'domain' => $domain, 'path' => $path, 'registered' => current_time( 'mysql' ) ) );
		$current_site->blog_id = $blog_id = $wpdb->insert_id;
		update_user_meta( $site_user->ID, '源代码_domain', $domain );
		update_user_meta( $site_user->ID, 'primary_blog', $blog_id );

		if ( $subdomain_install )
			$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
		else
			$wp_rewrite->set_permalink_structure( '/blog/%year%/%monthnum%/%day%/%postname%/' );

		flush_rewrite_rules();

		if ( ! $subdomain_install )
			return true;

		$vhost_ok = false;
		$errstr = '';
		$hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname!
		$page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) );
		if ( is_wp_error( $page ) )
			$errstr = $page->get_error_message();
		elseif ( 200 == wp_remote_retrieve_response_code( $page ) )
				$vhost_ok = true;

		if ( ! $vhost_ok ) {
			$msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>';

			$msg .= '<p>' . sprintf(
				/* translators: %s: host name */
				__( 'The installer attempted to contact a random hostname (%s) on your domain.' ),
				'<code>' . $hostname . '</code>'
			);
			if ( ! empty ( $errstr ) ) {
				/* translators: %s: error message */
				$msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), '<code>' . $errstr . '</code>' );
			}
			$msg .= '</p>';

			$msg .= '<p>' . sprintf(
				/* translators: %s: asterisk symbol (*) */
				__( 'To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a %s hostname record pointing at your web server in your DNS configuration tool.' ),
				'<code>*</code>'
			) . '</p>';

			$msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.' ) . '</p>';

			return new WP_Error( 'no_wildcard_dns', $msg );
		}
	}

	return true;
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-theme.php: WP_Theme::get_core_default_theme()
  • wp-admin/includes/schema.php: populate_network_meta
  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/theme.php: wp_get_theme()
  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: get_locale()
  • wp-includes/formatting.php: is_email()
  • wp-includes/pluggable.php: get_user_by()
  • wp-includes/pluggable.php: wp_get_current_user()
  • wp-includes/load.php: is_multisite()
  • wp-includes/functions.php: global_terms_enabled()
  • wp-includes/functions.php: current_time()
  • wp-includes/http.php: wp_remote_get()
  • wp-includes/http.php: wp_remote_retrieve_response_code()
  • 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/user.php: update_user_meta()
  • wp-includes/media.php: wp_get_video_extensions()
  • wp-includes/media.php: wp_get_audio_extensions()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::set_permalink_structure()
  • wp-includes/rewrite.php: flush_rewrite_rules()
  • wp-includes/wp-db.php: wpdb::get_var()
  • wp-includes/wp-db.php: wpdb::insert()
  • wp-includes/wp-db.php: wpdb::query()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 24 more uses Hide more uses

User Contributed Notes

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

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

发布评论

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