: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
do_action( 'wp_validate_site_data', $errors, $data, $old_site );
if ( ! empty( $errors->errors ) ) {
$data['site_id'] = $data['network_id'];
unset( $data['network_id'] );
* Normalizes data for a site prior to inserting or updating in the database.
* @param array $data Associative array of site data passed to the respective function.
* See {@see wp_insert_site()} for the possibly included data.
* @return array Normalized site data.
function wp_normalize_site_data( $data ) {
// Sanitize domain if passed.
if ( array_key_exists( 'domain', $data ) ) {
$data['domain'] = preg_replace( '/[^a-z0-9\-.:]+/i', '', $data['domain'] );
// Sanitize path if passed.
if ( array_key_exists( 'path', $data ) ) {
$data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
// Sanitize network ID if passed.
if ( array_key_exists( 'network_id', $data ) ) {
$data['network_id'] = (int) $data['network_id'];
// Sanitize status fields if passed.
$status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
foreach ( $status_fields as $status_field ) {
if ( array_key_exists( $status_field, $data ) ) {
$data[ $status_field ] = (int) $data[ $status_field ];
// Strip date fields if empty.
$date_fields = array( 'registered', 'last_updated' );
foreach ( $date_fields as $date_field ) {
if ( ! array_key_exists( $date_field, $data ) ) {
if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
unset( $data[ $date_field ] );
* Validates data for a site prior to inserting or updating in the database.
* @param WP_Error $errors Error object, passed by reference. Will contain validation errors if
* @param array $data Associative array of complete site data. See {@see wp_insert_site()}
* @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
* or null if it is a new site being inserted.
function wp_validate_site_data( $errors, $data, $old_site = null ) {
// A domain must always be present.
if ( empty( $data['domain'] ) ) {
$errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
// A path must always be present.
if ( empty( $data['path'] ) ) {
$errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
// A network ID must always be present.
if ( empty( $data['network_id'] ) ) {
$errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
// Both registration and last updated dates must always be present and valid.
$date_fields = array( 'registered', 'last_updated' );
foreach ( $date_fields as $date_field ) {
if ( empty( $data[ $date_field ] ) ) {
$errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
// Allow '0000-00-00 00:00:00', although it be stripped out at this point.
if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) {
$month = substr( $data[ $date_field ], 5, 2 );
$day = substr( $data[ $date_field ], 8, 2 );
$year = substr( $data[ $date_field ], 0, 4 );
$valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
$errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
if ( ! empty( $errors->errors ) ) {
// If a new site, or domain/path/network ID have changed, ensure uniqueness.
|| $data['domain'] !== $old_site->domain
|| $data['path'] !== $old_site->path
|| $data['network_id'] !== $old_site->network_id
if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
$errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
* Runs the initialization routine for a given site.
* This process includes creating the site's database tables and
* populating them with defaults.
* @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.
* 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
* @type array $options Custom option $key => $value pairs to use. Default
* @type array $meta Custom site metadata $key => $value pairs to use.
* @return true|WP_Error True on success, or error object on failure.
function wp_initialize_site( $site_id, array $args = array() ) {
if ( empty( $site_id ) ) {
return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
$site = get_site( $site_id );
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 );
$network = get_network();
/* translators: %d: Site ID. */
'title' => sprintf( __( 'Site %d' ), $site->id ),
* Filters the arguments for initializing a site.
* @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 ) {
if ( get_current_blog_id() !== $site->id ) {
switch_to_blog( $site->id );
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Set up the database tables.
make_db_current_silent( 'blog' );
$siteurl_scheme = 'http';
if ( ! is_subdomain_install() ) {
if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
$siteurl_scheme = 'https';
// Populate the site's options.
'home' => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
'siteurl' => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
'blogname' => wp_unslash( $args['title'] ),
'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' ),
// Clean blog cache after populating options.
clean_blog_cache( $site );
// Populate the site's 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 );
wp_installing( $orig_installing );
* Runs the uninitialization routine for a given site.
* This process includes dropping the site's database tables and deleting its uploads directory.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|WP_Site $site_id Site ID or object.
* @return true|WP_Error True on success, or error object on failure.
function wp_uninitialize_site( $site_id ) {
if ( empty( $site_id ) ) {
return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
$site = get_site( $site_id );
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_uninitialized', __( 'The site appears to be already uninitialized.' ) );
// Remove users from the site.
if ( ! empty( $users ) ) {
foreach ( $users as $user_id ) {
remove_user_from_blog( $user_id, $site->id );
if ( get_current_blog_id() !== $site->id ) {
switch_to_blog( $site->id );
$uploads = wp_get_upload_dir();
$tables = $wpdb->tables( 'blog' );
* Filters the tables to drop when the site is deleted.
* @param string[] $tables Array of names of the site tables to be dropped.
* @param int $site_id The ID of the site to drop tables for.
$drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id );
foreach ( (array) $drop_tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
* Filters the upload base directory to delete when the site is deleted.
* @param string $basedir Uploads path without subdirectory. See {@see wp_upload_dir()}.
* @param int $site_id The site ID.
$dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
$dir = rtrim( $dir, DIRECTORY_SEPARATOR );
while ( $index < count( $stack ) ) {
// Get indexed directory from stack.
// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
while ( false !== $file ) {
if ( '.' === $file || '..' === $file ) {
if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
} elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
@unlink( $dir . DIRECTORY_SEPARATOR . $file );
$stack = array_reverse( $stack ); // Last added directories are deepest.
foreach ( (array) $stack as $dir ) {
if ( $dir !== $top_dir ) {
// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
* Checks whether a site is initialized.
* A site is considered initialized when its database tables are present.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|WP_Site $site_id Site ID or object.
* @return bool True if the site is initialized, false otherwise.
function wp_is_site_initialized( $site_id ) {
if ( is_object( $site_id ) ) {
$site_id = $site_id->blog_id;
$site_id = (int) $site_id;
* Filters the check for whether a site is initialized before the database is accessed.
* Returning a non-null value will effectively short-circuit the function, returning
* @param bool|null $pre The value to return instead. Default null
* to continue with the check.
* @param int $site_id The site ID that is being checked.
$pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
if ( get_current_blog_id() !== $site_id ) {
remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
switch_to_blog( $site_id );
$suppress = $wpdb->suppress_errors();
$result = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
$wpdb->suppress_errors( $suppress );
add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
* @global bool $_wp_suspend_cache_invalidation
* @param WP_Site|int $blog The site object or ID to be cleared from cache.
function clean_blog_cache( $blog ) {
global $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
$blog = get_site( $blog_id );
if ( ! is_numeric( $blog_id ) ) {
// Make sure a WP_Site object exists even when the site has been deleted.
$blog_id = $blog->blog_id;
$domain_path_key = md5( $blog->domain . $blog->path );
wp_cache_delete( $blog_id, 'sites' );
wp_cache_delete( $blog_id, 'site-details' );
wp_cache_delete( $blog_id, 'blog-details' );
wp_cache_delete( $blog_id . 'short', 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-lookup' );
wp_cache_delete( $domain_path_key, 'blog-id-cache' );
wp_cache_delete( $blog_id, 'blog_meta' );
* Fires immediately after a site has been removed from the object cache.