Display PHP version warning via notice filter instead of stdout

This commit is contained in:
Tautvidas Sipavičius
2018-08-23 15:06:21 +03:00
parent ac73b4ee0a
commit c68f58a7c0
4 changed files with 60 additions and 28 deletions

View File

@@ -291,8 +291,7 @@ class Initializer {
function setupPHPVersionWarnings() { function setupPHPVersionWarnings() {
$php_version_warnings = new PHPVersionWarnings(); $php_version_warnings = new PHPVersionWarnings();
$warnings = $php_version_warnings->init(phpversion(), Menu::isOnMailPoetAdminPage()); $php_version_warnings->init(phpversion(), Menu::isOnMailPoetAdminPage());
if(is_string($warnings)) echo $warnings;
} }
function handleFailedInitialization($exception) { function handleFailedInitialization($exception) {

View File

@@ -3,6 +3,7 @@
namespace MailPoet\Config; namespace MailPoet\Config;
use MailPoet\Util\Helpers; use MailPoet\Util\Helpers;
use MailPoet\WP\Notice as WPNotice;
class PHPVersionWarnings { class PHPVersionWarnings {
@@ -13,22 +14,24 @@ class PHPVersionWarnings {
$this, $this,
'ajaxDismissNoticeHandler' 'ajaxDismissNoticeHandler'
)); ));
$error = null;
if (!$is_enabled) return $error; if ($is_enabled && $this->isOutdatedPHPVersion($php_version)) {
if (is_null($error)) $error = $this->checkPHP70Version($php_version); return $this->displayError($php_version);
return $error; }
} }
function checkPHP70Version($php_version) { function isOutdatedPHPVersion($php_version) {
$error_string = null; return version_compare($php_version, '7.0', '<') && !get_transient('dismissed-php-version-outdated-notice');
if(version_compare($php_version, '7.0', '<') && !get_transient('dismissed-php-version-outdated-notice')) { }
$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';
return sprintf('<div class="%1$s" data-notice="php-version-outdated"><p>%2$s</p></div>', $class, $error); 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'));
$classes = 'notice-php-warning is-dismissible';
$transient = 'php-version-outdated';
return WPNotice::displayError($error, $classes, $transient);
} }
function ajaxDismissNoticeHandler() { function ajaxDismissNoticeHandler() {

View File

@@ -11,41 +11,44 @@ class Notice {
private $type; private $type;
private $message; private $message;
function __construct($type, $message) { function __construct($type, $message, $classes = '', $transient = '') {
$this->type = $type; $this->type = $type;
$this->message = $message; $this->message = $message;
$this->classes = $classes;
$this->transient = $transient;
} }
static function displayError($message) { static function displayError($message, $classes = '', $transient = '') {
$message = sprintf( $message = sprintf(
"<b>%s </b> %s", "<b>%s </b> %s",
__('MailPoet Error:', 'mailpoet'), __('MailPoet Error:', 'mailpoet'),
$message $message
); );
self::createNotice(self::TYPE_ERROR, $message); self::createNotice(self::TYPE_ERROR, $message, $classes, $transient);
} }
static function displayWarning($message) { static function displayWarning($message, $classes = '', $transient = '') {
self::createNotice(self::TYPE_WARNING, $message); self::createNotice(self::TYPE_WARNING, $message, $classes, $transient);
} }
static function displaySuccess($message) { static function displaySuccess($message, $classes = '', $transient = '') {
self::createNotice(self::TYPE_SUCCESS, $message); self::createNotice(self::TYPE_SUCCESS, $message, $classes, $transient);
} }
static function displayInfo($message) { static function displayInfo($message, $classes = '', $transient = '') {
self::createNotice(self::TYPE_INFO, $message); self::createNotice(self::TYPE_INFO, $message, $classes, $transient);
} }
protected static function createNotice($type, $message) { protected static function createNotice($type, $message, $classes, $transient) {
$notice = new Notice($type, $message); $notice = new Notice($type, $message, $classes, $transient);
add_action('admin_notices', array($notice, 'displayWPNotice')); add_action('admin_notices', array($notice, 'displayWPNotice'));
} }
function 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); $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);
} }
} }

View File

@@ -2,6 +2,8 @@
namespace MailPoet\Config; namespace MailPoet\Config;
use AspectMock\Test as Mock;
class PHPVersionWarningsTest extends \MailPoetTest { class PHPVersionWarningsTest extends \MailPoetTest {
/** @var PHPVersionWarnings */ /** @var PHPVersionWarnings */
@@ -14,17 +16,42 @@ class PHPVersionWarningsTest extends \MailPoetTest {
function _after() { function _after() {
delete_transient('dismissed-php-version-outdated-notice'); 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() { function testItPrintsWarningFor55() {
$mock = Mock::double('MailPoet\WP\Notice', [
'displayError' => function($message, $classes, $transient) {
return $message;
}
]);
$warning = $this->phpVersionWarning->init('5.5.3', true); $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('Your website is running on PHP 5.5.3');
expect($warning)->contains('MailPoet will require version 7'); expect($warning)->contains('MailPoet will require version 7');
} }
function testItPrintsWarningFor56() { function testItPrintsWarningFor56() {
$mock = Mock::double('MailPoet\WP\Notice', [
'displayError' => function($message, $classes, $transient) {
return $message;
}
]);
$warning = $this->phpVersionWarning->init('5.6.3', true); $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('Your website is running on PHP 5.6');
expect($warning)->contains('MailPoet will require version 7'); expect($warning)->contains('MailPoet will require version 7');
} }