From db14f851a16e6659deebfb9d270efc51f2787d87 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 3 Mar 2022 12:30:46 +0100 Subject: [PATCH] Prevent installing translations from .org if they are available on .com [MAILPOET-4144] --- mailpoet/lib/Config/TranslationUpdater.php | 20 ++++- .../Config/TranslationUpdaterTest.php | 79 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/mailpoet/lib/Config/TranslationUpdater.php b/mailpoet/lib/Config/TranslationUpdater.php index bea188c653..83279ff185 100644 --- a/mailpoet/lib/Config/TranslationUpdater.php +++ b/mailpoet/lib/Config/TranslationUpdater.php @@ -38,7 +38,7 @@ class TranslationUpdater { } public function init(): void { - $this->wpFunctions->addFilter('pre_set_site_transient_update_plugins', [$this, 'checkForTranslations']); + $this->wpFunctions->addFilter('pre_set_site_transient_update_plugins', [$this, 'checkForTranslations'], 21); } public function checkForTranslations($transient) { @@ -53,8 +53,9 @@ class TranslationUpdater { $languagePacksData = $this->getAvailableTranslations($locales); $translations = $this->selectUpdatesToInstall($languagePacksData); - - $transient->translations = array_merge($transient->translations ?? [], $translations); + // We want to ignore translations from .org in case a translation pack for the same locale is available from .com + $dotOrgTranslations = $this->removeDuplicateTranslationsFromOrg($transient->translations ?? [], $languagePacksData[$this->freeSlug] ?? []); + $transient->translations = array_merge($dotOrgTranslations ?? [], $translations); return $transient; } @@ -136,4 +137,17 @@ class TranslationUpdater { return $translationsToInstall; } + + private function removeDuplicateTranslationsFromOrg(array $translationsDotOrg, array $translationsDotComData) { + $localesAvailableFromDotCom = array_unique(array_column($translationsDotComData, 'wp_locale')); + return array_filter($translationsDotOrg, function ($translation) use($localesAvailableFromDotCom) { + if ( + $translation['slug'] !== $this->freeSlug + || !in_array($translation['language'], $localesAvailableFromDotCom, true) + ) { + return true; + } + return false; + }); + } } diff --git a/mailpoet/tests/integration/Config/TranslationUpdaterTest.php b/mailpoet/tests/integration/Config/TranslationUpdaterTest.php index 43de02d4eb..d71499eb90 100644 --- a/mailpoet/tests/integration/Config/TranslationUpdaterTest.php +++ b/mailpoet/tests/integration/Config/TranslationUpdaterTest.php @@ -225,6 +225,85 @@ class TranslationUpdaterTest extends \MailPoetTest { expect($freeTranslation['package'])->equals('https:\/\/translate.files.wordpress.com\/2021\/08\/mailpoet-free-0_1-fr_fr.zip'); } + public function testItDoesNotInstallDotOrgTranslationsInCaseThereIsLanguagePackFromDotCom(): void { + $wpFunctions = Stub::construct( + $this->wp, + [], + [ + 'wpRemotePost' => function() { + return [ + 'response' => [ + 'code' => 200, + ], + 'body' => json_encode([ + 'success' => true, + 'data' => $this->getResponseData(), + ]), + ]; + }, + 'getAvailableLanguages' => function() { + return ['fr_FR', 'cs_CZ']; + }, + 'wpGetInstalledTranslations' => function() { + return []; + }, + ], + $this + ); + + $updateTransient = new \stdClass; + $updateTransient->translations = [ + // To be removed: Available on translate.wordpress.com + [ + 'type' => 'plugin', + 'slug' => 'mailpoet', + 'language' => 'fr_FR', + ], + // To be kept: Not available on translate.wordpress.com, so we want to install at least translations from .org + [ + 'type' => 'plugin', + 'slug' => 'mailpoet', + 'language' => 'cs_CZ', + ], + // To be kept: We don't want to touch other plugins + [ + 'type' => 'plugin', + 'slug' => 'askimet', + 'language' => 'fr_FR', + ], + ]; + $updater = Stub::construct( + $this->updater, + [ + $wpFunctions, + $this->freeSlug, + $this->freeVersion, + $this->premiumSlug, + $this->premiumVersion, + ] + ); + $result = $updater->checkForTranslations($updateTransient); + expect($result->translations)->count(4); // askimet + mailpoet cs_CZ and two packs from .com + + $mailPoetCs = $result->translations[0]; + expect($mailPoetCs['slug'])->equals('mailpoet'); + expect($mailPoetCs['language'])->equals('cs_CZ'); + + $askimetFr = $result->translations[1]; + expect($askimetFr['slug'])->equals('askimet'); + expect($askimetFr['language'])->equals('fr_FR'); + + $mailpoetFr = $result->translations[2]; + expect($mailpoetFr['slug'])->equals('mailpoet'); + expect($mailpoetFr['language'])->equals('fr_FR'); + expect($mailpoetFr['package'])->stringContainsString('translate.files.wordpress.com'); + + $mailpoetPremiumFr = $result->translations[3]; + expect($mailpoetPremiumFr['slug'])->equals('mailpoet-premium'); + expect($mailpoetPremiumFr['language'])->equals('fr_FR'); + expect($mailpoetPremiumFr['package'])->stringContainsString('translate.files.wordpress.com'); + } + public function testItDoesNotOverrideOtherPluginTranslations(): void { $wpFunctions = Stub::construct( $this->wp,