diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 0bb91b9ef0..6cab71c1db 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -193,8 +193,16 @@ class Initializer { } function setupHooks() { - $hooks = new Hooks(); - $hooks->init(); + if(!$this->plugin_initialized) { + return; + } + + try { + $hooks = new Hooks(); + $hooks->init(); + } catch(\Exception $e) { + $this->handleFailedInitialization($e); + } } function setupJSONAPI() { @@ -224,6 +232,7 @@ class Initializer { } function handleFailedInitialization($message) { + Menu::addErrorPage(); return WPNotice::displayError($message); } } \ No newline at end of file diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 0c392db4f7..9f795b3e47 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -576,6 +576,34 @@ class Menu { return (stripos($screen_id, 'mailpoet-') !== false); } + /** + * This error page is used when the initialization is failed + * to display admin notices only + */ + static function addErrorPage() { + if(!self::isOnMailPoetAdminPage()) { + return false; + } + // Check if page already exists + if(get_plugin_page_hook($_REQUEST['page'], '') + || get_plugin_page_hook($_REQUEST['page'], 'mailpoet-newsletters') + ) { + return false; + } + add_submenu_page( + true, + 'MailPoet', + 'MailPoet', + Env::$required_permission, + $_REQUEST['page'], + array(__CLASS__, 'errorPageCallback') + ); + } + + static function errorPageCallback() { + // Used for displaying admin notices only + } + function checkMailPoetAPIKey(ServicesChecker $checker = null) { if(self::isOnMailPoetAdminPage()) { $show_notices = isset($_REQUEST['page'])