Clicks.php
4 days ago
Opens.php
4 days ago
PageViewCookie.php
2 months ago
SubscriberActivityTracker.php
4 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
Opens.php
110 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Statistics\Track; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\SendingQueueEntity; |
| 10 | use MailPoet\Entities\StatisticsOpenEntity; |
| 11 | use MailPoet\Entities\SubscriberEntity; |
| 12 | use MailPoet\Entities\UserAgentEntity; |
| 13 | use MailPoet\Statistics\StatisticsOpensRepository; |
| 14 | use MailPoet\Statistics\UserAgentsRepository; |
| 15 | use MailPoet\Subscribers\SubscribersRepository; |
| 16 | use MailPoet\Subscribers\TrackingConsentController; |
| 17 | |
| 18 | class Opens { |
| 19 | /** @var StatisticsOpensRepository */ |
| 20 | private $statisticsOpensRepository; |
| 21 | |
| 22 | /** @var UserAgentsRepository */ |
| 23 | private $userAgentsRepository; |
| 24 | |
| 25 | /** @var SubscribersRepository */ |
| 26 | private $subscribersRepository; |
| 27 | |
| 28 | /** @var TrackingConsentController */ |
| 29 | private $trackingConsentController; |
| 30 | |
| 31 | public function __construct( |
| 32 | StatisticsOpensRepository $statisticsOpensRepository, |
| 33 | UserAgentsRepository $userAgentsRepository, |
| 34 | SubscribersRepository $subscribersRepository, |
| 35 | TrackingConsentController $trackingConsentController |
| 36 | ) { |
| 37 | $this->statisticsOpensRepository = $statisticsOpensRepository; |
| 38 | $this->userAgentsRepository = $userAgentsRepository; |
| 39 | $this->subscribersRepository = $subscribersRepository; |
| 40 | $this->trackingConsentController = $trackingConsentController; |
| 41 | } |
| 42 | |
| 43 | public function track($data, $displayImage = true) { |
| 44 | if (!$data) { |
| 45 | return $this->returnResponse($displayImage); |
| 46 | } |
| 47 | /** @var SubscriberEntity $subscriber */ |
| 48 | $subscriber = $data->subscriber; |
| 49 | // No tracking consent (CNIL/Garante): serve the image but record nothing — |
| 50 | // no statistics, no engagement update. This is the backstop for emails |
| 51 | // already sent; future sends have the pixel removed entirely (see |
| 52 | // Newsletter::prepareNewsletterForSending). |
| 53 | if (!$this->trackingConsentController->isTrackingAllowed($subscriber)) { |
| 54 | return $this->returnResponse($displayImage); |
| 55 | } |
| 56 | /** @var SendingQueueEntity $queue */ |
| 57 | $queue = $data->queue; |
| 58 | /** @var NewsletterEntity $newsletter */ |
| 59 | $newsletter = $data->newsletter; |
| 60 | $wpUserPreview = ($data->preview && ($subscriber->isWPUser())); |
| 61 | // log statistics only if the action did not come from |
| 62 | // a WP user previewing the newsletter |
| 63 | if (!$wpUserPreview) { |
| 64 | $oldStatistics = $this->statisticsOpensRepository->findOneBy([ |
| 65 | 'subscriber' => $subscriber->getId(), |
| 66 | 'newsletter' => $newsletter->getId(), |
| 67 | 'queue' => $queue->getId(), |
| 68 | ]); |
| 69 | // Open was already tracked |
| 70 | if ($oldStatistics) { |
| 71 | if (!empty($data->userAgent)) { |
| 72 | $userAgent = $this->userAgentsRepository->findOrCreate($data->userAgent); |
| 73 | if ( |
| 74 | $userAgent->getUserAgentType() === UserAgentEntity::USER_AGENT_TYPE_HUMAN |
| 75 | || $oldStatistics->getUserAgentType() === UserAgentEntity::USER_AGENT_TYPE_MACHINE |
| 76 | ) { |
| 77 | $oldStatistics->setUserAgent($userAgent); |
| 78 | $oldStatistics->setUserAgentType($userAgent->getUserAgentType()); |
| 79 | $this->statisticsOpensRepository->flush(); |
| 80 | } |
| 81 | } |
| 82 | $this->subscribersRepository->maybeUpdateLastOpenAt($subscriber); |
| 83 | return $this->returnResponse($displayImage); |
| 84 | } |
| 85 | $statistics = new StatisticsOpenEntity($newsletter, $queue, $subscriber); |
| 86 | if (!empty($data->userAgent)) { |
| 87 | $userAgent = $this->userAgentsRepository->findOrCreate($data->userAgent); |
| 88 | $statistics->setUserAgent($userAgent); |
| 89 | $statistics->setUserAgentType($userAgent->getUserAgentType()); |
| 90 | } |
| 91 | $this->statisticsOpensRepository->persist($statistics); |
| 92 | $this->statisticsOpensRepository->flush(); |
| 93 | $this->subscribersRepository->maybeUpdateLastOpenAt($subscriber); |
| 94 | $this->statisticsOpensRepository->recalculateSubscriberScore($subscriber); |
| 95 | } |
| 96 | return $this->returnResponse($displayImage); |
| 97 | } |
| 98 | |
| 99 | public function returnResponse($displayImage) { |
| 100 | if (!$displayImage) return; |
| 101 | // return 1x1 pixel transparent gif image |
| 102 | header('Content-Type: image/gif'); |
| 103 | |
| 104 | // Output of base64_decode is predetermined and safe in this case |
| 105 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 106 | echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='); |
| 107 | exit; |
| 108 | } |
| 109 | } |
| 110 |