From 881d79082f11c29d9ea6452a0f3489d868223024 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 10 Apr 2025 02:53:14 +0530 Subject: [PATCH 01/12] Apply the patch --- .../class-wp-rest-users-controller.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 43c29ac88837c..15c569ba72a42 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -656,6 +656,23 @@ public function create_item( $request ) { } } + /** + * Filters the type of notification sent upon new user registration. + * + * This filter allows customization of the notification type when a new user is created via REST API. + * + * @param string $notify Determines who gets notified. Accepts: + * - 'admin' (default) or an empty string: Notify only the site administrator. + * - 'user': Notify only the new user. + * - 'both': Notify both admin and user. + * - 'false': Disable notifications entirely. + * @param int $user_id User ID. + */ + $notify = apply_filters( 'rest_wp_user_created_notification', $notify = 'admin', $user_id ); + if ( $notify && 'false' != $notify ) { + wp_new_user_notification( $user_id, null, $notify ); + } + $user = get_user_by( 'id', $user_id ); /** From 6abb69fed9638bcbbe3957fa6637e52723cc0313 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 10 Apr 2025 02:53:44 +0530 Subject: [PATCH 02/12] Tests: Add REST API user creation email notification tests. --- .../tests/rest-api/rest-users-controller.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index b78e95b95f48d..77ed041a22281 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1376,6 +1376,52 @@ public function test_create_item() { $this->check_add_edit_user_response( $response ); } + /** + * @ticket 40477 + */ + public function test_create_user_sends_admin_notification() { + wp_set_current_user( self::$user ); + reset_phpmailer_instance(); + + $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); + $request->set_param( 'username', 'testuser' ); + $request->set_param( 'email', 'testuser@example.com' ); + $request->set_param( 'password', 'testpassword' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 201, $response->get_status() ); + + $mailer = tests_retrieve_phpmailer_instance(); + $this->assertNotEmpty( $mailer->mock_sent, 'No emails were sent' ); + $this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] ); + } + + /** + * @ticket 40477 + */ + public function test_create_user_notification_respects_filter() { + wp_set_current_user( self::$user ); + add_filter( 'rest_wp_user_created_notification', function() { + return 'both'; + }); + + reset_phpmailer_instance(); + + $user_email = 'testuser2@example.com'; + $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); + $request->set_param( 'username', 'testuser2' ); + $request->set_param( 'email', $user_email ); + $request->set_param( 'password', 'testpassword2' ); + rest_get_server()->dispatch( $request ); + + $mailer = tests_retrieve_phpmailer_instance(); + $this->assertCount( 2, $mailer->mock_sent, 'Expected 2 emails (admin + user)' ); + $this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] ); + $this->assertSame( $user_email, $mailer->mock_sent[1]['to'][0][0] ); + + remove_all_filters( 'rest_wp_user_created_notification' ); + } + public function test_create_item_invalid_username() { $this->allow_user_to_manage_multisite(); From 22af54269b0976070c212a8e0fd8b18509d15eee Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 10 Apr 2025 03:00:29 +0530 Subject: [PATCH 03/12] Use strong comparison to check if notifications are disabled --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 15c569ba72a42..6f75d5698a44e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -669,7 +669,7 @@ public function create_item( $request ) { * @param int $user_id User ID. */ $notify = apply_filters( 'rest_wp_user_created_notification', $notify = 'admin', $user_id ); - if ( $notify && 'false' != $notify ) { + if ( $notify && 'false' !== $notify ) { wp_new_user_notification( $user_id, null, $notify ); } From d7f55ab565093225d934a083d4dea6887586a9e3 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 10 Apr 2025 03:02:58 +0530 Subject: [PATCH 04/12] Fix linting errors and warnings --- .../tests/rest-api/rest-users-controller.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 77ed041a22281..a2dd14b9b456d 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1390,7 +1390,7 @@ public function test_create_user_sends_admin_notification() { $response = rest_get_server()->dispatch( $request ); $this->assertSame( 201, $response->get_status() ); - + $mailer = tests_retrieve_phpmailer_instance(); $this->assertNotEmpty( $mailer->mock_sent, 'No emails were sent' ); $this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] ); @@ -1401,14 +1401,17 @@ public function test_create_user_sends_admin_notification() { */ public function test_create_user_notification_respects_filter() { wp_set_current_user( self::$user ); - add_filter( 'rest_wp_user_created_notification', function() { - return 'both'; - }); + add_filter( + 'rest_wp_user_created_notification', + function () { + return 'both'; + } + ); reset_phpmailer_instance(); $user_email = 'testuser2@example.com'; - $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); $request->set_param( 'username', 'testuser2' ); $request->set_param( 'email', $user_email ); $request->set_param( 'password', 'testpassword2' ); From 765cde674f54150a994ff69fbc22760269913045 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Mon, 14 Apr 2025 13:07:20 +0530 Subject: [PATCH 05/12] Tests: Grant super-admin priviliges for multisites --- tests/phpunit/tests/rest-api/rest-users-controller.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index a2dd14b9b456d..950bb46d0fa86 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1380,6 +1380,10 @@ public function test_create_item() { * @ticket 40477 */ public function test_create_user_sends_admin_notification() { + if ( is_multisite() ) { + grant_super_admin( self::$user ); + } + wp_set_current_user( self::$user ); reset_phpmailer_instance(); @@ -1400,6 +1404,10 @@ public function test_create_user_sends_admin_notification() { * @ticket 40477 */ public function test_create_user_notification_respects_filter() { + if ( is_multisite() ) { + grant_super_admin( self::$user ); + } + wp_set_current_user( self::$user ); add_filter( 'rest_wp_user_created_notification', From db7c4f7a9c10ffe9bd296c45be71affc9d81c3e2 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Wed, 12 Nov 2025 20:39:55 +0530 Subject: [PATCH 06/12] fix(rest-api): Improve user creation notification filter handling --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 6f75d5698a44e..54818a8e614a8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -668,8 +668,8 @@ public function create_item( $request ) { * - 'false': Disable notifications entirely. * @param int $user_id User ID. */ - $notify = apply_filters( 'rest_wp_user_created_notification', $notify = 'admin', $user_id ); - if ( $notify && 'false' !== $notify ) { + $notify = apply_filters( 'rest_wp_user_created_notification', 'admin', $user_id ); + if ( false !== $notify ) { wp_new_user_notification( $user_id, null, $notify ); } From 7ce63e6fc35980283a908416d4cbe7ddbc71cee0 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Wed, 12 Nov 2025 20:42:14 +0530 Subject: [PATCH 07/12] fix(rest-api): Improve docs --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 54818a8e614a8..dd5c0569c579b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -665,7 +665,7 @@ public function create_item( $request ) { * - 'admin' (default) or an empty string: Notify only the site administrator. * - 'user': Notify only the new user. * - 'both': Notify both admin and user. - * - 'false': Disable notifications entirely. + * - false: Disable notifications entirely. * @param int $user_id User ID. */ $notify = apply_filters( 'rest_wp_user_created_notification', 'admin', $user_id ); From bf624442ab9bf30bc0254a1a0437cd33f8dee565 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Wed, 12 Nov 2025 21:17:05 +0530 Subject: [PATCH 08/12] Tests: Improve admin email notification verification in user creation test --- .../tests/rest-api/rest-users-controller.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 950bb46d0fa86..c9251a2a287b7 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1397,7 +1397,20 @@ public function test_create_user_sends_admin_notification() { $mailer = tests_retrieve_phpmailer_instance(); $this->assertNotEmpty( $mailer->mock_sent, 'No emails were sent' ); - $this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] ); + + // Check that at least one email was sent to the admin. + $admin_email = get_option( 'admin_email' ); + $recipients = array_column( $mailer->mock_sent, 'to' ); + $admin_notified = false; + + foreach ( $recipients as $recipient_list ) { + if ( isset( $recipient_list[0][0] ) && $recipient_list[0][0] === $admin_email ) { + $admin_notified = true; + break; + } + } + + $this->assertTrue( $admin_notified, 'Admin email notification was not sent' ); } /** From e538240c32fd5ad845b26acbba3cbb5bc4511402 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Wed, 12 Nov 2025 21:17:43 +0530 Subject: [PATCH 09/12] Tests: Update user creation email notification verification --- .../tests/rest-api/rest-users-controller.php | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index c9251a2a287b7..eed1d182f7004 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1439,9 +1439,27 @@ function () { rest_get_server()->dispatch( $request ); $mailer = tests_retrieve_phpmailer_instance(); - $this->assertCount( 2, $mailer->mock_sent, 'Expected 2 emails (admin + user)' ); - $this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] ); - $this->assertSame( $user_email, $mailer->mock_sent[1]['to'][0][0] ); + $this->assertGreaterThanOrEqual( 2, count( $mailer->mock_sent ), 'Expected at least 2 emails (admin + user)' ); + + // Verify both admin and user received notifications. + $admin_email = get_option( 'admin_email' ); + $recipients = array_column( $mailer->mock_sent, 'to' ); + $admin_notified = false; + $user_notified = false; + + foreach ( $recipients as $recipient_list ) { + if ( isset( $recipient_list[0][0] ) ) { + if ( $recipient_list[0][0] === $admin_email ) { + $admin_notified = true; + } + if ( $recipient_list[0][0] === $user_email ) { + $user_notified = true; + } + } + } + + $this->assertTrue( $admin_notified, 'Admin email notification was not sent' ); + $this->assertTrue( $user_notified, 'User email notification was not sent' ); remove_all_filters( 'rest_wp_user_created_notification' ); } From 808d1f96fbe9a8c868a13da6ec6f3799884be870 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Fri, 28 Nov 2025 19:27:32 +0530 Subject: [PATCH 10/12] REST: Update notification logic to restrict based on specific values --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index dd5c0569c579b..536aa21a0f9ae 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -665,11 +665,11 @@ public function create_item( $request ) { * - 'admin' (default) or an empty string: Notify only the site administrator. * - 'user': Notify only the new user. * - 'both': Notify both admin and user. - * - false: Disable notifications entirely. + * - Any other value (e.g. `false`), no notification is sent. * @param int $user_id User ID. */ $notify = apply_filters( 'rest_wp_user_created_notification', 'admin', $user_id ); - if ( false !== $notify ) { + if ( in_array( $notify, array( 'admin', 'user', 'both' ), true ) ) { wp_new_user_notification( $user_id, null, $notify ); } From e6d4ac527156ddaf8291ce9ba4f5e5a65d7d04e8 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Sat, 6 Dec 2025 20:58:53 +0530 Subject: [PATCH 11/12] Docs: Clarify notification type filter description for user creation --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 536aa21a0f9ae..83cad599e6c60 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -662,7 +662,7 @@ public function create_item( $request ) { * This filter allows customization of the notification type when a new user is created via REST API. * * @param string $notify Determines who gets notified. Accepts: - * - 'admin' (default) or an empty string: Notify only the site administrator. + * - 'admin': Notify only the site administrator. This is the default. * - 'user': Notify only the new user. * - 'both': Notify both admin and user. * - Any other value (e.g. `false`), no notification is sent. From bcd9c7d4a7827960c660e2d2ba2727528626a208 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Sat, 6 Dec 2025 21:00:10 +0530 Subject: [PATCH 12/12] Docs: Add version annotation --- .../rest-api/endpoints/class-wp-rest-users-controller.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 83cad599e6c60..38690532ebe49 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -661,6 +661,8 @@ public function create_item( $request ) { * * This filter allows customization of the notification type when a new user is created via REST API. * + * @since 7.0.0 + * * @param string $notify Determines who gets notified. Accepts: * - 'admin': Notify only the site administrator. This is the default. * - 'user': Notify only the new user.