From 2d835cdec1df34608f8043f0df7125cbe9e0e93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Thu, 20 Oct 2022 19:27:55 +0200 Subject: [PATCH] Add new action hooks for multiple changes [MAILPOET-4727] --- .../lib/Config/SubscriberChangesNotifier.php | 54 ++++++++---- mailpoet/lib/Entities/SubscriberEntity.php | 3 + .../Config/SubscriberChangesNotifierTest.php | 88 +++++++++++++------ 3 files changed, 104 insertions(+), 41 deletions(-) diff --git a/mailpoet/lib/Config/SubscriberChangesNotifier.php b/mailpoet/lib/Config/SubscriberChangesNotifier.php index 82f2f68a1f..6c678de48f 100644 --- a/mailpoet/lib/Config/SubscriberChangesNotifier.php +++ b/mailpoet/lib/Config/SubscriberChangesNotifier.php @@ -8,13 +8,13 @@ use MailPoetVendor\Carbon\Carbon; class SubscriberChangesNotifier { - /** @var array */ + /** @var array */ private $createdSubscriberIds = []; - /** @var array */ + /** @var array */ private $deletedSubscriberIds = []; - /** @var array */ + /** @var array */ private $updatedSubscriberIds = []; /** @var WPFunctions */ @@ -33,40 +33,59 @@ class SubscriberChangesNotifier { } private function notifyCreations(): void { - foreach ($this->createdSubscriberIds as $subscriberId => $createdAt) { - $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, $subscriberId, $createdAt->getTimestamp()); + if (count($this->createdSubscriberIds) === 1) { + foreach ($this->createdSubscriberIds as $subscriberId => $updatedAt) { + $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, $subscriberId); + } + } elseif ($this->createdSubscriberIds) { + $minTimestamp = min($this->createdSubscriberIds); + if ($minTimestamp) { + $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_CREATED, $minTimestamp); + } } } private function notifyUpdates(): void { - foreach ($this->updatedSubscriberIds as $subscriberId => $updatedAt) { - // do not notify about changes when subscriber is new - if (isset($this->createdSubscriberIds[$subscriberId])) { - continue; + // unset updated subscribers if subscriber is created + foreach ($this->createdSubscriberIds as $subscriberId => $timestamp) { + unset($this->updatedSubscriberIds[$subscriberId]); + } + + if (count($this->updatedSubscriberIds) === 1) { + foreach ($this->updatedSubscriberIds as $subscriberId => $updatedAt) { + $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, $subscriberId); + } + } elseif ($this->updatedSubscriberIds) { + $minTimestamp = min($this->updatedSubscriberIds); + if ($minTimestamp) { + $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_UPDATED, $minTimestamp); } - $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, $subscriberId, $updatedAt->getTimestamp()); } } private function notifyDeletes(): void { - foreach ($this->deletedSubscriberIds as $subscriberId => $deletedAt) { - $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, $subscriberId, $deletedAt->getTimestamp()); + if (count($this->deletedSubscriberIds) === 1) { + foreach ($this->deletedSubscriberIds as $subscriberId => $updatedAt) { + $this->wp->doAction(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, $subscriberId); + } + } elseif ($this->deletedSubscriberIds) { + $this->wp->doAction(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_DELETED, array_keys($this->deletedSubscriberIds)); } } public function subscriberCreated(int $subscriberId): void { // store id as a key and timestamp change as the value - $this->createdSubscriberIds[$subscriberId] = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'), 'UTC'); + $this->createdSubscriberIds[$subscriberId] = $this->getTimestamp(); } public function subscriberUpdated(int $subscriberId): void { // store id as a key and timestamp change as the value - $this->updatedSubscriberIds[$subscriberId] = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'), 'UTC'); + $this->updatedSubscriberIds[$subscriberId] = $this->getTimestamp(); } public function subscriberDeleted(int $subscriberId): void { // store id as a key and timestamp change as the value - $this->deletedSubscriberIds[$subscriberId] = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'), 'UTC'); + $this->deletedSubscriberIds[$subscriberId] = $this->getTimestamp(); } public function subscribersCreated(array $subscriberIds): void { @@ -86,4 +105,9 @@ class SubscriberChangesNotifier { $this->subscriberDeleted($subscriberId); } } + + private function getTimestamp(): int { + $dateTime = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'), 'UTC'); + return $dateTime->getTimestamp(); + } } diff --git a/mailpoet/lib/Entities/SubscriberEntity.php b/mailpoet/lib/Entities/SubscriberEntity.php index 22aed175ac..c82241d47c 100644 --- a/mailpoet/lib/Entities/SubscriberEntity.php +++ b/mailpoet/lib/Entities/SubscriberEntity.php @@ -25,6 +25,9 @@ class SubscriberEntity { public const HOOK_SUBSCRIBER_CREATED = 'mailpoet_subscriber_created'; public const HOOK_SUBSCRIBER_DELETED = 'mailpoet_subscriber_deleted'; public const HOOK_SUBSCRIBER_UPDATED = 'mailpoet_subscriber_updated'; + public const HOOK_MULTIPLE_SUBSCRIBERS_CREATED = 'mailpoet_multiple_subscribers_created'; + public const HOOK_MULTIPLE_SUBSCRIBERS_DELETED = 'mailpoet_multiple_subscribers_deleted'; + public const HOOK_MULTIPLE_SUBSCRIBERS_UPDATED = 'mailpoet_multiple_subscribers_updated'; // statuses const STATUS_BOUNCED = 'bounced'; diff --git a/mailpoet/tests/unit/Config/SubscriberChangesNotifierTest.php b/mailpoet/tests/unit/Config/SubscriberChangesNotifierTest.php index 72981792b5..3bb64ec240 100644 --- a/mailpoet/tests/unit/Config/SubscriberChangesNotifierTest.php +++ b/mailpoet/tests/unit/Config/SubscriberChangesNotifierTest.php @@ -16,55 +16,91 @@ class SubscriberChangesNotifierTest extends \MailPoetUnitTest { $this->wpFunctions = $this->createMock(WPFunctions::class); } - public function testItNotifyCreatedSubscriberIds(): void { + public function testItNotifyCreatedSubscriberId(): void { $this->wpFunctions->method('currentTime') ->willReturn(1234); - $this->wpFunctions->expects($this->at(2)) + $this->wpFunctions->expects($this->at(1)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 6, 1234) - ->willReturn(true); - - $this->wpFunctions->expects($this->at(3)) - ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 10, 1234) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 6) ->willReturn(true); $notifier = new SubscriberChangesNotifier($this->wpFunctions); $notifier->subscriberCreated(6); - $notifier->subscriberCreated(10); $notifier->notify(); } - public function testItNotifyUpdatedSubscriberIds(): void { - $this->wpFunctions->method('currentTime') - ->willReturn(4567); + public function testItNotifyMultipleSubscribersCreated(): void { + $this->wpFunctions->expects($this->at(0)) + ->method('currentTime') + ->willReturn(1234); + $this->wpFunctions->expects($this->at(1)) + ->method('currentTime') + ->willReturn(3456); $this->wpFunctions->expects($this->at(2)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, 2, 4567) + ->with(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_CREATED, 1234) ->willReturn(true); - $this->wpFunctions->expects($this->at(3)) + $notifier = new SubscriberChangesNotifier($this->wpFunctions); + $notifier->subscriberCreated(4); + $notifier->subscriberCreated(6); + $notifier->notify(); + } + + public function testItNotifyUpdatedSubscriberId(): void { + $this->wpFunctions->method('currentTime') + ->willReturn(4567); + $this->wpFunctions->expects($this->at(1)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, 11, 4567) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, 2) ->willReturn(true); $notifier = new SubscriberChangesNotifier($this->wpFunctions); $notifier->subscriberUpdated(2); - $notifier->subscriberUpdated(11); $notifier->notify(); } - public function testItNotifyDeletedSubscriberIds(): void { - $this->wpFunctions->method('currentTime') - ->willReturn(3456); + public function testItNotifyMultipleSubscribersUpdated(): void { + $this->wpFunctions->expects($this->at(0)) + ->method('currentTime') + ->willReturn(12345); + $this->wpFunctions->expects($this->at(1)) + ->method('currentTime') + ->willReturn(1234); $this->wpFunctions->expects($this->at(2)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, 1, 3456) + ->with(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_UPDATED, 1234) ->willReturn(true); - $this->wpFunctions->expects($this->at(3)) + $notifier = new SubscriberChangesNotifier($this->wpFunctions); + $notifier->subscriberUpdated(2); + $notifier->subscriberUpdated(41); + $notifier->notify(); + } + + public function testItNotifyDeletedSubscriberId(): void { + $this->wpFunctions->method('currentTime') + ->willReturn(3456); + $this->wpFunctions->expects($this->at(1)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, 12, 3456) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, 1) + ->willReturn(true); + + $notifier = new SubscriberChangesNotifier($this->wpFunctions); + $notifier->subscriberDeleted(1); + $notifier->notify(); + } + + public function testItNotifyMultipleSubscribersDeleted(): void { + $this->wpFunctions->expects($this->at(0)) + ->method('currentTime') + ->willReturn(3456); + $this->wpFunctions->expects($this->at(1)) + ->method('currentTime') + ->willReturn(98712); + $this->wpFunctions->expects($this->at(2)) + ->method('doAction') + ->with(SubscriberEntity::HOOK_MULTIPLE_SUBSCRIBERS_DELETED, [1, 12]) ->willReturn(true); $notifier = new SubscriberChangesNotifier($this->wpFunctions); @@ -78,17 +114,17 @@ class SubscriberChangesNotifierTest extends \MailPoetUnitTest { ->willReturn(12345); $this->wpFunctions->expects($this->at(3)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 1, 12345) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 1) ->willReturn(true); $this->wpFunctions->expects($this->at(4)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, 3, 12345) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_UPDATED, 3) ->willReturn(true); $this->wpFunctions->expects($this->at(5)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, 5, 12345) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_DELETED, 5) ->willReturn(true); $notifier = new SubscriberChangesNotifier($this->wpFunctions); @@ -103,7 +139,7 @@ class SubscriberChangesNotifierTest extends \MailPoetUnitTest { ->willReturn(1235); $this->wpFunctions->expects($this->at(2)) ->method('doAction') - ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 11, 1235) + ->with(SubscriberEntity::HOOK_SUBSCRIBER_CREATED, 11) ->willReturn(true); $notifier = new SubscriberChangesNotifier($this->wpFunctions);