From 3d19b28eaf68c24a2d391ff7eb3caffc4486d631 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Sun, 14 Jan 2024 10:59:48 +0000 Subject: [PATCH] Administration: Introduce `new_admin_email_subject` filter. This changeset introduces the `new_admin_email_subject` hook which allow developers to filter the subject of the email sent when a change of site admin email address is attempted. Props MadtownLems, johnbillion, alexanderkoledov, shooper, Marc_J, nikmeyer, xlthlx, devmuhib, nuhel, audrasjb. Fixes #59250. git-svn-id: https://develop.svn.wordpress.org/trunk@57283 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/misc.php | 23 ++++++++++++------- tests/phpunit/tests/admin/includesMisc.php | 26 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index 090282062fdfb..12d724f62c152 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -1531,16 +1531,23 @@ function update_option_new_admin_email( $old_value, $value ) { $site_title = parse_url( home_url(), PHP_URL_HOST ); } - wp_mail( - $value, - sprintf( - /* translators: New admin email address notification email subject. %s: Site title. */ - __( '[%s] New Admin Email Address' ), - $site_title - ), - $content + $subject = sprintf( + /* translators: New admin email address notification email subject. %s: Site title. */ + __( '[%s] New Admin Email Address' ), + $site_title ); + /** + * Filters the subject of the email sent when a change of site admin email address is attempted. + * + * @since 6.5.0 + * + * @param string $subject Subject of the email. + */ + $subject = apply_filters( 'new_admin_email_subject', $subject ); + + wp_mail( $value, $subject, $content ); + if ( $switched_locale ) { restore_previous_locale(); } diff --git a/tests/phpunit/tests/admin/includesMisc.php b/tests/phpunit/tests/admin/includesMisc.php index ce0ef7c6c5e76..2df7f661451fb 100644 --- a/tests/phpunit/tests/admin/includesMisc.php +++ b/tests/phpunit/tests/admin/includesMisc.php @@ -27,4 +27,30 @@ public function test_shorten_url() { $this->assertSame( $v, url_shorten( $k ) ); } } + + /** + * @ticket 59520 + */ + public function test_new_admin_email_subject_filter() { + // Default value + $mailer = tests_retrieve_phpmailer_instance(); + update_option_new_admin_email( 'old@example.com', 'new@example.com' ); + $this->assertEquals( '[Test Blog] New Admin Email Address', $mailer->get_sent()->subject ); + + // Filtered value + add_filter( + 'new_admin_email_subject', + function () { + return 'Filtered Admin Email Address'; + }, + 10, + 1 + ); + + $mailer->mock_sent = array(); + + $mailer = tests_retrieve_phpmailer_instance(); + update_option_new_admin_email( 'old@example.com', 'new@example.com' ); + $this->assertEquals( 'Filtered Admin Email Address', $mailer->get_sent()->subject ); + } }