Move new user notification emails to `add_action()` callbacks.

When a new user is created in various places throughout the interface,
notifications are sent to the site admin and the new user. Previously, these
notifications were fired through direct calls to `wp_new_user_notification()`,
making it difficult to stop or modify the messages.

This changeset introduces a number of new action hooks in place of direct calls
to `wp_new_user_notification()`, and hooks the new wrapper function
`wp_send_new_user_notifications()` to these hooks.

Props dshanske, thomaswm, boonebgorges.
Fixes #33587.
Built from https://develop.svn.wordpress.org/trunk@34251


git-svn-id: http://core.svn.wordpress.org/trunk@34215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-09-16 22:19:24 +00:00
parent f6fb4653eb
commit 85c00bd943
8 changed files with 62 additions and 8 deletions

View File

@ -176,7 +176,14 @@ function edit_user( $user_id = 0 ) {
$user_id = wp_update_user( $user ); $user_id = wp_update_user( $user );
} else { } else {
$user_id = wp_insert_user( $user ); $user_id = wp_insert_user( $user );
wp_new_user_notification( $user_id, null, 'both' ); /**
* Fires after a new user has been created.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
*/
do_action( 'edit_user_created_user', $user_id );
} }
return $user_id; return $user_id;
} }

View File

@ -91,10 +91,18 @@ if ( wp_validate_action( 'add-site' ) ) {
if ( !$user_id ) { // Create a new user with a random password if ( !$user_id ) { // Create a new user with a random password
$password = wp_generate_password( 12, false ); $password = wp_generate_password( 12, false );
$user_id = wpmu_create_user( $domain, $password, $email ); $user_id = wpmu_create_user( $domain, $password, $email );
if ( false === $user_id ) if ( false === $user_id ) {
wp_die( __( 'There was an error creating the user.' ) ); wp_die( __( 'There was an error creating the user.' ) );
else }
wp_new_user_notification( $user_id, null, 'both' );
/**
* Fires after a new user has been created via the network site-new.php page.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
*/
do_action( 'network_site_new_created_user', $user_id );
} }
$wpdb->hide_errors(); $wpdb->hide_errors();

View File

@ -77,9 +77,16 @@ if ( $action ) {
if ( false === $user_id ) { if ( false === $user_id ) {
$update = 'err_new_dup'; $update = 'err_new_dup';
} else { } else {
wp_new_user_notification( $user_id, null, 'both' );
add_user_to_blog( $id, $user_id, $_POST['new_role'] ); add_user_to_blog( $id, $user_id, $_POST['new_role'] );
$update = 'newuser'; $update = 'newuser';
/**
* Fires after a user has been created via the network site-users.php page.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
*/
do_action( 'network_site_users_created_user', $user_id );
} }
} }
break; break;

View File

@ -51,7 +51,14 @@ if ( wp_validate_action( 'add-user' ) ) {
if ( ! $user_id ) { if ( ! $user_id ) {
$add_user_errors = new WP_Error( 'add_user_fail', __( 'Cannot add user.' ) ); $add_user_errors = new WP_Error( 'add_user_fail', __( 'Cannot add user.' ) );
} else { } else {
wp_new_user_notification( $user_id, null, 'both' ); /**
* Fires after a new user has been created via the network user-new.php page.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
*/
do_action( 'network_user_new_created_user', $user_id );
wp_redirect( add_query_arg( array('update' => 'added'), 'user-new.php' ) ); wp_redirect( add_query_arg( array('update' => 'added'), 'user-new.php' ) );
exit; exit;
} }

View File

@ -339,6 +339,8 @@ add_action( 'wp_split_shared_term_batch', '_wp_batch_split_terms' );
add_action( 'comment_post', 'wp_new_comment_notify_moderator', 10, 2 ); add_action( 'comment_post', 'wp_new_comment_notify_moderator', 10, 2 );
add_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); add_action( 'comment_post', 'wp_new_comment_notify_postauthor' );
add_action( 'after_password_reset', 'wp_password_change_notification' ); add_action( 'after_password_reset', 'wp_password_change_notification' );
add_action( 'register_new_user', 'wp_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
/** /**
* Filters formerly mixed into wp-includes * Filters formerly mixed into wp-includes

View File

@ -27,6 +27,9 @@ add_action( 'wpmu_new_user', 'newuser_notify_siteadmin' );
add_action( 'wpmu_activate_user', 'add_new_user_to_blog', 10, 3 ); add_action( 'wpmu_activate_user', 'add_new_user_to_blog', 10, 3 );
add_action( 'wpmu_activate_user', 'wpmu_welcome_user_notification', 10, 3 ); add_action( 'wpmu_activate_user', 'wpmu_welcome_user_notification', 10, 3 );
add_action( 'after_signup_user', 'wpmu_signup_user_notification', 10, 4 ); add_action( 'after_signup_user', 'wpmu_signup_user_notification', 10, 4 );
add_action( 'network_site_new_created_user', 'wp_send_new_user_notifications' );
add_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
add_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
add_filter( 'sanitize_user', 'strtolower' ); add_filter( 'sanitize_user', 'strtolower' );
// Blogs // Blogs

View File

@ -2023,11 +2023,31 @@ function register_new_user( $user_login, $user_email ) {
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag. update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
wp_new_user_notification( $user_id, null, 'both' ); /**
* Fires after a new user registration has been recorded.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly registered user.
*/
do_action( 'register_new_user', $user_id );
return $user_id; return $user_id;
} }
/**
* Initiate email notifications related to the creation of new users.
*
* Notifications are sent both to the site admin and to the newly created user.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
*/
function wp_send_new_user_notifications( $user_id ) {
wp_new_user_notification( $user_id, null, 'both' );
}
/** /**
* Retrieve the current session token from the logged_in cookie. * Retrieve the current session token from the logged_in cookie.
* *

View File

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