This commit changes the behavior of the optin checkbox that MailPoet adds to the WooCommerce checkout. Now the checkbox is not checked by default for logged in users who already subscribed to a list. Also, users won't be unsubscribed anymore and have their global status change to unsubscribed if they uncheck the checkbox. The only action that is performed after this change is to subscribe users if they check the checkbox. This change was implemented both for the normal checkout and the block checkout. It was also necessary to remove the AutomateWoo integration that depended on subscribers being unsubscribed during checkout. [MAILPOET-4178]
73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\WooCommerce\Integrations;
|
|
|
|
use MailPoet\Entities\SubscriberEntity;
|
|
use MailPoet\Subscribers\SubscribersRepository;
|
|
use MailPoet\WP\Functions as WPFunctions;
|
|
|
|
class AutomateWooHooks {
|
|
const AUTOMATE_WOO_PLUGIN_SLUG = 'automatewoo/automatewoo.php';
|
|
|
|
/** @var SubscribersRepository */
|
|
private $subscribersRepository;
|
|
|
|
/** @var WPFunctions */
|
|
private $wp;
|
|
|
|
public function __construct(
|
|
SubscribersRepository $subscribersRepository,
|
|
WPFunctions $wp
|
|
) {
|
|
$this->subscribersRepository = $subscribersRepository;
|
|
$this->wp = $wp;
|
|
}
|
|
|
|
public function isAutomateWooActive(): bool {
|
|
return $this->wp->isPluginActive(self::AUTOMATE_WOO_PLUGIN_SLUG);
|
|
}
|
|
|
|
public function areMethodsAvailable(): bool {
|
|
return class_exists('AutomateWoo\Customer_Factory') && method_exists('AutomateWoo\Customer_Factory', 'get_by_email') &&
|
|
class_exists('AutomateWoo\Customer') && method_exists('AutomateWoo\Customer', 'opt_out');
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
|
|
public function setup(): void {
|
|
if (!$this->isAutomateWooActive() || !$this->areMethodsAvailable()) {
|
|
return;
|
|
}
|
|
$this->wp->addAction(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED, [$this, 'maybeOptOutSubscriber'], 10, 1);
|
|
}
|
|
|
|
public function optOutSubscriber($subscriber): void {
|
|
if (!$this->isAutomateWooActive() || !$this->areMethodsAvailable()) {
|
|
return;
|
|
}
|
|
|
|
$automateWooCustomer = $this->getAutomateWooCustomer($subscriber->getEmail());
|
|
if (!$automateWooCustomer) {
|
|
return;
|
|
}
|
|
|
|
$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);
|
|
}
|
|
}
|