Make WordPress Core

Changeset 46265


Ignore:
Timestamp:
09/23/2019 07:24:20 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Privacy: Introduce wp_privacy_personal_data_email_to and wp_privacy_personal_data_email_subject filters.

Pass email data to the wp_privacy_personal_data_email_content filter.

Props garrett-eclipse, thakkarhardik, birgire.
Fixes #46303.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/privacy-tools.php

    r46209 r46265  
    494494    $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
    495495
     496    $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
     497    $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     498    $site_url        = home_url();
     499
     500    /**
     501     * Filters the recipient of the personal data export email notification.
     502     * Should be used with great caution to avoid sending the data export link to wrong emails.
     503     *
     504     * @since 5.3.0
     505     *
     506     * @param string          $request_email The email address of the notification recipient.
     507     * @param WP_User_Request $request       The request that is initiating the notification.
     508     */
     509    $request_email = apply_filters( 'wp_privacy_personal_data_email_to', $request->email, $request );
     510
     511    $email_data = array(
     512        'request'           => $request,
     513        'expiration'        => $expiration,
     514        'expiration_date'   => $expiration_date,
     515        'message_recipient' => $request_email,
     516        'export_file_url'   => $export_file_url,
     517        'sitename'          => $site_name,
     518        'siteurl'           => $site_url,
     519    );
     520
     521    /* translators: Personal data export notification email subject. %s: Site title. */
     522    $subject = sprintf( __( '[%s] Personal Data Export' ), $site_name );
     523
     524    /**
     525     * Filters the subject of the email sent when an export request is completed.
     526     *
     527     * @since 5.3.0
     528     *
     529     * @param string $subject    The email subject.
     530     * @param string $sitename   The name of the site.
     531     * @param array  $email_data {
     532     *     Data relating to the account action email.
     533     *
     534     *     @type WP_User_Request $request           User request object.
     535     *     @type int             $expiration        The time in seconds until the export file expires.
     536     *     @type string          $expiration_date   The localized date and time when the export file expires.
     537     *     @type string          $message_recipient The address that the email will be sent to. Defaults
     538     *                                              to the value of `$request->email`, but can be changed
     539     *                                              by the `wp_privacy_personal_data_email_to` filter.
     540     *     @type string          $export_file_url   The export file URL.
     541     *     @type string          $sitename          The site name sending the mail.
     542     *     @type string          $siteurl           The site URL sending the mail.
     543     * }
     544     */
     545    $subject = apply_filters( 'wp_privacy_personal_data_email_subject', $subject, $site_name, $email_data );
     546
    496547    /* translators: Do not translate EXPIRATION, LINK, SITENAME, SITEURL: those are placeholders. */
    497548    $email_text = __(
     
    520571     *
    521572     * @since 4.9.6
    522      *
    523      * @param string $email_text     Text in the email.
    524      * @param int    $request_id     The request ID for this personal data export.
     573     * @since 5.3.0 Introduced the `$email_data` array.
     574     *
     575     * @param string $email_text Text in the email.
     576     * @param int    $request_id The request ID for this personal data export.
     577     * @param array  $email_data {
     578     *     Data relating to the account action email.
     579     *
     580     *     @type WP_User_Request $request           User request object.
     581     *     @type int             $expiration        The time in seconds until the export file expires.
     582     *     @type string          $expiration_date   The localized date and time when the export file expires.
     583     *     @type string          $message_recipient The address that the email will be sent to. Defaults
     584     *                                              to the value of `$request->email`, but can be changed
     585     *                                              by the `wp_privacy_personal_data_email_to` filter.
     586     *     @type string          $export_file_url   The export file URL.
     587     *     @type string          $sitename          The site name sending the mail.
     588     *     @type string          $siteurl           The site URL sending the mail.
    525589     */
    526     $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id );
    527 
    528     $email_address   = $request->email;
    529     $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
    530     $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    531     $site_url        = home_url();
     590    $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id, $email_data );
    532591
    533592    $content = str_replace( '###EXPIRATION###', $expiration_date, $content );
    534593    $content = str_replace( '###LINK###', esc_url_raw( $export_file_url ), $content );
    535     $content = str_replace( '###EMAIL###', $email_address, $content );
     594    $content = str_replace( '###EMAIL###', $request_email, $content );
    536595    $content = str_replace( '###SITENAME###', $site_name, $content );
    537596    $content = str_replace( '###SITEURL###', esc_url_raw( $site_url ), $content );
    538597
    539     $mail_success = wp_mail(
    540         $email_address,
    541         sprintf(
    542             /* translators: Personal data export notification email subject. %s: Site title. */
    543             __( '[%s] Personal Data Export' ),
    544             $site_name
    545         ),
    546         $content
    547     );
     598    $mail_success = wp_mail( $request_email, $subject, $content );
    548599
    549600    if ( $switched_locale ) {
  • trunk/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php

    r45063 r46265  
    176176
    177177    /**
     178     * The email address of the recipient of the personal data export notification should be filterable.
     179     *
     180     * @ticket 46303
     181     */
     182    public function test_email_address_of_recipient_should_be_filterable() {
     183        add_filter( 'wp_privacy_personal_data_email_to', array( $this, 'filter_email_address' ) );
     184        wp_privacy_send_personal_data_export_email( self::$request_id );
     185
     186        $mailer = tests_retrieve_phpmailer_instance();
     187
     188        $this->assertSame( 'modified-' . self::$requester_email, $mailer->get_recipient( 'to' )->address );
     189    }
     190
     191    /**
     192     * Filter callback that modifies the email address of the recipient of the personal data export notification.
     193     *
     194     * @since 5.3.0
     195     *
     196     * @param  string $user_email The email address of the notification recipient.
     197     * @return string $user_email The modified email address of the notification recipient.
     198     */
     199    public function filter_email_address( $user_email ) {
     200        return 'modified-' . $user_email;
     201    }
     202
     203    /**
     204     * The email subject of the personal data export notification should be filterable.
     205     *
     206     * @ticket 46303
     207     */
     208    public function test_email_subject_should_be_filterable() {
     209        add_filter( 'wp_privacy_personal_data_email_subject', array( $this, 'filter_email_subject' ) );
     210        wp_privacy_send_personal_data_export_email( self::$request_id );
     211
     212        $mailer = tests_retrieve_phpmailer_instance();
     213
     214        $this->assertSame( 'Modified subject', $mailer->get_sent()->subject );
     215    }
     216
     217    /**
     218     * Filter callback that modifies the email subject of the data erasure fulfillment notification.
     219     *
     220     * @since 5.3.0
     221     *
     222     * @param string $subject The email subject.
     223     * @return string $subject The email subject.
     224     */
     225    public function filter_email_subject( $subject ) {
     226        return 'Modified subject';
     227    }
     228
     229    /**
    178230     * The email content should be filterable.
    179231     *
     
    202254
    203255    /**
     256     * The email content should be filterable using the $email_data
     257     *
     258     * @ticket 46303
     259     */
     260    public function test_email_content_should_be_filterable_using_email_data() {
     261        add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content_with_email_data' ), 10, 3 );
     262        wp_privacy_send_personal_data_export_email( self::$request_id );
     263
     264        $site_url = home_url();
     265        $mailer   = tests_retrieve_phpmailer_instance();
     266        $this->assertContains( 'Custom content using the $site_url of $email_data: ' . $site_url, $mailer->get_sent()->body );
     267    }
     268
     269    /**
     270     * Filter callback that modifies the text of the email by using the $email_data sent with a personal data export file.
     271     *
     272     * @since 5.3.0
     273     *
     274     * @param string $email_text Text in the email.
     275     * @param int    $request_id The request ID for this personal data export.
     276     * @param array  $email_data {
     277     *     Data relating to the account action email.
     278     *
     279     *     @type WP_User_Request $request           User request object.
     280     *     @type int             $expiration        The time in seconds until the export file expires.
     281     *     @type string          $expiration_date   The localized date and time when the export file expires.
     282     *     @type string          $message_recipient The address that the email will be sent to. Defaults
     283     *                                              to the value of `$request->email`, but can be changed
     284     *                                              by the `wp_privacy_personal_data_email_to` filter.
     285     *     @type string          $export_file_url   The export file URL.
     286     *     @type string          $sitename          The site name sending the mail.
     287     *     @type string          $siteurl           The site URL sending the mail.
     288     * }
     289     *
     290     * @return string $email_text Text in the email.
     291     */
     292    public function modify_email_content_with_email_data( $email_text, $request_id, $email_data ) {
     293        return 'Custom content using the $site_url of $email_data: ' . $email_data['siteurl'];
     294    }
     295
     296    /**
    204297     * The function should respect the user locale settings when the site uses the default locale.
    205298     *
Note: See TracChangeset for help on using the changeset viewer.