Show a notice to admin if network activation
Mailpoet seems to be working on network activated multisites but we don't support it. [MAILPOET-923]
This commit is contained in:
36
lib/Config/DeferredAdminNotices.php
Normal file
36
lib/Config/DeferredAdminNotices.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\WP\Notice;
|
||||
|
||||
class DeferredAdminNotices {
|
||||
|
||||
const OPTIONS_KEY_NAME = 'mailpoet_deferred_admin_notices';
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function addNetworkAdminNotice($message) {
|
||||
$notices = get_option(DeferredAdminNotices::OPTIONS_KEY_NAME, array());
|
||||
$notices[] = array(
|
||||
"message" => $message,
|
||||
"networkAdmin" => true,// if we'll need to display the notice to anyone else
|
||||
);
|
||||
update_option(DeferredAdminNotices::OPTIONS_KEY_NAME, $notices);
|
||||
}
|
||||
|
||||
public function flushAll() {
|
||||
$notices = get_option(DeferredAdminNotices::OPTIONS_KEY_NAME, array());
|
||||
|
||||
foreach($notices as $notice) {
|
||||
$notice = new Notice(Notice::TYPE_WARNING, $notice["message"]);
|
||||
add_action('network_admin_notices', array($notice, 'displayWPNotice'));
|
||||
}
|
||||
|
||||
if(!empty($notices)) {
|
||||
delete_option(DeferredAdminNotices::OPTIONS_KEY_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -25,7 +25,6 @@ class Initializer {
|
||||
function init() {
|
||||
$requirements_check_results = $this->checkRequirements();
|
||||
|
||||
// abort initialization if PDO extension is missing
|
||||
if(!$requirements_check_results[RequirementsChecker::TEST_PDO_EXTENSION] ||
|
||||
!$requirements_check_results[RequirementsChecker::TEST_VENDOR_SOURCE]
|
||||
) {
|
||||
@ -43,6 +42,16 @@ class Initializer {
|
||||
)
|
||||
);
|
||||
|
||||
add_action('activated_plugin', array(
|
||||
new PluginActivatedHook(new DeferredAdminNotices),
|
||||
'action'
|
||||
), 10, 2);
|
||||
|
||||
add_action('admin_init', array(
|
||||
new DeferredAdminNotices,
|
||||
'flushAll'
|
||||
));
|
||||
|
||||
add_action('plugins_loaded', array(
|
||||
$this,
|
||||
'setup'
|
||||
@ -62,8 +71,8 @@ class Initializer {
|
||||
}
|
||||
|
||||
function checkRequirements() {
|
||||
$requrements = new RequirementsChecker();
|
||||
return $requrements->checkAllRequirements();
|
||||
$requirements = new RequirementsChecker();
|
||||
return $requirements->checkAllRequirements();
|
||||
}
|
||||
|
||||
function setupDB() {
|
||||
|
20
lib/Config/PluginActivatedHook.php
Normal file
20
lib/Config/PluginActivatedHook.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
class PluginActivatedHook {
|
||||
|
||||
/** @var DeferredAdminNotices */
|
||||
private $deferredAdminNotices;
|
||||
|
||||
public function __construct(DeferredAdminNotices $deferredAdminNotices) {
|
||||
$this->deferredAdminNotices = $deferredAdminNotices;
|
||||
}
|
||||
|
||||
public function action($plugin, $networkWide) {
|
||||
if($networkWide) {
|
||||
$this->deferredAdminNotices->addNetworkAdminNotice(__('We noticed that you\'re using an unsupported environment. While MailPoet might work within a MultiSite environment, we don’t support it.', 'mailpoet'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
35
tests/unit/Config/PluginActivatedHookTest.php
Normal file
35
tests/unit/Config/PluginActivatedHookTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use Codeception\Util\Stub;
|
||||
|
||||
class PluginActivatedHookTest extends \MailPoetTest {
|
||||
|
||||
|
||||
public function testItAddsANewMessageIfNetworkActivation() {
|
||||
$deferredAdminNotices = Stub::makeEmpty(
|
||||
'MailPoet\Config\DeferredAdminNotices',
|
||||
array(
|
||||
'addNetworkAdminNotice' => Stub::exactly(1, function () {
|
||||
}),
|
||||
),
|
||||
$this
|
||||
);
|
||||
$hook = new PluginActivatedHook($deferredAdminNotices);
|
||||
$hook->action("mailpoet", true);
|
||||
}
|
||||
|
||||
public function testItDoesntAddsAMessageIfNoNetworkActivation() {
|
||||
$deferredAdminNotices = Stub::makeEmpty(
|
||||
'MailPoet\Config\DeferredAdminNotices',
|
||||
array(
|
||||
'addNetworkAdminNotice' => Stub::never(),
|
||||
),
|
||||
$this
|
||||
);
|
||||
$hook = new PluginActivatedHook($deferredAdminNotices);
|
||||
$hook->action("mailpoet", false);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user