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
Installer.php
121 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\Util\License\License; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class Installer { |
| 15 | const PREMIUM_PLUGIN_SLUG = 'mailpoet-premium'; |
| 16 | const PREMIUM_PLUGIN_PATH = 'mailpoet-premium/mailpoet-premium.php'; |
| 17 | |
| 18 | private $slug; |
| 19 | |
| 20 | /** @var SettingsController */ |
| 21 | private $settings; |
| 22 | |
| 23 | public function __construct( |
| 24 | $slug |
| 25 | ) { |
| 26 | $this->slug = $slug; |
| 27 | $this->settings = SettingsController::getInstance(); |
| 28 | } |
| 29 | |
| 30 | public function init() { |
| 31 | WPFunctions::get()->addFilter('plugins_api', [$this, 'getPluginInformation'], 10, 3); |
| 32 | } |
| 33 | |
| 34 | public function generatePluginDownloadUrl(): string { |
| 35 | $premiumKey = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 36 | $freeMinorVersion = self::getFreeMinorVersionZero(); |
| 37 | return sprintf( |
| 38 | 'https://release.mailpoet.com/downloads/mailpoet-premium/%s/%s/mailpoet-premium.zip', |
| 39 | rawurlencode((string)$premiumKey), |
| 40 | rawurlencode($freeMinorVersion) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function generatePluginActivationUrl(string $plugin): string { |
| 45 | return WPFunctions::get()->adminUrl('plugins.php?' . implode('&', [ |
| 46 | 'action=activate', |
| 47 | 'plugin=' . urlencode($plugin), |
| 48 | '_wpnonce=' . WPFunctions::get()->wpCreateNonce('activate-plugin_' . $plugin), |
| 49 | ])); |
| 50 | } |
| 51 | |
| 52 | public function getPluginInformation($data, $action = '', $args = null) { |
| 53 | if ( |
| 54 | $action === 'plugin_information' |
| 55 | && isset($args->slug) |
| 56 | && $args->slug === $this->slug |
| 57 | ) { |
| 58 | $data = $this->retrievePluginInformation(); |
| 59 | } |
| 60 | |
| 61 | return $data; |
| 62 | } |
| 63 | |
| 64 | public static function getPremiumStatus() { |
| 65 | $slug = self::PREMIUM_PLUGIN_SLUG; |
| 66 | |
| 67 | $premiumPluginActive = License::getLicense(); |
| 68 | $premiumPluginInstalled = $premiumPluginActive || self::isPluginInstalled($slug); |
| 69 | $premiumPluginInitialized = defined('MAILPOET_PREMIUM_INITIALIZED') && MAILPOET_PREMIUM_INITIALIZED; |
| 70 | $installer = new Installer(self::PREMIUM_PLUGIN_SLUG); |
| 71 | $pluginInformation = $installer->retrievePluginInformation(); |
| 72 | |
| 73 | return [ |
| 74 | 'premium_plugin_active' => $premiumPluginActive, |
| 75 | 'premium_plugin_installed' => $premiumPluginInstalled, |
| 76 | 'premium_plugin_initialized' => $premiumPluginInitialized, |
| 77 | 'premium_plugin_info' => $pluginInformation, |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | public static function isPluginInstalled($slug) { |
| 82 | $installedPlugin = self::getInstalledPlugin($slug); |
| 83 | return !empty($installedPlugin); |
| 84 | } |
| 85 | |
| 86 | private static function getInstalledPlugin($slug) { |
| 87 | $installedPlugin = []; |
| 88 | if (is_dir(WP_PLUGIN_DIR . '/' . $slug)) { |
| 89 | $installedPlugin = WPFunctions::get()->getPlugins('/' . $slug); |
| 90 | } |
| 91 | return $installedPlugin; |
| 92 | } |
| 93 | |
| 94 | public static function getPluginFile($slug) { |
| 95 | $pluginFile = false; |
| 96 | $installedPlugin = self::getInstalledPlugin($slug); |
| 97 | if (!empty($installedPlugin)) { |
| 98 | $pluginFile = $slug . '/' . key($installedPlugin); |
| 99 | } |
| 100 | return $pluginFile; |
| 101 | } |
| 102 | |
| 103 | public function retrievePluginInformation() { |
| 104 | $key = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 105 | $api = new API($key); |
| 106 | return $api->getPluginInformation($this->slug); |
| 107 | } |
| 108 | |
| 109 | private static function getFreeMinorVersionZero(): string { |
| 110 | $version = defined('MAILPOET_VERSION') ? (string)MAILPOET_VERSION : ''; |
| 111 | if ($version === '') { |
| 112 | return 'latest'; |
| 113 | } |
| 114 | |
| 115 | $parts = explode('.', $version); |
| 116 | $major = $parts[0] ?? '0'; |
| 117 | $minor = $parts[1] ?? '0'; |
| 118 | return $major . '.' . $minor . '.0'; |
| 119 | } |
| 120 | } |
| 121 |