PopulatorData
4 months ago
AccessControl.php
2 years ago
Activator.php
3 months ago
AssetsLoader.php
1 month ago
Capabilities.php
4 months ago
Changelog.php
3 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
3 months ago
Env.php
7 months ago
Hooks.php
2 months ago
HooksWooCommerce.php
2 months ago
Initializer.php
2 months ago
Installer.php
1 year ago
Localizer.php
3 years ago
Menu.php
2 months ago
PersonalDataErasers.php
2 months ago
PersonalDataExporters.php
2 months ago
PluginActivatedHook.php
3 years ago
Populator.php
1 month ago
PrivacyPolicy.php
2 months ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
3 months ago
Router.php
3 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
3 months ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
3 months ago
TranslationUpdater.php
4 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
9 months ago
index.php
3 years ago
Updater.php
112 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\Config\Env; |
| 9 | use MailPoet\Services\Bridge; |
| 10 | use MailPoet\Services\Release\API; |
| 11 | use MailPoet\Settings\SettingsController; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class Updater { |
| 15 | private $plugin; |
| 16 | private $slug; |
| 17 | private $version; |
| 18 | public $currentFreeVersion; |
| 19 | |
| 20 | /** @var SettingsController */ |
| 21 | private $settings; |
| 22 | |
| 23 | public function __construct( |
| 24 | $pluginName, |
| 25 | $slug, |
| 26 | $version |
| 27 | ) { |
| 28 | $this->plugin = WPFunctions::get()->pluginBasename($pluginName); |
| 29 | $this->slug = $slug; |
| 30 | $this->version = $version; |
| 31 | $this->settings = SettingsController::getInstance(); |
| 32 | $this->currentFreeVersion = MAILPOET_VERSION; |
| 33 | } |
| 34 | |
| 35 | public function init() { |
| 36 | WPFunctions::get()->addFilter('pre_set_site_transient_update_plugins', [$this, 'checkForUpdate']); |
| 37 | } |
| 38 | |
| 39 | public function checkForUpdate($updateTransient) { |
| 40 | if (!$updateTransient instanceof \stdClass) { |
| 41 | $updateTransient = new \stdClass; |
| 42 | } |
| 43 | |
| 44 | $latestVersion = $this->getLatestVersion(); |
| 45 | |
| 46 | if (!isset($latestVersion->new_version)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 47 | return $updateTransient; // no latest version found. |
| 48 | } |
| 49 | |
| 50 | if (property_exists($updateTransient, 'response') && isset($updateTransient->response[$this->plugin])) { |
| 51 | unset($updateTransient->response[$this->plugin]); // remove the cached version from the transient. |
| 52 | } |
| 53 | |
| 54 | $latestFreeVersion = null; |
| 55 | if (property_exists($updateTransient, 'response') && isset($updateTransient->response[Env::$pluginPath]->new_version)) { |
| 56 | $latestFreeVersion = $updateTransient->response[Env::$pluginPath]->new_version; |
| 57 | } |
| 58 | |
| 59 | if (!$this->shouldShowUpdateNotice($latestVersion->new_version, $latestFreeVersion)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 60 | return $updateTransient; // skip update notice. |
| 61 | } |
| 62 | |
| 63 | if (version_compare((string)$this->version, $latestVersion->new_version, '<')) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 64 | $updateTransient->response[$this->plugin] = $latestVersion; |
| 65 | } else { |
| 66 | $updateTransient->no_update[$this->plugin] = $latestVersion; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 67 | } |
| 68 | $updateTransient->last_checked = time(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 69 | $updateTransient->checked[$this->plugin] = $this->version; |
| 70 | |
| 71 | return $updateTransient; |
| 72 | } |
| 73 | |
| 74 | public function getLatestVersion() { |
| 75 | $key = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 76 | $api = new API($key); |
| 77 | $data = $api->getPluginInformation($this->slug . '/latest'); |
| 78 | return $data; |
| 79 | } |
| 80 | |
| 81 | public function isVersionCompatible($premiumVersion, $freeVersion): bool { |
| 82 | if (empty($premiumVersion) || empty($freeVersion)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Extract major.minor from premium version (e.g., "5.17.0" -> "5.17") |
| 87 | $premiumParts = explode('.', $premiumVersion); |
| 88 | $requiredMainVersion = isset($premiumParts[0], $premiumParts[1]) |
| 89 | ? $premiumParts[0] . '.' . $premiumParts[1] |
| 90 | : $premiumVersion; |
| 91 | |
| 92 | // Extract major.minor from free version (e.g., "5.17.5" -> "5.17") |
| 93 | $freeParts = explode('.', $freeVersion); |
| 94 | $currentMainVersion = isset($freeParts[0], $freeParts[1]) |
| 95 | ? $freeParts[0] . '.' . $freeParts[1] |
| 96 | : $freeVersion; |
| 97 | |
| 98 | // Check compatibility: free version must be >= required version |
| 99 | return version_compare($currentMainVersion, $requiredMainVersion, '>='); |
| 100 | } |
| 101 | |
| 102 | public function shouldShowUpdateNotice($premiumLatestVersion, $latestFreeVersion = null): bool { |
| 103 | // first check if the free version in the update transient is compatible with the premium latest version |
| 104 | if (!empty($latestFreeVersion) && $this->isVersionCompatible($premiumLatestVersion, $latestFreeVersion)) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // then check if the current free version is compatible with the premium latest version |
| 109 | return $this->isVersionCompatible($premiumLatestVersion, $this->currentFreeVersion); |
| 110 | } |
| 111 | } |
| 112 |