Clicks.php
9 months ago
Opens.php
1 year ago
PageViewCookie.php
3 months ago
SubscriberActivityTracker.php
2 years ago
SubscriberCookie.php
3 months ago
SubscriberHandler.php
1 year ago
Unsubscribes.php
3 months ago
WooCommercePurchases.php
2 years ago
index.php
3 years ago
SubscriberActivityTracker.php
141 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 | |
| 14 | class SubscriberActivityTracker { |
| 15 | |
| 16 | const TRACK_INTERVAL = 60; // 1 minute |
| 17 | |
| 18 | /** @var PageViewCookie */ |
| 19 | private $pageViewCookie; |
| 20 | |
| 21 | /** @var SubscriberCookie */ |
| 22 | private $subscriberCookie; |
| 23 | |
| 24 | /** @var SubscribersRepository */ |
| 25 | private $subscribersRepository; |
| 26 | |
| 27 | /** @var WPFunctions */ |
| 28 | private $wp; |
| 29 | |
| 30 | /** @var TrackingConfig */ |
| 31 | private $trackingConfig; |
| 32 | |
| 33 | /** @var callable[] */ |
| 34 | private $callbacks = []; |
| 35 | |
| 36 | /** @var WooCommerceHelper */ |
| 37 | private $wooCommerceHelper; |
| 38 | |
| 39 | public function __construct( |
| 40 | PageViewCookie $pageViewCookie, |
| 41 | SubscriberCookie $subscriberCookie, |
| 42 | SubscribersRepository $subscribersRepository, |
| 43 | WPFunctions $wp, |
| 44 | WooCommerceHelper $wooCommerceHelper, |
| 45 | TrackingConfig $trackingConfig |
| 46 | ) { |
| 47 | $this->pageViewCookie = $pageViewCookie; |
| 48 | $this->subscriberCookie = $subscriberCookie; |
| 49 | $this->subscribersRepository = $subscribersRepository; |
| 50 | $this->wp = $wp; |
| 51 | $this->wooCommerceHelper = $wooCommerceHelper; |
| 52 | $this->trackingConfig = $trackingConfig; |
| 53 | } |
| 54 | |
| 55 | public function trackActivity(): bool { |
| 56 | // Don't track in admin interface |
| 57 | if ($this->wp->isAdmin()) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | $subscriber = null; |
| 62 | $latestTimestamp = $this->getLatestTimestampFromCookie(); |
| 63 | |
| 64 | // If cookie tracking is not allowed try use last activity from subscriber data |
| 65 | if ($latestTimestamp === null) { |
| 66 | $subscriber = $this->getSubscriber(); |
| 67 | if (!$subscriber) { |
| 68 | return false; // Can't determine timestamp |
| 69 | } |
| 70 | $latestTimestamp = $this->getLatestTimestampFromSubscriber($subscriber); |
| 71 | } |
| 72 | |
| 73 | if ($latestTimestamp + self::TRACK_INTERVAL > $this->wp->currentTime('timestamp', true)) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | if ($subscriber === null) { |
| 78 | $subscriber = $this->getSubscriber(); |
| 79 | } |
| 80 | |
| 81 | if (!$subscriber) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | $this->processTracking($subscriber); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | public function registerCallback(string $slug, callable $callback): void { |
| 90 | $this->callbacks[$slug] = $callback; |
| 91 | } |
| 92 | |
| 93 | public function unregisterCallback(string $slug): void { |
| 94 | unset($this->callbacks[$slug]); |
| 95 | } |
| 96 | |
| 97 | private function processTracking(SubscriberEntity $subscriber): void { |
| 98 | $this->subscribersRepository->maybeUpdateLastPageViewAt($subscriber); |
| 99 | $this->pageViewCookie->setPageViewTimestamp($this->wp->currentTime('timestamp', true)); |
| 100 | foreach ($this->callbacks as $callback) { |
| 101 | $callback($subscriber); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private function getLatestTimestampFromCookie(): ?int { |
| 106 | if ($this->trackingConfig->isCookieTrackingEnabled()) { |
| 107 | return $this->pageViewCookie->getPageViewTimestamp() ?? 0; |
| 108 | } |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | private function getLatestTimestampFromSubscriber(SubscriberEntity $subscriber): int { |
| 113 | return $subscriber->getLastEngagementAt() ? $subscriber->getLastEngagementAt()->getTimestamp() : 0; |
| 114 | } |
| 115 | |
| 116 | private function getSubscriber(): ?SubscriberEntity { |
| 117 | $wpUser = $this->wp->wpGetCurrentUser(); |
| 118 | if ($wpUser->exists()) { |
| 119 | return $this->subscribersRepository->findOneBy(['wpUserId' => $wpUser->ID]); |
| 120 | } |
| 121 | |
| 122 | $subscriberId = $this->subscriberCookie->getSubscriberId(); |
| 123 | if ($subscriberId) { |
| 124 | return $this->subscribersRepository->findOneById($subscriberId); |
| 125 | } |
| 126 | |
| 127 | if (!$this->wooCommerceHelper->isWooCommerceActive()) { |
| 128 | return null; |
| 129 | } |
| 130 | $wooCommerce = $this->wooCommerceHelper->WC(); |
| 131 | if (!$wooCommerce || !$wooCommerce->session) { |
| 132 | return null; |
| 133 | } |
| 134 | $customer = $wooCommerce->session->get('customer'); |
| 135 | if (!is_array($customer) || empty($customer['email'])) { |
| 136 | return null; |
| 137 | } |
| 138 | return $this->subscribersRepository->findOneBy(['email' => $customer['email']]); |
| 139 | } |
| 140 | } |
| 141 |