diff --git a/lib/Config/Env.php b/lib/Config/Env.php index 8784fb7aa8..ef61f0b54e 100644 --- a/lib/Config/Env.php +++ b/lib/Config/Env.php @@ -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, ' '', '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); diff --git a/lib/Config/RequirementsChecker.php b/lib/Config/RequirementsChecker.php new file mode 100644 index 0000000000..05f59ea3f8 --- /dev/null +++ b/lib/Config/RequirementsChecker.php @@ -0,0 +1,76 @@ +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, '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; + } +} \ No newline at end of file diff --git a/tests/unit/Config/EnvTest.php b/tests/unit/Config/EnvTest.php index ffa20f2c8d..6b34e3f6c8 100644 --- a/tests/unit/Config/EnvTest.php +++ b/tests/unit/Config/EnvTest.php @@ -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('true(); - expect(file_get_contents(Env::$cache_path . '/index.php'))->contains('