From 2acfe77e40423b99fb5c84004758a34f59970664 Mon Sep 17 00:00:00 2001 From: Alexander Quezada <101373246+irisfield@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:43:11 -0700 Subject: [PATCH] Users: Do not send new user notifications for a user that does not exist. See #66872. --- src/wp-includes/pluggable.php | 5 ++++ tests/phpunit/tests/user.php | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 46995a9e8a5d1..022fd03d7716f 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -2303,6 +2303,7 @@ function wp_password_change_notification( $user ) { * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. + * @since 7.3.0 No notifications are sent if the user does not exist. * * @param int $user_id User ID. * @param mixed $deprecated Not used. @@ -2321,6 +2322,10 @@ function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) $user = get_userdata( $user_id ); + if ( ! $user ) { + return; + } + /* * The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). * We want to reverse this for the plain text arena of emails. diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 7d819969d7d13..2f20315471be2 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -2068,6 +2068,53 @@ static function ( $email ) use ( &$message ) { $this->assertFalse( str_ends_with( $reset_link_line, '>' ), 'The reset password link should not end with an angle bracket.' ); } + /** + * No email is sent, and the admin send filter does not run, for an ID that has no user. + * + * @ticket 66872 + * + * @covers ::wp_new_user_notification + */ + public function test_wp_new_user_notification_does_nothing_for_nonexistent_user_id() { + $admin_filter = new MockAction(); + + add_filter( 'wp_send_new_user_notification_to_admin', array( $admin_filter, 'filter' ) ); + + wp_new_user_notification( PHP_INT_MAX, null, 'both' ); + + $this->assertSame( array(), tests_retrieve_phpmailer_instance()->mock_sent, 'No email should be sent.' ); + $this->assertSame( 0, $admin_filter->get_call_count(), 'The admin send filter should not run.' ); + } + + /** + * No email is sent when edit_user() fails to create a user. + * + * @ticket 66872 + * + * @covers ::edit_user + * @covers ::wp_new_user_notification + */ + public function test_edit_user_sends_no_notification_when_user_creation_fails() { + wp_set_current_user( self::$admin_id ); + + $_POST = array(); + $_GET = array(); + $_REQUEST = array(); + $_POST['user_login'] = '@'; + $_POST['email'] = 'empty-nicename@example.com'; + $_POST['pass1'] = 'password'; + $_POST['pass2'] = 'password'; + $_POST['role'] = 'subscriber'; + + $response = edit_user(); + + $_POST = array(); + + $this->assertWPError( $response ); + $this->assertSame( 'empty_user_nicename', $response->get_error_code() ); + $this->assertSame( array(), tests_retrieve_phpmailer_instance()->mock_sent, 'No email should be sent.' ); + } + /** * Callback that returns 0.0. *