Breaks plugin initialization into 3 stages: preInit, init and postInit

This commit is contained in:
Vlad
2017-09-11 11:33:03 -04:00
parent dff9c1012b
commit 2ac7d1d4ab

View File

@ -45,9 +45,6 @@ class Initializer {
));
}
// configure renderer
$this->setupRenderer();
// activation function
register_activation_hook(
Env::$file,
@ -64,17 +61,17 @@ class Initializer {
add_action('init', array(
$this,
'onInit'
));
'preInitialize'
), 0);
add_action('widgets_init', array(
add_action('init', array(
$this,
'setupWidget'
'initialize'
));
add_action('wp_loaded', array(
$this,
'setupHooks'
'postInitialize'
));
add_action('admin_init', array(
@ -98,7 +95,27 @@ class Initializer {
$database->init();
}
function onInit() {
function preInitialize() {
try {
$this->setupRenderer();
$this->setupWidget();
} catch(\Exception $e) {
$this->handleFailedInitialization($e);
}
}
function setupRenderer() {
$caching = !WP_DEBUG;
$debugging = WP_DEBUG;
$this->renderer = new Renderer($caching, $debugging);
}
function setupWidget() {
$widget = new Widget($this->renderer);
$widget->init();
}
function initialize() {
try {
$this->setupAccessControl();
@ -115,8 +132,6 @@ class Initializer {
$this->setupCronTrigger();
$this->setupConflictResolver();
$this->setupJSONAPI();
$this->setupRouter();
$this->setupPages();
do_action('mailpoet_initialized', MAILPOET_VERSION);
@ -164,17 +179,6 @@ class Initializer {
$updater->init();
}
function setupRenderer() {
$caching = !WP_DEBUG;
$debugging = WP_DEBUG;
$this->renderer = new Renderer($caching, $debugging);
}
function setupWidget() {
$widget = new Widget($this->renderer);
$widget->init();
}
function setupLocalizer() {
$localizer = new Localizer();
$localizer->init();
@ -212,6 +216,17 @@ class Initializer {
$conflict_resolver->init();
}
function postInitialize() {
try {
if(!defined(self::INITIALIZED)) return;
$this->setupHooks();
$this->setupJSONAPI();
$this->setupRouter();
} catch(\Exception $e) {
$this->handleFailedInitialization($e);
}
}
function setupJSONAPI() {
$json_api = API\API::JSON($this->access_control);
$json_api->init();
@ -228,13 +243,8 @@ class Initializer {
}
function setupHooks() {
if(!defined(self::INITIALIZED)) return;
try {
$hooks = new Hooks();
$hooks->init();
} catch(\Exception $e) {
$this->handleFailedInitialization($e);
}
$hooks = new Hooks();
$hooks->init();
}
function handleFailedInitialization($exception) {