Fix init/hooks so that we properly initialize db/plugin

- Added activation hook in main file (mailpoet.php)
- auto deactivate plugin in case of fatal errors during init
This commit is contained in:
Jonathan Labreuille
2016-01-27 12:52:40 +01:00
parent 91981cc324
commit c60425afb2
4 changed files with 39 additions and 32 deletions

View File

@ -9,13 +9,6 @@ class Activator {
function __construct() { function __construct() {
} }
function init() {
register_activation_hook(
Env::$file,
array($this, 'activate')
);
}
function activate() { function activate() {
$migrator = new Migrator(); $migrator = new Migrator();
$migrator->up(); $migrator->up();

View File

@ -18,8 +18,8 @@ class Initializer {
} }
function init() { function init() {
try {
$this->setupDB(); $this->setupDB();
$this->setupActivator();
$this->setupRenderer(); $this->setupRenderer();
$this->setupLocalizer(); $this->setupLocalizer();
$this->setupMenu(); $this->setupMenu();
@ -34,6 +34,11 @@ class Initializer {
$this->setupHooks(); $this->setupHooks();
$this->setupPages(); $this->setupPages();
$this->setupImages(); $this->setupImages();
} catch(\Exception $e) {
// if anything goes wrong during init
// automatically deactivate the plugin
deactivate_plugins(Env::$file);
}
} }
function setupDB() { function setupDB() {
@ -81,9 +86,10 @@ class Initializer {
define('MP_NEWSLETTER_STATISTICS_TABLE', $newsletter_statistics); define('MP_NEWSLETTER_STATISTICS_TABLE', $newsletter_statistics);
} }
function setupActivator() { function runPopulator() {
$activator = new Activator(); $this->init();
$activator->init(); $populator = new Populator();
$populator->up();
} }
function setupRenderer() { function setupRenderer() {
@ -138,6 +144,7 @@ class Initializer {
$shortcodes = new Shortcodes(); $shortcodes = new Shortcodes();
$shortcodes->init(); $shortcodes->init();
} }
function setupHooks() { function setupHooks() {
$hooks = new Hooks(); $hooks = new Hooks();
$hooks->init(); $hooks->init();

View File

@ -1,5 +1,6 @@
<?php <?php
use \MailPoet\Config\Initializer; use \MailPoet\Config\Initializer;
use \MailPoet\Config\Migrator;
if (!defined('ABSPATH')) exit; if (!defined('ABSPATH')) exit;
@ -29,4 +30,10 @@ $initializer = new Initializer(array(
'file' => __FILE__, 'file' => __FILE__,
'version' => MAILPOET_VERSION 'version' => MAILPOET_VERSION
)); ));
$initializer->init();
$migrator = new Migrator();
register_activation_hook(__FILE__, array($migrator, 'up'));
register_activation_hook(__FILE__, array($initializer, 'runPopulator'));
add_action('init', array($initializer, 'init'));