Make WordPress Core

Changeset 49127


Ignore:
Timestamp:
10/11/2020 07:27:21 PM (4 years ago)
Author:
johnbillion
Message:

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

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/network/site-new.php

    r47853 r49127  
    146146        }
    147147
    148         wp_mail(
    149             get_site_option( 'admin_email' ),
    150             sprintf(
    151                 /* translators: New site notification email subject. %s: Network title. */
    152                 __( '[%s] New Site Created' ),
    153                 get_network()->site_name
    154             ),
    155             sprintf(
    156                 /* translators: New site notification email. 1: User login, 2: Site URL, 3: Site title. */
    157                 __(
    158                     'New site created by %1$s
    159 
    160 Address: %2$s
    161 Name: %3$s'
    162                 ),
    163                 $current_user->user_login,
    164                 get_site_url( $id ),
    165                 wp_unslash( $title )
    166             ),
    167             sprintf(
    168                 'From: "%1$s" <%2$s>',
    169                 _x( 'Site Admin', 'email "From" field' ),
    170                 get_site_option( 'admin_email' )
    171             )
    172         );
     148        wpmu_new_site_admin_notification( $id, $user_id );
    173149        wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
    174150        wp_redirect(
  • trunk/src/wp-includes/ms-functions.php

    r49108 r49127  
    16881688
    16891689    wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     1690
     1691    if ( $switched_locale ) {
     1692        restore_previous_locale();
     1693    }
     1694
     1695    return true;
     1696}
     1697
     1698/**
     1699 * Notifies the Multisite network administrator that a new site was created.
     1700 *
     1701 * Filter {@see 'send_new_site_email'} to disable or bypass.
     1702 *
     1703 * Filter {@see 'new_site_email'} to filter the contents.
     1704 *
     1705 * @since 5.6.0
     1706 *
     1707 * @param int $site_id Site ID of the new site.
     1708 * @param int $user_id User ID of the administrator of the new site.
     1709 * @return bool Whether the email notification was sent.
     1710 */
     1711function wpmu_new_site_admin_notification( $site_id, $user_id ) {
     1712    $site  = get_site( $site_id );
     1713    $user  = get_userdata( $user_id );
     1714    $email = get_site_option( 'admin_email' );
     1715
     1716    if ( ! $site || ! $user || ! $email ) {
     1717        return false;
     1718    }
     1719
     1720    /**
     1721     * Filters whether to send an email to the Multisite network administrator when a new site is created.
     1722     *
     1723     * Return false to disable sending the email.
     1724     *
     1725     * @since 5.6.0
     1726     *
     1727     * @param bool    $send Whether to send the email.
     1728     * @param WP_Site $site Site object of the new site.
     1729     * @param WP_User $user User object of the administrator of the new site.
     1730     */
     1731    if ( ! apply_filters( 'send_new_site_email', true, $site, $user ) ) {
     1732        return false;
     1733    }
     1734
     1735    $switched_locale = false;
     1736    $network_admin   = get_user_by( 'email', $email );
     1737
     1738    if ( $network_admin ) {
     1739        // If the network admin email address corresponds to a user, switch to their locale
     1740        $switched_locale = switch_to_locale( get_user_locale( $network_admin ) );
     1741    } else {
     1742        // Otherwise switch to the locale of the current site
     1743        $switched_locale = switch_to_locale( get_locale() );
     1744    }
     1745
     1746    $subject = sprintf(
     1747        /* translators: New site notification email subject. %s: Network title. */
     1748        __( '[%s] New Site Created' ),
     1749        get_network()->site_name
     1750    );
     1751
     1752    $message = sprintf(
     1753        /* translators: New site notification email. 1: User login, 2: Site URL, 3: Site title. */
     1754        __(
     1755            'New site created by %1$s
     1756
     1757Address: %2$s
     1758Name: %3$s'
     1759        ),
     1760        $user->user_login,
     1761        get_site_url( $site->id ),
     1762        get_blog_option( $site->id, 'blogname' )
     1763    );
     1764
     1765    $header = sprintf(
     1766        'From: "%1$s" <%2$s>',
     1767        _x( 'Site Admin', 'email "From" field' ),
     1768        $email
     1769    );
     1770
     1771    $new_site_email = array(
     1772        'to'      => $email,
     1773        'subject' => $subject,
     1774        'message' => $message,
     1775        'headers' => $header,
     1776    );
     1777
     1778    /**
     1779     * Filters the content of the email sent to the Multisite network administrator when a new site is created.
     1780     *
     1781     * Content should be formatted for transmission via wp_mail().
     1782     *
     1783     * @since 5.6.0
     1784     *
     1785     * @param array $new_site_email {
     1786     *     Used to build wp_mail().
     1787     *
     1788     *     @type string $to      The email address of the recipient.
     1789     *     @type string $subject The subject of the email.
     1790     *     @type string $message The content of the email.
     1791     *     @type string $headers Headers.
     1792     * }
     1793     * @param WP_Site $site         Site object of the new site.
     1794     * @param WP_User $user         User object of the administrator of the new site.
     1795     */
     1796    $new_site_email = apply_filters( 'new_site_email', $new_site_email, $site, $user );
     1797
     1798    wp_mail(
     1799        $new_site_email['to'],
     1800        wp_specialchars_decode( $new_site_email['subject'] ),
     1801        $new_site_email['message'],
     1802        $new_site_email['headers']
     1803    );
    16901804
    16911805    if ( $switched_locale ) {
Note: See TracChangeset for help on using the changeset viewer.