From b166cfce33489cbd036da6987d335c3dd8df8bfb Mon Sep 17 00:00:00 2001 From: Robin H. Johnson Date: Sun, 3 Oct 2010 19:57:43 -0700 Subject: [PATCH 2/2] Creation of blogs with specific ID. - New argument $preferred_blog_id to wpmu_create_blog. - Document wpmu_create_blog function. --- wp-admin/ms-edit.php | 4 ++-- wp-includes/ms-functions.php | 34 +++++++++++++++++++++++++++++----- wp-signup.php | 3 ++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/wp-admin/ms-edit.php b/wp-admin/ms-edit.php index 5f2ad8d..3d3b036 100644 --- a/wp-admin/ms-edit.php +++ b/wp-admin/ms-edit.php @@ -91,7 +91,7 @@ switch ( $_GET['action'] ) { $path = trailingslashit( $current_site->path . $dashboard_blog ); } $wpdb->hide_errors(); - $dashboard_blog_id = wpmu_create_blog( $domain, $path, __( 'My Dashboard' ), $current_user->id , array( 'public' => 0 ), $current_site->id ); + $dashboard_blog_id = wpmu_create_blog( $domain, $path, __( 'My Dashboard' ), $current_user->id , array( 'public' => 0 ), $current_site->id, -1 ); $wpdb->show_errors(); } else { $dashboard_blog_id = $blog_details->blog_id; @@ -186,7 +186,7 @@ switch ( $_GET['action'] ) { } $wpdb->hide_errors(); - $id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ), $current_site->id ); + $id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ), $current_site->id, -1 ); $wpdb->show_errors(); if ( !is_wp_error( $id ) ) { $dashboard_blog = get_dashboard_blog(); diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 28dddc9..9a3dd0a 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -725,7 +725,7 @@ function wpmu_activate_signup($key) { return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta); } - $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid ); + $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid, -1 ); // TODO: What to do if we create a user but cannot create a blog? if ( is_wp_error($blog_id) ) { @@ -775,7 +775,26 @@ function wpmu_create_user( $user_name, $password, $email, $preferred_user_id = - return $user_id; } -function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) { +/** + * A simple way of creating a new wpmu blog + * + * Creates a new blog, belonging to the specified user. + * + * If you use the 'preferred_blog_id' field, you MUST ensure that your + * auto-increment sequence will not collide with it. + * + * @since 3.0.0 + * + * @param string $domain Domain to host the new blog at. + * @param string $path Full path to the new blog. + * @param string $title Title for new blog + * @param int $user_id Owner's user id + * @param varies $meta String or Array of meta attributes for the new blog. + * @param int $site_id Site to assign new blog under. + * @param int $preferred_blog_id ID of blog to attempt to create + * @return varies The new blog ID or a WP_Error object + */ +function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1, $preferred_blog_id = -1) { $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) ); if ( is_subdomain_install() ) @@ -794,7 +813,7 @@ function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id if ( !defined('WP_INSTALLING') ) define( 'WP_INSTALLING', true ); - if ( ! $blog_id = insert_blog($domain, $path, $site_id) ) + if ( ! $blog_id = insert_blog($domain, $path, $site_id, $preferred_blog_id ) ) return new WP_Error('insert_blog', __('Could not create site.')); switch_to_blog($blog_id); @@ -875,13 +894,18 @@ function domain_exists($domain, $path, $site_id = 1) { return $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) ); } -function insert_blog($domain, $path, $site_id) { +function insert_blog($domain, $path, $site_id, $blog_id) { global $wpdb; $path = trailingslashit($path); $site_id = (int) $site_id; - $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) ); + $data = compact('site_id', 'domain', 'path'); + if(!empty($blog_id) && is_numeric($blog_id) && $blog_id > 0) { + $data += compact('blog_id'); + } + + $result = $wpdb->insert( $wpdb->blogs, $data + array('registered' => current_time('mysql')) ); if ( ! $result ) return false; diff --git a/wp-signup.php b/wp-signup.php index 28b561a..19b659d 100644 --- a/wp-signup.php +++ b/wp-signup.php @@ -215,7 +215,8 @@ function validate_another_blog_signup() { $meta = apply_filters( 'signup_create_blog_meta', array( 'lang_id' => 1, 'public' => $public ) ); // deprecated $meta = apply_filters( 'add_signup_meta', $meta ); - wpmu_create_blog( $domain, $path, $blog_title, $current_user->id, $meta, $wpdb->siteid ); + wpmu_create_blog( $domain, $path, $blog_title, $current_user->id, $meta, $wpdb->siteid, -1 ); + // TODO: check that the blog was created confirm_another_blog_signup($domain, $path, $blog_title, $current_user->user_login, $current_user->user_email, $meta); return true; } -- 1.7.3