/** * Create / Add users */ function ns_wp_add_user($target_id, $useremail, $username, $userpass = '', $userrole = 'administrator', $logfile = false) { global $ns_cloner; ns_log_write("ENTER ns_wp_add_user - target_id:{$target_id}, useremail:{$useremail}, username:{$username}, userrole:{$userrole}", $logfile); $useremail = stripslashes($useremail); $username = stripslashes($username); $userpass = stripslashes($userpass); $user_by_email = get_user_by('email', $useremail); $user_by_username = get_user_by('username', $username); // check for existing user by email if (!empty($user_by_email)) { $user_id = $user_by_email->ID; ns_log_write("Found user with email '{$useremail}' (id={$user_id})", $logfile); } elseif (!empty($user_by_username)) { $user_id = $user_by_username->ID; ns_log_write("Found user with username '{$username}' (id={$user_id})", $logfile); } else { if (empty($userpass) || $userpass == strtolower('null')) { $userpass = wp_generate_password(); } $user_id = wpmu_create_user($username, $userpass, $useremail); if ($user_id != false) { ns_log_write("Created new user '{$username}' with email '{$useremail}'", $logfile); // send notification to new users if the option is set if (isset($ns_cloner->request['do_user_notify'])) { wpmu_welcome_notification($target_id, $user_id, $userpass, 'New Site with ID: ' . $target_id); ns_log_write("Sent welcome email to new user '{$username}' with email '{$useremail}'", $logfile); } } else { ns_log_write("Failed creating user '{$username}' with email '{$useremail}' - that username or email is probably already taken for a different user.", $logfile); } } // we now have a user id (or should) - give them privileges on this blog if (!empty($target_id) && !empty($user_id) && !empty($userrole)) { $result = add_user_to_blog($target_id, $user_id, $userrole); if ($result === true) { ns_log_write("Successfully added user with id {$user_id} to blog {$target_id}", $logfile); } else { $error_message = $result->get_error_message(); ns_log_write("Failed adding user to blog. WP error: {$error_message}", $logfile); } return $result; } else { $error_message = "Target id, user id, or user role were empty"; ns_log_write("Failed adding user to blog. {$error_message}", $logfile); return new WP_Error(false, $error_message); } }
/** * Activate a signup. * * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events * that should happen only when users or sites are self-created (since * those actions are not called when users and sites are created * by a Super Admin). * * @since MU * @uses wp_generate_password() * @uses wpmu_welcome_user_notification() * @uses add_user_to_blog() * @uses add_new_user_to_blog() * @uses wpmu_create_user() * @uses wpmu_create_blog() * @uses wpmu_welcome_notification() * * @param string $key The activation key provided to the user. * @return array An array containing information about the activated user and/or blog */ function wpmu_activate_signup($key) { global $wpdb, $current_site; $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)); if (empty($signup)) { return new WP_Error('invalid_key', __('Invalid activation key.')); } if ($signup->active) { if (empty($signup->domain)) { return new WP_Error('already_active', __('The user is already active.'), $signup); } else { return new WP_Error('already_active', __('The site is already active.'), $signup); } } $meta = unserialize($signup->meta); $user_login = $wpdb->escape($signup->user_login); $user_email = $wpdb->escape($signup->user_email); $password = wp_generate_password(12, false); $user_id = username_exists($user_login); if (!$user_id) { $user_id = wpmu_create_user($user_login, $password, $user_email); } else { $user_already_exists = true; } if (!$user_id) { return new WP_Error('create_user', __('Could not create user'), $signup); } $now = current_time('mysql', true); if (empty($signup->domain)) { $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); if (isset($user_already_exists)) { return new WP_Error('user_already_exists', __('That username is already activated.'), $signup); } wpmu_welcome_user_notification($user_id, $password, $meta); add_new_user_to_blog($user_id, $user_email, $meta); do_action('wpmu_activate_user', $user_id, $password, $meta); 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); // TODO: What to do if we create a user but cannot create a blog? if (is_wp_error($blog_id)) { // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. if ('blog_taken' == $blog_id->get_error_code()) { $blog_id->add_data($signup); $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); } return $blog_id; } $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta); do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta); return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta); }
wp_die(__('There was an error creating the user.')); } else { wp_new_user_notification($user_id, $password); } } $wpdb->hide_errors(); $id = wpmu_create_blog($newdomain, $path, $title, $user_id, array('public' => 1), $current_site->id); $wpdb->show_errors(); if (!is_wp_error($id)) { $dashboard_blog = get_dashboard_blog(); if (get_user_option('primary_blog', $user_id) == $dashboard_blog->blog_id) { update_user_option($user_id, 'primary_blog', $id, true); } $content_mail = sprintf(__("New site created by %1s\n\nAddress: http://%2s\nName: %3s"), $current_user->user_login, $newdomain . $path, stripslashes($title)); wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New Site Created'), $current_site->site_name), $content_mail, 'From: "Site Admin" <' . get_site_option('admin_email') . '>'); wpmu_welcome_notification($id, $user_id, $password, $title, array('public' => 1)); wp_redirect(add_query_arg(array('updated' => 'true', 'action' => 'add-blog'), wp_get_referer())); exit; } else { wp_die($id->get_error_message()); } break; case 'updateblog': check_admin_referer('editblog'); if (!current_user_can('manage_sites')) { wp_die(__('You do not have permission to access this page.')); } if (empty($_POST)) { wp_die(sprintf(__('You probably need to go back to the <a href="%s">sites page</a>', esc_url(admin_url('ms-sites.php'))))); } switch_to_blog($id);
public static function create_new_multisite($user_id, $config, $lead, $password) { global $current_site; $form = RGFormsModel::get_form_meta($lead['form_id']); $ms_options = rgars($config, 'meta/multisite_options'); $is_update_feed = rgars($config, 'meta/feed_type') == 'update'; $user = new WP_User($user_id); $password_field = rgars($config, 'meta/password'); $set_password = $password_field && rgar($lead, $password_field); $password = $password ? $password : rgar($lead, $password_field); // @review, verify what this is doing and notate here if (!$set_password) { remove_filter('update_welcome_email', 'bp_core_filter_blog_welcome_email'); } // is create site option enabled? if (!rgar($ms_options, 'create_site')) { return false; } $site_data = self::get_site_data($lead, $form, $config, $is_update_feed); if (!$site_data) { return false; } // create the new site! $meta = apply_filters('gform_user_registration_new_site_meta', array('public' => 1), $form, $lead, $config, $user_id, $is_update_feed); $blog_id = wpmu_create_blog($site_data['domain'], $site_data['path'], $site_data['title'], $user_id, $meta, $current_site->id); if (is_wp_error($blog_id)) { return false; } // add entry ID to site meta for new site GFUserData::update_site_meta($blog_id, 'entry_id', $lead['id']); if (!is_super_admin($user_id) && get_user_option('primary_blog', $user_id) == $current_site->blog_id) { update_user_option($user_id, 'primary_blog', $blog_id, true); } if (rgar($ms_options, 'site_role')) { $user = new WP_User($user_id, null, $blog_id); $user->set_role(rgar($ms_options, 'site_role')); } $root_role = rgar($ms_options, 'root_role'); // if no root role, remove user from current site if (!$root_role) { remove_user_from_blog($user_id); } else { if ($root_role == 'gfur_preserve_role') { } else { $user = new WP_User($user_id); $user->set_role($root_role); } } self::log_debug("Calling wpmu_welcome_notification to send multisite welcome - blog_id: {$blog_id} user_id: {$user_id}"); wpmu_welcome_notification($blog_id, $user_id, $password, $site_data['title'], array('public' => 1)); self::log_debug("Done with wpmu_welcome_notification"); do_action('gform_site_created', $blog_id, $user_id, $lead, $config, $password); // return new blog ID return $blog_id; }
<?php /* Notify the site admin that his site is now created and activated with all the features he selected */ wpmu_welcome_notification($new_blog_id, $admin_user_id, $password, $title);
function createNetworkBlog(Am_Record $record, User $user) { $current_site = get_current_site(); $blog = array('domain' => $user->login, 'email' => $user->email, 'title' => sprintf("%s %s's Blog", $user->name_f, $user->name_l)); $domain = strtolower($blog['domain']); // If not a subdomain install, make sure the domain isn't a reserved word if (!is_subdomain_install()) { $subdirectory_reserved_names = array('page', 'comments', 'blog', 'files', 'feed'); if (in_array($domain, $subdirectory_reserved_names)) { throw new Am_Exception_InputError(sprintf(___('The following words are reserved : <code>%s</code>'), implode('</code>, <code>', $subdirectory_reserved_names))); } } if (is_subdomain_install()) { $newdomain = $domain . '.' . preg_replace('|^www\\.|', '', $current_site->domain); $path = $current_site->path; } else { $newdomain = $current_site->domain; $path = $current_site->path . $domain . '/'; } $user_id = $record->pk(); $id = wpmu_create_blog($newdomain, $path, $blog['title'], $user_id, array('public' => 1), $current_site->id); if (!is_wp_error($id)) { if (!is_super_admin($user_id) && !get_user_option('primary_blog', $user_id)) { update_user_option($user_id, 'primary_blog', $id, true); } wpmu_welcome_notification($id, $user_id, $password, $title, array('public' => 1)); } else { throw new Am_Exception_InputError($id->get_error_message()); } }
/** * Activate a signup. * * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events * that should happen only when users or sites are self-created (since * those actions are not called when users and sites are created * by a Super Admin). * * @since MU * * @global wpdb $wpdb * * @param string $key The activation key provided to the user. * @return array|WP_Error An array containing information about the activated user and/or blog */ function wpmu_activate_signup($key) { global $wpdb; $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) ); if ( empty( $signup ) ) return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) ); if ( $signup->active ) { if ( empty( $signup->domain ) ) return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup ); else return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup ); } $meta = maybe_unserialize($signup->meta); $password = wp_generate_password( 12, false ); $user_id = username_exists($signup->user_login); if ( ! $user_id ) $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email); else $user_already_exists = true; if ( ! $user_id ) return new WP_Error('create_user', __('Could not create user'), $signup); $now = current_time('mysql', true); if ( empty($signup->domain) ) { $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); if ( isset( $user_already_exists ) ) return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup); wpmu_welcome_user_notification( $user_id, $password, $meta ); /** * Fires immediately after a new user is activated. * * @since MU * * @param int $user_id User ID. * @param int $password User password. * @param array $meta Signup meta data. */ do_action( 'wpmu_activate_user', $user_id, $password, $meta ); 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 ); // TODO: What to do if we create a user but cannot create a blog? if ( is_wp_error($blog_id) ) { // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. if ( 'blog_taken' == $blog_id->get_error_code() ) { $blog_id->add_data( $signup ); $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) ); } return $blog_id; } $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta); /** * Fires immediately after a site is activated. * * @since MU * * @param int $blog_id Blog ID. * @param int $user_id User ID. * @param int $password User password. * @param string $signup_title Site title. * @param array $meta Signup meta data. */ do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta ); return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta); }
public static function create_new_multisite($user_id, $config, $entry, $password) { global $wpdb, $current_site, $current_user, $base; $multisite_options = $config['meta']['multisite_options']; $user = get_userdata($user_id); $user_set_pass = rgar($config['meta'], 'password') && rgar($entry, rgar($config['meta'], 'password')); $pass = rgar($entry, rgar($config['meta'], 'password')); if (!$user_set_pass) { remove_filter('update_welcome_email', 'bp_core_filter_blog_welcome_email'); } // make sure multisite 'create site' option is set (user registration plugin) if (empty($multisite_options['create_site'])) { return; } // get the domain name $domain = ''; $site_address = $entry[$multisite_options['site_address']]; if (!preg_match('/(--)/', $site_address) && preg_match('|^([a-zA-Z0-9-])+$|', $site_address)) { $domain = strtolower($site_address); } // get the site title and user email $title = $entry[$multisite_options['site_title']]; $email = $user->user_email; // final check to make sure our essentials are good to go if (empty($domain) || empty($email) || !is_email($email)) { return; } if (is_subdomain_install()) { $newdomain = $domain . '.' . preg_replace('|^www\\.|', '', $current_site->domain); $path = $base; } else { $newdomain = $current_site->domain; $path = $base . $domain . '/'; } // create the new site! $id = wpmu_create_blog($newdomain, $path, $title, $user_id, array('public' => 1), $current_site->id); if (is_wp_error($id)) { return; } // add entry ID to site meta for new site GFUserData::update_site_meta($id, 'entry_id', $entry['id']); $dashboard_blog = get_dashboard_blog(); if (!is_super_admin($user_id) && get_user_option('primary_blog', $user_id) == $dashboard_blog->blog_id) { update_user_option($user_id, 'primary_blog', $id, true); } if (rgar($multisite_options, 'site_role')) { $user = new WP_User($user_id, null, $id); $user->set_role(rgar($multisite_options, 'site_role')); } $root_role = rgar($multisite_options, 'root_role'); // if no root role, remove user from current site if (!$root_role) { remove_user_from_blog($user_id); } else { $user = new WP_User($user_id); $user->set_role($root_role); } $content_mail = sprintf(__("New site created by %1s\n\nAddress: http://%2s\nName: %3s", "gravityformsuserregistration"), $current_user->user_login, $newdomain . $path, stripslashes($title)); wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New Site Created', "gravityformsuserregistration"), $current_site->site_name), $content_mail, 'From: "Site Admin" <' . get_site_option('admin_email') . '>'); wpmu_welcome_notification($id, $user_id, $password, $title, array('public' => 1)); do_action('gform_site_created', $id, $user_id, $entry, $config, $password); }
function ra_create_blog($email, $domain = '', $title, $username = '', $password = '******', $copy_id = 0) { global $wpdb, $current_site, $base, $current_user; if (!$email) { return; } $user_id = email_exists(sanitize_email($email)); if (!$user_id) { $password = generate_random_password(); $user_id = wpmu_create_user($username, $password, $email); if (!$user_id) { return __('There was an error creating the user'); } wp_new_user_notification($user_id, $password); } if ($domain && $title) { if (is_subdomain_install()) { $newdomain = $domain . "." . $current_site->domain; $path = $base; } else { $newdomain = $current_site->domain; $path = $base . $domain . '/'; } remove_action('wpmu_new_blog', 'ra_copy_blog', 10); $wpdb->hide_errors(); $new_id = wpmu_create_blog($newdomain, $path, $title, $user_id, array("public" => 1), $current_site->id); $wpdb->show_errors(); if (!is_wp_error($new_id)) { $dashboard_blog = get_dashboard_blog(); if (!is_super_admin() && get_user_option('primary_blog', $user_id) == $dashboard_blog->blog_id) { update_user_option($user_id, 'primary_blog', $new_id, true); } $content_mail = sprintf(__("New site created by %1s\n\nAddress: http://%2s\nName: %3s"), $current_user->user_login, $newdomain . $path, stripslashes($title)); wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New ' . ($is_wp30 ? 'Site' : 'Blog') . ' Created'), $current_site->site_name), $content_mail, 'From: "Site Admin" <' . get_site_option('admin_email') . '>'); wpmu_welcome_notification($new_id, $user_id, $password, $title, array("public" => 1)); // now copy if ($copy_id) { ra_copy_blog($new_id, $copy_id, $user_id); $msg = __('Replicated'); } } else { $msg = $new_id->get_error_message(); } } return $msg; }
function create_microweb() { global $_POST, $current_site, $current_user; if (!is_array($_POST['blog'])) { wp_die(__('Can’t create an empty site.')); } $blog = $_POST['blog']; $email = sanitize_email($blog['email']); $title = $blog['title']; $domain = $blog['domain']; if (empty($domain)) { print_r(__('Missing or invalid site address.')); } if (empty($email)) { print_r(__('Missing email address.')); } if (!is_email($email)) { print_r(__('Invalid email address.')); } if (is_subdomain_install()) { $newdomain = $domain . '.' . preg_replace('|^www\\.|', '', $current_site->domain); $path = $current_site->path; } else { $newdomain = $current_site->domain; $path = $current_site->path . $domain . '/'; } $password = '******'; $user_id = email_exists($email); if ($user_id) { $password = wp_generate_password(12, false); $user_id = wp_update_user(array("ID" => $user_id, "user_pass" => $password)); if (false == $user_id) { print_r(__('There was an error updating the user.')); } else { wp_new_user_notification($user_id, $password); } } $id = wpmu_create_blog($newdomain, $path, $title, $user_id, array('public' => 1), $current_site->id); if (!is_wp_error($id)) { update_user_meta($user_id, "agent-web", $path); if (!is_super_admin($user_id) && !get_user_option('primary_blog', $user_id)) { update_user_option($user_id, 'primary_blog', $id, true); } $content_mail = sprintf(__('New site created by %1$s Address: %2$s Name: %3$s'), $current_user->user_login, get_site_url($id), stripslashes($title)); wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New Site Created'), $current_site->site_name), $content_mail, 'From: "Site Admin" <' . get_site_option('admin_email') . '>'); wpmu_welcome_notification($id, $user_id, $password, $title, array('public' => 1)); // wp_redirect( add_query_arg( array( 'update' => 'added', 'id' => $id ), 'site-new.php' ) ); // exit; } else { wp_die($id->get_error_message()); } }