Charsets.php
3 years ago
Hosts.php
4 weeks ago
Pages.php
7 months ago
SettingsChangeHandler.php
4 weeks ago
SettingsController.php
2 weeks ago
SettingsRepository.php
1 year ago
TrackingConfig.php
4 months ago
UserFlagsController.php
1 year ago
UserFlagsRepository.php
3 years ago
index.php
3 years ago
SettingsChangeHandler.php
128 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Settings; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\CronWorkerScheduler; |
| 9 | use MailPoet\Cron\Workers\InactiveSubscribers; |
| 10 | use MailPoet\Cron\Workers\UnconfirmedSubscribersCleanup; |
| 11 | use MailPoet\Cron\Workers\WooCommerceSync; |
| 12 | use MailPoet\Entities\ScheduledTaskEntity; |
| 13 | use MailPoet\Mailer\Mailer; |
| 14 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 15 | use MailPoet\Services\Bridge; |
| 16 | use MailPoet\Services\SubscribersCountReporter; |
| 17 | use MailPoetVendor\Carbon\Carbon; |
| 18 | |
| 19 | class SettingsChangeHandler { |
| 20 | |
| 21 | /** @var ScheduledTasksRepository */ |
| 22 | private $scheduledTasksRepository; |
| 23 | |
| 24 | /** @var SettingsController */ |
| 25 | private $settingsController; |
| 26 | |
| 27 | /** @var Bridge */ |
| 28 | private $bridge; |
| 29 | |
| 30 | /** @var SubscribersCountReporter */ |
| 31 | private $subscribersCountReporter; |
| 32 | |
| 33 | /** @var CronWorkerScheduler */ |
| 34 | private $cronWorkerScheduler; |
| 35 | |
| 36 | public function __construct( |
| 37 | ScheduledTasksRepository $scheduledTasksRepository, |
| 38 | SettingsController $settingsController, |
| 39 | Bridge $bridge, |
| 40 | SubscribersCountReporter $subscribersCountReporter, |
| 41 | CronWorkerScheduler $cronWorkerScheduler |
| 42 | ) { |
| 43 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 44 | $this->settingsController = $settingsController; |
| 45 | $this->bridge = $bridge; |
| 46 | $this->subscribersCountReporter = $subscribersCountReporter; |
| 47 | $this->cronWorkerScheduler = $cronWorkerScheduler; |
| 48 | } |
| 49 | |
| 50 | public function onSubscribeOldWoocommerceCustomersChange(): void { |
| 51 | $task = $this->scheduledTasksRepository->findOneBy([ |
| 52 | 'type' => WooCommerceSync::TASK_TYPE, |
| 53 | 'status' => ScheduledTaskEntity::STATUS_SCHEDULED, |
| 54 | 'deletedAt' => null, |
| 55 | ], ['createdAt' => 'DESC']); |
| 56 | if (!($task instanceof ScheduledTaskEntity)) { |
| 57 | $task = $this->createScheduledTask(WooCommerceSync::TASK_TYPE); |
| 58 | } |
| 59 | $datetime = Carbon::now()->millisecond(0); |
| 60 | $task->setScheduledAt($datetime->subMinute()); |
| 61 | $this->scheduledTasksRepository->persist($task); |
| 62 | $this->scheduledTasksRepository->flush(); |
| 63 | } |
| 64 | |
| 65 | public function onInactiveSubscribersIntervalChange(): void { |
| 66 | $task = $this->scheduledTasksRepository->findOneBy([ |
| 67 | 'type' => InactiveSubscribers::TASK_TYPE, |
| 68 | 'status' => ScheduledTaskEntity::STATUS_SCHEDULED, |
| 69 | 'deletedAt' => null, |
| 70 | ], ['createdAt' => 'DESC']); |
| 71 | if (!($task instanceof ScheduledTaskEntity)) { |
| 72 | $task = $this->createScheduledTask(InactiveSubscribers::TASK_TYPE); |
| 73 | } |
| 74 | $datetime = Carbon::now()->millisecond(0); |
| 75 | $task->setScheduledAt($datetime->subMinute()); |
| 76 | $this->scheduledTasksRepository->persist($task); |
| 77 | $this->scheduledTasksRepository->flush(); |
| 78 | } |
| 79 | |
| 80 | public function onUnconfirmedSubscribersCleanupEnable(): void { |
| 81 | $this->cronWorkerScheduler->scheduleImmediatelyIfNotRunning(UnconfirmedSubscribersCleanup::TASK_TYPE); |
| 82 | } |
| 83 | |
| 84 | public function onMSSActivate($newSettings) { |
| 85 | // see mailpoet/assets/js/src/wizard/create_sender_settings.jsx:freeAddress |
| 86 | $httpHost = isset($_SERVER['HTTP_HOST']) && is_string($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; |
| 87 | $domain = str_replace('www.', '', $httpHost); |
| 88 | if ( |
| 89 | isset($newSettings['sender']['address']) |
| 90 | && !empty($newSettings['reply_to']['address']) |
| 91 | && ($newSettings['sender']['address'] === ('wordpress@' . $domain)) |
| 92 | ) { |
| 93 | $sender = [ |
| 94 | 'name' => $newSettings['reply_to']['name'] ?? '', |
| 95 | 'address' => $newSettings['reply_to']['address'], |
| 96 | ]; |
| 97 | $this->settingsController->set('sender', $sender); |
| 98 | $this->settingsController->set('reply_to', null); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private function createScheduledTask(string $type): ScheduledTaskEntity { |
| 103 | $task = new ScheduledTaskEntity(); |
| 104 | $task->setType($type); |
| 105 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 106 | return $task; |
| 107 | } |
| 108 | |
| 109 | public function updateApiKeyState($settings) { |
| 110 | $apiKey = $settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key'] ?? null; |
| 111 | $premiumKey = $settings['premium']['premium_key'] ?? null; |
| 112 | if (!empty($apiKey)) { |
| 113 | $apiKeyState = $this->bridge->checkMSSKey($apiKey); |
| 114 | $this->bridge->storeMSSKeyAndState($apiKey, $apiKeyState); |
| 115 | } |
| 116 | if (!empty($premiumKey)) { |
| 117 | $premiumState = $this->bridge->checkPremiumKey($premiumKey); |
| 118 | $this->bridge->storePremiumKeyAndState($premiumKey, $premiumState); |
| 119 | } |
| 120 | if ($apiKey && !empty($apiKeyState) && in_array($apiKeyState['state'], [Bridge::KEY_VALID, Bridge::KEY_VALID_UNDERPRIVILEGED], true)) { |
| 121 | return $this->subscribersCountReporter->report($apiKey); |
| 122 | } |
| 123 | if ($premiumKey && !empty($premiumState) && in_array($premiumState['state'], [Bridge::KEY_VALID, Bridge::KEY_VALID_UNDERPRIVILEGED], true)) { |
| 124 | return $this->subscribersCountReporter->report($premiumKey); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |