- Updates license check logic

- Updates subscriber limit check logic
- Updates unit tests
- Updates Menu's check for subscriber limit
This commit is contained in:
Vlad
2016-10-27 12:35:57 -04:00
parent a4395f2350
commit 7d2e13b9a3
6 changed files with 38 additions and 69 deletions

View File

@ -107,7 +107,6 @@ class Initializer {
$this->setupShortcodes(); $this->setupShortcodes();
$this->setupHooks(); $this->setupHooks();
$this->setupImages(); $this->setupImages();
$this->setupLicense();
$this->setupCronTrigger(); $this->setupCronTrigger();
$this->plugin_initialized = true; $this->plugin_initialized = true;
@ -211,9 +210,4 @@ class Initializer {
function handleFailedInitialization($message) { function handleFailedInitialization($message) {
return WPNotice::displayError($message); return WPNotice::displayError($message);
} }
function setupLicense() {
$license = new License();
$license->init();
}
} }

View File

@ -14,7 +14,7 @@ use MailPoet\Settings\Hosts;
use MailPoet\Settings\Pages; use MailPoet\Settings\Pages;
use MailPoet\Subscribers\ImportExport\ImportExportFactory; use MailPoet\Subscribers\ImportExport\ImportExportFactory;
use MailPoet\Listing; use MailPoet\Listing;
use MailPoet\Util\License\License as License; use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\WP\DateTime; use MailPoet\WP\DateTime;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@ -23,6 +23,8 @@ class Menu {
function __construct($renderer, $assets_url) { function __construct($renderer, $assets_url) {
$this->renderer = $renderer; $this->renderer = $renderer;
$this->assets_url = $assets_url; $this->assets_url = $assets_url;
$subscribers_feature = new SubscribersFeature();
$this->subscribers_over_limit = $subscribers_feature->check();
} }
function init() { function init() {
@ -243,7 +245,7 @@ class Menu {
} }
function settings() { function settings() {
do_action(License::CHECK_PERMISSION, 'Subscribers'); if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate();
$settings = Setting::getAll(); $settings = Setting::getAll();
$flags = $this->_getFlags(); $flags = $this->_getFlags();
@ -317,7 +319,7 @@ class Menu {
} }
function segments() { function segments() {
do_action(License::CHECK_PERMISSION, 'Subscribers'); if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate();
$data = array(); $data = array();
$data['items_per_page'] = $this->getLimitPerPage('segments'); $data['items_per_page'] = $this->getLimitPerPage('segments');
@ -325,7 +327,7 @@ class Menu {
} }
function forms() { function forms() {
do_action(License::CHECK_PERMISSION, 'Subscribers'); if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate();
$data = array(); $data = array();
@ -336,7 +338,7 @@ class Menu {
} }
function newsletters() { function newsletters() {
do_action(License::CHECK_PERMISSION, 'Subscribers'); if ($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate();
global $wp_roles; global $wp_roles;
@ -438,4 +440,11 @@ class Menu {
? (int)$listing_per_page ? (int)$listing_per_page
: Listing\Handler::DEFAULT_LIMIT_PER_PAGE; : Listing\Handler::DEFAULT_LIMIT_PER_PAGE;
} }
function displaySubscriberLimitExceededTemplate() {
echo $this->renderer->render('limit.html', array(
'limit' => SubscribersFeature::SUBSCRIBERS_LIMIT
));
exit;
}
} }

View File

@ -1,29 +1,19 @@
<?php <?php
namespace MailPoet\Util\License\Features; namespace MailPoet\Util\License\Features;
use MailPoet\Config\Renderer;
use MailPoet\Models\Subscriber as SubscriberModel; use MailPoet\Models\Subscriber as SubscriberModel;
use MailPoet\Util\License\License; use MailPoet\Util\License\License;
class Subscribers { class Subscribers {
static $subscribers_limit = 2000; public $license;
const SUBSCRIBERS_LIMIT = 2000;
function check($subscribers_limit = false) { function __construct($license = false) {
$subscribers_limit = ($subscribers_limit !== false) ? $this->license = ($license) ? $license : License::getLicense();
$subscribers_limit :
self::$subscribers_limit;
if(!License::getLicense() &&
SubscriberModel::getTotalSubscribers() > $subscribers_limit
) {
$renderer = new Renderer();
echo $renderer->init()->render('limit.html', array(
'limit' => $subscribers_limit
));
return $this->terminateRequest();
}
} }
function terminateRequest() { function check($subscribers_limit = self::SUBSCRIBERS_LIMIT) {
exit; if($this->license) return false;
return SubscriberModel::getTotalSubscribers() > $subscribers_limit;
} }
} }

View File

@ -2,24 +2,12 @@
namespace MailPoet\Util\License; namespace MailPoet\Util\License;
class License { class License {
const CHECK_PERMISSION = 'mailpoet_premium_feature_permission'; static function getLicense($license = false) {
const FEATURE_NAMESPACE = 'MailPoet\Util\License\Features\\'; if(!$license) {
$license = defined('MAILPOET_PREMIUM_LICENSE') ?
function init() {
add_action(self::CHECK_PERMISSION, array($this, 'checkFeaturePermission'));
}
static function getLicense() {
return (defined('MAILPOET_PREMIUM_LICENSE')) ?
MAILPOET_PREMIUM_LICENSE : MAILPOET_PREMIUM_LICENSE :
false; false;
} }
return $license;
function checkFeaturePermission($feature) {
$feature = self::FEATURE_NAMESPACE . $feature;
if(class_exists($feature)) {
$feature = new $feature();
$feature->check();
}
} }
} }

View File

@ -6,24 +6,26 @@ use MailPoet\Models\Subscriber;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
class SubscribersFeaturesTest extends MailPoetTest { class SubscribersFeaturesTest extends MailPoetTest {
function testItEnforcesSubscriberLimitWithoutPremiumLicense() { function testChecksIfSubscribersWithinLimitWhenPremiumLicenseDoesNotExist() {
$subscribers_feature = Stub::make(new SubscribersFeature(), array( $subscribers_feature = new SubscribersFeature();
'terminateRequest' => function() { return true; } expect($subscribers_feature->check(0))->false();
), $this);
expect($subscribers_feature->check(0))->null();
$subscriber = Subscriber::create(); $subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template')); $subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->save(); $subscriber->save();
expect($subscribers_feature->check(0))->true(); expect($subscribers_feature->check(0))->true();
} }
function testItDoesNotEnforceSubscriberLimitWithPremiumLicense() { function testChecksIfSubscribersWithinLimitWhenPremiumLicenseExists() {
define('MAILPOET_PREMIUM_LICENSE', true); $subscribers_feature = Stub::construct(
$subscribers_feature = new SubscribersFeature(); new SubscribersFeature(),
array(
'license' => true
)
);
$subscriber = Subscriber::create(); $subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template')); $subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->save(); $subscriber->save();
expect($subscribers_feature->check(0))->null(); expect($subscribers_feature->check(0))->false();
} }
function _after() { function _after() {

View File

@ -4,21 +4,7 @@ use MailPoet\Util\License\License;
class LicenseTest extends MailPoetTest { class LicenseTest extends MailPoetTest {
function testItGetsLicense() { function testItGetsLicense() {
if(!defined('MAILPOET_PREMIUM_LICENSE')) {
expect(License::getLicense())->false(); expect(License::getLicense())->false();
define('MAILPOET_PREMIUM_LICENSE', 'valid'); expect(License::getLicense('valid'))->equals('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();
} }
} }