- Adds new Requirements Checker class

- Updates Initializer to check requirements
This commit is contained in:
Vlad
2016-11-16 21:44:36 -05:00
parent 8fb29e2418
commit 7d15c391ea
4 changed files with 84 additions and 33 deletions

View File

@@ -39,12 +39,8 @@ class Env {
self::$assets_path = self::$path . '/assets';
self::$assets_url = plugins_url('/assets', $file);
$wp_upload_dir = wp_upload_dir();
self::$temp_path = self::intializePath(
$wp_upload_dir['basedir'] . '/' . self::$plugin_name
);
self::$cache_path = self::intializePath(
self::$temp_path . '/cache'
);
self::$temp_path = $wp_upload_dir['basedir'] . '/' . self::$plugin_name;
self::$cache_path = self::$temp_path . '/cache';
self::$temp_url = $wp_upload_dir['baseurl'] . '/' . self::$plugin_name;
self::$languages_path = self::$path . '/lang';
self::$lib_path = self::$path . '/lib';
@@ -68,20 +64,6 @@ class Env {
self::$db_timezone_offset = self::getDbTimezoneOffset();
}
static function intializePath($path) {
if(!is_dir($path)) {
@mkdir($path);
if(!is_dir($path)) {
throw new \Exception(__("Failed to create a temporary folder inside the WordPress's uploads folder."));
}
file_put_contents(
$path . '/index.php',
str_replace('\n', PHP_EOL, '<?php\n\n// Silence is golden')
);
}
return $path;
}
private static function dbSourceName($host, $socket, $port) {
$source_name = array(
(!$socket) ? 'mysql:host=' : 'mysql:unix_socket=',

View File

@@ -18,14 +18,11 @@ class Initializer {
'file' => '',
'version' => '1.0.0'
)) {
try {
Env::init($params['file'], $params['version']);
} catch(\Exception $e) {
$this->handleFailedInitialization($e->getMessage());
}
}
function init() {
$this->checkRequirements();
$this->setupDB();
register_activation_hook(Env::$file, array($this, 'runMigrator'));
@@ -36,6 +33,11 @@ class Initializer {
add_action('widgets_init', array($this, 'setupWidget'));
}
function checkRequirements() {
$requrements = new RequirementsChecker();
$requrements->check();
}
function setupDB() {
\ORM::configure(Env::$db_source_name);
\ORM::configure('username', Env::$db_username);

View File

@@ -0,0 +1,76 @@
<?php
namespace MailPoet\Config;
use MailPoet\WP\Notice as WPNotice;
if(!defined('ABSPATH')) exit;
class RequirementsChecker {
function check() {
$available_tests = array(
'PHPVersion',
'WritableTempAndCacheFolders',
'PDOExtension',
'MbstringExtension'
);
$test_results = array();
foreach($available_tests as $test) {
$test_results[$test] = call_user_func(array($this, 'check' . $test));
}
return $test_results;
}
function checkPHPVersion() {
if(version_compare(phpversion(), '5.3.0', '<')) {
return $this->displayError(
__('This plugin requires PHP version 5.3 or newer.', 'mailpoet')
);
}
return true;
}
function checkWritableTempAndCacheFolders() {
$paths = array(
'temp_path' => Env::$temp_path,
'cache_path' => Env::$cache_path
);
if(!is_dir($paths['cache_path']) && !wp_mkdir_p($paths['cache_path'])) {
return $this->displayError(
__('This plugin requires read/write permissions inside a WordPress uploads folder.', 'mailpoet')
);
}
foreach($paths as $path) {
$index_file = $path . '/index.php';
if(!file_exists($index_file)) {
file_put_contents(
$path . '/index.php',
str_replace('\n', PHP_EOL, '<?php\n\n// Silence is golden')
);
}
}
return true;
}
function checkPDOExtension() {
if(!extension_loaded('pdo') && !extension_loaded('pdo_mysql')) {
$this->displayError(
__('This plugin requires PDO and PDO_MYSQL PHP extensions.', 'mailpoet')
);
}
return true;
}
function checkMbstringExtension() {
if(!extension_loaded('mbstring')) {
return $this->displayError(
__('This plugin requires mbstring PHP extension.', 'mailpoet')
);
}
return true;
}
function displayError($error) {
WPNotice::displayError($error);
return false;
}
}

View File

@@ -54,15 +54,6 @@ class EnvTest extends MailPoetTest {
expect(Env::$db_charset)->equals($charset);
}
function testItCanInitializeTempAndCacheFolders() {
// temp and cache folders should exist and contain index.php
expect(is_dir(Env::$temp_path))->true();
expect(file_exists(Env::$temp_path . '/index.php'))->true();
expect(file_get_contents(Env::$temp_path . '/index.php'))->contains('<?php');
expect(is_dir(Env::$cache_path))->true();
expect(file_get_contents(Env::$cache_path . '/index.php'))->contains('<?php');
}
function testItCanGenerateDbSourceName() {
$source_name = ((!ENV::$db_socket) ? 'mysql:host=' : 'mysql:unix_socket=') .
ENV::$db_host . ';port=' . ENV::$db_port . ';dbname=' . DB_NAME;