diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 8d9a31ae57..eecc1565c6 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -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) { diff --git a/lib/Config/PHPVersionWarnings.php b/lib/Config/PHPVersionWarnings.php index 79c2fce87e..cd6a942b17 100644 --- a/lib/Config/PHPVersionWarnings.php +++ b/lib/Config/PHPVersionWarnings.php @@ -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')) { - $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'; + function isOutdatedPHPVersion($php_version) { + return version_compare($php_version, '7.0', '<') && !get_transient('dismissed-php-version-outdated-notice'); + } - return sprintf('

%2$s

', $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() { diff --git a/lib/WP/Notice.php b/lib/WP/Notice.php index 6dc3791637..12cb77692e 100644 --- a/lib/WP/Notice.php +++ b/lib/WP/Notice.php @@ -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( "%s %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('

%2$s

', $class, $message); + printf('

%2$s

', $class, $message, $transient); } } diff --git a/tests/unit/Config/PHPVersionWarningsTest.php b/tests/unit/Config/PHPVersionWarningsTest.php index d36f90cc9d..eb272604db 100644 --- a/tests/unit/Config/PHPVersionWarningsTest.php +++ b/tests/unit/Config/PHPVersionWarningsTest.php @@ -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'); }