Editor
2 months ago
Embed
1 month ago
Links
2 months ago
Listing
9 months ago
Options
2 years ago
Preview
1 month ago
Renderer
5 days ago
RestApi
1 week ago
Scheduler
5 days ago
Segment
1 year ago
Sending
1 week ago
Sharing
1 month ago
Shortcodes
3 days ago
Statistics
1 week ago
ViewInBrowser
1 month ago
ApiDataSanitizer.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmailsRepository.php
3 years ago
BlockPostQuery.php
2 months ago
BulkActionController.php
1 month ago
BulkActionException.php
1 month ago
DynamicProducts.php
11 months ago
NewsletterCoupon.php
2 months ago
NewsletterDeleteController.php
2 years ago
NewsletterHtmlSanitizer.php
2 years ago
NewsletterPostsRepository.php
2 years ago
NewsletterResendController.php
5 days ago
NewsletterSaveController.php
1 month ago
NewsletterValidator.php
1 year ago
NewslettersRepository.php
2 weeks ago
StatusController.php
1 month ago
Url.php
2 months ago
index.php
3 years ago
AutomaticEmailsRepository.php
63 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter; |
| 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 MailPoetVendor\Doctrine\ORM\QueryBuilder; |
| 12 | |
| 13 | /** |
| 14 | * @extends Repository<NewsletterEntity> |
| 15 | */ |
| 16 | class AutomaticEmailsRepository extends Repository { |
| 17 | protected function getEntityClassName() { |
| 18 | return NewsletterEntity::class; |
| 19 | } |
| 20 | |
| 21 | public function wasScheduledForSubscriber(int $newsletterId, int $subscriberId): bool { |
| 22 | $query = $this->doctrineRepository->createQueryBuilder('n') |
| 23 | ->select('COUNT(q)') |
| 24 | ->from(SendingQueueEntity::class, 'q'); |
| 25 | $query = $this->getAllQueuesForSubscscriberQuery($query, $newsletterId, $subscriberId); |
| 26 | $count = $query->getQuery() |
| 27 | ->getSingleScalarResult() ?: 0; |
| 28 | return ((int)$count) > 0; |
| 29 | } |
| 30 | |
| 31 | private function getAllQueuesForSubscscriberQuery(QueryBuilder $query, int $newsletterId, int $subscriberId): QueryBuilder { |
| 32 | return $query |
| 33 | ->join('q.task', 't') |
| 34 | ->join('t.subscribers', 's') |
| 35 | ->andWhere('q.newsletter = :newsletterId') |
| 36 | ->andWhere('s.subscriber = :subscriberId') |
| 37 | ->setParameter('newsletterId', $newsletterId) |
| 38 | ->setParameter('subscriberId', $subscriberId); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Search products/categories in meta if all of the ordered products have already been sent to the subscriber. |
| 43 | */ |
| 44 | public function alreadySentAllProducts(int $newsletterId, int $subscriberId, string $orderedKey, array $ordered): bool { |
| 45 | $query = $this->doctrineRepository->createQueryBuilder('n') |
| 46 | ->select('q') |
| 47 | ->from(SendingQueueEntity::class, 'q'); |
| 48 | $queues = $this->getAllQueuesForSubscscriberQuery($query, $newsletterId, $subscriberId) |
| 49 | ->getQuery() |
| 50 | ->getResult(); |
| 51 | $sent = []; |
| 52 | foreach ($queues as $queue) { |
| 53 | $meta = $queue->getMeta(); |
| 54 | if (isset($meta[$orderedKey])) { |
| 55 | $sent = array_merge($sent, $meta[$orderedKey]); |
| 56 | } |
| 57 | } |
| 58 | $notSentProducts = array_diff($ordered, $sent); |
| 59 | |
| 60 | return empty($notSentProducts); |
| 61 | } |
| 62 | } |
| 63 |