Server IP : 104.21.14.103 / Your IP : 18.220.203.200 Web Server : LiteSpeed System : Linux business53.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : giankuin ( 1871) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/self/root/home/giankuin/thietke365.net/wp-content/plugins/mailpoet/lib/WooCommerce/Integrations/ |
Upload File : |
<?php declare(strict_types = 1); namespace MailPoet\WooCommerce\Integrations; if (!defined('ABSPATH')) exit; 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); } }