PopulatorData
2 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
2 weeks ago
Capabilities.php
2 months ago
Changelog.php
2 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
6 months ago
Hooks.php
1 month ago
HooksWooCommerce.php
1 month ago
Initializer.php
1 month ago
Installer.php
10 months ago
Localizer.php
3 years ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
2 weeks ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
2 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 month ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
2 months ago
TranslationUpdater.php
3 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
5 days ago
index.php
3 years ago
Initializer.php
682 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Bootstrap as EmailEditorBootstrap; |
| 9 | use Automattic\WooCommerce\EmailEditor\Email_Editor_Container; |
| 10 | use Automattic\WooCommerce\EmailEditor\Engine\Logger\Email_Editor_Logger; |
| 11 | use MailPoet\Abilities\Abilities; |
| 12 | use MailPoet\API\JSON\API; |
| 13 | use MailPoet\API\REST\API as RestApi; |
| 14 | use MailPoet\AutomaticEmails\AutomaticEmails; |
| 15 | use MailPoet\Automation\Engine\Engine; |
| 16 | use MailPoet\Automation\Engine\Hooks as AutomationHooks; |
| 17 | use MailPoet\Automation\Integrations\MailPoet\MailPoetIntegration; |
| 18 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerceIntegration; |
| 19 | use MailPoet\Cron\CliCommands\Cli as CronCli; |
| 20 | use MailPoet\Cron\CronTrigger; |
| 21 | use MailPoet\Cron\DaemonActionSchedulerRunner; |
| 22 | use MailPoet\CustomFields\RestApi\Api as CustomFieldsRestApi; |
| 23 | use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController; |
| 24 | use MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor as MailpoetEmailEditorIntegration; |
| 25 | use MailPoet\EmailEditor\Integrations\MailPoet\Logger; |
| 26 | use MailPoet\Form\RestApi\Api as FormsRestApi; |
| 27 | use MailPoet\InvalidStateException; |
| 28 | use MailPoet\Logging\LogsDownload; |
| 29 | use MailPoet\Logging\RestApi\Api as LogsRestApi; |
| 30 | use MailPoet\Migrator\Cli as MigratorCli; |
| 31 | use MailPoet\Newsletter\RestApi\Api as NewslettersRestApi; |
| 32 | use MailPoet\Newsletter\Sharing\PublicEmailRoute; |
| 33 | use MailPoet\PostEditorBlocks\PostEditorBlock; |
| 34 | use MailPoet\PostEditorBlocks\WooCommerceBlocksIntegration; |
| 35 | use MailPoet\Router; |
| 36 | use MailPoet\Segments\RestApi\Api as SegmentsRestApi; |
| 37 | use MailPoet\Settings\SettingsController; |
| 38 | use MailPoet\Statistics\Track\SubscriberActivityTracker; |
| 39 | use MailPoet\Subscribers\ImportExport\Import\Cli as ImportCli; |
| 40 | use MailPoet\Subscribers\RestApi\Api as SubscribersRestApi; |
| 41 | use MailPoet\Tags\RestApi\Api as TagsRestApi; |
| 42 | use MailPoet\Util\ConflictResolver; |
| 43 | use MailPoet\Util\LegacyDatabase; |
| 44 | use MailPoet\Util\Notices\PermanentNotices; |
| 45 | use MailPoet\Util\Url; |
| 46 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 47 | use MailPoet\WooCommerce\TransactionalEmailHooks as WCTransactionalEmails; |
| 48 | use MailPoet\WP\Functions as WPFunctions; |
| 49 | use MailPoet\WP\Notice as WPNotice; |
| 50 | |
| 51 | class Initializer { |
| 52 | /** @var AccessControl */ |
| 53 | private $accessControl; |
| 54 | |
| 55 | /** @var Renderer */ |
| 56 | private $renderer; |
| 57 | |
| 58 | /** @var RendererFactory */ |
| 59 | private $rendererFactory; |
| 60 | |
| 61 | /** @var API */ |
| 62 | private $api; |
| 63 | |
| 64 | /** @var RestApi */ |
| 65 | private $restApi; |
| 66 | |
| 67 | /** @var Activator */ |
| 68 | private $activator; |
| 69 | |
| 70 | /** @var SettingsController */ |
| 71 | private $settings; |
| 72 | |
| 73 | /** @var MigratorCli */ |
| 74 | private $migratorCli; |
| 75 | |
| 76 | /** @var ImportCli */ |
| 77 | private $importCli; |
| 78 | |
| 79 | /** @var CronCli */ |
| 80 | private $cronCli; |
| 81 | |
| 82 | /** @var Router\Router */ |
| 83 | private $router; |
| 84 | |
| 85 | /** @var Hooks */ |
| 86 | private $hooks; |
| 87 | |
| 88 | /** @var Changelog */ |
| 89 | private $changelog; |
| 90 | |
| 91 | /** @var PublicEmailRoute */ |
| 92 | private $publicEmailRoute; |
| 93 | |
| 94 | /** @var Menu */ |
| 95 | private $menu; |
| 96 | |
| 97 | /** @var CronTrigger */ |
| 98 | private $cronTrigger; |
| 99 | |
| 100 | /** @var PermanentNotices */ |
| 101 | private $permanentNotices; |
| 102 | |
| 103 | /** @var Shortcodes */ |
| 104 | private $shortcodes; |
| 105 | |
| 106 | /** @var WCTransactionalEmails */ |
| 107 | private $wcTransactionalEmails; |
| 108 | |
| 109 | /** @var WooCommerceHelper */ |
| 110 | private $wcHelper; |
| 111 | |
| 112 | /** @var \MailPoet\PostEditorBlocks\PostEditorBlock */ |
| 113 | private $postEditorBlock; |
| 114 | |
| 115 | /** @var \MailPoet\PostEditorBlocks\WooCommerceBlocksIntegration */ |
| 116 | private $woocommerceBlocksIntegration; |
| 117 | |
| 118 | /** @var Localizer */ |
| 119 | private $localizer; |
| 120 | |
| 121 | /** @var AutomaticEmails */ |
| 122 | private $automaticEmails; |
| 123 | |
| 124 | /** @var WPFunctions */ |
| 125 | private $wpFunctions; |
| 126 | |
| 127 | /** @var AssetsLoader */ |
| 128 | private $assetsLoader; |
| 129 | |
| 130 | /** @var SubscriberActivityTracker */ |
| 131 | private $subscriberActivityTracker; |
| 132 | |
| 133 | /** @var Engine */ |
| 134 | private $automationEngine; |
| 135 | |
| 136 | /** @var TagsRestApi */ |
| 137 | private $tagsRestApi; |
| 138 | |
| 139 | /** @var CustomFieldsRestApi */ |
| 140 | private $customFieldsRestApi; |
| 141 | |
| 142 | /** @var FormsRestApi */ |
| 143 | private $formsRestApi; |
| 144 | |
| 145 | /** @var SegmentsRestApi */ |
| 146 | private $segmentsRestApi; |
| 147 | |
| 148 | /** @var LogsRestApi */ |
| 149 | private $logsRestApi; |
| 150 | |
| 151 | /** @var SubscribersRestApi */ |
| 152 | private $subscribersRestApi; |
| 153 | |
| 154 | /** @var NewslettersRestApi */ |
| 155 | private $newslettersRestApi; |
| 156 | |
| 157 | /** @var LogsDownload */ |
| 158 | private $logsDownload; |
| 159 | |
| 160 | /** @var MailPoetIntegration */ |
| 161 | private $automationMailPoetIntegration; |
| 162 | |
| 163 | /** @var WooCommerceIntegration */ |
| 164 | private $woocommerceIntegration; |
| 165 | |
| 166 | /** @var PersonalDataExporters */ |
| 167 | private $personalDataExporters; |
| 168 | |
| 169 | /** @var DaemonActionSchedulerRunner */ |
| 170 | private $actionSchedulerRunner; |
| 171 | |
| 172 | /** @var MailpoetEmailEditorIntegration */ |
| 173 | private $mailpoetEmailEditorIntegration; |
| 174 | |
| 175 | /** @var BlockTypesController */ |
| 176 | private $blockTypesController; |
| 177 | |
| 178 | /** @var Url */ |
| 179 | private $urlHelper; |
| 180 | |
| 181 | private EmailEditorBootstrap $emailEditorBootstrap; |
| 182 | |
| 183 | private Email_Editor_Logger $emailEditorLogger; |
| 184 | |
| 185 | const INITIALIZED = 'MAILPOET_INITIALIZED'; |
| 186 | |
| 187 | const PLUGIN_ACTIVATED = 'mailpoet_plugin_activated'; |
| 188 | |
| 189 | public function __construct( |
| 190 | RendererFactory $rendererFactory, |
| 191 | AccessControl $accessControl, |
| 192 | API $api, |
| 193 | RestApi $restApi, |
| 194 | Activator $activator, |
| 195 | SettingsController $settings, |
| 196 | MigratorCli $migratorCli, |
| 197 | ImportCli $importCli, |
| 198 | CronCli $cronCli, |
| 199 | Router\Router $router, |
| 200 | Hooks $hooks, |
| 201 | Changelog $changelog, |
| 202 | Menu $menu, |
| 203 | CronTrigger $cronTrigger, |
| 204 | PermanentNotices $permanentNotices, |
| 205 | Shortcodes $shortcodes, |
| 206 | WCTransactionalEmails $wcTransactionalEmails, |
| 207 | PostEditorBlock $postEditorBlock, |
| 208 | WooCommerceBlocksIntegration $woocommerceBlocksIntegration, |
| 209 | WooCommerceHelper $wcHelper, |
| 210 | Localizer $localizer, |
| 211 | AutomaticEmails $automaticEmails, |
| 212 | SubscriberActivityTracker $subscriberActivityTracker, |
| 213 | WPFunctions $wpFunctions, |
| 214 | AssetsLoader $assetsLoader, |
| 215 | Engine $automationEngine, |
| 216 | MailPoetIntegration $automationMailPoetIntegration, |
| 217 | WooCommerceIntegration $woocommerceIntegration, |
| 218 | PersonalDataExporters $personalDataExporters, |
| 219 | DaemonActionSchedulerRunner $actionSchedulerRunner, |
| 220 | BlockTypesController $blockTypesController, |
| 221 | MailpoetEmailEditorIntegration $mailpoetEmailEditorIntegration, |
| 222 | Url $urlHelper, |
| 223 | TagsRestApi $tagsRestApi, |
| 224 | CustomFieldsRestApi $customFieldsRestApi, |
| 225 | FormsRestApi $formsRestApi, |
| 226 | SegmentsRestApi $segmentsRestApi, |
| 227 | LogsRestApi $logsRestApi, |
| 228 | SubscribersRestApi $subscribersRestApi, |
| 229 | NewslettersRestApi $newslettersRestApi, |
| 230 | LogsDownload $logsDownload, |
| 231 | PublicEmailRoute $publicEmailRoute |
| 232 | ) { |
| 233 | $this->rendererFactory = $rendererFactory; |
| 234 | $this->accessControl = $accessControl; |
| 235 | $this->api = $api; |
| 236 | $this->restApi = $restApi; |
| 237 | $this->activator = $activator; |
| 238 | $this->settings = $settings; |
| 239 | $this->migratorCli = $migratorCli; |
| 240 | $this->importCli = $importCli; |
| 241 | $this->cronCli = $cronCli; |
| 242 | $this->router = $router; |
| 243 | $this->hooks = $hooks; |
| 244 | $this->changelog = $changelog; |
| 245 | $this->menu = $menu; |
| 246 | $this->cronTrigger = $cronTrigger; |
| 247 | $this->permanentNotices = $permanentNotices; |
| 248 | $this->shortcodes = $shortcodes; |
| 249 | $this->wcTransactionalEmails = $wcTransactionalEmails; |
| 250 | $this->wcHelper = $wcHelper; |
| 251 | $this->postEditorBlock = $postEditorBlock; |
| 252 | $this->woocommerceBlocksIntegration = $woocommerceBlocksIntegration; |
| 253 | $this->localizer = $localizer; |
| 254 | $this->automaticEmails = $automaticEmails; |
| 255 | $this->subscriberActivityTracker = $subscriberActivityTracker; |
| 256 | $this->wpFunctions = $wpFunctions; |
| 257 | $this->assetsLoader = $assetsLoader; |
| 258 | $this->automationEngine = $automationEngine; |
| 259 | $this->automationMailPoetIntegration = $automationMailPoetIntegration; |
| 260 | $this->woocommerceIntegration = $woocommerceIntegration; |
| 261 | $this->personalDataExporters = $personalDataExporters; |
| 262 | $this->actionSchedulerRunner = $actionSchedulerRunner; |
| 263 | $this->mailpoetEmailEditorIntegration = $mailpoetEmailEditorIntegration; |
| 264 | $this->blockTypesController = $blockTypesController; |
| 265 | $this->urlHelper = $urlHelper; |
| 266 | $this->tagsRestApi = $tagsRestApi; |
| 267 | $this->customFieldsRestApi = $customFieldsRestApi; |
| 268 | $this->formsRestApi = $formsRestApi; |
| 269 | $this->segmentsRestApi = $segmentsRestApi; |
| 270 | $this->logsRestApi = $logsRestApi; |
| 271 | $this->subscribersRestApi = $subscribersRestApi; |
| 272 | $this->newslettersRestApi = $newslettersRestApi; |
| 273 | $this->logsDownload = $logsDownload; |
| 274 | $this->publicEmailRoute = $publicEmailRoute; |
| 275 | |
| 276 | $emailEditorContainer = Email_Editor_Container::container(); |
| 277 | $this->emailEditorBootstrap = $emailEditorContainer->get(EmailEditorBootstrap::class); |
| 278 | $this->emailEditorLogger = $emailEditorContainer->get(Email_Editor_Logger::class); |
| 279 | } |
| 280 | |
| 281 | public function init() { |
| 282 | // Initialize Action Scheduler. It needs to be called early because it hooks into `plugins_loaded`. |
| 283 | require_once __DIR__ . '/../../vendor/woocommerce/action-scheduler/action-scheduler.php'; |
| 284 | |
| 285 | // define legacy constants for DB tables - for back compatibility |
| 286 | LegacyDatabase::defineTableConstants(); |
| 287 | |
| 288 | // load translations and setup translations update/download |
| 289 | $this->setupLocalizer(); |
| 290 | |
| 291 | // activation function |
| 292 | $this->wpFunctions->registerActivationHook( |
| 293 | Env::$file, |
| 294 | [ |
| 295 | $this, |
| 296 | 'runActivator', |
| 297 | ] |
| 298 | ); |
| 299 | |
| 300 | // deactivation function |
| 301 | $this->wpFunctions->registerDeactivationHook( |
| 302 | Env::$file, |
| 303 | [ |
| 304 | $this, |
| 305 | 'runDeactivation', |
| 306 | ] |
| 307 | ); |
| 308 | |
| 309 | $this->emailEditorBootstrap->init(); |
| 310 | $this->setupAbilities(); |
| 311 | |
| 312 | $this->wpFunctions->addAction('activated_plugin', [ |
| 313 | new PluginActivatedHook(new DeferredAdminNotices), |
| 314 | 'action', |
| 315 | ], 10, 2); |
| 316 | |
| 317 | $this->wpFunctions->addAction('plugins_loaded', [ |
| 318 | $this, |
| 319 | 'pluginsLoaded', |
| 320 | ], 0); |
| 321 | |
| 322 | $this->wpFunctions->addAction('init', [ |
| 323 | $this, |
| 324 | 'preInitialize', |
| 325 | ], 0); |
| 326 | |
| 327 | $this->wpFunctions->addAction('init', [ |
| 328 | $this, |
| 329 | 'initialize', |
| 330 | ]); |
| 331 | |
| 332 | $this->wpFunctions->addAction( |
| 333 | 'init', |
| 334 | [$this, 'maybeRunActivator'], |
| 335 | PHP_INT_MIN |
| 336 | ); |
| 337 | |
| 338 | $this->wpFunctions->addAction('admin_init', [ |
| 339 | $this, |
| 340 | 'setupPrivacyPolicy', |
| 341 | ]); |
| 342 | |
| 343 | $this->wpFunctions->addAction('wp_loaded', [ |
| 344 | $this, |
| 345 | 'postInitialize', |
| 346 | ]); |
| 347 | |
| 348 | $this->wpFunctions->addAction('admin_init', [ |
| 349 | new DeferredAdminNotices, |
| 350 | 'printAndClean', |
| 351 | ]); |
| 352 | |
| 353 | $this->wpFunctions->addFilter('wpmu_drop_tables', [ |
| 354 | $this, |
| 355 | 'multisiteDropTables', |
| 356 | ]); |
| 357 | |
| 358 | $this->wpFunctions->addFilter('woocommerce_email_editor_initialized', [ |
| 359 | $this, |
| 360 | 'setupEmailEditorIntegrations', |
| 361 | ]); |
| 362 | |
| 363 | $this->wpFunctions->addAction('woocommerce_init', [ |
| 364 | $this, |
| 365 | 'setupMarketingConfirmationEmail', |
| 366 | ]); |
| 367 | |
| 368 | WPFunctions::get()->addAction(AutomationHooks::INITIALIZE, [ |
| 369 | $this->automationMailPoetIntegration, |
| 370 | 'register', |
| 371 | ]); |
| 372 | WPFunctions::get()->addAction(AutomationHooks::INITIALIZE, [ |
| 373 | $this->woocommerceIntegration, |
| 374 | 'register', |
| 375 | ]); |
| 376 | |
| 377 | WPFunctions::get()->addAction('admin_init', [ |
| 378 | $this, |
| 379 | 'afterPluginActivation', |
| 380 | ]); |
| 381 | |
| 382 | $this->hooks->initEarlyHooks(); |
| 383 | } |
| 384 | |
| 385 | private function setupAbilities(): void { |
| 386 | require_once __DIR__ . '/../Abilities/Abilities.php'; |
| 387 | Abilities::init(); |
| 388 | } |
| 389 | |
| 390 | public function runActivator() { |
| 391 | try { |
| 392 | $this->wpFunctions->addOption(self::PLUGIN_ACTIVATED, true); // used in afterPluginActivation |
| 393 | $this->activator->activate(); |
| 394 | } catch (InvalidStateException $e) { |
| 395 | return $this->handleRunningMigration($e); |
| 396 | } catch (\Exception $e) { |
| 397 | return $this->handleFailedInitialization($e); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | public function pluginsLoaded() { |
| 402 | $this->hooks->init(); |
| 403 | $this->publicEmailRoute->init(); |
| 404 | } |
| 405 | |
| 406 | public function preInitialize() { |
| 407 | try { |
| 408 | $this->renderer = $this->rendererFactory->getRenderer(); |
| 409 | $this->setupWidget(); |
| 410 | $this->setupWoocommerceTransactionalEmails(); |
| 411 | $this->assetsLoader->loadStyles(); |
| 412 | $this->emailEditorLogger->set_logger(new Logger()); |
| 413 | } catch (\Exception $e) { |
| 414 | $this->handleFailedInitialization($e); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | public function setupWidget() { |
| 419 | $this->wpFunctions->registerWidget('\MailPoet\Form\Widget'); |
| 420 | } |
| 421 | |
| 422 | public function initialize() { |
| 423 | try { |
| 424 | $this->migratorCli->initialize(); |
| 425 | $this->importCli->initialize(); |
| 426 | $this->cronCli->initialize(); |
| 427 | $this->setupInstaller(); |
| 428 | $this->setupUpdater(); |
| 429 | |
| 430 | $this->setupCapabilities(); |
| 431 | $this->menu->init(); |
| 432 | $this->setupShortcodes(); |
| 433 | $this->setupImages(); |
| 434 | $this->setupPersonalDataExporters(); |
| 435 | $this->setupPersonalDataErasers(); |
| 436 | |
| 437 | $this->changelog->init(); |
| 438 | $this->setupCronTrigger(); |
| 439 | $this->setupConflictResolver(); |
| 440 | |
| 441 | $this->setupPages(); |
| 442 | |
| 443 | $this->setupPermanentNotices(); |
| 444 | $this->setupAutomaticEmails(); |
| 445 | $this->setupWoocommerceBlocksIntegration(); |
| 446 | $this->setupDeactivationPoll(); |
| 447 | $this->subscriberActivityTracker->trackActivity(); |
| 448 | $this->postEditorBlock->init(); |
| 449 | $this->automationEngine->initialize(); |
| 450 | $this->tagsRestApi->initialize(); |
| 451 | $this->customFieldsRestApi->initialize(); |
| 452 | $this->formsRestApi->initialize(); |
| 453 | $this->segmentsRestApi->initialize(); |
| 454 | $this->logsRestApi->initialize(); |
| 455 | $this->subscribersRestApi->initialize(); |
| 456 | $this->newslettersRestApi->initialize(); |
| 457 | $this->logsDownload->initialize(); |
| 458 | $this->blockTypesController->initialize(); |
| 459 | $this->wpFunctions->doAction('mailpoet_initialized', MAILPOET_VERSION); |
| 460 | } catch (InvalidStateException $e) { |
| 461 | return $this->handleRunningMigration($e); |
| 462 | } catch (\Exception $e) { |
| 463 | return $this->handleFailedInitialization($e); |
| 464 | } |
| 465 | |
| 466 | define(self::INITIALIZED, true); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Walk around for getting this to work correctly |
| 471 | * |
| 472 | * Read more here: https://developer.wordpress.org/reference/functions/register_activation_hook/ |
| 473 | * and https://github.com/mailpoet/mailpoet/pull/4620#discussion_r1058210174 |
| 474 | * @return void |
| 475 | */ |
| 476 | public function afterPluginActivation() { |
| 477 | if (!$this->wpFunctions->isAdmin() || !defined(self::INITIALIZED) || !$this->wpFunctions->getOption(self::PLUGIN_ACTIVATED)) return; |
| 478 | |
| 479 | $currentUrl = $this->urlHelper->getCurrentUrl(); |
| 480 | |
| 481 | // wp automatically redirect to `wp-admin/plugins.php?activate=true&...` after plugin activation |
| 482 | $activatedByWpAdmin = !empty(strpos($currentUrl, 'plugins.php')) && isset($_GET['activate']) && (bool)$_GET['activate']; |
| 483 | |
| 484 | // We want to run this only once immediately after activation. |
| 485 | // Delete the flag to prevent triggering on subsequent page loads. |
| 486 | $this->wpFunctions->deleteOption(self::PLUGIN_ACTIVATED); |
| 487 | |
| 488 | // If not activated by wp. Do not redirect e.g WooCommerce NUX |
| 489 | if ($activatedByWpAdmin) { |
| 490 | $this->changelog->redirectToLandingPage(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Checks if the plugin was updated and runs the activator if needed. The activator |
| 496 | * will run the database migrations and update the db version among a few other things. |
| 497 | * |
| 498 | * @return void |
| 499 | * @throws InvalidStateException |
| 500 | */ |
| 501 | public function maybeRunActivator() { |
| 502 | try { |
| 503 | $currentDbVersion = $this->settings->get('db_version'); |
| 504 | } catch (\Exception $e) { |
| 505 | $currentDbVersion = null; |
| 506 | } |
| 507 | |
| 508 | if (version_compare((string)$currentDbVersion, Env::$version) === 0) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | // if current db version and plugin version differ |
| 513 | try { |
| 514 | $this->activator->activate(); |
| 515 | } catch (InvalidStateException $e) { |
| 516 | $this->handleRunningMigration($e); |
| 517 | } catch (\Exception $e) { |
| 518 | $this->handleFailedInitialization($e); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | public function setupInstaller() { |
| 523 | $installer = new Installer( |
| 524 | Installer::PREMIUM_PLUGIN_SLUG |
| 525 | ); |
| 526 | $installer->init(); |
| 527 | } |
| 528 | |
| 529 | public function setupUpdater() { |
| 530 | $premiumSlug = Installer::PREMIUM_PLUGIN_SLUG; |
| 531 | $premiumPluginFile = Installer::getPluginFile($premiumSlug); |
| 532 | $premiumVersion = defined('MAILPOET_PREMIUM_VERSION') ? MAILPOET_PREMIUM_VERSION : null; |
| 533 | |
| 534 | if (empty($premiumPluginFile) || !$premiumVersion) { |
| 535 | return false; |
| 536 | } |
| 537 | $updater = new Updater( |
| 538 | $premiumPluginFile, |
| 539 | $premiumSlug, |
| 540 | MAILPOET_PREMIUM_VERSION |
| 541 | ); |
| 542 | $updater->init(); |
| 543 | } |
| 544 | |
| 545 | public function setupLocalizer() { |
| 546 | $this->localizer->init($this->wpFunctions); |
| 547 | } |
| 548 | |
| 549 | public function setupCapabilities() { |
| 550 | $caps = new Capabilities($this->renderer); |
| 551 | $caps->init(); |
| 552 | } |
| 553 | |
| 554 | public function setupShortcodes() { |
| 555 | $this->shortcodes->init(); |
| 556 | } |
| 557 | |
| 558 | public function setupImages() { |
| 559 | $this->wpFunctions->addImageSize('mailpoet_newsletter_max', Env::NEWSLETTER_CONTENT_WIDTH); |
| 560 | } |
| 561 | |
| 562 | public function setupCronTrigger() { |
| 563 | $this->cronTrigger->init((string)php_sapi_name()); |
| 564 | } |
| 565 | |
| 566 | public function setupConflictResolver() { |
| 567 | $conflictResolver = new ConflictResolver(); |
| 568 | $conflictResolver->init(); |
| 569 | } |
| 570 | |
| 571 | public function postInitialize() { |
| 572 | if (!defined(self::INITIALIZED)) return; |
| 573 | try { |
| 574 | $this->api->init(); |
| 575 | $this->restApi->init(); |
| 576 | $this->router->init(); |
| 577 | $this->setupUserLocale(); |
| 578 | } catch (\Exception $e) { |
| 579 | $this->handleFailedInitialization($e); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | public function setupUserLocale() { |
| 584 | if (get_user_locale() === $this->wpFunctions->getLocale()) return; |
| 585 | $this->wpFunctions->unloadTextdomain(Env::$pluginName); |
| 586 | $this->localizer->init($this->wpFunctions); |
| 587 | } |
| 588 | |
| 589 | public function setupPages() { |
| 590 | $pages = new \MailPoet\Settings\Pages(); |
| 591 | $pages->init(); |
| 592 | } |
| 593 | |
| 594 | public function setupPrivacyPolicy() { |
| 595 | $privacyPolicy = new PrivacyPolicy(); |
| 596 | $privacyPolicy->init(); |
| 597 | } |
| 598 | |
| 599 | public function setupPersonalDataExporters() { |
| 600 | $this->personalDataExporters->init(); |
| 601 | } |
| 602 | |
| 603 | public function setupPersonalDataErasers() { |
| 604 | $erasers = new PersonalDataErasers(); |
| 605 | $erasers->init(); |
| 606 | } |
| 607 | |
| 608 | public function setupPermanentNotices() { |
| 609 | $this->permanentNotices->init(); |
| 610 | } |
| 611 | |
| 612 | public function handleFailedInitialization($exception) { |
| 613 | // check if we are able to add pages at this point |
| 614 | if (function_exists('wp_get_current_user')) { |
| 615 | Menu::addErrorPage($this->accessControl); |
| 616 | } |
| 617 | return WPNotice::displayError($exception); |
| 618 | } |
| 619 | |
| 620 | private function handleRunningMigration(InvalidStateException $exception) { |
| 621 | if (function_exists('wp_get_current_user')) { |
| 622 | Menu::addErrorPage($this->accessControl); |
| 623 | } |
| 624 | return WPNotice::displayWarning($exception->getMessage()); |
| 625 | } |
| 626 | |
| 627 | public function setupAutomaticEmails() { |
| 628 | $this->automaticEmails->init(); |
| 629 | $this->automaticEmails->getAutomaticEmails(); |
| 630 | } |
| 631 | |
| 632 | public function multisiteDropTables($tables) { |
| 633 | global $wpdb; |
| 634 | $tablePrefix = $wpdb->prefix . Env::$pluginPrefix; |
| 635 | $mailpoetTables = $wpdb->get_col( |
| 636 | $wpdb->prepare( |
| 637 | "SHOW TABLES LIKE %s", |
| 638 | $wpdb->esc_like($tablePrefix) . '%' |
| 639 | ) |
| 640 | ); |
| 641 | return array_merge($tables, $mailpoetTables); |
| 642 | } |
| 643 | |
| 644 | public function setupEmailEditorIntegrations() { |
| 645 | $this->mailpoetEmailEditorIntegration->initialize(); |
| 646 | } |
| 647 | |
| 648 | public function runDeactivation() { |
| 649 | $this->actionSchedulerRunner->deactivate(); |
| 650 | } |
| 651 | |
| 652 | private function setupWoocommerceTransactionalEmails() { |
| 653 | $wcEnabled = $this->wcHelper->isWooCommerceActive(); |
| 654 | $optInEnabled = $this->settings->get('woocommerce.use_mailpoet_editor', false); |
| 655 | if ($wcEnabled && $optInEnabled) { |
| 656 | $this->wcTransactionalEmails->overrideStylesForWooEmails(); |
| 657 | $this->wcTransactionalEmails->useTemplateForWoocommerceEmails(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | public function setupMarketingConfirmationEmail() { |
| 662 | $wcEnabled = $this->wcHelper->isWooCommerceActive(); |
| 663 | if ($wcEnabled) { |
| 664 | $emails = new \MailPoet\WooCommerce\Emails(); |
| 665 | $emails->init(); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | private function setupWoocommerceBlocksIntegration() { |
| 670 | $wcEnabled = $this->wcHelper->isWooCommerceActive(); |
| 671 | $wcBlocksEnabled = $this->wcHelper->isWooCommerceBlocksActive('8.0.0'); |
| 672 | if ($wcEnabled && $wcBlocksEnabled) { |
| 673 | $this->woocommerceBlocksIntegration->init(); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | private function setupDeactivationPoll(): void { |
| 678 | $deactivationPoll = new DeactivationPoll($this->wpFunctions, $this->renderer); |
| 679 | $deactivationPoll->init(); |
| 680 | } |
| 681 | } |
| 682 |