Clicks.php
5 days ago
Opens.php
5 days ago
PageViewCookie.php
2 months ago
SubscriberActivityTracker.php
5 days ago
SubscriberCookie.php
2 months ago
SubscriberHandler.php
1 year ago
Unsubscribes.php
2 months ago
WooCommercePurchases.php
2 years ago
index.php
3 years ago
SubscriberActivityTracker.php
156 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Statistics\Track; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\SubscriberEntity; |
| 9 | use MailPoet\Settings\TrackingConfig; |
| 10 | use MailPoet\Subscribers\SubscribersRepository; |
| 11 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | use MailPoetVendor\Doctrine\DBAL\Exception\InvalidFieldNameException; |
| 14 | use MailPoetVendor\Doctrine\DBAL\Exception\TableNotFoundException; |
| 15 | |
| 16 | class SubscriberActivityTracker { |
| 17 | |
| 18 | const TRACK_INTERVAL = 60; // 1 minute |
| 19 | |
| 20 | /** @var PageViewCookie */ |
| 21 | private $pageViewCookie; |
| 22 | |
| 23 | /** @var SubscriberCookie */ |
| 24 | private $subscriberCookie; |
| 25 | |
| 26 | /** @var SubscribersRepository */ |
| 27 | private $subscribersRepository; |
| 28 | |
| 29 | /** @var WPFunctions */ |
| 30 | private $wp; |
| 31 | |
| 32 | /** @var TrackingConfig */ |
| 33 | private $trackingConfig; |
| 34 | |
| 35 | /** @var callable[] */ |
| 36 | private $callbacks = []; |
| 37 | |
| 38 | /** @var WooCommerceHelper */ |
| 39 | private $wooCommerceHelper; |
| 40 | |
| 41 | public function __construct( |
| 42 | PageViewCookie $pageViewCookie, |
| 43 | SubscriberCookie $subscriberCookie, |
| 44 | SubscribersRepository $subscribersRepository, |
| 45 | WPFunctions $wp, |
| 46 | WooCommerceHelper $wooCommerceHelper, |
| 47 | TrackingConfig $trackingConfig |
| 48 | ) { |
| 49 | $this->pageViewCookie = $pageViewCookie; |
| 50 | $this->subscriberCookie = $subscriberCookie; |
| 51 | $this->subscribersRepository = $subscribersRepository; |
| 52 | $this->wp = $wp; |
| 53 | $this->wooCommerceHelper = $wooCommerceHelper; |
| 54 | $this->trackingConfig = $trackingConfig; |
| 55 | } |
| 56 | |
| 57 | public function trackActivity(): bool { |
| 58 | // Don't track in admin interface |
| 59 | if ($this->wp->isAdmin()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | $subscriber = null; |
| 64 | $latestTimestamp = $this->getLatestTimestampFromCookie(); |
| 65 | |
| 66 | // If cookie tracking is not allowed try use last activity from subscriber data |
| 67 | if ($latestTimestamp === null) { |
| 68 | $subscriber = $this->getSubscriber(); |
| 69 | if (!$subscriber) { |
| 70 | return false; // Can't determine timestamp |
| 71 | } |
| 72 | $latestTimestamp = $this->getLatestTimestampFromSubscriber($subscriber); |
| 73 | } |
| 74 | |
| 75 | if ($latestTimestamp + self::TRACK_INTERVAL > $this->wp->currentTime('timestamp', true)) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if ($subscriber === null) { |
| 80 | $subscriber = $this->getSubscriber(); |
| 81 | } |
| 82 | |
| 83 | if (!$subscriber) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $this->processTracking($subscriber); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | public function registerCallback(string $slug, callable $callback): void { |
| 92 | $this->callbacks[$slug] = $callback; |
| 93 | } |
| 94 | |
| 95 | public function unregisterCallback(string $slug): void { |
| 96 | unset($this->callbacks[$slug]); |
| 97 | } |
| 98 | |
| 99 | private function processTracking(SubscriberEntity $subscriber): void { |
| 100 | $this->subscribersRepository->maybeUpdateLastPageViewAt($subscriber); |
| 101 | $this->pageViewCookie->setPageViewTimestamp($this->wp->currentTime('timestamp', true)); |
| 102 | foreach ($this->callbacks as $callback) { |
| 103 | $callback($subscriber); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private function getLatestTimestampFromCookie(): ?int { |
| 108 | if ($this->trackingConfig->isCookieTrackingEnabled()) { |
| 109 | return $this->pageViewCookie->getPageViewTimestamp() ?? 0; |
| 110 | } |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | private function getLatestTimestampFromSubscriber(SubscriberEntity $subscriber): int { |
| 115 | return $subscriber->getLastEngagementAt() ? $subscriber->getLastEngagementAt()->getTimestamp() : 0; |
| 116 | } |
| 117 | |
| 118 | private function getSubscriber(): ?SubscriberEntity { |
| 119 | try { |
| 120 | return $this->findSubscriber(); |
| 121 | } catch (InvalidFieldNameException | TableNotFoundException $e) { |
| 122 | // The subscribers schema may be mid-migration during a plugin update (e.g. the |
| 123 | // tracking_consent column added in STOMAIL-8268). Skip activity tracking for this |
| 124 | // request rather than aborting Initializer::initialize(), which would take the whole |
| 125 | // mailpoet/v1 REST namespace down with it. Tracking resumes on the next request once |
| 126 | // the migration completes. |
| 127 | return null; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private function findSubscriber(): ?SubscriberEntity { |
| 132 | $wpUser = $this->wp->wpGetCurrentUser(); |
| 133 | if ($wpUser->exists()) { |
| 134 | return $this->subscribersRepository->findOneBy(['wpUserId' => $wpUser->ID]); |
| 135 | } |
| 136 | |
| 137 | $subscriberId = $this->subscriberCookie->getSubscriberId(); |
| 138 | if ($subscriberId) { |
| 139 | return $this->subscribersRepository->findOneById($subscriberId); |
| 140 | } |
| 141 | |
| 142 | if (!$this->wooCommerceHelper->isWooCommerceActive()) { |
| 143 | return null; |
| 144 | } |
| 145 | $wooCommerce = $this->wooCommerceHelper->WC(); |
| 146 | if (!$wooCommerce || !$wooCommerce->session) { |
| 147 | return null; |
| 148 | } |
| 149 | $customer = $wooCommerce->session->get('customer'); |
| 150 | if (!is_array($customer) || empty($customer['email'])) { |
| 151 | return null; |
| 152 | } |
| 153 | return $this->subscribersRepository->findOneBy(['email' => $customer['email']]); |
| 154 | } |
| 155 | } |
| 156 |