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
4 days ago
index.php
3 years ago
SubscriberChangesNotifier.php
189 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\SubscriberEntity; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | use MailPoetVendor\Carbon\Carbon; |
| 11 | |
| 12 | class SubscriberChangesNotifier { |
| 13 | |
| 14 | /** @var array<int, int> */ |
| 15 | private $createdSubscriberIds = []; |
| 16 | |
| 17 | /** @var array<int, int> */ |
| 18 | private $deletedSubscriberIds = []; |
| 19 | |
| 20 | /** @var array<int, int> */ |
| 21 | private $updatedSubscriberIds = []; |
| 22 | |
| 23 | /** @var array<int, int> */ |
| 24 | private $statusChangedSubscriberIds = []; |
| 25 | |
| 26 | /** @var array<int, int> */ |
| 27 | private $countChangedSubscriberIds = []; |
| 28 | |
| 29 | /** @var array<int, int> */ |
| 30 | private $createdSubscriberBatches = []; |
| 31 | |
| 32 | /** @var array<int, int> */ |
| 33 | private $updatedSubscriberBatches = []; |
| 34 | |
| 35 | /** @var WPFunctions */ |
| 36 | private $wp; |
| 37 | |
| 38 | public function __construct( |
| 39 | WPFunctions $wp |
| 40 | ) { |
| 41 | $this->wp = $wp; |
| 42 | } |
| 43 | |
| 44 | public function notify() { |
| 45 | $this->notifyCreations(); |
| 46 | $this->notifyUpdates(); |
| 47 | $this->notifyDeletes(); |
| 48 | $this->notifyCountChanges(); |
| 49 | } |
| 50 | |
| 51 | private function notifyCreations(): void { |
| 52 | if (count($this->createdSubscriberIds) > 1) { |
| 53 | $minTimestamp = min($this->createdSubscriberIds); |
| 54 | if ($minTimestamp) { |
| 55 | $this->createdSubscriberBatches[] = $minTimestamp; |
| 56 | $this->createdSubscriberIds = []; // reset created subscribers |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | foreach ($this->createdSubscriberIds as $subscriberId => $updatedAt) { |
| 61 | $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, $subscriberId); |
| 62 | } |
| 63 | |
| 64 | if ($this->createdSubscriberBatches) { |
| 65 | $minTimestamp = min($this->createdSubscriberBatches); |
| 66 | if ($minTimestamp) { |
| 67 | $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_CREATED, $minTimestamp); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private function notifyUpdates(): void { |
| 73 | // unset updated subscribers if subscriber is created |
| 74 | foreach ($this->createdSubscriberIds as $subscriberId => $timestamp) { |
| 75 | unset($this->updatedSubscriberIds[$subscriberId]); |
| 76 | unset($this->statusChangedSubscriberIds[$subscriberId]); |
| 77 | } |
| 78 | |
| 79 | if (count($this->updatedSubscriberIds) > 1) { |
| 80 | $minTimestamp = min($this->updatedSubscriberIds); |
| 81 | if ($minTimestamp) { |
| 82 | $this->updatedSubscriberBatches[] = $minTimestamp; |
| 83 | $this->updatedSubscriberIds = []; // reset updated subscribers |
| 84 | $this->statusChangedSubscriberIds = []; // reset status changed subscribers |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | foreach ($this->updatedSubscriberIds as $subscriberId => $updatedAt) { |
| 89 | $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, $subscriberId); |
| 90 | } |
| 91 | |
| 92 | foreach ($this->statusChangedSubscriberIds as $subscriberId => $updatedAt) { |
| 93 | $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED, $subscriberId); |
| 94 | } |
| 95 | |
| 96 | if ($this->updatedSubscriberBatches) { |
| 97 | $minTimestamp = min($this->updatedSubscriberBatches); |
| 98 | if ($minTimestamp) { |
| 99 | $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_UPDATED, $minTimestamp); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private function notifyDeletes(): void { |
| 105 | if (count($this->deletedSubscriberIds) === 1) { |
| 106 | foreach ($this->deletedSubscriberIds as $subscriberId => $updatedAt) { |
| 107 | $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, $subscriberId); |
| 108 | } |
| 109 | } elseif ($this->deletedSubscriberIds) { |
| 110 | $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_DELETED, array_keys($this->deletedSubscriberIds)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private function notifyCountChanges(): void { |
| 115 | if (empty($this->countChangedSubscriberIds)) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBERS_COUNT_CHANGED, array_keys($this->countChangedSubscriberIds)); |
| 120 | } |
| 121 | |
| 122 | public function subscriberCreated(int $subscriberId): void { |
| 123 | // store id as a key and timestamp change as the value |
| 124 | $timestamp = $this->getTimestamp(); |
| 125 | $this->createdSubscriberIds[$subscriberId] = $timestamp; |
| 126 | $this->countChangedSubscriberIds[$subscriberId] = $timestamp; |
| 127 | } |
| 128 | |
| 129 | public function subscriberUpdated(int $subscriberId): void { |
| 130 | // store id as a key and timestamp change as the value |
| 131 | $this->updatedSubscriberIds[$subscriberId] = $this->getTimestamp(); |
| 132 | } |
| 133 | |
| 134 | public function subscriberStatusChanged(int $subscriberId): void { |
| 135 | // store id as a key and timestamp change as the value |
| 136 | $timestamp = $this->getTimestamp(); |
| 137 | $this->statusChangedSubscriberIds[$subscriberId] = $timestamp; |
| 138 | $this->countChangedSubscriberIds[$subscriberId] = $timestamp; |
| 139 | } |
| 140 | |
| 141 | public function subscriberDeleted(int $subscriberId): void { |
| 142 | // store id as a key and timestamp change as the value |
| 143 | $timestamp = $this->getTimestamp(); |
| 144 | $this->deletedSubscriberIds[$subscriberId] = $timestamp; |
| 145 | $this->countChangedSubscriberIds[$subscriberId] = $timestamp; |
| 146 | } |
| 147 | |
| 148 | public function subscribersCreated(array $subscriberIds): void { |
| 149 | foreach ($subscriberIds as $subscriberId) { |
| 150 | $this->subscriberCreated((int)$subscriberId); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public function subscribersUpdated(array $subscriberIds): void { |
| 155 | foreach ($subscriberIds as $subscriberId) { |
| 156 | $this->subscriberUpdated((int)$subscriberId); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public function subscribersDeleted(array $subscriberIds): void { |
| 161 | foreach ($subscriberIds as $subscriberId) { |
| 162 | $this->subscriberDeleted((int)$subscriberId); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | public function subscriberCountChanged(int $subscriberId): void { |
| 167 | $this->countChangedSubscriberIds[$subscriberId] = $this->getTimestamp(); |
| 168 | } |
| 169 | |
| 170 | public function subscribersCountChanged(array $subscriberIds): void { |
| 171 | foreach ($subscriberIds as $subscriberId) { |
| 172 | $this->subscriberCountChanged((int)$subscriberId); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function subscribersBatchCreate(): void { |
| 177 | $this->createdSubscriberBatches[] = $this->getTimestamp(); |
| 178 | } |
| 179 | |
| 180 | public function subscribersBatchUpdate(): void { |
| 181 | $this->updatedSubscriberBatches[] = $this->getTimestamp(); |
| 182 | } |
| 183 | |
| 184 | private function getTimestamp(): int { |
| 185 | $dateTime = Carbon::createFromTimestamp($this->wp->currentTime('timestamp', true), 'UTC'); |
| 186 | return $dateTime->getTimestamp(); |
| 187 | } |
| 188 | } |
| 189 |