WordPress Version: 5.7
/**
* Runs the initialization routine for a given site.
*
* This process includes creating the site's database tables and
* populating them with defaults.
*
* @since 5.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global WP_Roles $wp_roles WordPress role management object.
*
* @param int|WP_Site $site_id Site ID or object.
* @param array $args {
* Optional. Arguments to modify the initialization behavior.
*
* @type int $user_id Required. User ID for the site administrator.
* @type string $title Site title. Default is 'Site %d' where %d is the
* site ID.
* @type array $options Custom option $key => $value pairs to use. Default
* empty array.
* @type array $meta Custom site metadata $key => $value pairs to use.
* Default empty array.
* }
* @return true|WP_Error True on success, or error object on failure.
*/
function wp_initialize_site($site_id, array $args = array())
{
global $wpdb, $wp_roles;
if (empty($site_id)) {
return new WP_Error('site_empty_id', __('Site ID must not be empty.'));
}
$site = get_site($site_id);
if (!$site) {
return new WP_Error('site_invalid_id', __('Site with the ID does not exist.'));
}
if (wp_is_site_initialized($site)) {
return new WP_Error('site_already_initialized', __('The site appears to be already initialized.'));
}
$network = get_network($site->network_id);
if (!$network) {
$network = get_network();
}
$args = wp_parse_args($args, array(
'user_id' => 0,
/* translators: %d: Site ID. */
'title' => sprintf(__('Site %d'), $site->id),
'options' => array(),
'meta' => array(),
));
/**
* Filters the arguments for initializing a site.
*
* @since 5.1.0
*
* @param array $args Arguments to modify the initialization behavior.
* @param WP_Site $site Site that is being initialized.
* @param WP_Network $network Network that the site belongs to.
*/
$args = apply_filters('wp_initialize_site_args', $args, $site, $network);
$orig_installing = wp_installing();
if (!$orig_installing) {
wp_installing(true);
}
$switch = false;
if (get_current_blog_id() !== $site->id) {
$switch = true;
switch_to_blog($site->id);
}
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Set up the database tables.
make_db_current_silent('blog');
$home_scheme = 'http';
$siteurl_scheme = 'http';
if (!is_subdomain_install()) {
if ('https' === parse_url(get_home_url($network->site_id), PHP_URL_SCHEME)) {
$home_scheme = 'https';
}
if ('https' === parse_url(get_network_option($network->id, 'siteurl'), PHP_URL_SCHEME)) {
$siteurl_scheme = 'https';
}
}
// Populate the site's options.
populate_options(array_merge(array('home' => untrailingslashit($home_scheme . '://' . $site->domain . $site->path), 'siteurl' => untrailingslashit($siteurl_scheme . '://' . $site->domain . $site->path), 'blogname' => wp_unslash($args['title']), 'admin_email' => '', 'upload_path' => get_network_option($network->id, 'ms_files_rewriting') ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option($network->site_id, 'upload_path'), 'blog_public' => (int) $site->public, 'WPLANG' => get_network_option($network->id, 'WPLANG')), $args['options']));
// Clean blog cache after populating options.
clean_blog_cache($site);
// Populate the site's roles.
populate_roles();
$wp_roles = new WP_Roles();
// Populate metadata for the site.
populate_site_meta($site->id, $args['meta']);
// Remove all permissions that may exist for the site.
$table_prefix = $wpdb->get_blog_prefix();
delete_metadata('user', 0, $table_prefix . 'user_level', null, true);
// Delete all.
delete_metadata('user', 0, $table_prefix . 'capabilities', null, true);
// Delete all.
// Install default site content.
wp_install_defaults($args['user_id']);
// Set the site administrator.
add_user_to_blog($site->id, $args['user_id'], 'administrator');
if (!user_can($args['user_id'], 'manage_network') && !get_user_meta($args['user_id'], 'primary_blog', true)) {
update_user_meta($args['user_id'], 'primary_blog', $site->id);
}
if ($switch) {
restore_current_blog();
}
wp_installing($orig_installing);
return true;
}