diff --git a/assets/js/src/notice-php-warning.jsx b/assets/js/src/notice-php-warning.jsx new file mode 100644 index 0000000000..8e95a41761 --- /dev/null +++ b/assets/js/src/notice-php-warning.jsx @@ -0,0 +1,15 @@ +import jQuery from 'jquery'; + +jQuery(($) => { + $(document).on('click', '.notice-php-warning .notice-dismiss', function xyz() { + const type = $(this).closest('.notice-php-warning').data('notice'); + $.ajax(window.ajaxurl, + { + type: 'POST', + data: { + action: 'dismissed_notice_handler', + type, + }, + }); + }); +}); diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index bab47f0e8c..4e62140558 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -138,6 +138,8 @@ class Initializer { $this->setupPages(); + $this->setupPHPVersionWarnings(); + do_action('mailpoet_initialized', MAILPOET_VERSION); } catch(\Exception $e) { return $this->handleFailedInitialization($e); @@ -270,6 +272,12 @@ class Initializer { $exporters->init(); } + function setupPHPVersionWarnings() { + $php_version_warnings = new PHPVersionWarnings(); + $warnings = $php_version_warnings->init(phpversion(), Menu::isOnMailPoetAdminPage()); + if(is_string($warnings)) echo $warnings; + } + function handleFailedInitialization($exception) { // check if we are able to add pages at this point if(function_exists('wp_get_current_user')) { diff --git a/lib/Config/PHPVersionWarnings.php b/lib/Config/PHPVersionWarnings.php new file mode 100644 index 0000000000..5cb18c5b1b --- /dev/null +++ b/lib/Config/PHPVersionWarnings.php @@ -0,0 +1,54 @@ +checkPHP53Version($php_version); + if (is_null($error)) $error = $this->checkPHP55Version($php_version); + return $error; + } + + function checkPHP53Version($php_version) { + $error_string = null; + if(version_compare($php_version, '5.5', '<')) { + $error_string = 'Your website is running on PHP %s. MailPoet will require version 7 soon. Please consider upgrading your site\'s PHP version. [link]Your host can help you.[/link]'; + $error_string = sprintf($error_string, $php_version); + $error = Helpers::replaceLinkTags(__($error_string, 'mailpoet'), 'https://beta.docs.mailpoet.com/article/251-upgrading-the-websites-php-version', array('target' => '_blank')); + return $this->displayWPNotice($error, false); + } + } + + function checkPHP55Version($php_version) { + $error_string = null; + if(version_compare($php_version, '5.6', '<')) { + $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]'; + $error_string = sprintf($error_string, $php_version); + $error = Helpers::replaceLinkTags(__($error_string, 'mailpoet'), 'https://beta.docs.mailpoet.com/article/251-upgrading-the-websites-php-version', array('target' => '_blank')); + return $this->displayWPNotice($error, true); + } + } + + private function displayWPNotice($message, $dismisable = false) { + $class = 'notice notice-error notice-php-warning mailpoet_notice_server'; + if($dismisable) $class .= ' is-dismissible'; + + if(!get_option('dismissed-php-version-outdated-notice', false)) { + return sprintf('

%2$s

', $class, $message); + } + } + + function ajaxDismissNoticeHandler() { + update_option('dismissed-php-version-outdated-notice', true); + } + +} \ No newline at end of file diff --git a/lib/Config/RequirementsChecker.php b/lib/Config/RequirementsChecker.php index 0539ea43c7..72f0c56e5c 100644 --- a/lib/Config/RequirementsChecker.php +++ b/lib/Config/RequirementsChecker.php @@ -13,7 +13,6 @@ class RequirementsChecker { const TEST_XML_EXTENSION = 'XmlExtension'; const TEST_ZIP_EXTENSION = 'ZipExtension'; const TEST_VENDOR_SOURCE = 'VendorSource'; - const TEST_PHP_VERSION = 'PHPVersion'; const TWIG_SUPPORTED_VERSIONS = '1.26.0-1.34.4'; public $display_error_notice; @@ -50,8 +49,7 @@ class RequirementsChecker { self::TEST_MBSTRING_EXTENSION, self::TEST_XML_EXTENSION, self::TEST_ZIP_EXTENSION, - self::TEST_VENDOR_SOURCE, - self::TEST_PHP_VERSION, + self::TEST_VENDOR_SOURCE ); $results = array(); foreach($available_tests as $test) { @@ -122,23 +120,6 @@ class RequirementsChecker { return $this->processError($error); } - function checkPHPVersion() { - $php_version = phpversion(); - $error_string = null; - if(version_compare($php_version, '5.5', '<')) { - $error_string = 'Your website is running on PHP %s. MailPoet will require version 7 soon. Please consider upgrading your site\'s PHP version. [link]Your host can help you.[/link]'; - } - if(!is_null($error_string) && Menu::isOnMailPoetAdminPage()) { - $error_string = sprintf($error_string, $php_version); - $error = Helpers::replaceLinkTags( - __($error_string, 'mailpoet'), - 'https://beta.docs.mailpoet.com/article/251-upgrading-the-websites-php-version', - array('target' => '_blank') - ); - $this->processError($error); - } - } - function checkVendorSource() { foreach($this->vendor_classes as $dependency) { $dependency_path = $this->getDependencyPath($dependency); diff --git a/tests/unit/Config/PHPVersionWarningsTest.php b/tests/unit/Config/PHPVersionWarningsTest.php new file mode 100644 index 0000000000..864f7bc208 --- /dev/null +++ b/tests/unit/Config/PHPVersionWarningsTest.php @@ -0,0 +1,54 @@ +phpVersionWarning = new PHPVersionWarnings(); + update_option('dismissed-php-version-outdated-notice', false); + } + + function _after() { + update_option('dismissed-php-version-outdated-notice', false); + } + + function testItPrintsWarningFor53() { + $warning = $this->phpVersionWarning->init('5.3.2', true); + expect($warning)->contains('Your website is running on PHP 5.3.2'); + expect($warning)->notContains('is-dismissible'); + } + + function testItPrintsWarningFor54() { + $warning = $this->phpVersionWarning->init('5.4.1', true); + expect($warning)->contains('Your website is running on PHP 5.4.1'); + expect($warning)->notContains('is-dismissible'); + } + + function testItPrintsWarningFor55() { + $warning = $this->phpVersionWarning->init('5.5.3', true); + expect($warning)->contains('Your website is running on PHP 5.5.3'); + expect($warning)->contains('is-dismissible'); + } + + function testItPrintsNoWarningFor56() { + $warning = $this->phpVersionWarning->init('5.6.3', true); + expect($warning)->null(); + } + + function testItPrintsNoWarningWhenDisabled() { + $warning = $this->phpVersionWarning->init('5.3.2', false); + expect($warning)->null(); + } + + function testItPrintsNoWarningWhenDismised() { + $this->phpVersionWarning->init('5.3.2', true); + do_action('wp_ajax_dismissed_notice_handler'); + $warning = $this->phpVersionWarning->init('5.3.2', true); + expect($warning)->null(); + } + +} diff --git a/webpack.config.js b/webpack.config.js index 85a5bf5ff9..fa7b74c4b4 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -195,6 +195,7 @@ var adminConfig = { 'analytics_event', 'help-tooltip.jsx', 'help-tooltip', + 'notice-php-warning.jsx', ], admin_vendor: [ 'react',