Make WordPress Core

Ticket #46303: 46303.4.diff

File 46303.4.diff, 9.6 KB (added by garrett-eclipse, 5 years ago)

Refreshed patch to apply cleanly in new privacy-tools.php, updated versions and CS issues. Introduced unit tests.

  • src/wp-admin/includes/privacy-tools.php

     
    485485        $expiration      = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
    486486        $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
    487487
     488        $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
     489        $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     490        $site_url        = home_url();
     491
     492        /**
     493         * Filters the recipient of the personal data export email notification.
     494         *
     495         * @since 5.3.0
     496         *
     497         * @param string          $request_email The email address of the notification recipient.
     498         * @param WP_User_Request $request       The request that is initiating the notification.
     499         */
     500        $request_email = apply_filters( 'wp_privacy_personal_data_email_to', $request->email, $request );
     501
     502        $email_data = array(
     503                'request'           => $request,
     504                'expiration'        => $expiration,
     505                'expiration_date'   => $expiration_date,
     506                'message_recipient' => $request_email,
     507                'export_file_url'   => $export_file_url,
     508                'sitename'          => $site_name,
     509                'siteurl'           => $site_url,
     510        );
     511
     512        $subject = sprintf(
     513                /* translators: %s: Site name. */
     514                __( '[%s] Personal Data Export' ),
     515                $site_name
     516        );
     517
     518        /**
     519         * Filters the subject of the email sent when an export request is completed.
     520         *
     521         * @since 5.3.0
     522         *
     523         * @param string $subject    The email subject.
     524         * @param string $sitename   The name of the site.
     525         * @param array  $email_data {
     526         *     Data relating to the account action email.
     527         *
     528         *     @type WP_User_Request $request           User request object.
     529         *     @type int             $expiration        The time in seconds until the export file expires.
     530         *     @type string          $expiration_date   The localized date and time when the export file expires.
     531         *     @type string          $message_recipient The address that the email will be sent to. Defaults
     532         *                                              to the value of `$request->email`, but can be changed
     533         *                                              by the `wp_privacy_personal_data_email_to` filter.
     534         *     @type string          $export_file_url   The export file URL.
     535         *     @type string          $sitename          The site name sending the mail.
     536         *     @type string          $siteurl           The site URL sending the mail.
     537         * }
     538         */
     539        $subject = apply_filters( 'wp_privacy_personal_data_email_subject', $subject, $site_name, $email_data );
     540
    488541        /* translators: Do not translate EXPIRATION, LINK, SITENAME, SITEURL: those are placeholders. */
    489542        $email_text = __(
    490543                'Howdy,
     
    511564         * ###SITEURL###            The URL to the site.
    512565         *
    513566         * @since 4.9.6
     567         * @since 5.3.0 Introduced the $email_data array.
    514568         *
    515          * @param string $email_text     Text in the email.
    516          * @param int    $request_id     The request ID for this personal data export.
     569         * @param string $email_text Text in the email.
     570         * @param int    $request_id The request ID for this personal data export.
     571         * @param array  $email_data {
     572         *     Data relating to the account action email.
     573         *
     574         *     @type WP_User_Request $request           User request object.
     575         *     @type int             $expiration        The time in seconds until the export file expires.
     576         *     @type string          $expiration_date   The localized date and time when the export file expires.
     577         *     @type string          $message_recipient The address that the email will be sent to. Defaults
     578         *                                              to the value of `$request->email`, but can be changed
     579         *                                              by the `wp_privacy_personal_data_email_to` filter.
     580         *     @type string          $export_file_url   The export file URL.
     581         *     @type string          $sitename          The site name sending the mail.
     582         *     @type string          $siteurl           The site URL sending the mail.
    517583         */
    518         $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id );
     584        $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id, $email_data );
    519585
    520         $email_address   = $request->email;
    521         $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
    522         $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    523         $site_url        = home_url();
    524 
    525586        $content = str_replace( '###EXPIRATION###', $expiration_date, $content );
    526587        $content = str_replace( '###LINK###', esc_url_raw( $export_file_url ), $content );
    527         $content = str_replace( '###EMAIL###', $email_address, $content );
     588        $content = str_replace( '###EMAIL###', $request_email, $content );
    528589        $content = str_replace( '###SITENAME###', $site_name, $content );
    529590        $content = str_replace( '###SITEURL###', esc_url_raw( $site_url ), $content );
    530591
    531         $mail_success = wp_mail(
    532                 $email_address,
    533                 sprintf(
    534                         /* translators: Personal data export notification email subject. %s: Site title. */
    535                         __( '[%s] Personal Data Export' ),
    536                         $site_name
    537                 ),
    538                 $content
    539         );
     592        $mail_success = wp_mail( $request_email, $subject, $content );
    540593
    541594        if ( $switched_locale ) {
    542595                restore_previous_locale();
  • tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php

     
    175175        }
    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         *
    180232         * @since 4.9.6
     
    201253        }
    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         *
    206299         * @since 5.2.0