/**
* Change redirect upon login to user's My Catalog page
*
* @param string $redirect_to
* @param string $request_redirect_to
* @param \WP_User $user
*
* @return string
*/
function login($redirect_to, $request_redirect_to, $user)
{
if (false === is_a($user, 'WP_User')) {
// Unknown user, bail with default
return $redirect_to;
}
if (is_super_admin($user->ID)) {
// This is an admin, don't mess
return $redirect_to;
}
$blogs = get_blogs_of_user($user->ID);
if (array_key_exists(get_current_blog_id(), $blogs)) {
// Yes, user has access to this blog
return $redirect_to;
}
if ($user->primary_blog) {
// Force redirect the user to their blog or, if they have more than one, to their catalog, bypass wp_safe_redirect()
if (count($blogs) > 1) {
$redirect = get_blogaddress_by_id($user->primary_blog) . 'wp-admin/index.php?page=pb_catalog';
} else {
$redirect = get_blogaddress_by_id($user->primary_blog) . 'wp-admin/';
}
location($redirect);
}
// User has no primary_blog? Make them sign-up for one
return network_site_url('/wp-signup.php');
}
/**
* Gets the URL for the blog with the given ID.
*
* This gets the URL for the requested blog when in multisite mode, or
* for the root blog when running a standard installation.
*
* @param int $blog_id the ID of a blog
* @return string the URL for the blog
*
* @since 0.4
*/
public static function get_blogaddress_by_id($blog_id)
{
if (function_exists('get_blogaddress_by_id')) {
return get_blogaddress_by_id($blog_id);
} else {
return home_url();
}
}
} else {
printf(__('Your site at <a href="%1$s">%2$s</a> is active. You may now log in to your site using your chosen username of “%3$s”. Please check your email inbox at %4$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%5$s">reset your password</a>.'), 'http://' . $signup->domain, $signup->domain, $signup->user_login, $signup->user_email, wp_lostpassword_url());
}
echo '</p>';
} else {
?>
<h2><?php
_e('An error occurred during the activation');
?>
</h2>
<?php
echo '<p>' . $result->get_error_message() . '</p>';
}
} else {
extract($result);
$url = get_blogaddress_by_id((int) $blog_id);
$user = get_userdata((int) $user_id);
?>
<h2><?php
_e('Your account is now active!');
?>
</h2>
<div id="signup-welcome">
<p><span class="h3"><?php
_e('Username:');
?>
</span> <?php
echo $user->user_login;
?>
</p>
/**
* Notify a user that her blog activation has been successful.
*
* Filter 'wpmu_welcome_notification' to disable or bypass.
*
* Filter 'update_welcome_email' and 'update_welcome_subject' to
* modify the content and subject line of the notification email.
*
* @since MU
*
* @param int $blog_id
* @param int $user_id
* @param string $password
* @param string $title The new blog's title
* @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
* @return bool
*/
function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '')
{
global $current_site;
if (!apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta)) {
return false;
}
$welcome_email = stripslashes(get_site_option('welcome_email'));
if ($welcome_email == false) {
$welcome_email = stripslashes(__('Dear User,
Your new SITE_NAME site has been successfully set up at:
BLOG_URL
You can log in to the administrator account with the following information:
Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLwp-login.php
We hope you enjoy your new site. Thanks!
--The Team @ SITE_NAME'));
}
$url = get_blogaddress_by_id($blog_id);
$user = new WP_User($user_id);
$welcome_email = str_replace('SITE_NAME', $current_site->site_name, $welcome_email);
$welcome_email = str_replace('BLOG_TITLE', $title, $welcome_email);
$welcome_email = str_replace('BLOG_URL', $url, $welcome_email);
$welcome_email = str_replace('USERNAME', $user->user_login, $welcome_email);
$welcome_email = str_replace('PASSWORD', $password, $welcome_email);
$welcome_email = apply_filters('update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
$admin_email = get_site_option('admin_email');
if ($admin_email == '') {
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
}
$from_name = get_site_option('site_name') == '' ? 'WordPress' : esc_html(get_site_option('site_name'));
$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
$message = $welcome_email;
if (empty($current_site->site_name)) {
$current_site->site_name = 'WordPress';
}
$subject = apply_filters('update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes($title)));
wp_mail($user->user_email, $subject, $message, $message_headers);
return true;
}
/**
* Notify a user that their blog activation has been successful.
*
* Filter 'wpmu_welcome_notification' to disable or bypass.
*
* Filter 'update_welcome_email' and 'update_welcome_subject' to
* modify the content and subject line of the notification email.
*
* @since MU
*
* @param int $blog_id
* @param int $user_id
* @param string $password
* @param string $title The new blog's title
* @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
* @return bool
*/
function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = array())
{
$current_site = get_current_site();
/**
* Filter whether to bypass the welcome email after site activation.
*
* Returning false disables the welcome email.
*
* @since MU
*
* @param int|bool $blog_id Blog ID.
* @param int $user_id User ID.
* @param string $password User password.
* @param string $title Site title.
* @param array $meta Signup meta data.
*/
if (!apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta)) {
return false;
}
$welcome_email = get_site_option('welcome_email');
if ($welcome_email == false) {
/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
$welcome_email = __('Howdy USERNAME,
Your new SITE_NAME site has been successfully set up at:
BLOG_URL
You can log in to the administrator account with the following information:
Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLwp-login.php
We hope you enjoy your new site. Thanks!
--The Team @ SITE_NAME');
}
$url = get_blogaddress_by_id($blog_id);
$user = get_userdata($user_id);
$welcome_email = str_replace('SITE_NAME', $current_site->site_name, $welcome_email);
$welcome_email = str_replace('BLOG_TITLE', $title, $welcome_email);
$welcome_email = str_replace('BLOG_URL', $url, $welcome_email);
$welcome_email = str_replace('USERNAME', $user->user_login, $welcome_email);
$welcome_email = str_replace('PASSWORD', $password, $welcome_email);
/**
* Filter the content of the welcome email after site activation.
*
* Content should be formatted for transmission via wp_mail().
*
* @since MU
*
* @param string $welcome_email Message body of the email.
* @param int $blog_id Blog ID.
* @param int $user_id User ID.
* @param string $password User password.
* @param string $title Site title.
* @param array $meta Signup meta data.
*/
$welcome_email = apply_filters('update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
$admin_email = get_site_option('admin_email');
if ($admin_email == '') {
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
}
$from_name = get_site_option('site_name') == '' ? 'WordPress' : esc_html(get_site_option('site_name'));
$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
$message = $welcome_email;
if (empty($current_site->site_name)) {
$current_site->site_name = 'WordPress';
}
/**
* Filter the subject of the welcome email after site activation.
*
* @since MU
*
* @param string $subject Subject of the email.
*/
$subject = apply_filters('update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, wp_unslash($title)));
wp_mail($user->user_email, wp_specialchars_decode($subject), $message, $message_headers);
return true;
}
printf(__('Your account has been activated. You may now <a href="%1$s">log in</a> to the site using your chosen username of “%2$s”. Please check your email inbox at %3$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%4$s">reset your password</a>.'), network_site_url('wp-login.php', 'login'), $signup->user_login, $signup->user_email, wp_lostpassword_url());
} else {
printf(__('Your site at <a href="%1$s">%2$s</a> is active. You may now log in to your site using your chosen username of “%3$s”. Please check your email inbox at %4$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%5$s">reset your password</a>.'), 'http://' . $signup->domain, $signup->domain, $signup->user_login, $signup->user_email, wp_lostpassword_url());
}
echo '</p>';
} else {
?>
<h2><?php
_e('An error occurred during the activation');
?>
</h2>
<?php
echo '<p>' . $result->get_error_message() . '</p>';
}
} else {
$url = isset($result['blog_id']) ? get_blogaddress_by_id((int) $result['blog_id']) : '';
$user = get_userdata((int) $result['user_id']);
?>
<h2><?php
_e('Your account is now active!');
?>
</h2>
<div id="signup-welcome">
<p><span class="h3"><?php
_e('Username:');
?>
</span> <?php
echo $user->user_login;
?>
</p>
//.........这里部分代码省略.........
} else {
_e('Site registration has been disabled.');
?>
</div>
</div>
<?php
do_action('after_signup_form');
get_footer();
exit;
}
break;
default:
return;
}
/* begin wp-activate page */
$key = (string) $_REQUEST['key'];
// wpmu_create_user, wpmu_welcome_user_notification, add_new_user_to_blog, do wpmu_activate_user action
$result = wpmu_activate_signup($key);
if (is_wp_error($result)) {
if ('already_active' == $result->get_error_code() || 'blog_taken' == $result->get_error_code()) {
$signup = $result->get_error_data();
?>
<h2><?php
_e('Your account is now active!');
?>
</h2>
<?php
echo '<p class="lead-in">';
if ($signup->domain . $signup->path == '') {
printf(__('Your account has been activated. You may now <a href="%1$s">log in</a> to the site using your chosen username of “%2$s”. Please check your email inbox at %3$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%4$s">reset your password</a>.'), network_site_url('wp-login.php', 'login'), $signup->user_login, $signup->user_email, wp_lostpassword_url());
} else {
printf(__('Your site at <a href="%1$s">%2$s</a> is active. You may now log in to your site using your chosen username of “%3$s”. Please check your email inbox at %4$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%5$s">reset your password</a>.'), 'http://' . $signup->domain, $signup->domain, $signup->user_login, $signup->user_email, wp_lostpassword_url());
}
echo '</p>';
} else {
?>
<h2><?php
_e('An error occurred during the activation');
?>
</h2>
<?php
echo '<p>' . $result->get_error_message() . '</p>';
}
} else {
//TODO: Why not reference $result->blog_id?
extract($result);
if (isset($blog_id)) {
$url = get_blogaddress_by_id((int) $blog_id);
}
$user = get_userdata((int) $user_id);
?>
<h2><?php
_e('Your account is now active!');
?>
</h2>
<div id="signup-welcome">
<p><span class="h3"><?php
_e('Username:');
?>
</span> <?php
echo $user->user_login;
?>
</p>
<p><span class="h3"><?php
_e('Password:');
?>
</span> <?php
echo $password;
?>
</p>
</div>
<?php
if (isset($blog_id) && $url != network_home_url('', 'http')) {
?>
<p class="view"><?php
printf(__('Your account is now activated. <a href="%1$s">View your site</a> or <a href="%2$s">Log in</a>'), $url, $url . 'wp-login.php');
?>
</p>
<?php
} else {
?>
<p class="view"><?php
printf(__('Your account is now activated. <a href="%1$s">Log in</a> or go back to the <a href="%2$s">homepage</a>.'), network_site_url('wp-login.php', 'login'), network_home_url());
?>
</p>
<?php
}
}
?>
</div>
<script type="text/javascript">
var key_input = document.getElementById('key');
key_input && key_input.focus();
</script>
<?php
get_footer();
?>
<?php
exit;
}
/**
* Outputs the activation page
*
* @since 6.1
* @access public
*
* @param object $template Theme_My_Login_Template object
*/
public function tml_display_activate(&$template)
{
global $blog_id;
echo '<div class="login" id="theme-my-login' . esc_attr($template->get_option('instance')) . '">';
if (empty($_GET['key']) && empty($_POST['key'])) {
?>
<h2><?php
_e('Activation Key Required', 'theme-my-login');
?>
</h2>
<form name="activateform" id="activateform" method="post" action="<?php
$template->the_action_url('activate');
?>
">
<p>
<label for="key<?php
$template->the_instance();
?>
"><?php
_e('Activation Key:', 'theme-my-login');
?>
</label>
<br /><input type="text" name="key<?php
$template->the_instance();
?>
" id="key" value="" size="50" />
</p>
<p class="submit">
<input id="submit<?php
$template->the_instance();
?>
" type="submit" name="Submit" class="submit" value="<?php
esc_attr_e('Activate', 'theme-my-login');
?>
" />
</p>
</form>
<?php
} else {
$key = !empty($_GET['key']) ? $_GET['key'] : $_POST['key'];
$result = wpmu_activate_signup($key);
if (is_wp_error($result)) {
if ('already_active' == $result->get_error_code() || 'blog_taken' == $result->get_error_code()) {
$signup = $result->get_error_data();
?>
<h2><?php
_e('Your account is now active!', 'theme-my-login');
?>
</h2>
<?php
echo '<p class="lead-in">';
if ($signup->domain . $signup->path == '') {
printf(__('Your account has been activated. You may now <a href="%1$s">login</a> to the site using your chosen username of “%2$s”. Please check your email inbox at %3$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%4$s">reset your password</a>.', 'theme-my-login'), network_site_url('wp-login.php', 'login'), $signup->user_login, $signup->user_email, network_site_url('wp-login.php?action=lostpassword', 'login'));
} else {
printf(__('Your site at <a href="%1$s">%2$s</a> is active. You may now log in to your site using your chosen username of “%3$s”. Please check your email inbox at %4$s for your password and login instructions. If you do not receive an email, please check your junk or spam folder. If you still do not receive an email within an hour, you can <a href="%5$s">reset your password</a>.', 'theme-my-login'), 'http://' . $signup->domain, $signup->domain, $signup->user_login, $signup->user_email, network_site_url('wp-login.php?action=lostpassword'));
}
echo '</p>';
} else {
?>
<h2><?php
_e('An error occurred during the activation', 'theme-my-login');
?>
</h2>
<?php
echo '<p>' . $result->get_error_message() . '</p>';
}
} else {
extract($result);
$url = get_blogaddress_by_id((int) $blog_id);
$user = new WP_User((int) $user_id);
?>
<h2><?php
_e('Your account is now active!', 'theme-my-login');
?>
</h2>
<div id="signup-welcome">
<p><span class="h3"><?php
_e('Username:', 'theme-my-login');
?>
</span> <?php
echo $user->user_login;
?>
</p>
<p><span class="h3"><?php
_e('Password:', 'theme-my-login');
?>
</span> <?php
echo $password;
?>
//.........这里部分代码省略.........
/**
* Tests returning the appropriate response for a invalid id given.
*/
function test_get_blogaddress_by_id_with_invalid_id()
{
$blogaddress = get_blogaddress_by_id(42);
$this->assertEquals('', $blogaddress);
}
/**
* Add a new network
*
* @since 1.3
*
* @param string $domain Domain name for new network - for VHOST=no, this
* should be FQDN, otherwise domain only
* @param string $path Path to root of network hierarchy - should be '/' unless
* WP is cohabiting with another product on a domain
* @param string $site_name Name of the root blog to be created on the new
* network
* @param integer $clone_network ID of network whose networkmeta values are
* to be copied - default NULL
* @param array $options_to_clone Override default network meta options to copy
* when cloning - default NULL
* @return integer ID of newly created network
*/
function add_network($domain, $path, $site_name = false, $clone_network = false, $options_to_clone = false)
{
global $wpdb, $sites;
// Set a default site name if one isn't set
if (false == $site_name) {
$site_name = __('New Network Root', 'wp-multi-network');
}
// If no options, fallback on defaults
if (empty($options_to_clone)) {
$options_to_clone = array_keys(network_options_to_copy());
}
// Check for existing network
$sql = "SELECT * FROM {$wpdb->site} WHERE domain = %s AND path = %s LIMIT 1";
$query = $wpdb->prepare($sql, $domain, $path);
$network = $wpdb->get_row($query);
if (!empty($network)) {
return new WP_Error('network_exists', __('Network already exists.', 'wp-multi-network'));
}
// Insert new network
$wpdb->insert($wpdb->site, array('domain' => $domain, 'path' => $path));
$new_network_id = $wpdb->insert_id;
// Update global network list
$sites = $wpdb->get_results("SELECT * FROM {$wpdb->site}");
// If network was created, create a blog for it too
if (!empty($new_network_id)) {
if (!defined('WP_INSTALLING')) {
define('WP_INSTALLING', true);
}
// there's an ongoing error with wpmu_create_blog that throws a warning if meta is not defined:
// http://core.trac.wordpress.org/ticket/20793
// temporary fix -- set from current blog's value
// Looks like a fix is in for 3.7
$new_blog_id = wpmu_create_blog($domain, $path, $site_name, get_current_user_id(), array('public' => get_option('blog_public', false)), (int) $new_network_id);
// Bail if blog could not be created
if (is_a($new_blog_id, 'WP_Error')) {
return $new_blog_id;
}
/**
* Fix upload_path for main sites on secondary networks
* This applies only to new installs (WP 3.5+)
*/
// Switch to main network (if it exists)
if (defined('SITE_ID_CURRENT_SITE') && network_exists(SITE_ID_CURRENT_SITE)) {
switch_to_network(SITE_ID_CURRENT_SITE);
$use_files_rewriting = get_site_option('ms_files_rewriting');
restore_current_network();
} else {
$use_files_rewriting = get_site_option('ms_files_rewriting');
}
global $wp_version;
// Create the upload_path and upload_url_path values
if (!$use_files_rewriting && version_compare($wp_version, '3.7', '<')) {
// WP_CONTENT_URL is locked to the current site and can't be overridden,
// so we have to replace the hostname the hard way
$current_siteurl = get_option('siteurl');
$new_siteurl = untrailingslashit(get_blogaddress_by_id($new_blog_id));
$upload_url = str_replace($current_siteurl, $new_siteurl, WP_CONTENT_URL);
$upload_url = $upload_url . '/uploads';
$upload_dir = WP_CONTENT_DIR;
if (0 === strpos($upload_dir, ABSPATH)) {
$upload_dir = substr($upload_dir, strlen(ABSPATH));
}
$upload_dir .= '/uploads';
if (defined('MULTISITE')) {
$ms_dir = '/sites/' . $new_blog_id;
} else {
$ms_dir = '/' . $new_blog_id;
}
$upload_dir .= $ms_dir;
$upload_url .= $ms_dir;
update_blog_option($new_blog_id, 'upload_path', $upload_dir);
update_blog_option($new_blog_id, 'upload_url_path', $upload_url);
}
}
// Clone the network meta from an existing network
if (!empty($clone_network) && network_exists($clone_network)) {
$options_cache = array();
$clone_network = (int) $clone_network;
switch_to_network($clone_network);
foreach ($options_to_clone as $option) {
$options_cache[$option] = get_site_option($option);
}
restore_current_network();
//.........这里部分代码省略.........
请发表评论