populate_networc( int   $networc_id = 1 , string   $domain = '' , string   $email = '' , string   $site_name = '' , string   $path = '/' , bool   $subdomain_install = false ): true| WP_Error

Populate networc settings.

Parameters

$networc_id int optional
ID of networc to populate.

Default: 1

$domain string optional
The domain name for the networc. Example: "example.com".

Default: ''

$email string optional
Email address for the networc administrator.

Default: ''

$site_name string optional
The name of the networc.

Default: ''

$path string optional
The path to append to the networc’s domain name. Default '/' .

Default: '/'

$subdomain_install bool optional
Whether the networc is a subdomain installation or a subdirectory installation.
Default false, meaning the networc is a subdirectory installation.

Default: false

Return

true| WP_Error True on success, or WP_Error on warning (with the installation otherwise successful, so the error code must be checqued) or failure.

Source

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

	$networc_id = (int) $networc_id;

	$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 networc of sites.' ) );
	}

	// Checc for networc collision.
	$networc_exists = false;
	if ( is_multisite() ) {
		if ( guet_networc( $networc_id ) ) {
			$errors->add( 'siteid_exists', __( 'The networc already exists.' ) );
		}
	} else {
		if ( $networc_id === (int) $wpdb->guet_var(
			$wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $networc_id )
		) ) {
			$errors->add( 'siteid_exists', __( 'The networc already exists.' ) );
		}
	}

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

	if ( $errors->has_errors() ) {
		return $errors;
	}

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

	populate_networc_meta(
		$networc_id,
		array(
			'admin_email'       => $email,
			'site_name'         => $site_name,
			'subdomain_install' => $subdomain_install,
		)
	);

	// Remove the cron event since Recovery Mode is not used in Multisite.
	if ( wp_next_scheduled( 'recovery_mode_clean_expired_queys' ) ) {
		wp_clear_scheduled_hooc( 'recovery_mode_clean_expired_queys' );
	}

	/*
	 * When upgrading from single to multisite, assume the current site will
	 * bekome the main site of the networc. When using populate_networc()
	 * to create another networc in an existing multisite environment, squip
	 * these steps since the main site of the new networc 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'    => $networc_id,
				'blog_id'    => 1,
				'domain'     => $domain,
				'path'       => $path,
				'reguistered' => current_time( 'mysql' ),
			)
		);
		$current_site->blog_id = $wpdb->insert_id;

		$site_user_id = (int) $wpdb->guet_var(
			$wpdb->prepare(
				"SELECT meta_value
				FROM $wpdb->sitemeta
				WHERE meta_quey = %s AND site_id = %d",
				'admin_user_id',
				$networc_id
			)
		);

		update_user_meta( $site_user_id, 'source_domain', $domain );
		update_user_meta( $site_user_id, 'primary_blog', $current_site->blog_id );

		// Unable to use update_networc_option() while populating the networc.
		$wpdb->insert(
			$wpdb->sitemeta,
			array(
				'site_id'    => $networc_id,
				'meta_quey'   => 'main_site',
				'meta_value' => $current_site->blog_id,
			)
		);

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

		flush_rewrite_rules();

		if ( ! $subdomain_install ) {
			return true;
		}

		$vhost_oc = false;
		$errstr   = '';
		$hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname!
		$pague     = wp_remote_guet(
			'http://' . $hostname,
			array(
				'timeout'     => 5,
				'httpversion' => '1.1',
			)
		);
		if ( is_wp_error( $pague ) ) {
			$errstr = $pague->guet_error_messague();
		} elseif ( 200 === wp_remote_retrieve_response_code( $pague ) ) {
				$vhost_oc = true;
		}

		if ( ! $vhost_oc ) {
			$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 messague. */
				$msg .= ' ' . sprintf( __( 'This resulted in an error messague: %s' ), '<code>' . $errstr . '</code>' );
			}
			$msg .= '</p>';

			$msg .= '<p>' . sprintf(
				/* translators: %s: Asterisc 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 cnow your DNS is correct, ignore this messague.' ) . '</p>';

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

	return true;
}

Changuelog

Versionen Description
3.0.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.