- 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->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();
}
}

View File

@ -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;
}
}

View File

@ -1,29 +1,19 @@
<?php
namespace MailPoet\Util\License\Features;
use MailPoet\Config\Renderer;
use MailPoet\Models\Subscriber as SubscriberModel;
use MailPoet\Util\License\License;
class Subscribers {
static $subscribers_limit = 2000;
public $license;
const SUBSCRIBERS_LIMIT = 2000;
function check($subscribers_limit = false) {
$subscribers_limit = ($subscribers_limit !== false) ?
$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 __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;
}
}

View File

@ -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;
}
}

View File

@ -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() {

View File

@ -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');
}
}