From 85c00bd94360a084ba1d179176aaa6b2376a1869 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 16 Sep 2015 22:19:24 +0000 Subject: [PATCH] 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 --- wp-admin/includes/user.php | 9 ++++++++- wp-admin/network/site-new.php | 14 +++++++++++--- wp-admin/network/site-users.php | 9 ++++++++- wp-admin/network/user-new.php | 9 ++++++++- wp-includes/default-filters.php | 2 ++ wp-includes/ms-default-filters.php | 3 +++ wp-includes/user-functions.php | 22 +++++++++++++++++++++- wp-includes/version.php | 2 +- 8 files changed, 62 insertions(+), 8 deletions(-) diff --git a/wp-admin/includes/user.php b/wp-admin/includes/user.php index 6a5ccdbc53..2d2fc37ea2 100644 --- a/wp-admin/includes/user.php +++ b/wp-admin/includes/user.php @@ -176,7 +176,14 @@ function edit_user( $user_id = 0 ) { $user_id = wp_update_user( $user ); } else { $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; } diff --git a/wp-admin/network/site-new.php b/wp-admin/network/site-new.php index 46be489945..18088b7a3b 100644 --- a/wp-admin/network/site-new.php +++ b/wp-admin/network/site-new.php @@ -91,10 +91,18 @@ if ( wp_validate_action( 'add-site' ) ) { if ( !$user_id ) { // Create a new user with a random password $password = wp_generate_password( 12, false ); $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.' ) ); - 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(); diff --git a/wp-admin/network/site-users.php b/wp-admin/network/site-users.php index f3a75ae3f5..d27c79ef09 100644 --- a/wp-admin/network/site-users.php +++ b/wp-admin/network/site-users.php @@ -77,9 +77,16 @@ if ( $action ) { if ( false === $user_id ) { $update = 'err_new_dup'; } else { - wp_new_user_notification( $user_id, null, 'both' ); add_user_to_blog( $id, $user_id, $_POST['new_role'] ); $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; diff --git a/wp-admin/network/user-new.php b/wp-admin/network/user-new.php index 4b330c0819..6af8744b29 100644 --- a/wp-admin/network/user-new.php +++ b/wp-admin/network/user-new.php @@ -51,7 +51,14 @@ if ( wp_validate_action( 'add-user' ) ) { if ( ! $user_id ) { $add_user_errors = new WP_Error( 'add_user_fail', __( 'Cannot add user.' ) ); } 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' ) ); exit; } diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 396bfbf385..4081be43c1 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -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_postauthor' ); 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 diff --git a/wp-includes/ms-default-filters.php b/wp-includes/ms-default-filters.php index eab4fc40bf..08d752a34d 100644 --- a/wp-includes/ms-default-filters.php +++ b/wp-includes/ms-default-filters.php @@ -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', 'wpmu_welcome_user_notification', 10, 3 ); 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' ); // Blogs diff --git a/wp-includes/user-functions.php b/wp-includes/user-functions.php index 19f564344d..9ee3823341 100644 --- a/wp-includes/user-functions.php +++ b/wp-includes/user-functions.php @@ -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. - 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; } +/** + * 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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index a0a0115da7..4aa448715f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @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.