PopulatorData
2 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
2 weeks ago
Capabilities.php
2 months ago
Changelog.php
2 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
6 months ago
Hooks.php
4 weeks ago
HooksWooCommerce.php
1 month ago
Initializer.php
4 weeks ago
Installer.php
10 months ago
Localizer.php
3 years ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
2 weeks ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
2 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 month 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
4 days ago
index.php
3 years ago
Updater.php
105 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\Services\Bridge; |
| 9 | use MailPoet\Services\Release\API; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class Updater { |
| 14 | private $plugin; |
| 15 | private $slug; |
| 16 | private $version; |
| 17 | public $currentFreeVersion; |
| 18 | |
| 19 | /** @var SettingsController */ |
| 20 | private $settings; |
| 21 | |
| 22 | public function __construct( |
| 23 | $pluginName, |
| 24 | $slug, |
| 25 | $version |
| 26 | ) { |
| 27 | $this->plugin = WPFunctions::get()->pluginBasename($pluginName); |
| 28 | $this->slug = $slug; |
| 29 | $this->version = $version; |
| 30 | $this->settings = SettingsController::getInstance(); |
| 31 | $this->currentFreeVersion = MAILPOET_VERSION; |
| 32 | } |
| 33 | |
| 34 | public function init() { |
| 35 | WPFunctions::get()->addFilter('pre_set_site_transient_update_plugins', [$this, 'checkForUpdate']); |
| 36 | } |
| 37 | |
| 38 | public function checkForUpdate($updateTransient) { |
| 39 | if (!$updateTransient instanceof \stdClass) { |
| 40 | $updateTransient = new \stdClass; |
| 41 | } |
| 42 | |
| 43 | $latestVersion = $this->getLatestVersion(); |
| 44 | |
| 45 | if (!isset($latestVersion->new_version)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 46 | return $updateTransient; // no latest version found. |
| 47 | } |
| 48 | |
| 49 | if (property_exists($updateTransient, 'response') && isset($updateTransient->response[$this->plugin])) { |
| 50 | unset($updateTransient->response[$this->plugin]); // remove the cached version from the transient. |
| 51 | } |
| 52 | |
| 53 | if (!$this->shouldShowUpdateNotice($latestVersion->new_version)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 54 | return $updateTransient; // skip update notice. |
| 55 | } |
| 56 | |
| 57 | if (version_compare((string)$this->version, $latestVersion->new_version, '<')) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 58 | $updateTransient->response[$this->plugin] = $latestVersion; |
| 59 | } else { |
| 60 | $updateTransient->no_update[$this->plugin] = $latestVersion; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 61 | } |
| 62 | $updateTransient->last_checked = time(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 63 | $updateTransient->checked[$this->plugin] = $this->version; |
| 64 | |
| 65 | return $updateTransient; |
| 66 | } |
| 67 | |
| 68 | public function getLatestVersion() { |
| 69 | $key = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 70 | $api = new API($key); |
| 71 | $data = $api->getPluginInformation($this->slug . '/latest'); |
| 72 | return $data; |
| 73 | } |
| 74 | |
| 75 | public function isVersionCompatible($premiumVersion, $freeVersion): bool { |
| 76 | if (empty($premiumVersion) || empty($freeVersion)) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Extract major.minor from premium version (e.g., "5.17.0" -> "5.17") |
| 81 | $premiumParts = explode('.', $premiumVersion); |
| 82 | $requiredMainVersion = isset($premiumParts[0], $premiumParts[1]) |
| 83 | ? $premiumParts[0] . '.' . $premiumParts[1] |
| 84 | : $premiumVersion; |
| 85 | |
| 86 | // Extract major.minor from free version (e.g., "5.17.5" -> "5.17") |
| 87 | $freeParts = explode('.', $freeVersion); |
| 88 | $currentMainVersion = isset($freeParts[0], $freeParts[1]) |
| 89 | ? $freeParts[0] . '.' . $freeParts[1] |
| 90 | : $freeVersion; |
| 91 | |
| 92 | // Check compatibility: free version must be >= required version |
| 93 | return version_compare($currentMainVersion, $requiredMainVersion, '>='); |
| 94 | } |
| 95 | |
| 96 | public function shouldShowUpdateNotice($premiumLatestVersion): bool { |
| 97 | // Compare against the free version that's actually installed, not one that's |
| 98 | // merely available. Since wordpress.org now holds new releases for up to 24h |
| 99 | // before distributing them (https://wordpress.org/news/2026/06/pts/), the free |
| 100 | // update can lag behind Premium. Gating on the installed version keeps Premium |
| 101 | // from jumping ahead and disabling itself. |
| 102 | return $this->isVersionCompatible($premiumLatestVersion, $this->currentFreeVersion); |
| 103 | } |
| 104 | } |
| 105 |