Move old versions code to own class

[MAILPOET-1316]
This commit is contained in:
Pavel Dohnal
2018-05-01 12:18:22 +01:00
parent ea47524cc4
commit df58322f0e
6 changed files with 133 additions and 20 deletions

View File

@ -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,
},
});
});
});

View File

@ -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')) {

View File

@ -0,0 +1,54 @@
<?php
namespace MailPoet\Config;
use MailPoet\Util\Helpers;
class PHPVersionWarnings {
function init($php_version, $is_enabled) {
add_action('wp_ajax_dismissed_notice_handler', array(
$this,
'ajaxDismissNoticeHandler'
));
$error = null;
if (!$is_enabled) return $error;
if (is_null($error)) $error = $this->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('<div class="%1$s" data-notice="php-version-outdated"><p>%2$s</p></div>', $class, $message);
}
}
function ajaxDismissNoticeHandler() {
update_option('dismissed-php-version-outdated-notice', true);
}
}

View File

@ -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);

View File

@ -0,0 +1,54 @@
<?php
namespace MailPoet\Config;
class PHPVersionWarningsTest extends \MailPoetTest {
/** @var PHPVersionWarnings */
private $phpVersionWarning;
function _before() {
$this->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();
}
}

View File

@ -195,6 +195,7 @@ var adminConfig = {
'analytics_event',
'help-tooltip.jsx',
'help-tooltip',
'notice-php-warning.jsx',
],
admin_vendor: [
'react',