diff --git a/mailpoet/lib/WooCommerce/Integrations/AutomateWooHooks.php b/mailpoet/lib/WooCommerce/Integrations/AutomateWooHooks.php index cd9127133f..0cea9406db 100644 --- a/mailpoet/lib/WooCommerce/Integrations/AutomateWooHooks.php +++ b/mailpoet/lib/WooCommerce/Integrations/AutomateWooHooks.php @@ -32,7 +32,10 @@ class AutomateWooHooks { class_exists('AutomateWoo\Customer') && method_exists('AutomateWoo\Customer', 'opt_out'); } - public function getAutomateWooCustomer(string $email): ?\AutomateWoo\Customer { + /** + * @return \AutomateWoo\Customer|false + */ + public function getAutomateWooCustomer(string $email) { // AutomateWoo\Customer_Factory::get_by_email() returns false if customer is not found // Second parameter is set to false to prevent creating new customer if not found return \AutomateWoo\Customer_Factory::get_by_email($email, false); @@ -43,18 +46,14 @@ class AutomateWooHooks { return; } $this->wp->addAction(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED, [$this, 'maybeOptOutSubscriber'], 10, 1); + $this->wp->addAction('mailpoet_woocommerce_segment_unsubscribed', [$this, 'optOutSubscriber'], 10, 1); } - public function maybeOptOutSubscriber(int $subscriberId): void { + public function optOutSubscriber($subscriber): void { if (!$this->isAutomateWooActive() || !$this->areMethodsAvailable()) { return; } - $subscriber = $this->subscribersRepository->findOneById($subscriberId); - if (!$subscriber || !$subscriber->getEmail() || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) { - return; - } - $automateWooCustomer = $this->getAutomateWooCustomer($subscriber->getEmail()); if (!$automateWooCustomer) { return; @@ -62,4 +61,13 @@ class AutomateWooHooks { $automateWooCustomer->opt_out(); } + + public function maybeOptOutSubscriber(int $subscriberId): void { + $subscriber = $this->subscribersRepository->findOneById($subscriberId); + if (!$subscriber || !$subscriber->getEmail() || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) { + return; + } + + $this->optOutSubscriber($subscriber); + } } diff --git a/mailpoet/lib/WooCommerce/Subscription.php b/mailpoet/lib/WooCommerce/Subscription.php index 3ceefe18d4..53c756bc1f 100644 --- a/mailpoet/lib/WooCommerce/Subscription.php +++ b/mailpoet/lib/WooCommerce/Subscription.php @@ -223,6 +223,7 @@ class Subscription { $signupConfirmation = $this->settings->get('signup_confirmation'); if (!$checkoutOptin) { + $this->wp->doAction('mailpoet_woocommerce_segment_unsubscribed', $subscriber); // Opt-in is disabled or checkbox is unchecked $this->subscriberSegmentRepository->unsubscribeFromSegments($subscriber, [$wcSegment]); diff --git a/mailpoet/tests/acceptance/Subscriptions/WooCheckoutAutomateWooSubscriptionsCest.php b/mailpoet/tests/acceptance/Subscriptions/WooCheckoutAutomateWooSubscriptionsCest.php index 0d9e898d42..bfdf57172e 100644 --- a/mailpoet/tests/acceptance/Subscriptions/WooCheckoutAutomateWooSubscriptionsCest.php +++ b/mailpoet/tests/acceptance/Subscriptions/WooCheckoutAutomateWooSubscriptionsCest.php @@ -77,4 +77,20 @@ class WooCheckoutAutomateWooSubscriptionsCest { $i->see($customerEmail, '.automatewoo-content'); $i->seeConfirmationEmailWasReceived(); } + + public function checkoutOptInCheckedAndUnchecked(\AcceptanceTester $i) { + $this->settingsFactory->withWooCommerceCheckoutOptinEnabled(); + $this->settingsFactory->withConfirmationEmailEnabled(); + $customerEmail = 'woo_guest_check@example.com'; + $i->orderProductWithRegistration($this->product, $customerEmail, true); + $i->login(); + $i->checkSubscriberStatusAndLists($customerEmail, SubscriberEntity::STATUS_UNCONFIRMED, ['WooCommerce Customers']); + $i->amOnPage('/wp-admin/admin.php?page=automatewoo-opt-ins'); + $i->see($customerEmail, '.automatewoo-content'); + $i->logout(); + $i->orderProductWithoutRegistration($this->product, $customerEmail, false); + $i->login(); + $i->amOnPage('/wp-admin/admin.php?page=automatewoo-opt-ins'); + $i->dontSee($customerEmail, '.automatewoo-content'); + } } diff --git a/mailpoet/tests/integration/WooCommerce/Integration/AutomateWooHooksTest.php b/mailpoet/tests/integration/WooCommerce/Integration/AutomateWooHooksTest.php index f6806e5d6e..941743f2be 100644 --- a/mailpoet/tests/integration/WooCommerce/Integration/AutomateWooHooksTest.php +++ b/mailpoet/tests/integration/WooCommerce/Integration/AutomateWooHooksTest.php @@ -41,13 +41,16 @@ class AutomateWooHooksTest extends \MailPoetTest { } return false; }, - 'addAction' => function($name, $callback) { - expect($name)->equals(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED); - }, + 'addAction' => Expected::exactly(2), ]); - $subscribersRepository = $this->createMock(SubscribersRepository::class); - $automateWooHooks = new AutomateWooHooks($subscribersRepository, $wp); - $automateWooHooks->setup(); + + $automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class) + ->setConstructorArgs([$this->subscribersRepository, $wp]) + ->onlyMethods(['areMethodsAvailable']) + ->getMock(); + $automateWooHooksPartialMock->expects($this->once())->method('areMethodsAvailable')->willReturn(true); + + $automateWooHooksPartialMock->setup(); } public function testOptsOutUnsubscribedSubscriber() { @@ -58,12 +61,10 @@ class AutomateWooHooksTest extends \MailPoetTest { $automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class) ->setConstructorArgs([$this->subscribersRepository, $this->wp]) - ->onlyMethods(['getAutomateWooCustomer']) + ->onlyMethods(['optOutSubscriber']) ->getMock(); - $automateWooCustomer = $this->make(new \AutomateWoo\Customer, ['opt_out' => Expected::once(function() { - })]); - $automateWooHooksPartialMock->expects($this->once())->method('getAutomateWooCustomer')->willReturn($automateWooCustomer); + $automateWooHooksPartialMock->expects($this->once())->method('optOutSubscriber'); $automateWooHooksPartialMock->maybeOptOutSubscriber((int)$unsubscribedSubscriber->getId()); } @@ -76,9 +77,9 @@ class AutomateWooHooksTest extends \MailPoetTest { $automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class) ->setConstructorArgs([$this->subscribersRepository, $this->wp]) - ->onlyMethods(['getAutomateWooCustomer']) + ->onlyMethods(['optOutSubscriber']) ->getMock(); - $automateWooHooksPartialMock->expects($this->never())->method('getAutomateWooCustomer'); + $automateWooHooksPartialMock->expects($this->never())->method('optOutSubscriber'); $automateWooHooksPartialMock->maybeOptOutSubscriber((int)$subscribedSubscriber->getId()); }