From 87d4d0ae925d9360aecb079681f43a4664d59e2c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 28 Mar 2019 21:28:37 +0000 Subject: [PATCH] Privacy: Ensure the personal data export email is sent in the locale of the user whose data is being exported (or the site's default locale if they are not a registered user) when the administrator fulfilling the request uses a different locale. Props garrett-eclipse. Fixes #46056. git-svn-id: https://develop.svn.wordpress.org/trunk@45062 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/file.php | 13 ++ tests/phpunit/data/languages/de_DE.mo | Bin 777 -> 863 bytes tests/phpunit/data/languages/de_DE.po | 10 +- tests/phpunit/data/languages/es_ES.mo | Bin 939 -> 1027 bytes tests/phpunit/data/languages/es_ES.po | 10 +- .../wpPrivacySendPersonalDataExportEmail.php | 164 ++++++++++++++++++ 6 files changed, 191 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 11e5f9231f..bbae7bed4d 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -2353,6 +2353,15 @@ function wp_privacy_send_personal_data_export_email( $request_id ) { return new WP_Error( 'invalid_request', __( 'Invalid request ID when sending personal data export email.' ) ); } + // Localize message content for user; fallback to site default for visitors. + if ( ! empty( $request->user_id ) ) { + $locale = get_user_locale( $request->user_id ); + } else { + $locale = get_locale(); + } + + $switched_locale = switch_to_locale( $locale ); + /** This filter is documented in wp-includes/functions.php */ $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS ); $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration ); @@ -2409,6 +2418,10 @@ All at ###SITENAME### $content ); + if ( $switched_locale ) { + restore_previous_locale(); + } + if ( ! $mail_success ) { return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export email.' ) ); } diff --git a/tests/phpunit/data/languages/de_DE.mo b/tests/phpunit/data/languages/de_DE.mo index 3698ce3fda3e65a3b6db942142c49644de9ccf51..dcfef58f489228b3208aef0fbc17704d06c6fe8c 100644 GIT binary patch delta 324 zcmeBVyU$jCPl#nI0}yZmu?!HW05LBRuK{8ZcmTv8{zD*^2IBWXEC+X15U@Zfupah_M|~xuRf}U40#b{L z^YapO6kHNZ5*1u43i69eCJQoFvs)+_T3VSHZJx}S!Ds|l3XxPO0P9H2OG>TEPfyKD ZEdpsx&0|PO)pc=Y&|%PH$SEme003qKF600J delta 238 zcmcc5*2z|XPl#nI0}yZku?!H$05LZZ&jDf(I03}GKztI2rGfZ55X%AaXCM{^Vj)I| zxEhpp0@6Z2@i-uD48(;%EC%E+Wny5E2hw|hG)Vs)AkE6a;KuM9$N;KgU}T2S%s?85 z7+9fvAd`U&h}nS{;$7d#E{s*|<_bneRz~KVk1}R3@};Eey0|jv MFz7MlloT-l0C=4j$N&HU diff --git a/tests/phpunit/data/languages/de_DE.po b/tests/phpunit/data/languages/de_DE.po index c3ff771d5e..aa97af62e9 100644 --- a/tests/phpunit/data/languages/de_DE.po +++ b/tests/phpunit/data/languages/de_DE.po @@ -1,8 +1,8 @@ -# Translation of 4.9.x in German -# This file is distributed under the same license as the 4.9.x package. +# Translation of 5.2.x in German +# This file is distributed under the same license as the 5.2.x package. msgid "" msgstr "" -"PO-Revision-Date: 2019-03-27 22:27+0300\n" +"PO-Revision-Date: 2019-03-28 19:42+0300\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -53,3 +53,7 @@ msgstr "[%1$s] Aktion bestätigen: %2$s" #: wp-includes/user.php:3175 msgid "[%s] Erasure Request Fulfilled" msgstr "[%s] Löschauftrag ausgeführt" + +#: wp-admin/includes/file.php:2415 +msgid "[%s] Personal Data Export" +msgstr "[%s] Export personenbezogener Daten" diff --git a/tests/phpunit/data/languages/es_ES.mo b/tests/phpunit/data/languages/es_ES.mo index 3fe721e0018eb94dba6696bfb16c297d9e68a9a3..bd56b36d4c6cbe5ea010577f06ffbf07853a1b54 100644 GIT binary patch delta 315 zcmZ3@-po;dPl#nI0}yZmu?!HW05LBRuK{8ZcmTvUK>QGhg@9O&k%2)PNE-raIUwx| zq$PoL4ph7iN-qM^Aocrzv@sB01Y#c`pOu+`K^#a20ciyWhB$^SAVUQx&<&(jf%FC- z4YJ@Gkmdo>pMW$FG5iM-Kr_Gq`9IFtJ zT2!2$mzbmAl30?c;960TUsN(Vm@%E*Lc!3|%EWl{LB_p|s$iuM$;9N$!<+LIQc@LC T5=-)n6$+pRq!v%EW!3`#Db6uU delta 227 zcmZqXSj}F4Pl#nI0}yZku?!H$05LZZ&jDf(I03{qKztI2g@Blik%2)PNJ|1~IUsEc z<%dA&0w4`i*8`-Dfp|U;`vCcunHU(vfwTfM5CdroAg#i{;KmRMWPl8;1JXP|dKQod zB8KHa0;m@ZKzxt`fo8w~JCMZ!#B4y!I`MA*user->create_and_get( + array( + 'user_email' => self::$requester_email, + 'role' => 'subscriber', + ) + ); + self::$admin_user = $factory->user->create_and_get( + array( + 'user_email' => 'admin@local.dev', + 'role' => 'administrator', + ) + ); + self::$request_id = wp_create_user_request( self::$requester_email, 'export_personal_data' ); _wp_privacy_account_request_confirmed( self::$request_id ); @@ -167,4 +199,136 @@ class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase public function modify_email_content( $email_text, $request_id ) { return 'Custom content for request ID: ' . $request_id; } + + /** + * The function should respect the user locale settings when the site uses the default locale. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_user_locale() { + update_user_meta( self::$request_user->ID, 'locale', 'es_ES' ); + + wp_privacy_send_personal_data_export_email( self::$request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject ); + } + + /** + * The function should respect the user locale settings when the site does not use en_US, the administrator + * uses the site's default locale, and the user has a different locale. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_user_locale_when_site_is_not_en_us() { + update_option( 'WPLANG', 'es_ES' ); + switch_to_locale( 'es_ES' ); + + update_user_meta( self::$request_user->ID, 'locale', 'de_DE' ); + wp_set_current_user( self::$admin_user->ID ); + + wp_privacy_send_personal_data_export_email( self::$request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Export personenbezogener Daten', $mailer->get_sent()->subject ); + } + + /** + * The function should respect the user locale settings when the site is not en_US, the administrator + * has a different selected locale, and the user uses the site's default locale. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_user_locale_when_admin_and_site_have_different_locales() { + update_option( 'WPLANG', 'es_ES' ); + switch_to_locale( 'es_ES' ); + + update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' ); + wp_set_current_user( self::$admin_user->ID ); + + wp_privacy_send_personal_data_export_email( self::$request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject ); + } + + /** + * The function should respect the user locale settings when the site is not en_US and both the + * administrator and the user use different locales. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_user_locale_when_both_have_different_locales_than_site() { + update_option( 'WPLANG', 'es_ES' ); + switch_to_locale( 'es_ES' ); + + update_user_meta( self::$admin_user->ID, 'locale', 'en_US' ); + update_user_meta( self::$request_user->ID, 'locale', 'de_DE' ); + + wp_set_current_user( self::$admin_user->ID ); + + wp_privacy_send_personal_data_export_email( self::$request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Export personenbezogener Daten', $mailer->get_sent()->subject ); + } + + /** + * The function should respect the site's locale when the request is for an unregistered user and the + * administrator does not use the site's locale. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_site_locale() { + update_user_meta( self::$admin_user->ID, 'locale', 'es_ES' ); + wp_set_current_user( self::$admin_user->ID ); + + $request_id = wp_create_user_request( 'export-user-not-registered@example.com', 'export_personal_data' ); + + _wp_privacy_account_request_confirmed( self::$request_id ); + wp_privacy_send_personal_data_export_email( $request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Personal Data Export', $mailer->get_sent()->subject ); + } + + /** + * The function should respect the site's locale when it is not en_US, the request is for an + * unregistered user, and the administrator does not use the site's default locale. + * + * @since 5.2.0 + * @ticket 46056 + * @group l10n + */ + public function test_should_send_personal_data_export_email_in_site_locale_when_not_en_us_and_admin_has_different_locale() { + update_option( 'WPLANG', 'es_ES' ); + switch_to_locale( 'es_ES' ); + + update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' ); + wp_set_current_user( self::$admin_user->ID ); + + $request_id = wp_create_user_request( 'export-user-not-registered@example.com', 'export_personal_data' ); + + _wp_privacy_account_request_confirmed( self::$request_id ); + wp_privacy_send_personal_data_export_email( $request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject ); + } }