Track
4 days ago
GATracking.php
6 months ago
StatisticsBouncesRepository.php
3 years ago
StatisticsClicksRepository.php
2 years ago
StatisticsFormsRepository.php
3 years ago
StatisticsNewslettersRepository.php
1 year ago
StatisticsOpensRepository.php
2 days ago
StatisticsUnsubscribesRepository.php
2 months ago
StatisticsWooCommercePurchasesRepository.php
1 year ago
UnsubscribeReasonTracker.php
2 months ago
UserAgentsRepository.php
1 year ago
index.php
3 years ago
StatisticsUnsubscribesRepository.php
102 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Statistics; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\Repository; |
| 9 | use MailPoet\Entities\NewsletterEntity; |
| 10 | use MailPoet\Entities\SendingQueueEntity; |
| 11 | use MailPoet\Entities\StatisticsUnsubscribeEntity; |
| 12 | use MailPoet\Entities\SubscriberEntity; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | /** |
| 16 | * @extends Repository<StatisticsUnsubscribeEntity> |
| 17 | */ |
| 18 | class StatisticsUnsubscribesRepository extends Repository { |
| 19 | protected function getEntityClassName() { |
| 20 | return StatisticsUnsubscribeEntity::class; |
| 21 | } |
| 22 | |
| 23 | public function getTotalForMonths(int $forMonths): int { |
| 24 | $from = (new Carbon())->subMonths($forMonths); |
| 25 | $count = $this->entityManager->createQueryBuilder() |
| 26 | ->select('count(stats.id)') |
| 27 | ->from(StatisticsUnsubscribeEntity::class, 'stats') |
| 28 | ->andWhere('stats.createdAt >= :dateTime') |
| 29 | ->setParameter('dateTime', $from) |
| 30 | ->getQuery() |
| 31 | ->getSingleScalarResult(); |
| 32 | |
| 33 | return intval($count); |
| 34 | } |
| 35 | |
| 36 | public function getCountPerMethodForMonths(int $forMonths): array { |
| 37 | $from = (new Carbon())->subMonths($forMonths); |
| 38 | return $this->entityManager->createQueryBuilder() |
| 39 | ->select('count(stats.id) as count, stats.method as method') |
| 40 | ->from(StatisticsUnsubscribeEntity::class, 'stats') |
| 41 | ->andWhere('stats.createdAt >= :dateTime') |
| 42 | ->groupBy('stats.method') |
| 43 | ->setParameter('dateTime', $from) |
| 44 | ->getQuery() |
| 45 | ->getResult(); |
| 46 | } |
| 47 | |
| 48 | public function findOneBySubscriberAndQueue(SubscriberEntity $subscriber, SendingQueueEntity $queue, NewsletterEntity $newsletter): ?StatisticsUnsubscribeEntity { |
| 49 | $statistics = $this->findOneBy([ |
| 50 | 'queue' => $queue, |
| 51 | 'newsletter' => $newsletter, |
| 52 | 'subscriber' => $subscriber, |
| 53 | ]); |
| 54 | return $statistics instanceof StatisticsUnsubscribeEntity ? $statistics : null; |
| 55 | } |
| 56 | |
| 57 | public function findLatestForSubscriber(SubscriberEntity $subscriber): ?StatisticsUnsubscribeEntity { |
| 58 | $result = $this->entityManager->createQueryBuilder() |
| 59 | ->select('stats') |
| 60 | ->from(StatisticsUnsubscribeEntity::class, 'stats') |
| 61 | ->andWhere('stats.subscriber = :subscriber') |
| 62 | ->setParameter('subscriber', $subscriber) |
| 63 | ->orderBy('stats.createdAt', 'DESC') |
| 64 | ->addOrderBy('stats.id', 'DESC') |
| 65 | ->setMaxResults(1) |
| 66 | ->getQuery() |
| 67 | ->getOneOrNullResult(); |
| 68 | |
| 69 | return $result instanceof StatisticsUnsubscribeEntity ? $result : null; |
| 70 | } |
| 71 | |
| 72 | public function getReasonCountsForNewsletter(NewsletterEntity $newsletter): array { |
| 73 | $reasonCounts = $this->entityManager->createQueryBuilder() |
| 74 | ->select('stats.reason as reason, count(stats.id) as count') |
| 75 | ->from(StatisticsUnsubscribeEntity::class, 'stats') |
| 76 | ->andWhere('stats.newsletter = :newsletter') |
| 77 | ->andWhere('stats.reason IS NOT NULL') |
| 78 | ->groupBy('stats.reason') |
| 79 | ->setParameter('newsletter', $newsletter) |
| 80 | ->getQuery() |
| 81 | ->getArrayResult(); |
| 82 | |
| 83 | $unspecifiedCount = $this->entityManager->createQueryBuilder() |
| 84 | ->select('count(stats.id)') |
| 85 | ->from(StatisticsUnsubscribeEntity::class, 'stats') |
| 86 | ->andWhere('stats.newsletter = :newsletter') |
| 87 | ->andWhere('stats.reason IS NULL') |
| 88 | ->setParameter('newsletter', $newsletter) |
| 89 | ->getQuery() |
| 90 | ->getSingleScalarResult(); |
| 91 | |
| 92 | if ((int)$unspecifiedCount > 0) { |
| 93 | $reasonCounts[] = [ |
| 94 | 'reason' => StatisticsUnsubscribeEntity::REASON_UNSPECIFIED, |
| 95 | 'count' => $unspecifiedCount, |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | return $reasonCounts; |
| 100 | } |
| 101 | } |
| 102 |