DynamicSegments
3 days ago
RestApi
1 week ago
SegmentDependencyValidator.php
3 years ago
SegmentListingRepository.php
2 weeks ago
SegmentSaveController.php
2 weeks ago
SegmentSubscribersRepository.php
1 month ago
SegmentsFinder.php
3 years ago
SegmentsRepository.php
2 weeks ago
SegmentsSimpleListRepository.php
1 month ago
SubscribersFinder.php
1 month ago
WP.php
3 weeks ago
WooCommerce.php
1 month ago
index.php
3 years ago
SubscribersFinder.php
221 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Segments; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Entities\ScheduledTaskSubscriberEntity; |
| 10 | use MailPoet\Entities\SegmentEntity; |
| 11 | use MailPoet\Entities\SubscriberEntity; |
| 12 | use MailPoet\Entities\SubscriberSegmentEntity; |
| 13 | use MailPoet\InvalidStateException; |
| 14 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository; |
| 15 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 16 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 17 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 18 | |
| 19 | class SubscribersFinder { |
| 20 | |
| 21 | /** @var SegmentSubscribersRepository */ |
| 22 | private $segmentSubscriberRepository; |
| 23 | |
| 24 | /** @var SegmentsRepository */ |
| 25 | private $segmentsRepository; |
| 26 | |
| 27 | /** @var EntityManager */ |
| 28 | private $entityManager; |
| 29 | |
| 30 | /** @var ScheduledTaskSubscribersRepository */ |
| 31 | private $scheduledTaskSubscribersRepository; |
| 32 | |
| 33 | public function __construct( |
| 34 | SegmentSubscribersRepository $segmentSubscriberRepository, |
| 35 | SegmentsRepository $segmentsRepository, |
| 36 | EntityManager $entityManager, |
| 37 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository |
| 38 | ) { |
| 39 | $this->segmentSubscriberRepository = $segmentSubscriberRepository; |
| 40 | $this->segmentsRepository = $segmentsRepository; |
| 41 | $this->entityManager = $entityManager; |
| 42 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return array |
| 47 | * @throws InvalidStateException |
| 48 | */ |
| 49 | public function findSubscribersInSegments($subscribersToProcessIds, $newsletterSegmentsIds, ?int $filterSegmentId = null) { |
| 50 | $result = []; |
| 51 | foreach ($newsletterSegmentsIds as $segmentId) { |
| 52 | $segment = $this->segmentsRepository->findOneById($segmentId); |
| 53 | if (!$segment instanceof SegmentEntity) { |
| 54 | continue; // skip deleted segments |
| 55 | } |
| 56 | $result = array_merge($result, $this->findSubscribersInSegment($segment, $subscribersToProcessIds)); |
| 57 | } |
| 58 | |
| 59 | if (is_int($filterSegmentId)) { |
| 60 | $filterSegment = $this->segmentsRepository->verifyDynamicSegmentExists($filterSegmentId); |
| 61 | $idsInFilterSegment = $this->findSubscribersInSegment($filterSegment, $subscribersToProcessIds); |
| 62 | $result = array_intersect($result, $idsInFilterSegment); |
| 63 | } |
| 64 | |
| 65 | return $this->unique($result); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param int[] $newsletterSegmentsIds |
| 70 | * @return int[] |
| 71 | * @throws InvalidStateException |
| 72 | */ |
| 73 | public function getSubscriberIdsFromSegments(array $newsletterSegmentsIds, ?int $filterSegmentId = null): array { |
| 74 | $result = []; |
| 75 | foreach ($newsletterSegmentsIds as $segmentId) { |
| 76 | $segment = $this->segmentsRepository->findOneById($segmentId); |
| 77 | if (!$segment instanceof SegmentEntity) { |
| 78 | continue; |
| 79 | } |
| 80 | try { |
| 81 | $result = array_merge($result, $this->segmentSubscriberRepository->getSubscriberIdsInSegment((int)$segment->getId())); |
| 82 | } catch (InvalidStateException $exception) { |
| 83 | continue; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (is_int($filterSegmentId)) { |
| 88 | $filterSegment = $this->segmentsRepository->verifyDynamicSegmentExists($filterSegmentId); |
| 89 | $filterSubscriberIds = $this->segmentSubscriberRepository->getSubscriberIdsInSegment((int)$filterSegment->getId()); |
| 90 | $result = array_intersect($result, $filterSubscriberIds); |
| 91 | } |
| 92 | |
| 93 | return array_values($this->unique($result)); |
| 94 | } |
| 95 | |
| 96 | private function findSubscribersInSegment(SegmentEntity $segment, $subscribersToProcessIds): array { |
| 97 | try { |
| 98 | return $this->segmentSubscriberRepository->findSubscribersIdsInSegment((int)$segment->getId(), $subscribersToProcessIds); |
| 99 | } catch (InvalidStateException $e) { |
| 100 | return []; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param ScheduledTaskEntity $task |
| 106 | * @param array<int> $segmentIds |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function addSubscribersToTaskFromSegments(ScheduledTaskEntity $task, array $segmentIds, ?int $filterSegmentId = null): void { |
| 111 | // Prepare subscribers on the DB side for performance reasons |
| 112 | if (is_int($filterSegmentId)) { |
| 113 | try { |
| 114 | $this->segmentsRepository->verifyDynamicSegmentExists($filterSegmentId); |
| 115 | } catch (InvalidStateException $exception) { |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | $staticSegmentIds = []; |
| 120 | $dynamicSegmentIds = []; |
| 121 | foreach ($segmentIds as $segment) { |
| 122 | $segment = $this->segmentsRepository->findOneById($segment); |
| 123 | if ($segment instanceof SegmentEntity) { |
| 124 | if ($segment->isStatic()) { |
| 125 | $staticSegmentIds[] = (int)$segment->getId(); |
| 126 | } elseif ($segment->getType() === SegmentEntity::TYPE_DYNAMIC) { |
| 127 | $dynamicSegmentIds[] = (int)$segment->getId(); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | $count = 0; |
| 132 | if (!empty($staticSegmentIds)) { |
| 133 | $count += $this->addSubscribersToTaskFromStaticSegments($task, $staticSegmentIds, $filterSegmentId); |
| 134 | } |
| 135 | if (!empty($dynamicSegmentIds)) { |
| 136 | $count += $this->addSubscribersToTaskFromDynamicSegments($task, $dynamicSegmentIds, $filterSegmentId); |
| 137 | } |
| 138 | if ($count > 0) { |
| 139 | $this->entityManager->refresh($task); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param ScheduledTaskEntity $task |
| 145 | * @param array<int> $segmentIds |
| 146 | * |
| 147 | * @return int |
| 148 | */ |
| 149 | private function addSubscribersToTaskFromStaticSegments(ScheduledTaskEntity $task, array $segmentIds, ?int $filterSegmentId = null) { |
| 150 | $scheduledTaskSubscriberTable = $this->entityManager->getClassMetadata(ScheduledTaskSubscriberEntity::class)->getTableName(); |
| 151 | $subscriberSegmentTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 152 | $subscriberTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 153 | |
| 154 | $connection = $this->entityManager->getConnection(); |
| 155 | $selectQueryBuilder = $connection->createQueryBuilder(); |
| 156 | $selectQueryBuilder |
| 157 | ->select('DISTINCT :task_id as task_id', 'subscribers.id as subscriber_id', ':processed as processed') |
| 158 | ->from($subscriberSegmentTable, 'relation') |
| 159 | ->join('relation', $subscriberTable, 'subscribers', 'subscribers.id = relation.subscriber_id') |
| 160 | ->where('subscribers.deleted_at IS NULL') |
| 161 | ->andWhere('subscribers.status = :subscribers_status') |
| 162 | ->andWhere('relation.status = :relation_status') |
| 163 | ->andWhere($selectQueryBuilder->expr()->in('relation.segment_id', ':segment_ids')) |
| 164 | ->setParameter('task_id', $task->getId(), ParameterType::INTEGER) |
| 165 | ->setParameter('processed', ScheduledTaskSubscriberEntity::STATUS_UNPROCESSED, ParameterType::INTEGER) |
| 166 | ->setParameter('subscribers_status', SubscriberEntity::STATUS_SUBSCRIBED, ParameterType::STRING) |
| 167 | ->setParameter('relation_status', SubscriberEntity::STATUS_SUBSCRIBED, ParameterType::STRING) |
| 168 | ->setParameter('segment_ids', $segmentIds, ArrayParameterType::INTEGER); |
| 169 | |
| 170 | if ($filterSegmentId) { |
| 171 | $filterSegmentSubscriberIds = $this->segmentSubscriberRepository->findSubscribersIdsInSegment($filterSegmentId); |
| 172 | $selectQueryBuilder |
| 173 | ->andWhere($selectQueryBuilder->expr()->in('subscribers.id', ':filterSegmentSubscriberIds')) |
| 174 | ->setParameter('filterSegmentSubscriberIds', $filterSegmentSubscriberIds, ArrayParameterType::INTEGER); |
| 175 | } |
| 176 | |
| 177 | // queryBuilder doesn't support INSERT IGNORE directly |
| 178 | $sql = "INSERT IGNORE INTO $scheduledTaskSubscriberTable (task_id, subscriber_id, processed) " . $selectQueryBuilder->getSQL(); |
| 179 | $result = $connection->executeQuery($sql, $selectQueryBuilder->getParameters(), $selectQueryBuilder->getParameterTypes()); |
| 180 | |
| 181 | return (int)$result->rowCount(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param ScheduledTaskEntity $task |
| 186 | * @param array<int> $segmentIds |
| 187 | * |
| 188 | * @return int |
| 189 | */ |
| 190 | private function addSubscribersToTaskFromDynamicSegments(ScheduledTaskEntity $task, array $segmentIds, ?int $filterSegmentId = null) { |
| 191 | $count = 0; |
| 192 | foreach ($segmentIds as $segmentId) { |
| 193 | $count += $this->addSubscribersToTaskFromDynamicSegment($task, (int)$segmentId, $filterSegmentId); |
| 194 | } |
| 195 | return $count; |
| 196 | } |
| 197 | |
| 198 | private function addSubscribersToTaskFromDynamicSegment(ScheduledTaskEntity $task, int $segmentId, ?int $filterSegmentId) { |
| 199 | $count = 0; |
| 200 | $subscriberIds = $this->segmentSubscriberRepository->getSubscriberIdsInSegment($segmentId); |
| 201 | |
| 202 | if ($filterSegmentId) { |
| 203 | $filterSegmentSubscriberIds = $this->segmentSubscriberRepository->getSubscriberIdsInSegment($filterSegmentId); |
| 204 | $subscriberIds = array_intersect($subscriberIds, $filterSegmentSubscriberIds); |
| 205 | } |
| 206 | |
| 207 | if ($subscriberIds) { |
| 208 | $count += $this->scheduledTaskSubscribersRepository->addSubscribersByIds($task, $subscriberIds); |
| 209 | } |
| 210 | return $count; |
| 211 | } |
| 212 | |
| 213 | private function unique(array $subscriberIds) { |
| 214 | $result = []; |
| 215 | foreach ($subscriberIds as $id) { |
| 216 | $result[$id] = $id; |
| 217 | } |
| 218 | return $result; |
| 219 | } |
| 220 | } |
| 221 |