Display PHP version warning via notice filter instead of stdout
This commit is contained in:
@@ -291,8 +291,7 @@ class Initializer {
|
||||
|
||||
function setupPHPVersionWarnings() {
|
||||
$php_version_warnings = new PHPVersionWarnings();
|
||||
$warnings = $php_version_warnings->init(phpversion(), Menu::isOnMailPoetAdminPage());
|
||||
if(is_string($warnings)) echo $warnings;
|
||||
$php_version_warnings->init(phpversion(), Menu::isOnMailPoetAdminPage());
|
||||
}
|
||||
|
||||
function handleFailedInitialization($exception) {
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\WP\Notice as WPNotice;
|
||||
|
||||
class PHPVersionWarnings {
|
||||
|
||||
@@ -13,22 +14,24 @@ class PHPVersionWarnings {
|
||||
$this,
|
||||
'ajaxDismissNoticeHandler'
|
||||
));
|
||||
$error = null;
|
||||
if (!$is_enabled) return $error;
|
||||
if (is_null($error)) $error = $this->checkPHP70Version($php_version);
|
||||
return $error;
|
||||
|
||||
if ($is_enabled && $this->isOutdatedPHPVersion($php_version)) {
|
||||
return $this->displayError($php_version);
|
||||
}
|
||||
}
|
||||
|
||||
function checkPHP70Version($php_version) {
|
||||
$error_string = null;
|
||||
if(version_compare($php_version, '7.0', '<') && !get_transient('dismissed-php-version-outdated-notice')) {
|
||||
function isOutdatedPHPVersion($php_version) {
|
||||
return version_compare($php_version, '7.0', '<') && !get_transient('dismissed-php-version-outdated-notice');
|
||||
}
|
||||
|
||||
function displayError($php_version) {
|
||||
$error_string = __('Your website is running on PHP %s. MailPoet will require version 7 by the end of the year. Please consider upgrading your site\'s PHP version. [link]Your host can help you.[/link]', 'mailpoet');
|
||||
$error_string = sprintf($error_string, $php_version);
|
||||
$error = Helpers::replaceLinkTags($error_string, 'https://beta.docs.mailpoet.com/article/251-upgrading-the-websites-php-version', array('target' => '_blank'));
|
||||
$class = 'notice notice-error notice-php-warning mailpoet_notice_server is-dismissible';
|
||||
$classes = 'notice-php-warning is-dismissible';
|
||||
$transient = 'php-version-outdated';
|
||||
|
||||
return sprintf('<div class="%1$s" data-notice="php-version-outdated"><p>%2$s</p></div>', $class, $error);
|
||||
}
|
||||
return WPNotice::displayError($error, $classes, $transient);
|
||||
}
|
||||
|
||||
function ajaxDismissNoticeHandler() {
|
||||
|
@@ -11,41 +11,44 @@ class Notice {
|
||||
private $type;
|
||||
private $message;
|
||||
|
||||
function __construct($type, $message) {
|
||||
function __construct($type, $message, $classes = '', $transient = '') {
|
||||
$this->type = $type;
|
||||
$this->message = $message;
|
||||
$this->classes = $classes;
|
||||
$this->transient = $transient;
|
||||
}
|
||||
|
||||
static function displayError($message) {
|
||||
static function displayError($message, $classes = '', $transient = '') {
|
||||
$message = sprintf(
|
||||
"<b>%s </b> %s",
|
||||
__('MailPoet Error:', 'mailpoet'),
|
||||
$message
|
||||
);
|
||||
self::createNotice(self::TYPE_ERROR, $message);
|
||||
self::createNotice(self::TYPE_ERROR, $message, $classes, $transient);
|
||||
}
|
||||
|
||||
static function displayWarning($message) {
|
||||
self::createNotice(self::TYPE_WARNING, $message);
|
||||
static function displayWarning($message, $classes = '', $transient = '') {
|
||||
self::createNotice(self::TYPE_WARNING, $message, $classes, $transient);
|
||||
}
|
||||
|
||||
static function displaySuccess($message) {
|
||||
self::createNotice(self::TYPE_SUCCESS, $message);
|
||||
static function displaySuccess($message, $classes = '', $transient = '') {
|
||||
self::createNotice(self::TYPE_SUCCESS, $message, $classes, $transient);
|
||||
}
|
||||
|
||||
static function displayInfo($message) {
|
||||
self::createNotice(self::TYPE_INFO, $message);
|
||||
static function displayInfo($message, $classes = '', $transient = '') {
|
||||
self::createNotice(self::TYPE_INFO, $message, $classes, $transient);
|
||||
}
|
||||
|
||||
protected static function createNotice($type, $message) {
|
||||
$notice = new Notice($type, $message);
|
||||
protected static function createNotice($type, $message, $classes, $transient) {
|
||||
$notice = new Notice($type, $message, $classes, $transient);
|
||||
add_action('admin_notices', array($notice, 'displayWPNotice'));
|
||||
}
|
||||
|
||||
function displayWPNotice() {
|
||||
$class = sprintf('notice notice-%s mailpoet_notice_server', $this->type);
|
||||
$class = sprintf('notice notice-%s mailpoet_notice_server %s', $this->type, $this->classes);
|
||||
$message = nl2br($this->message);
|
||||
$transient = !empty($this->transient) ? sprintf('data-notice="%s"', $this->transient) : '';
|
||||
|
||||
printf('<div class="%1$s"><p>%2$s</p></div>', $class, $message);
|
||||
printf('<div class="%1$s" %3$s><p>%2$s</p></div>', $class, $message, $transient);
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use AspectMock\Test as Mock;
|
||||
|
||||
class PHPVersionWarningsTest extends \MailPoetTest {
|
||||
|
||||
/** @var PHPVersionWarnings */
|
||||
@@ -14,17 +16,42 @@ class PHPVersionWarningsTest extends \MailPoetTest {
|
||||
|
||||
function _after() {
|
||||
delete_transient('dismissed-php-version-outdated-notice');
|
||||
Mock::clean();
|
||||
}
|
||||
|
||||
function testPHP55IsOutdated() {
|
||||
expect($this->phpVersionWarning->isOutdatedPHPVersion('5.5.3'))->true();
|
||||
}
|
||||
|
||||
function testPHP56IsOutdated() {
|
||||
expect($this->phpVersionWarning->isOutdatedPHPVersion('5.5.3'))->true();
|
||||
}
|
||||
|
||||
function testPHP72IsNotOutdated() {
|
||||
expect($this->phpVersionWarning->isOutdatedPHPVersion('7.2'))->false();
|
||||
}
|
||||
|
||||
function testItPrintsWarningFor55() {
|
||||
$mock = Mock::double('MailPoet\WP\Notice', [
|
||||
'displayError' => function($message, $classes, $transient) {
|
||||
return $message;
|
||||
}
|
||||
]);
|
||||
$warning = $this->phpVersionWarning->init('5.5.3', true);
|
||||
$mock->verifyInvoked('displayError');
|
||||
expect($warning)->contains('Your website is running on PHP 5.5.3');
|
||||
expect($warning)->contains('MailPoet will require version 7');
|
||||
}
|
||||
|
||||
|
||||
function testItPrintsWarningFor56() {
|
||||
$mock = Mock::double('MailPoet\WP\Notice', [
|
||||
'displayError' => function($message, $classes, $transient) {
|
||||
return $message;
|
||||
}
|
||||
]);
|
||||
$warning = $this->phpVersionWarning->init('5.6.3', true);
|
||||
$mock->verifyInvoked('displayError');
|
||||
expect($warning)->contains('Your website is running on PHP 5.6');
|
||||
expect($warning)->contains('MailPoet will require version 7');
|
||||
}
|
||||
|
Reference in New Issue
Block a user