Multisite: Complete the new CRUD API for managing sites.

New functions `wp_insert_site( $data )`, `wp_update_site( $id, $data )` and `wp_delete_site( $id )` are introduced to manage site rows in the `wp_blogs` table, forming the new CRUD API together with the existing `get_site()` / `get_sites()`. The new API provides various benefits over the previously existing API, fixing several cache invalidation issues and being hook-driven so that normalization and validation of the passed data can be fully customized.

New hooks introduced as part of this are the actions `wp_insert_site`, `wp_update_site`, `wp_delete_site`, `wp_validate_site_data` and the filter `wp_normalize_site_data`.

At this point, `wp_insert_site()` does not handle setting up the site's database tables, and `wp_delete_site()` does not handle dropping the site's database tables, so the two can not yet be used directly as full replacements of `wpmu_create_blog()` and `wpmu_delete_blog()`. Managing the site's database tables will be added via hooks as part of the follow-up ticket #41333.

The existing functions `wpmu_create_blog()`, `update_blog_details()`, and `wpmu_delete_blog()` make use of the respective new counterpart and will be obsolete once #41333 has been completed.

Props flixos90, jeremyfelt, spacedmonkey.
Fixes #40364.

Built from https://develop.svn.wordpress.org/trunk@43548


git-svn-id: http://core.svn.wordpress.org/trunk@43377 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2018-08-01 13:06:26 +00:00
parent d38f04eada
commit 9e2e491f1f
6 changed files with 602 additions and 239 deletions

View File

@ -56,6 +56,7 @@ function check_upload_size( $file ) {
* Delete a site.
*
* @since 3.0.0
* @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
@ -142,7 +143,7 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
}
}
$wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
wp_delete_site( $blog_id );
/**
* Filters the upload base directory to delete when the site is deleted.

View File

@ -297,132 +297,12 @@ function update_blog_details( $blog_id, $details = array() ) {
$details = get_object_vars( $details );
}
$current_details = get_site( $blog_id );
if ( empty( $current_details ) ) {
$site = wp_update_site( $blog_id, $details );
if ( is_wp_error( $site ) ) {
return false;
}
$current_details = get_object_vars( $current_details );
$details = array_merge( $current_details, $details );
$details['last_updated'] = current_time( 'mysql', true );
$update_details = array();
$fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
if ( 'path' === $field ) {
$details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
}
$update_details[ $field ] = $details[ $field ];
}
$result = $wpdb->update( $wpdb->blogs, $update_details, array( 'blog_id' => $blog_id ) );
if ( false === $result ) {
return false;
}
// If spam status changed, issue actions.
if ( $details['spam'] != $current_details['spam'] ) {
if ( $details['spam'] == 1 ) {
/**
* Fires when the 'spam' status is added to a blog.
*
* @since MU (3.0.0)
*
* @param int $blog_id Blog ID.
*/
do_action( 'make_spam_blog', $blog_id );
} else {
/**
* Fires when the 'spam' status is removed from a blog.
*
* @since MU (3.0.0)
*
* @param int $blog_id Blog ID.
*/
do_action( 'make_ham_blog', $blog_id );
}
}
// If mature status changed, issue actions.
if ( $details['mature'] != $current_details['mature'] ) {
if ( $details['mature'] == 1 ) {
/**
* Fires when the 'mature' status is added to a blog.
*
* @since 3.1.0
*
* @param int $blog_id Blog ID.
*/
do_action( 'mature_blog', $blog_id );
} else {
/**
* Fires when the 'mature' status is removed from a blog.
*
* @since 3.1.0
*
* @param int $blog_id Blog ID.
*/
do_action( 'unmature_blog', $blog_id );
}
}
// If archived status changed, issue actions.
if ( $details['archived'] != $current_details['archived'] ) {
if ( $details['archived'] == 1 ) {
/**
* Fires when the 'archived' status is added to a blog.
*
* @since MU (3.0.0)
*
* @param int $blog_id Blog ID.
*/
do_action( 'archive_blog', $blog_id );
} else {
/**
* Fires when the 'archived' status is removed from a blog.
*
* @since MU (3.0.0)
*
* @param int $blog_id Blog ID.
*/
do_action( 'unarchive_blog', $blog_id );
}
}
// If deleted status changed, issue actions.
if ( $details['deleted'] != $current_details['deleted'] ) {
if ( $details['deleted'] == 1 ) {
/**
* Fires when the 'deleted' status is added to a blog.
*
* @since 3.5.0
*
* @param int $blog_id Blog ID.
*/
do_action( 'make_delete_blog', $blog_id );
} else {
/**
* Fires when the 'deleted' status is removed from a blog.
*
* @since 3.5.0
*
* @param int $blog_id Blog ID.
*/
do_action( 'make_undelete_blog', $blog_id );
}
}
if ( isset( $details['public'] ) ) {
switch_to_blog( $blog_id );
update_option( 'blog_public', $details['public'] );
restore_current_blog();
}
clean_blog_cache( $blog_id );
return true;
}
@ -517,6 +397,170 @@ function clean_site_details_cache( $site_id = 0 ) {
wp_cache_delete( $site_id, 'blog-details' );
}
/**
* Inserts a new site into the database.
*
* @since 5.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $data {
* Data for the new site that should be inserted.
*
* @type string $domain Site domain. Default empty string.
* @type string $path Site path. Default '/'.
* @type int $network_id The site's network ID. Default is the current network ID.
* @type string $registered When the site was registered, in SQL datetime format. Default is
* the current time.
* @type string $last_updated When the site was last updated, in SQL datetime format. Default is
* the value of $registered.
* @type int $public Whether the site is public. Default 1.
* @type int $archived Whether the site is archived. Default 0.
* @type int $mature Whether the site is mature. Default 0.
* @type int $spam Whether the site is spam. Default 0.
* @type int $deleted Whether the site is deleted. Default 0.
* @type int $lang_id The site's language ID. Currently unused. Default 0.
* }
* @return int|WP_Error The new site's ID on success, or error object on failure.
*/
function wp_insert_site( array $data ) {
global $wpdb;
$now = current_time( 'mysql', true );
$defaults = array(
'domain' => '',
'path' => '/',
'network_id' => get_current_network_id(),
'registered' => $now,
'last_updated' => $now,
'public' => 1,
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
'lang_id' => 0,
);
$data = wp_prepare_site_data( $data, $defaults );
if ( is_wp_error( $data ) ) {
return $data;
}
if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
}
$new_site = get_site( $wpdb->insert_id );
clean_blog_cache( $new_site );
/**
* Fires once a site has been inserted into the database.
*
* @since 5.0.0
*
* @param WP_Site $new_site New site object.
*/
do_action( 'wp_insert_site', $new_site );
return (int) $new_site->id;
}
/**
* Updates a site in the database.
*
* @since 5.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $site_id ID of the site that should be updated.
* @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys.
* @return int|WP_Error The updated site's ID on success, or error object on failure.
*/
function wp_update_site( $site_id, array $data ) {
global $wpdb;
if ( empty( $site_id ) ) {
return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
}
$old_site = get_site( $site_id );
if ( ! $old_site ) {
return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
}
$defaults = $old_site->to_array();
$defaults['network_id'] = (int) $defaults['site_id'];
$defaults['last_updated'] = current_time( 'mysql', true );
unset( $defaults['blog_id'], $defaults['site_id'] );
$data = wp_prepare_site_data( $data, $defaults, $old_site );
if ( is_wp_error( $data ) ) {
return $data;
}
if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
}
clean_blog_cache( $old_site );
$new_site = get_site( $old_site->id );
/**
* Fires once a site has been updated in the database.
*
* @since 5.0.0
*
* @param WP_Site $new_site New site object.
* @param WP_Site $old_site Old site object.
*/
do_action( 'wp_update_site', $new_site, $old_site );
return (int) $new_site->id;
}
/**
* Deletes a site from the database.
*
* @since 5.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $site_id ID of the site that should be deleted.
* @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
*/
function wp_delete_site( $site_id ) {
global $wpdb;
if ( empty( $site_id ) ) {
return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
}
$old_site = get_site( $site_id );
if ( ! $old_site ) {
return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
}
if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
}
clean_blog_cache( $old_site );
/**
* Fires once a site has been deleted from the database.
*
* @since 5.0.0
*
* @param WP_Site $old_site Deleted site object.
*/
do_action( 'wp_delete_site', $old_site );
return $old_site;
}
/**
* Retrieves site data given a site ID or site object.
*
@ -687,6 +731,187 @@ function get_sites( $args = array() ) {
return $query->query( $args );
}
/**
* Prepares site data for insertion or update in the database.
*
* @since 5.0.0
*
* @param array $data Associative array of site data passed to the respective function.
* See {@see wp_insert_site()} for the possibly included data.
* @param array $defaults Site data defaults to parse $data against.
* @param WP_Site|null $old_site Optional. Old site object if an update, or null if an insertion.
* Default null.
* @return array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation
* error occurred.
*/
function wp_prepare_site_data( $data, $defaults, $old_site = null ) {
// Maintain backward-compatibility with `$site_id` as network ID.
if ( isset( $data['site_id'] ) ) {
if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) {
$data['network_id'] = $data['site_id'];
}
unset( $data['site_id'] );
}
/**
* Filters passed site data in order to normalize it.
*
* @since 5.0.0
*
* @param array $data Associative array of site data passed to the respective function.
* See {@see wp_insert_site()} for the possibly included data.
*/
$data = apply_filters( 'wp_normalize_site_data', $data );
$whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
$data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) );
$errors = new WP_Error();
/**
* Fires when data should be validated for a site prior to inserting or updating in the database.
*
* Plugins should amend the `$errors` object via its `WP_Error::add()` method.
*
* @since 5.0.0
*
* @param WP_Error $errors Error object to add validation errors to.
* @param array $data Associative array of complete site data. See {@see wp_insert_site()}
* for the included data.
* @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.
*/
do_action( 'wp_validate_site_data', $errors, $data, $old_site );
if ( ! empty( $errors->errors ) ) {
return $errors;
}
// Prepare for database.
$data['site_id'] = $data['network_id'];
unset( $data['network_id'] );
return $data;
}
/**
* Normalizes data for a site prior to inserting or updating in the database.
*
* @since 5.0.0
*
* @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'] = trim( $data['domain'] );
$data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) );
if ( is_subdomain_install() ) {
$data['domain'] = str_replace( '@', '', $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 ) ) {
continue;
}
if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
unset( $data[ $date_field ] );
}
}
return $data;
}
/**
* Validates data for a site prior to inserting or updating in the database.
*
* @since 5.0.0
*
* @param WP_Error $errors Error object, passed by reference. Will contain validation errors if
* any occurred.
* @param array $data Associative array of complete site data. See {@see wp_insert_site()}
* for the included data.
* @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.' ) );
break;
}
// 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 ] );
if ( ! $valid_date ) {
$errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
break;
}
}
}
if ( ! empty( $errors->errors ) ) {
return;
}
// If a new site, or domain/path/network ID have changed, ensure uniqueness.
if ( ! $old_site
|| $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!' ) );
}
}
}
/**
* Retrieve option value for a given blog id based on name of option.
*
@ -1193,6 +1418,7 @@ function update_archived( $id, $archived ) {
* Update a blog details field.
*
* @since MU (3.0.0)
* @since 5.0.0 Use wp_update_site() internally.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
@ -1213,63 +1439,14 @@ function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
return $value;
}
$result = $wpdb->update(
$wpdb->blogs, array(
$pref => $value,
'last_updated' => current_time( 'mysql', true ),
), array( 'blog_id' => $blog_id )
);
$result = wp_update_site( $blog_id, array(
$pref => $value,
) );
if ( false === $result ) {
if ( is_wp_error( $result ) ) {
return false;
}
clean_blog_cache( $blog_id );
if ( 'spam' == $pref ) {
if ( $value == 1 ) {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'make_spam_blog', $blog_id );
} else {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'make_ham_blog', $blog_id );
}
} elseif ( 'mature' == $pref ) {
if ( $value == 1 ) {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'mature_blog', $blog_id );
} else {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'unmature_blog', $blog_id );
}
} elseif ( 'archived' == $pref ) {
if ( $value == 1 ) {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'archive_blog', $blog_id );
} else {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'unarchive_blog', $blog_id );
}
} elseif ( 'deleted' == $pref ) {
if ( $value == 1 ) {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'make_delete_blog', $blog_id );
} else {
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'make_undelete_blog', $blog_id );
}
} elseif ( 'public' == $pref ) {
/**
* Fires after the current blog's 'public' setting is updated.
*
* @since MU (3.0.0)
*
* @param int $blog_id Blog ID.
* @param string $value The value of blog status.
*/
do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
}
return $value;
}
@ -1537,3 +1714,177 @@ function _update_posts_count_on_transition_post_status( $new_status, $old_status
update_posts_count();
}
/**
* Updates the count of sites for a network based on a changed site.
*
* @since 5.0.0
*
* @param WP_Site $new_site The site object that has been inserted, updated or deleted.
* @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
* state of that site. Default null.
*/
function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
if ( null === $old_site ) {
wp_maybe_update_network_site_counts( $new_site->network_id );
return;
}
if ( $new_site->network_id != $old_site->network_id ) {
wp_maybe_update_network_site_counts( $new_site->network_id );
wp_maybe_update_network_site_counts( $old_site->network_id );
}
}
/**
* Triggers actions on site status updates.
*
* @since 5.0.0
*
* @param WP_Site $new_site The site object after the update.
* @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
* state of that site. Default null.
*/
function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
$site_id = $new_site->id;
// Use the default values for a site if no previous state is given.
if ( ! $old_site ) {
$old_site = new WP_Site( new stdClass() );
}
if ( $new_site->spam != $old_site->spam ) {
if ( $new_site->spam == 1 ) {
/**
* Fires when the 'spam' status is added to a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'make_spam_blog', $site_id );
} else {
/**
* Fires when the 'spam' status is removed from a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'make_ham_blog', $site_id );
}
}
if ( $new_site->mature != $old_site->mature ) {
if ( $new_site->mature == 1 ) {
/**
* Fires when the 'mature' status is added to a site.
*
* @since 3.1.0
*
* @param int $site_id Site ID.
*/
do_action( 'mature_blog', $site_id );
} else {
/**
* Fires when the 'mature' status is removed from a site.
*
* @since 3.1.0
*
* @param int $site_id Site ID.
*/
do_action( 'unmature_blog', $site_id );
}
}
if ( $new_site->archived != $old_site->archived ) {
if ( $new_site->archived == 1 ) {
/**
* Fires when the 'archived' status is added to a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'archive_blog', $site_id );
} else {
/**
* Fires when the 'archived' status is removed from a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'unarchive_blog', $site_id );
}
}
if ( $new_site->deleted != $old_site->deleted ) {
if ( $new_site->deleted == 1 ) {
/**
* Fires when the 'deleted' status is added to a site.
*
* @since 3.5.0
*
* @param int $site_id Site ID.
*/
do_action( 'make_delete_blog', $site_id );
} else {
/**
* Fires when the 'deleted' status is removed from a site.
*
* @since 3.5.0
*
* @param int $site_id Site ID.
*/
do_action( 'make_undelete_blog', $site_id );
}
}
if ( $new_site->public != $old_site->public ) {
/**
* Fires after the current blog's 'public' setting is updated.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
* @param string $value The value of the site status.
*/
do_action( 'update_blog_public', $site_id, $new_site->public );
}
}
/**
* Cleans the necessary caches after specific site data has been updated.
*
* @since 5.0.0
*
* @param WP_Site $new_site The site object after the update.
* @param WP_Site $old_site The site obejct prior to the update.
*/
function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
clean_blog_cache( $new_site );
}
}
/**
* Updates the `blog_public` option for a given site ID.
*
* @since 5.0.0
*
* @param int $site_id Site ID.
* @param string $public The value of the site status.
*/
function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
update_blog_option( $site_id, 'blog_public', $public );
}

View File

@ -41,6 +41,15 @@ add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 );
add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
add_filter( 'wp_normalize_site_data', 'wp_normalize_site_data', 10, 1 );
add_action( 'wp_validate_site_data', 'wp_validate_site_data', 10, 3 );
add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 );
add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 );
add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 );
add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
// Register Nonce
add_action( 'signup_hidden_fields', 'signup_nonce_fields' );

View File

@ -546,3 +546,37 @@ function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
return isset( $current_user->$local_key );
}
/**
* Store basic site info in the blogs table.
*
* This function creates a row in the wp_blogs table and returns
* the new blog's ID. It is the first step in creating a new blog.
*
* @since MU (3.0.0)
* @deprecated 5.0.0 Use `wp_insert_site()`
* @see wp_insert_site()
*
* @param string $domain The domain of the new site.
* @param string $path The path of the new site.
* @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
* @return int|false The ID of the new row
*/
function insert_blog($domain, $path, $site_id) {
_deprecated_function( __FUNCTION__, '5.0.0', 'wp_insert_site()' );
$data = array(
'domain' => $domain,
'path' => $path,
'site_id' => $site_id,
);
$site_id = wp_insert_site( $data );
if ( is_wp_error( $site_id ) ) {
return false;
}
clean_blog_cache( $site_id );
return $site_id;
}

View File

@ -1273,19 +1273,9 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n
);
$meta = wp_parse_args( $meta, $defaults );
$domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
if ( is_subdomain_install() ) {
$domain = str_replace( '@', '', $domain );
}
$title = strip_tags( $title );
$user_id = (int) $user_id;
if ( empty( $path ) ) {
$path = '/';
}
// Check if the domain has been used already. We should return an error message.
if ( domain_exists( $domain, $path, $network_id ) ) {
return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
@ -1295,8 +1285,28 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n
wp_installing( true );
}
if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) {
return new WP_Error( 'insert_blog', __( 'Could not create site.' ) );
$site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
$site_data = array_merge(
array(
'domain' => $domain,
'path' => $path,
'network_id' => $network_id,
),
array_intersect_key(
$meta,
array_flip( $site_data_whitelist )
)
);
$meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );
remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
$blog_id = wp_insert_site( $site_data );
add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
switch_to_blog( $blog_id );
@ -1306,20 +1316,19 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n
add_user_to_blog( $blog_id, $user_id, 'administrator' );
foreach ( $meta as $key => $value ) {
if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
update_blog_status( $blog_id, $key, $value );
} else {
update_option( $key, $value );
}
update_option( $key, $value );
}
update_option( 'blog_public', (int) $meta['public'] );
update_option( 'blog_public', (int) $site_data['public'] );
if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
update_user_meta( $user_id, 'primary_blog', $blog_id );
}
restore_current_blog();
$site = get_site( $blog_id );
/**
* Fires immediately after a new site is created.
*
@ -1332,7 +1341,7 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n
* @param int $network_id Network ID. Only relevant on multi-network installations.
* @param array $meta Meta data. Used to set initial site options.
*/
do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
wp_cache_set( 'last_changed', microtime(), 'sites' );
@ -1485,47 +1494,6 @@ function domain_exists( $domain, $path, $network_id = 1 ) {
return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
}
/**
* Store basic site info in the blogs table.
*
* This function creates a row in the wp_blogs table and returns
* the new blog's ID. It is the first step in creating a new blog.
*
* @since MU (3.0.0)
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $domain The domain of the new site.
* @param string $path The path of the new site.
* @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.
* @return int|false The ID of the new row
*/
function insert_blog( $domain, $path, $network_id ) {
global $wpdb;
$path = trailingslashit( $path );
$network_id = (int) $network_id;
$result = $wpdb->insert(
$wpdb->blogs, array(
'site_id' => $network_id,
'domain' => $domain,
'path' => $path,
'registered' => current_time( 'mysql' ),
)
);
if ( ! $result ) {
return false;
}
$blog_id = $wpdb->insert_id;
clean_blog_cache( $blog_id );
wp_maybe_update_network_site_counts( $network_id );
return $blog_id;
}
/**
* Install an empty blog.
*
@ -1538,7 +1506,7 @@ function insert_blog( $domain, $path, $network_id ) {
* @global wpdb $wpdb
* @global WP_Roles $wp_roles
*
* @param int $blog_id The value returned by insert_blog().
* @param int $blog_id The value returned by wp_insert_site().
* @param string $blog_title The title of the new site.
*/
function install_blog( $blog_id, $blog_title = '' ) {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-43545';
$wp_version = '5.0-alpha-43548';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.