PopulatorData
3 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
1 month ago
Capabilities.php
3 months ago
Changelog.php
3 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
7 months ago
Hooks.php
1 month ago
HooksWooCommerce.php
1 month ago
Initializer.php
1 month ago
Installer.php
11 months ago
Localizer.php
1 week ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
1 month ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
3 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 week ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
2 months ago
TranslationUpdater.php
3 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
2 weeks ago
index.php
3 years ago
Localizer.php
93 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class Localizer { |
| 11 | public function init(WPFunctions $wpFunctions) { |
| 12 | $this->loadGlobalText(); |
| 13 | $this->setupTranslationsUpdater($wpFunctions); |
| 14 | } |
| 15 | |
| 16 | private function setupTranslationsUpdater(WPFunctions $wpFunctions) { |
| 17 | $translationUpdater = $this->getUpdater($wpFunctions); |
| 18 | $translationUpdater->init(); |
| 19 | } |
| 20 | |
| 21 | public function loadGlobalText() { |
| 22 | $languagePath = sprintf( |
| 23 | '%s/%s-%s.mo', |
| 24 | Env::$languagesPath, |
| 25 | Env::$pluginName, |
| 26 | $this->locale() |
| 27 | ); |
| 28 | WPFunctions::get()->loadTextdomain(Env::$pluginName, $languagePath); |
| 29 | } |
| 30 | |
| 31 | public function locale() { |
| 32 | // Reading the user locale resolves the current user. Before 'plugins_loaded' |
| 33 | // (e.g. when network activated on multisite) the auth cookie constants may |
| 34 | // not be defined yet, and resolving the user fatals on PHP 8 if pluggable.php |
| 35 | // was already loaded by another early component. Fall back to the site |
| 36 | // locale; Initializer::setupUserLocale() switches to the user locale on |
| 37 | // 'wp_loaded'. |
| 38 | $defaultLocale = WPFunctions::get()->didAction('plugins_loaded') |
| 39 | ? WPFunctions::get()->getUserLocale() |
| 40 | : WPFunctions::get()->getLocale(); |
| 41 | $locale = WPFunctions::get()->applyFilters( |
| 42 | 'plugin_locale', |
| 43 | $defaultLocale, |
| 44 | Env::$pluginName |
| 45 | ); |
| 46 | return $locale; |
| 47 | } |
| 48 | |
| 49 | public function forceInstallLanguagePacks(WPFunctions $wpFunctions) { |
| 50 | $translationUpdater = $this->getUpdater($wpFunctions); |
| 51 | // Add MailPoet translation update to the update_plugins site transient via inner hook |
| 52 | $transient = $translationUpdater->checkForTranslations(new \stdClass()); |
| 53 | $mailpoetTranslations = []; |
| 54 | $translationUpdates = $transient->translations ?? []; |
| 55 | foreach ($translationUpdates as $translationUpdate) { |
| 56 | $mailpoetTranslations[] = (object)$translationUpdate; |
| 57 | } |
| 58 | |
| 59 | if (!empty($mailpoetTranslations)) { |
| 60 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 61 | require_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; |
| 62 | $upgrader = new \Language_Pack_Upgrader(new SilentUpgraderSkin()); |
| 63 | $upgrader->bulk_upgrade($mailpoetTranslations); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function forceLoadWebsiteLocaleText() { |
| 68 | $languagePath = sprintf( |
| 69 | '%s/%s-%s.mo', |
| 70 | Env::$languagesPath, |
| 71 | Env::$pluginName, |
| 72 | WPFunctions::get()->getLocale() |
| 73 | ); |
| 74 | WPFunctions::get()->unloadTextdomain(Env::$pluginName); |
| 75 | WPFunctions::get()->loadTextdomain(Env::$pluginName, $languagePath); |
| 76 | } |
| 77 | |
| 78 | private function getUpdater(WPFunctions $wp): TranslationUpdater { |
| 79 | $premiumSlug = Installer::PREMIUM_PLUGIN_SLUG; |
| 80 | $premiumVersion = defined('MAILPOET_PREMIUM_VERSION') ? MAILPOET_PREMIUM_VERSION : null; |
| 81 | $freeSlug = Env::$pluginName; |
| 82 | $freeVersion = MAILPOET_VERSION; |
| 83 | |
| 84 | return new TranslationUpdater( |
| 85 | $wp, |
| 86 | $freeSlug, |
| 87 | $freeVersion, |
| 88 | $premiumSlug, |
| 89 | $premiumVersion |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 |