Index: src/wp-admin/includes/schema.php
===================================================================
--- src/wp-admin/includes/schema.php	(revision 43627)
+++ src/wp-admin/includes/schema.php	(working copy)
@@ -383,7 +383,8 @@
 	}
 
 	// If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme.
-	$stylesheet = $template = WP_DEFAULT_THEME;
+	$stylesheet = WP_DEFAULT_THEME;
+	$template   = WP_DEFAULT_THEME;
 	$theme      = wp_get_theme( WP_DEFAULT_THEME );
 	if ( ! $theme->exists() ) {
 		$theme = WP_Theme::get_core_default_theme();
@@ -402,7 +403,7 @@
 	 * or a valid timezone string (America/New_York). See https://secure.php.net/manual/en/timezones.php
 	 * for all timezone strings supported by PHP.
 	 */
-	$offset_or_tz = _x( '0', 'default GMT offset or timezone string' );
+	$offset_or_tz = _x( '0', 'default GMT offset or timezone string' ); // phpcs:ignore WordPress.WP.I18n.NoEmptyStrings
 	if ( is_numeric( $offset_or_tz ) ) {
 		$gmt_offset = $offset_or_tz;
 	} elseif ( $offset_or_tz && in_array( $offset_or_tz, timezone_identifiers_list() ) ) {
@@ -562,7 +563,7 @@
 	$fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' );
 
 	$keys             = "'" . implode( "', '", array_keys( $options ) ) . "'";
-	$existing_options = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name in ( $keys )" );
+	$existing_options = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name in ( $keys )" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
 
 	$insert = '';
 	foreach ( $options as $option => $value ) {
@@ -585,7 +586,7 @@
 	}
 
 	if ( ! empty( $insert ) ) {
-		$wpdb->query( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert );
+		$wpdb->query( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
 	}
 
 	// In case it is set, but blank, update "home".
@@ -960,7 +961,6 @@
  *
  * @global wpdb       $wpdb
  * @global object     $current_site
- * @global int        $wp_db_version
  * @global WP_Rewrite $wp_rewrite
  *
  * @param int    $network_id        ID of network to populate.
@@ -974,7 +974,7 @@
  *                       so the error code must be checked) or failure.
  */
 function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
-	global $wpdb, $current_site, $wp_db_version, $wp_rewrite;
+	global $wpdb, $current_site, $wp_rewrite;
 
 	$errors = new WP_Error();
 	if ( '' == $domain ) {
@@ -1004,13 +1004,149 @@
 		return $errors;
 	}
 
+	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,
+			)
+		);
+	}
+
+	populate_network_meta(
+		$network_id,
+		array(
+			'admin_email'       => $email,
+			'site_name'         => $site_name,
+			'subdomain_install' => $subdomain_install,
+		)
+	);
+
+	$site_user = get_userdata( (int) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", 'admin_user_id', $network_id ) ) );
+
+	/*
+	 * 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 = $wpdb->insert_id;
+		update_user_meta( $site_user->ID, 'source_domain', $domain );
+		update_user_meta( $site_user->ID, 'primary_blog', $current_site->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;
+}
+
+/**
+ * Creates WordPress network meta and sets the default values.
+ *
+ * @since 5.0.0
+ *
+ * @global wpdb $wpdb          WordPress database abstraction object.
+ * @global int  $wp_db_version WordPress database version.
+ *
+ * @param int   $network_id Network ID to populate meta for.
+ * @param array $meta       Optional. Custom meta $key => $value pairs to use. Default empty array.
+ */
+function populate_network_meta( $network_id, array $meta = array() ) {
+	global $wpdb, $wp_db_version;
+
+	$network_id = (int) $network_id;
+
+	$email             = ! empty( $meta['admin_email'] ) ? $meta['admin_email'] : '';
+	$subdomain_install = isset( $meta['subdomain_install'] ) ? (int) $meta['subdomain_install'] : 0;
+
 	// 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 );
+	$site_user = ! empty( $email ) ? get_user_by( 'email', $email ) : false;
 	if ( false === $site_user ) {
 		$site_user = wp_get_current_user();
 	}
 
-	// Set up site tables.
+	if ( empty( $email ) ) {
+		$email = $site_user->user_email;
+	}
+
 	$template       = get_option( 'template' );
 	$stylesheet     = get_option( 'stylesheet' );
 	$allowed_themes = array( $stylesheet => true );
@@ -1025,31 +1161,12 @@
 
 	// 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() ) {
+		$core_default = WP_Theme::get_core_default_theme();
+		if ( $core_default ) {
 			$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() ) {
@@ -1089,7 +1206,7 @@
 --The Team @ SITE_NAME'
 	);
 
-	$misc_exts        = array(
+	$misc_exts = array(
 		// Images.
 		'jpg',
 		'jpeg',
@@ -1122,7 +1239,7 @@
 	$upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) );
 
 	$sitemeta = array(
-		'site_name'                   => $site_name,
+		'site_name'                   => __( 'My Network' ),
 		'admin_email'                 => $email,
 		'admin_user_id'               => $site_user->ID,
 		'registration'                => 'none',
@@ -1140,7 +1257,7 @@
 		'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 ),
+		'subdomain_install'           => $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' ),
@@ -1151,6 +1268,8 @@
 		$sitemeta['illegal_names'][] = 'blog';
 	}
 
+	$sitemeta = wp_parse_args( $meta, $sitemeta );
+
 	/**
 	 * Filters meta for a network on creation.
 	 *
@@ -1171,87 +1290,5 @@
 		}
 		$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, 'source_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;
+	$wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
 }
Index: tests/phpunit/tests/admin/includesSchema.php
===================================================================
--- tests/phpunit/tests/admin/includesSchema.php	(revision 43627)
+++ tests/phpunit/tests/admin/includesSchema.php	(working copy)
@@ -1,4 +1,5 @@
 <?php
+// phpcs:ignoreFile WordPress.WP.PreparedSQL.NotPrepared
 
 /**
  * @group admin
@@ -6,6 +7,7 @@
 class Tests_Admin_Includes_Schema extends WP_UnitTestCase {
 
 	private static $options;
+	private static $sitemeta;
 
 	/**
 	 * Make sure the schema code is loaded before the tests are run.
@@ -14,8 +16,10 @@
 		global $wpdb;
 
 		self::$options  = 'testprefix_options';
+		self::$sitemeta = 'testprefix_sitemeta';
 
-		$options = self::$options;
+		$options  = self::$options;
+		$sitemeta = self::$sitemeta;
 
 		require_once( ABSPATH . 'wp-admin/includes/schema.php' );
 
@@ -34,6 +38,19 @@
 			) {$charset_collate}
 			"
 		);
+		$wpdb->query(
+			"
+			CREATE TABLE {$sitemeta} (
+				meta_id bigint(20) unsigned NOT NULL auto_increment,
+				site_id bigint(20) unsigned NOT NULL default '0',
+				meta_key varchar(255) default NULL,
+				meta_value longtext,
+				PRIMARY KEY  (meta_id),
+				KEY meta_key (meta_key({$max_index_length})),
+				KEY site_id (site_id)
+			) {$charset_collate}
+			"
+		);
 	}
 
 	/**
@@ -42,9 +59,11 @@
 	public static function wpTearDownAfterClass() {
 		global $wpdb;
 
-		$options = self::$options;
+		$options  = self::$options;
+		$sitemeta = self::$sitemeta;
 
 		$wpdb->query( "DROP TABLE IF EXISTS {$options}" );
+		$wpdb->query( "DROP TABLE IF EXISTS {$sitemeta}" );
 	}
 
 	/**
@@ -54,10 +73,6 @@
 	function test_populate_options( $options, $expected ) {
 		global $wpdb;
 
-		remove_all_filters( 'option_admin_email' );
-		remove_all_filters( 'pre_option_admin_email' );
-		remove_all_filters( 'default_option_admin_email' );
-
 		$orig_options  = $wpdb->options;
 		$wpdb->options = self::$options;
 
@@ -92,8 +107,8 @@
 			),
 			array(
 				array(
-					'posts_per_rss'    => 7,
-					'rss_use_excerpt'  => 1,
+					'posts_per_rss'   => 7,
+					'rss_use_excerpt' => 1,
 				),
 				array(
 					// Random options to check.
@@ -129,15 +144,83 @@
 			),
 			array(
 				array(
-					'rss_0123456789abcdef0123456789abcdef'    => '1',
+					'rss_0123456789abcdef0123456789abcdef' => '1',
 					'rss_0123456789abcdef0123456789abcdef_ts' => '1',
 				),
 				array(
 					// These options would be obsolete magpie cache data and should never exist.
-					'rss_0123456789abcdef0123456789abcdef'    => false,
+					'rss_0123456789abcdef0123456789abcdef' => false,
 					'rss_0123456789abcdef0123456789abcdef_ts' => false,
 				),
 			),
 		);
 	}
+
+	/**
+	 * @ticket 44895
+	 * @dataProvider data_populate_network_meta
+	 */
+	function test_populate_network_meta( $meta, $expected ) {
+		global $wpdb;
+
+		$orig_sitemeta  = $wpdb->sitemeta;
+		$wpdb->sitemeta = self::$sitemeta;
+
+		populate_network_meta( 42, $meta );
+
+		$results = array();
+		foreach ( $expected as $meta_key => $value ) {
+			if ( is_multisite() ) {
+				$results[ $meta_key ] = get_network_option( 42, $meta_key );
+			} else {
+				$results[ $meta_key ] = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $meta_key, 42 ) );
+			}
+		}
+
+		$wpdb->query( "TRUNCATE TABLE {$wpdb->sitemeta}" );
+
+		$wpdb->sitemeta = $orig_sitemeta;
+
+		$this->assertEquals( $expected, $results );
+	}
+
+	public function data_populate_network_meta() {
+		return array(
+			array(
+				array(),
+				array(
+					// Random meta to check.
+					'registration'      => 'none',
+					'blog_upload_space' => 100,
+					'fileupload_maxk'   => 1500,
+				),
+			),
+			array(
+				array(
+					'site_name' => 'My Great Network',
+					'WPLANG'    => 'fr_FR',
+				),
+				array(
+					// Random meta to check.
+					'site_name'         => 'My Great Network',
+					'registration'      => 'none',
+					'blog_upload_space' => 100,
+					'fileupload_maxk'   => 1500,
+					'WPLANG'            => 'fr_FR',
+				),
+			),
+			array(
+				array(
+					'custom_meta' => '1',
+				),
+				array(
+					// Random meta to check.
+					'custom_meta'       => '1',
+					'registration'      => 'none',
+					'blog_upload_space' => 100,
+					'fileupload_maxk'   => 1500,
+				),
+			),
+		);
+	}
 }
