Clicks.php
9 months ago
Opens.php
2 years ago
PageViewCookie.php
4 months ago
SubscriberActivityTracker.php
2 years ago
SubscriberCookie.php
4 months ago
SubscriberHandler.php
1 year ago
Unsubscribes.php
4 months ago
WooCommercePurchases.php
2 years ago
index.php
3 years ago
Opens.php
97 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 | |
| 17 | class Opens { |
| 18 | /** @var StatisticsOpensRepository */ |
| 19 | private $statisticsOpensRepository; |
| 20 | |
| 21 | /** @var UserAgentsRepository */ |
| 22 | private $userAgentsRepository; |
| 23 | |
| 24 | /** @var SubscribersRepository */ |
| 25 | private $subscribersRepository; |
| 26 | |
| 27 | public function __construct( |
| 28 | StatisticsOpensRepository $statisticsOpensRepository, |
| 29 | UserAgentsRepository $userAgentsRepository, |
| 30 | SubscribersRepository $subscribersRepository |
| 31 | ) { |
| 32 | $this->statisticsOpensRepository = $statisticsOpensRepository; |
| 33 | $this->userAgentsRepository = $userAgentsRepository; |
| 34 | $this->subscribersRepository = $subscribersRepository; |
| 35 | } |
| 36 | |
| 37 | public function track($data, $displayImage = true) { |
| 38 | if (!$data) { |
| 39 | return $this->returnResponse($displayImage); |
| 40 | } |
| 41 | /** @var SubscriberEntity $subscriber */ |
| 42 | $subscriber = $data->subscriber; |
| 43 | /** @var SendingQueueEntity $queue */ |
| 44 | $queue = $data->queue; |
| 45 | /** @var NewsletterEntity $newsletter */ |
| 46 | $newsletter = $data->newsletter; |
| 47 | $wpUserPreview = ($data->preview && ($subscriber->isWPUser())); |
| 48 | // log statistics only if the action did not come from |
| 49 | // a WP user previewing the newsletter |
| 50 | if (!$wpUserPreview) { |
| 51 | $oldStatistics = $this->statisticsOpensRepository->findOneBy([ |
| 52 | 'subscriber' => $subscriber->getId(), |
| 53 | 'newsletter' => $newsletter->getId(), |
| 54 | 'queue' => $queue->getId(), |
| 55 | ]); |
| 56 | // Open was already tracked |
| 57 | if ($oldStatistics) { |
| 58 | if (!empty($data->userAgent)) { |
| 59 | $userAgent = $this->userAgentsRepository->findOrCreate($data->userAgent); |
| 60 | if ( |
| 61 | $userAgent->getUserAgentType() === UserAgentEntity::USER_AGENT_TYPE_HUMAN |
| 62 | || $oldStatistics->getUserAgentType() === UserAgentEntity::USER_AGENT_TYPE_MACHINE |
| 63 | ) { |
| 64 | $oldStatistics->setUserAgent($userAgent); |
| 65 | $oldStatistics->setUserAgentType($userAgent->getUserAgentType()); |
| 66 | $this->statisticsOpensRepository->flush(); |
| 67 | } |
| 68 | } |
| 69 | $this->subscribersRepository->maybeUpdateLastOpenAt($subscriber); |
| 70 | return $this->returnResponse($displayImage); |
| 71 | } |
| 72 | $statistics = new StatisticsOpenEntity($newsletter, $queue, $subscriber); |
| 73 | if (!empty($data->userAgent)) { |
| 74 | $userAgent = $this->userAgentsRepository->findOrCreate($data->userAgent); |
| 75 | $statistics->setUserAgent($userAgent); |
| 76 | $statistics->setUserAgentType($userAgent->getUserAgentType()); |
| 77 | } |
| 78 | $this->statisticsOpensRepository->persist($statistics); |
| 79 | $this->statisticsOpensRepository->flush(); |
| 80 | $this->subscribersRepository->maybeUpdateLastOpenAt($subscriber); |
| 81 | $this->statisticsOpensRepository->recalculateSubscriberScore($subscriber); |
| 82 | } |
| 83 | return $this->returnResponse($displayImage); |
| 84 | } |
| 85 | |
| 86 | public function returnResponse($displayImage) { |
| 87 | if (!$displayImage) return; |
| 88 | // return 1x1 pixel transparent gif image |
| 89 | header('Content-Type: image/gif'); |
| 90 | |
| 91 | // Output of base64_decode is predetermined and safe in this case |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 93 | echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='); |
| 94 | exit; |
| 95 | } |
| 96 | } |
| 97 |