<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing namespace MailPoet\Config; if (!defined('ABSPATH')) exit; use MailPoet\Cron\ActionScheduler\ActionScheduler as CronActionScheduler; use MailPoet\Cron\CronTrigger; use MailPoet\InvalidStateException; use MailPoet\Migrator\Migrator; use MailPoet\Settings\SettingsController; use MailPoet\Util\Notices\DisabledMailFunctionNotice; use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Doctrine\DBAL\Connection; class Activator { public const TRANSIENT_ACTIVATE_KEY = 'mailpoet_activator_activate'; private const TRANSIENT_EXPIRATION = 120; // seconds /** @var Connection */ private $connection; /** @var SettingsController */ private $settings; /** @var Populator */ private $populator; /** @var WPFunctions */ private $wp; /** @var Migrator */ private $migrator; /** @var CronActionScheduler */ private $cronActionSchedulerRunner; public function __construct( Connection $connection, SettingsController $settings, Populator $populator, WPFunctions $wp, Migrator $migrator, CronActionScheduler $cronActionSchedulerRunner ) { $this->connection = $connection; $this->settings = $settings; $this->populator = $populator; $this->wp = $wp; $this->migrator = $migrator; $this->cronActionSchedulerRunner = $cronActionSchedulerRunner; } public function activate() { $isRunning = $this->wp->getTransient(self::TRANSIENT_ACTIVATE_KEY); if ($isRunning === false) { $this->lockActivation(); try { $this->processActivate(); } finally { $this->unlockActivation(); } } else { throw new InvalidStateException(__('MailPoet version update is in progress, please refresh the page in a minute.', 'mailpoet')); } } private function lockActivation(): void { $this->wp->setTransient(self::TRANSIENT_ACTIVATE_KEY, '1', self::TRANSIENT_EXPIRATION); } private function unlockActivation(): void { $this->wp->deleteTransient(self::TRANSIENT_ACTIVATE_KEY); } private function processActivate(): void { $this->migrator->run(); $this->deactivateCronActions(); $this->populator->up(); $this->updateDbVersion(); $caps = new Capabilities(); $caps->setupWPCapabilities(); $localizer = new Localizer(); $localizer->forceInstallLanguagePacks($this->wp); $this->checkForDisabledMailFunction(); } public function deactivate() { $this->lockActivation(); $this->deleteAllMailPoetTablesAndData(); $caps = new Capabilities(); $caps->removeWPCapabilities(); $this->unlockActivation(); } /** * Deactivate action scheduler cron actions when the migration run. * This should prevent processing actions during migrations. * They are later re-activated in CronTrigger * * @return void */ private function deactivateCronActions(): void { $currentMethod = $this->settings->get(CronTrigger::SETTING_NAME . '.method'); if ($currentMethod !== CronTrigger::METHOD_ACTION_SCHEDULER) { return; } $this->cronActionSchedulerRunner->unscheduleAllCronActions(); } public function updateDbVersion() { try { $currentDbVersion = $this->settings->get('db_version'); } catch (\Exception $e) { $currentDbVersion = null; } $this->settings->set('db_version', Env::$version); // if current db version and plugin version differ, log an update if (version_compare((string)$currentDbVersion, Env::$version) !== 0) { $updatesLog = (array)$this->settings->get('updates_log', []); $updatesLog[] = [ 'previous_version' => $currentDbVersion, 'new_version' => Env::$version, 'date' => date('Y-m-d H:i:s'), ]; $this->settings->set('updates_log', $updatesLog); } } private function checkForDisabledMailFunction() { $version = $this->settings->get('version'); if (!is_null($version)) return; // not a new user // check for valid mail function on new installs $this->settings->set(DisabledMailFunctionNotice::QUEUE_DISABLED_MAIL_FUNCTION_CHECK, true); } private function deleteAllMailPoetTablesAndData(): void { $prefix = Env::$dbPrefix; if (!$prefix) { throw InvalidStateException::create()->withMessage('No database table prefix was set.'); } // list all MailPoet tables by prefix $prefixSql = $this->wp->escSql($prefix); $tables = $this->connection->executeQuery("SHOW TABLES LIKE '$prefixSql%'")->fetchFirstColumn(); // drop all MailPoet tables in a single query $tablesSql = implode( ',', array_map(function ($table): string { /** @var string $table */ return $this->wp->escSql(strval($table)); }, $tables) ); $this->connection->executeStatement('SET foreign_key_checks = 0'); $this->connection->executeStatement("DROP TABLE IF EXISTS $tablesSql"); $this->connection->executeStatement('SET foreign_key_checks = 1'); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
PopulatorData | Folder | 0755 |
|
|
AccessControl.php | File | 3.89 KB | 0644 |
|
Activator.php | File | 4.94 KB | 0644 |
|
AssetsLoader.php | File | 1.68 KB | 0644 |
|
Capabilities.php | File | 2.87 KB | 0644 |
|
Changelog.php | File | 5.04 KB | 0644 |
|
DeactivationPoll.php | File | 1.43 KB | 0644 |
|
DeferredAdminNotices.php | File | 1.16 KB | 0644 |
|
Env.php | File | 4.16 KB | 0644 |
|
Hooks.php | File | 20.06 KB | 0644 |
|
HooksWooCommerce.php | File | 6.76 KB | 0644 |
|
Initializer.php | File | 16.58 KB | 0644 |
|
Installer.php | File | 3.12 KB | 0644 |
|
Localizer.php | File | 2.49 KB | 0644 |
|
Menu.php | File | 23.54 KB | 0644 |
|
PersonalDataErasers.php | File | 769 B | 0644 |
|
PersonalDataExporters.php | File | 3.22 KB | 0644 |
|
PluginActivatedHook.php | File | 776 B | 0644 |
|
Populator.php | File | 23.37 KB | 0644 |
|
PrivacyPolicy.php | File | 4.53 KB | 0644 |
|
Renderer.php | File | 4.34 KB | 0644 |
|
RendererFactory.php | File | 670 B | 0644 |
|
RequirementsChecker.php | File | 3.73 KB | 0644 |
|
Router.php | File | 1006 B | 0644 |
|
ServicesChecker.php | File | 7.04 KB | 0644 |
|
Shortcodes.php | File | 8.63 KB | 0644 |
|
SilentUpgraderSkin.php | File | 570 B | 0644 |
|
SubscriberChangesNotifier.php | File | 5 KB | 0644 |
|
TranslationUpdater.php | File | 7.92 KB | 0644 |
|
TwigEnvironment.php | File | 694 B | 0644 |
|
TwigFileSystemCache.php | File | 853 B | 0644 |
|
Updater.php | File | 1.97 KB | 0644 |
|
index.php | File | 6 B | 0644 |
|