From e11accb4113f9ac84a14a5b8d9d2d3353b414e34 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 11 Oct 2020 19:29:08 +0000 Subject: [PATCH] Networks and Sites: Extract into a new function the email that gets sent to the network administrator when a new site is created, and introduce filters to disable and modify its contents. * The `send_new_site_email` filter can be used to disable this email. * The `new_site_email` filter can be used to modify its contents. Props Dhruvin, Dharm1025, dharmin16, jipmoors Fixes #42134 Built from https://develop.svn.wordpress.org/trunk@49127 git-svn-id: http://core.svn.wordpress.org/trunk@48889 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/network/site-new.php | 26 +------- wp-includes/ms-functions.php | 114 ++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 116 insertions(+), 26 deletions(-) diff --git a/wp-admin/network/site-new.php b/wp-admin/network/site-new.php index 7e9716bf37..ffe2f8fddd 100644 --- a/wp-admin/network/site-new.php +++ b/wp-admin/network/site-new.php @@ -145,31 +145,7 @@ if ( isset( $_REQUEST['action'] ) && 'add-site' === $_REQUEST['action'] ) { update_user_option( $user_id, 'primary_blog', $id, true ); } - wp_mail( - get_site_option( 'admin_email' ), - sprintf( - /* translators: New site notification email subject. %s: Network title. */ - __( '[%s] New Site Created' ), - get_network()->site_name - ), - sprintf( - /* translators: New site notification email. 1: User login, 2: Site URL, 3: Site title. */ - __( - 'New site created by %1$s - -Address: %2$s -Name: %3$s' - ), - $current_user->user_login, - get_site_url( $id ), - wp_unslash( $title ) - ), - sprintf( - 'From: "%1$s" <%2$s>', - _x( 'Site Admin', 'email "From" field' ), - get_site_option( 'admin_email' ) - ) - ); + wpmu_new_site_admin_notification( $id, $user_id ); wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) ); wp_redirect( add_query_arg( diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 9740a50483..8419fe08e1 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -1695,6 +1695,120 @@ We hope you enjoy your new site. Thanks! return true; } +/** + * Notifies the Multisite network administrator that a new site was created. + * + * Filter {@see 'send_new_site_email'} to disable or bypass. + * + * Filter {@see 'new_site_email'} to filter the contents. + * + * @since 5.6.0 + * + * @param int $site_id Site ID of the new site. + * @param int $user_id User ID of the administrator of the new site. + * @return bool Whether the email notification was sent. + */ +function wpmu_new_site_admin_notification( $site_id, $user_id ) { + $site = get_site( $site_id ); + $user = get_userdata( $user_id ); + $email = get_site_option( 'admin_email' ); + + if ( ! $site || ! $user || ! $email ) { + return false; + } + + /** + * Filters whether to send an email to the Multisite network administrator when a new site is created. + * + * Return false to disable sending the email. + * + * @since 5.6.0 + * + * @param bool $send Whether to send the email. + * @param WP_Site $site Site object of the new site. + * @param WP_User $user User object of the administrator of the new site. + */ + if ( ! apply_filters( 'send_new_site_email', true, $site, $user ) ) { + return false; + } + + $switched_locale = false; + $network_admin = get_user_by( 'email', $email ); + + if ( $network_admin ) { + // If the network admin email address corresponds to a user, switch to their locale + $switched_locale = switch_to_locale( get_user_locale( $network_admin ) ); + } else { + // Otherwise switch to the locale of the current site + $switched_locale = switch_to_locale( get_locale() ); + } + + $subject = sprintf( + /* translators: New site notification email subject. %s: Network title. */ + __( '[%s] New Site Created' ), + get_network()->site_name + ); + + $message = sprintf( + /* translators: New site notification email. 1: User login, 2: Site URL, 3: Site title. */ + __( + 'New site created by %1$s + +Address: %2$s +Name: %3$s' + ), + $user->user_login, + get_site_url( $site->id ), + get_blog_option( $site->id, 'blogname' ) + ); + + $header = sprintf( + 'From: "%1$s" <%2$s>', + _x( 'Site Admin', 'email "From" field' ), + $email + ); + + $new_site_email = array( + 'to' => $email, + 'subject' => $subject, + 'message' => $message, + 'headers' => $header, + ); + + /** + * Filters the content of the email sent to the Multisite network administrator when a new site is created. + * + * Content should be formatted for transmission via wp_mail(). + * + * @since 5.6.0 + * + * @param array $new_site_email { + * Used to build wp_mail(). + * + * @type string $to The email address of the recipient. + * @type string $subject The subject of the email. + * @type string $message The content of the email. + * @type string $headers Headers. + * } + * @param WP_Site $site Site object of the new site. + * @param WP_User $user User object of the administrator of the new site. + */ + $new_site_email = apply_filters( 'new_site_email', $new_site_email, $site, $user ); + + wp_mail( + $new_site_email['to'], + wp_specialchars_decode( $new_site_email['subject'] ), + $new_site_email['message'], + $new_site_email['headers'] + ); + + if ( $switched_locale ) { + restore_previous_locale(); + } + + return true; +} + /** * Notify a user that their account activation has been successful. * diff --git a/wp-includes/version.php b/wp-includes/version.php index a66d90d3d0..576dfb71eb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49126'; +$wp_version = '5.6-alpha-49127'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.