$user_id = email_exists($email); if (!$user_id) { // Create a new user with a random password $password = wp_generate_password(); $user_id = wpmu_create_user($domain, $password, $email); if (false == $user_id) { 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')) {
/** * @expectedDeprecated get_dashboard_blog */ function test_get_dashboard_blog() { // if there is no dashboard blog set, current blog is used $dashboard_blog = get_dashboard_blog(); $this->assertEquals( 1, $dashboard_blog->blog_id ); $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) ); $this->assertInternalType( 'int', $blog_id ); // set the dashboard blog to another one update_site_option( 'dashboard_blog', $blog_id ); $dashboard_blog = get_dashboard_blog(); $this->assertEquals( $blog_id, $dashboard_blog->blog_id ); }
/** * Copy the blog * * @param string $domain url of the new blog * @param string $title title of the new blog * @param int $from_blog_id ID of the blog being copied from. * @param bool $copy_files true if files should be copied * @return string status message */ private function copy_blog($domain, $title, $from_blog_id = 0, $copy_files = true) { global $wpdb, $current_site, $base; $email = get_blog_option($from_blog_id, 'admin_email'); $user_id = email_exists(sanitize_email($email)); if (!$user_id) { // Use current user instead $user_id = get_current_user_id(); } // The user id of the user that will become the blog admin of the new blog. $user_id = apply_filters('copy_blog_user_id', $user_id, $from_blog_id); if (is_subdomain_install()) { $newdomain = $domain . "." . $current_site->domain; $path = $base; } else { $newdomain = $current_site->domain; $path = trailingslashit($base . $domain); } // The new domain that will be created for the destination blog. $newdomain = apply_filters('copy_blog_domain', $newdomain, $domain); // The new path that will be created for the destination blog. $path = apply_filters('copy_blog_path', $path, $domain); $wpdb->hide_errors(); $to_blog_id = wpmu_create_blog($newdomain, $path, $title, $user_id, array("public" => 1), $current_site->id); $wpdb->show_errors(); if (!is_wp_error($to_blog_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', $to_blog_id, true); } // now copy if ($from_blog_id) { $this->copy_blog_data($from_blog_id, $to_blog_id); if ($copy_files) { $this->copy_blog_files($from_blog_id, $to_blog_id); $this->replace_content_urls($from_blog_id, $to_blog_id); } $msg = sprintf(__('Copied: %s in %s seconds', $this->_domain), '<a href="http://' . $newdomain . '" target="_blank">' . $title . '</a>', number_format_i18n(timer_stop())); do_action('log', __('Copy Complete!', $this->_domain), $this->_domain, $msg); } } else { $msg = $to_blog_id->get_error_message(); } return $msg; }
function redirect_user_to_blog() { global $current_user; $c = 0; if (isset($_GET['c'])) { $c = (int) $_GET['c']; } if ($c >= 5) { wp_die(__("You don’t have permission to view this site. Please contact the system administrator.")); } $c++; $blog = get_active_blog_for_user($current_user->ID); $dashboard_blog = get_dashboard_blog(); if (is_object($blog)) { wp_redirect(get_admin_url($blog->blog_id, '?c=' . $c)); // redirect and count to 5, "just in case" exit; } /* If the user is a member of only 1 blog and the user's primary_blog isn't set to that blog, then update the primary_blog record to match the user's blog */ $blogs = get_blogs_of_user($current_user->ID); if (!empty($blogs)) { foreach ($blogs as $blogid => $blog) { if ($blogid != $dashboard_blog->blog_id && get_user_meta($current_user->ID, 'primary_blog', true) == $dashboard_blog->blog_id) { update_user_meta($current_user->ID, 'primary_blog', $blogid); continue; } } $blog = get_blog_details(get_user_meta($current_user->ID, 'primary_blog', true)); wp_redirect(get_admin_url($blog->blog_id, '?c=' . $c)); exit; } wp_die(__('You do not have sufficient permissions to access this page.')); }
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 (!$ms_options['create_site']) { return; } $site_data = self::get_site_data($lead, $form, $config, $is_update_feed); if (!$site_data) { return; } // create the new site! $blog_id = wpmu_create_blog($site_data['domain'], $site_data['path'], $site_data['title'], $user_id, array('public' => 1), $current_site->id); if (is_wp_error($blog_id)) { return; } // add entry ID to site meta for new site GFUserData::update_site_meta($blog_id, 'entry_id', $lead['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', $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 { $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; }
function redirect_mu_dashboard() { global $current_site, $current_blog; $dashboard_blog = get_dashboard_blog(); if ($current_blog->blog_id == $dashboard_blog->blog_id && $dashboard_blog->blog_id != $current_site->blog_id) { $protocol = is_ssl() ? 'https://' : 'http://'; wp_redirect($protocol . $dashboard_blog->domain . trailingslashit($dashboard_blog->path) . 'wp-admin/'); die; } }
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; }