Prevent installing translations from .org if they are available on .com

[MAILPOET-4144]
This commit is contained in:
Rostislav Wolny
2022-03-03 12:30:46 +01:00
committed by Veljko V
parent 7791219d7d
commit db14f851a1
2 changed files with 96 additions and 3 deletions

View File

@@ -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;
});
}
}

View File

@@ -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,