diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 048dd4e575..b106f85685 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -107,7 +107,6 @@ class Initializer { $this->setupShortcodes(); $this->setupHooks(); $this->setupImages(); - $this->setupLicense(); $this->setupCronTrigger(); $this->plugin_initialized = true; @@ -211,9 +210,4 @@ class Initializer { function handleFailedInitialization($message) { return WPNotice::displayError($message); } - - function setupLicense() { - $license = new License(); - $license->init(); - } } \ No newline at end of file diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 17c720f21c..4fb42f4d74 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -14,7 +14,7 @@ use MailPoet\Settings\Hosts; use MailPoet\Settings\Pages; use MailPoet\Subscribers\ImportExport\ImportExportFactory; use MailPoet\Listing; -use MailPoet\Util\License\License as License; +use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; use MailPoet\WP\DateTime; if(!defined('ABSPATH')) exit; @@ -23,6 +23,8 @@ class Menu { function __construct($renderer, $assets_url) { $this->renderer = $renderer; $this->assets_url = $assets_url; + $subscribers_feature = new SubscribersFeature(); + $this->subscribers_over_limit = $subscribers_feature->check(); } function init() { @@ -243,7 +245,7 @@ class Menu { } function settings() { - do_action(License::CHECK_PERMISSION, 'Subscribers'); + if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); $settings = Setting::getAll(); $flags = $this->_getFlags(); @@ -317,7 +319,7 @@ class Menu { } function segments() { - do_action(License::CHECK_PERMISSION, 'Subscribers'); + if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); $data = array(); $data['items_per_page'] = $this->getLimitPerPage('segments'); @@ -325,7 +327,7 @@ class Menu { } function forms() { - do_action(License::CHECK_PERMISSION, 'Subscribers'); + if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); $data = array(); @@ -336,7 +338,7 @@ class Menu { } function newsletters() { - do_action(License::CHECK_PERMISSION, 'Subscribers'); + if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); global $wp_roles; @@ -438,4 +440,11 @@ class Menu { ? (int)$listing_per_page : Listing\Handler::DEFAULT_LIMIT_PER_PAGE; } -} + + function displaySubscriberLimitExceededTemplate() { + echo $this->renderer->render('limit.html', array( + 'limit' => SubscribersFeature::SUBSCRIBERS_LIMIT + )); + exit; + } +} \ No newline at end of file diff --git a/lib/Util/License/Features/Subscribers.php b/lib/Util/License/Features/Subscribers.php index 714e28ed03..cb0c57c2ee 100644 --- a/lib/Util/License/Features/Subscribers.php +++ b/lib/Util/License/Features/Subscribers.php @@ -1,29 +1,19 @@ $subscribers_limit - ) { - $renderer = new Renderer(); - echo $renderer->init()->render('limit.html', array( - 'limit' => $subscribers_limit - )); - return $this->terminateRequest(); - } + function __construct($license = false) { + $this->license = ($license) ? $license : License::getLicense(); } - function terminateRequest() { - exit; + function check($subscribers_limit = self::SUBSCRIBERS_LIMIT) { + if($this->license) return false; + return SubscriberModel::getTotalSubscribers() > $subscribers_limit; } } \ No newline at end of file diff --git a/lib/Util/License/License.php b/lib/Util/License/License.php index b910544765..42070787b6 100644 --- a/lib/Util/License/License.php +++ b/lib/Util/License/License.php @@ -2,24 +2,12 @@ namespace MailPoet\Util\License; class License { - const CHECK_PERMISSION = 'mailpoet_premium_feature_permission'; - const FEATURE_NAMESPACE = 'MailPoet\Util\License\Features\\'; - - function init() { - add_action(self::CHECK_PERMISSION, array($this, 'checkFeaturePermission')); - } - - static function getLicense() { - return (defined('MAILPOET_PREMIUM_LICENSE')) ? + static function getLicense($license = false) { + if(!$license) { + $license = defined('MAILPOET_PREMIUM_LICENSE') ? MAILPOET_PREMIUM_LICENSE : false; - } - - function checkFeaturePermission($feature) { - $feature = self::FEATURE_NAMESPACE . $feature; - if(class_exists($feature)) { - $feature = new $feature(); - $feature->check(); } + return $license; } } \ No newline at end of file diff --git a/tests/unit/Util/License/Features/SubscribersTest.php b/tests/unit/Util/License/Features/SubscribersTest.php index c2db95719a..4820ef75c4 100644 --- a/tests/unit/Util/License/Features/SubscribersTest.php +++ b/tests/unit/Util/License/Features/SubscribersTest.php @@ -6,24 +6,26 @@ use MailPoet\Models\Subscriber; use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; class SubscribersFeaturesTest extends MailPoetTest { - function testItEnforcesSubscriberLimitWithoutPremiumLicense() { - $subscribers_feature = Stub::make(new SubscribersFeature(), array( - 'terminateRequest' => function() { return true; } - ), $this); - expect($subscribers_feature->check(0))->null(); + function testChecksIfSubscribersWithinLimitWhenPremiumLicenseDoesNotExist() { + $subscribers_feature = new SubscribersFeature(); + expect($subscribers_feature->check(0))->false(); $subscriber = Subscriber::create(); $subscriber->hydrate(Fixtures::get('subscriber_template')); $subscriber->save(); expect($subscribers_feature->check(0))->true(); } - function testItDoesNotEnforceSubscriberLimitWithPremiumLicense() { - define('MAILPOET_PREMIUM_LICENSE', true); - $subscribers_feature = new SubscribersFeature(); + function testChecksIfSubscribersWithinLimitWhenPremiumLicenseExists() { + $subscribers_feature = Stub::construct( + new SubscribersFeature(), + array( + 'license' => true + ) + ); $subscriber = Subscriber::create(); $subscriber->hydrate(Fixtures::get('subscriber_template')); $subscriber->save(); - expect($subscribers_feature->check(0))->null(); + expect($subscribers_feature->check(0))->false(); } function _after() { diff --git a/tests/unit/Util/License/LicenseTest.php b/tests/unit/Util/License/LicenseTest.php index 1c2f14d8f6..37361891d6 100644 --- a/tests/unit/Util/License/LicenseTest.php +++ b/tests/unit/Util/License/LicenseTest.php @@ -4,21 +4,7 @@ use MailPoet\Util\License\License; class LicenseTest extends MailPoetTest { function testItGetsLicense() { - if(!defined('MAILPOET_PREMIUM_LICENSE')) { expect(License::getLicense())->false(); - define('MAILPOET_PREMIUM_LICENSE', 'valid'); - expect(License::getLicense())->equals('valid'); - } - else { - expect(License::getLicense())->equals(MAILPOET_PREMIUM_LICENSE); - } - } - - function testItAddsFeaturePermissionCheckAction() { - remove_all_actions(License::CHECK_PERMISSION); - expect(has_action(License::CHECK_PERMISSION))->false(); - $license = new License(); - $license->init(); - expect(has_action(License::CHECK_PERMISSION))->true(); + expect(License::getLicense('valid'))->equals('valid'); } } \ No newline at end of file