Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
Premium.php
90 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\Error as APIError; |
| 10 | use MailPoet\Config\AccessControl; |
| 11 | use MailPoet\Config\Installer; |
| 12 | use MailPoet\Config\ServicesChecker; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 15 | |
| 16 | class Premium extends APIEndpoint { |
| 17 | const PREMIUM_PLUGIN_SLUG = 'mailpoet-premium'; |
| 18 | const PREMIUM_PLUGIN_PATH = 'mailpoet-premium/mailpoet-premium.php'; |
| 19 | // This is the path to the managed plugin on Dotcom platform. It is relative to WP_PLUGIN_DIR. |
| 20 | const DOTCOM_SYMLINK_PATH = '../../../../wordpress/plugins/mailpoet-premium/latest'; |
| 21 | |
| 22 | public $permissions = [ |
| 23 | 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS, |
| 24 | ]; |
| 25 | |
| 26 | private ServicesChecker $servicesChecker; |
| 27 | |
| 28 | private WPFunctions $wp; |
| 29 | |
| 30 | private DotcomHelperFunctions $dotcomHelperFunctions; |
| 31 | |
| 32 | private Installer $premiumInstaller; |
| 33 | |
| 34 | public function __construct( |
| 35 | ServicesChecker $servicesChecker, |
| 36 | WPFunctions $wp, |
| 37 | DotcomHelperFunctions $dotcomHelperFunctions, |
| 38 | ?Installer $premiumInstaller = null |
| 39 | ) { |
| 40 | $this->servicesChecker = $servicesChecker; |
| 41 | $this->wp = $wp; |
| 42 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 43 | $this->premiumInstaller = $premiumInstaller ?? new Installer(Installer::PREMIUM_PLUGIN_SLUG); |
| 44 | } |
| 45 | |
| 46 | public function installPlugin() { |
| 47 | $premiumKeyValid = $this->servicesChecker->isPremiumKeyValid(false); |
| 48 | if (!$premiumKeyValid) { |
| 49 | return $this->error(__('Premium key is not valid.', 'mailpoet')); |
| 50 | } |
| 51 | |
| 52 | // If we are in Dotcom platform, we try to symlink the plugin instead of downloading it |
| 53 | try { |
| 54 | if ($this->dotcomHelperFunctions->isDotcom()) { |
| 55 | $result = symlink(self::DOTCOM_SYMLINK_PATH, WP_PLUGIN_DIR . '/' . self::PREMIUM_PLUGIN_SLUG); |
| 56 | if ($result === true) { |
| 57 | return $this->successResponse(); |
| 58 | } |
| 59 | } |
| 60 | } catch (\Exception $e) { |
| 61 | // Do nothing and continue with a regular installation |
| 62 | } |
| 63 | |
| 64 | $result = $this->wp->installPlugin($this->premiumInstaller->generatePluginDownloadUrl()); |
| 65 | if ($result !== true) { |
| 66 | return $this->error(__('Error when installing MailPoet Premium plugin.', 'mailpoet')); |
| 67 | } |
| 68 | return $this->successResponse(); |
| 69 | } |
| 70 | |
| 71 | public function activatePlugin() { |
| 72 | $premiumKeyValid = $this->servicesChecker->isPremiumKeyValid(false); |
| 73 | if (!$premiumKeyValid) { |
| 74 | return $this->error(__('Premium key is not valid.', 'mailpoet')); |
| 75 | } |
| 76 | |
| 77 | $result = $this->wp->activatePlugin(self::PREMIUM_PLUGIN_PATH); |
| 78 | if ($result !== null) { |
| 79 | return $this->error(__('Error when activating MailPoet Premium plugin.', 'mailpoet')); |
| 80 | } |
| 81 | return $this->successResponse(); |
| 82 | } |
| 83 | |
| 84 | private function error($message) { |
| 85 | return $this->badRequest([ |
| 86 | APIError::BAD_REQUEST => $message, |
| 87 | ]); |
| 88 | } |
| 89 | } |
| 90 |