| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace MailPoet\Config;
- if (!defined('ABSPATH')) exit;
- use MailPoet\API\JSON\API;
- use MailPoet\AutomaticEmails\AutomaticEmails;
- use MailPoet\Cron\CronTrigger;
- use MailPoet\InvalidStateException;
- use MailPoet\PostEditorBlocks\PostEditorBlock;
- use MailPoet\Router;
- use MailPoet\Settings\SettingsController;
- use MailPoet\Util\ConflictResolver;
- use MailPoet\Util\Helpers;
- use MailPoet\Util\Notices\PermanentNotices;
- use MailPoet\WooCommerce\Helper as WooCommerceHelper;
- use MailPoet\WooCommerce\TransactionalEmailHooks as WCTransactionalEmails;
- use MailPoet\WP\Functions as WPFunctions;
- use MailPoet\WP\Notice as WPNotice;
- class Initializer {
- public $automaticEmails;
- /** @var AccessControl */
- private $accessControl;
- /** @var Renderer */
- private $renderer;
- /** @var RendererFactory */
- private $rendererFactory;
- /** @var API */
- private $api;
- /** @var Activator */
- private $activator;
- /** @var SettingsController */
- private $settings;
- /** @var Router\Router */
- private $router;
- /** @var Hooks */
- private $hooks;
- /** @var Changelog */
- private $changelog;
- /** @var Menu */
- private $menu;
- /** @var CronTrigger */
- private $cronTrigger;
- /** @var PermanentNotices */
- private $permanentNotices;
- /** @var Shortcodes */
- private $shortcodes;
- /** @var DatabaseInitializer */
- private $databaseInitializer;
- /** @var WCTransactionalEmails */
- private $wcTransactionalEmails;
- /** @var WooCommerceHelper */
- private $wcHelper;
- /** @var \MailPoet\PostEditorBlocks\PostEditorBlock */
- private $postEditorBlock;
- /** @var Localizer */
- private $localizer;
- /** @var AssetsLoader */
- private $assetsLoader;
- const INITIALIZED = 'MAILPOET_INITIALIZED';
- public function __construct(
- RendererFactory $rendererFactory,
- AccessControl $accessControl,
- API $api,
- Activator $activator,
- SettingsController $settings,
- Router\Router $router,
- Hooks $hooks,
- Changelog $changelog,
- Menu $menu,
- CronTrigger $cronTrigger,
- PermanentNotices $permanentNotices,
- Shortcodes $shortcodes,
- DatabaseInitializer $databaseInitializer,
- WCTransactionalEmails $wcTransactionalEmails,
- PostEditorBlock $postEditorBlock,
- WooCommerceHelper $wcHelper,
- Localizer $localizer,
- AssetsLoader $assetsLoader
- ) {
- $this->rendererFactory = $rendererFactory;
- $this->accessControl = $accessControl;
- $this->api = $api;
- $this->activator = $activator;
- $this->settings = $settings;
- $this->router = $router;
- $this->hooks = $hooks;
- $this->changelog = $changelog;
- $this->menu = $menu;
- $this->cronTrigger = $cronTrigger;
- $this->permanentNotices = $permanentNotices;
- $this->shortcodes = $shortcodes;
- $this->databaseInitializer = $databaseInitializer;
- $this->wcTransactionalEmails = $wcTransactionalEmails;
- $this->wcHelper = $wcHelper;
- $this->postEditorBlock = $postEditorBlock;
- $this->localizer = $localizer;
- $this->assetsLoader = $assetsLoader;
- }
- public function init() {
- // load translations
- $this->setupLocalizer();
- try {
- $this->databaseInitializer->initializeConnection();
- } catch (\Exception $e) {
- return WPNotice::displayError(Helpers::replaceLinkTags(
- WPFunctions::get()->__('Unable to connect to the database (the database is unable to open a file or folder), the connection is likely not configured correctly. Please read our [link] Knowledge Base article [/link] for steps how to resolve it.', 'mailpoet'),
- 'https://kb.mailpoet.com/article/200-solving-database-connection-issues',
- [
- 'target' => '_blank',
- 'data-beacon-article' => '596de7db2c7d3a73488b2f8d',
- ]
- ));
- }
- // activation function
- WPFunctions::get()->registerActivationHook(
- Env::$file,
- [
- $this,
- 'runActivator',
- ]
- );
- WPFunctions::get()->addAction('activated_plugin', [
- new PluginActivatedHook(new DeferredAdminNotices),
- 'action',
- ], 10, 2);
- WPFunctions::get()->addAction('init', [
- $this,
- 'preInitialize',
- ], 0);
- WPFunctions::get()->addAction('init', [
- $this,
- 'initialize',
- ]);
- WPFunctions::get()->addAction('admin_init', [
- $this,
- 'setupPrivacyPolicy',
- ]);
- WPFunctions::get()->addAction('wp_loaded', [
- $this,
- 'postInitialize',
- ]);
- WPFunctions::get()->addAction('admin_init', [
- new DeferredAdminNotices,
- 'printAndClean',
- ]);
- WPFunctions::get()->addFilter('wpmu_drop_tables', [
- $this,
- 'multisiteDropTables',
- ]);
- $this->hooks->initEarlyHooks();
- }
- public function runActivator() {
- try {
- $this->activator->activate();
- } catch (InvalidStateException $e) {
- return $this->handleRunningMigration($e);
- } catch (\Exception $e) {
- return $this->handleFailedInitialization($e);
- }
- }
- public function preInitialize() {
- try {
- $this->renderer = $this->rendererFactory->getRenderer();
- $this->setupWidget();
- $this->hooks->init();
- $this->setupWoocommerceTransactionalEmails();
- $this->assetsLoader->loadStyles();
- } catch (\Exception $e) {
- $this->handleFailedInitialization($e);
- }
- }
- public function setupWidget() {
- WPFunctions::get()->registerWidget('\MailPoet\Form\Widget');
- }
- public function initialize() {
- try {
- $this->maybeDbUpdate();
- $this->setupInstaller();
- $this->setupUpdater();
- $this->setupCapabilities();
- $this->menu->init();
- $this->setupShortcodes();
- $this->setupImages();
- $this->setupPersonalDataExporters();
- $this->setupPersonalDataErasers();
- $this->changelog->init();
- $this->setupCronTrigger();
- $this->setupConflictResolver();
- $this->setupPages();
- $this->setupPermanentNotices();
- $this->setupAutomaticEmails();
- $this->postEditorBlock->init();
- WPFunctions::get()->doAction('mailpoet_initialized', MAILPOET_VERSION);
- } catch (InvalidStateException $e) {
- return $this->handleRunningMigration($e);
- } catch (\Exception $e) {
- return $this->handleFailedInitialization($e);
- }
- define(self::INITIALIZED, true);
- }
- public function maybeDbUpdate() {
- try {
- $currentDbVersion = $this->settings->get('db_version');
- } catch (\Exception $e) {
- $currentDbVersion = null;
- }
- // if current db version and plugin version differ
- if (version_compare($currentDbVersion, Env::$version) !== 0) {
- $this->activator->activate();
- }
- }
- public function setupInstaller() {
- $installer = new Installer(
- Installer::PREMIUM_PLUGIN_SLUG
- );
- $installer->init();
- }
- public function setupUpdater() {
- $slug = Installer::PREMIUM_PLUGIN_SLUG;
- $pluginFile = Installer::getPluginFile($slug);
- if (empty($pluginFile) || !defined('MAILPOET_PREMIUM_VERSION')) {
- return false;
- }
- $updater = new Updater(
- $pluginFile,
- $slug,
- MAILPOET_PREMIUM_VERSION
- );
- $updater->init();
- }
- public function setupLocalizer() {
- $this->localizer->init();
- }
- public function setupCapabilities() {
- $caps = new Capabilities($this->renderer);
- $caps->init();
- }
- public function setupShortcodes() {
- $this->shortcodes->init();
- }
- public function setupImages() {
- WPFunctions::get()->addImageSize('mailpoet_newsletter_max', Env::NEWSLETTER_CONTENT_WIDTH);
- }
- public function setupCronTrigger() {
- // setup cron trigger only outside of cli environment
- if (php_sapi_name() !== 'cli') {
- $this->cronTrigger->init();
- }
- }
- public function setupConflictResolver() {
- $conflictResolver = new ConflictResolver();
- $conflictResolver->init();
- }
- public function postInitialize() {
- if (!defined(self::INITIALIZED)) return;
- try {
- $this->api->init();
- $this->router->init();
- $this->setupUserLocale();
- } catch (\Exception $e) {
- $this->handleFailedInitialization($e);
- }
- }
- public function setupUserLocale() {
- if (get_user_locale() === WPFunctions::get()->getLocale()) return;
- WPFunctions::get()->unloadTextdomain(Env::$pluginName);
- $this->localizer->init();
- }
- public function setupPages() {
- $pages = new \MailPoet\Settings\Pages();
- $pages->init();
- }
- public function setupPrivacyPolicy() {
- $privacyPolicy = new PrivacyPolicy();
- $privacyPolicy->init();
- }
- public function setupPersonalDataExporters() {
- $exporters = new PersonalDataExporters();
- $exporters->init();
- }
- public function setupPersonalDataErasers() {
- $erasers = new PersonalDataErasers();
- $erasers->init();
- }
- public function setupPermanentNotices() {
- $this->permanentNotices->init();
- }
- public function handleFailedInitialization($exception) {
- // check if we are able to add pages at this point
- if (function_exists('wp_get_current_user')) {
- Menu::addErrorPage($this->accessControl);
- }
- return WPNotice::displayError($exception);
- }
- private function handleRunningMigration(InvalidStateException $exception) {
- if (function_exists('wp_get_current_user')) {
- Menu::addErrorPage($this->accessControl);
- }
- return WPNotice::displayWarning($exception->getMessage());
- }
- public function setupAutomaticEmails() {
- $automaticEmails = new AutomaticEmails();
- $automaticEmails->init();
- $automaticEmails->getAutomaticEmails();
- }
- public function multisiteDropTables($tables) {
- global $wpdb;
- $tablePrefix = $wpdb->prefix . Env::$pluginPrefix;
- $mailpoetTables = $wpdb->get_col("SHOW TABLES LIKE '$tablePrefix%'");
- return array_merge($tables, $mailpoetTables);
- }
- private function setupWoocommerceTransactionalEmails() {
- $wcEnabled = $this->wcHelper->isWooCommerceActive();
- $optInEnabled = $this->settings->get('woocommerce.use_mailpoet_editor', false);
- if ($wcEnabled && $optInEnabled) {
- $this->wcTransactionalEmails->overrideStylesForWooEmails();
- $this->wcTransactionalEmails->useTemplateForWoocommerceEmails();
- }
- }
- }
|