BrandStyles.php
3 years ago
NewsletterTemplatesRepository.php
2 months ago
TemplateImageLoader.php
2 years ago
ThumbnailSaver.php
2 months ago
index.php
3 years ago
NewsletterTemplatesRepository.php
126 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\NewsletterTemplates; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\Repository; |
| 9 | use MailPoet\Entities\NewsletterEntity; |
| 10 | use MailPoet\Entities\NewsletterTemplateEntity; |
| 11 | |
| 12 | /** |
| 13 | * @extends Repository<NewsletterTemplateEntity> |
| 14 | */ |
| 15 | class NewsletterTemplatesRepository extends Repository { |
| 16 | const RECENTLY_SENT_CATEGORIES = '["recent"]'; |
| 17 | const RECENTLY_SENT_COUNT = 12; |
| 18 | |
| 19 | protected function getEntityClassName() { |
| 20 | return NewsletterTemplateEntity::class; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return NewsletterTemplateEntity[] |
| 25 | */ |
| 26 | public function findAllForListing(): array { |
| 27 | return $this->doctrineRepository->createQueryBuilder('nt') |
| 28 | ->select('PARTIAL nt.{id,categories,thumbnail,name,readonly}') |
| 29 | ->addOrderBy('nt.readonly', 'ASC') |
| 30 | ->addOrderBy('nt.createdAt', 'DESC') |
| 31 | ->addOrderBy('nt.id', 'DESC') |
| 32 | ->getQuery() |
| 33 | ->getResult(); |
| 34 | } |
| 35 | |
| 36 | public function createOrUpdate(array $data): NewsletterTemplateEntity { |
| 37 | $template = !empty($data['newsletter_id']) |
| 38 | ? $this->findOneBy(['newsletter' => (int)$data['newsletter_id']]) |
| 39 | : null; |
| 40 | |
| 41 | if (!$template) { |
| 42 | $template = new NewsletterTemplateEntity($data['name'] ?? ''); |
| 43 | $this->entityManager->persist($template); |
| 44 | } |
| 45 | |
| 46 | if (isset($data['newsletter_id'])) { |
| 47 | $template->setNewsletter($this->entityManager->getReference(NewsletterEntity::class, (int)$data['newsletter_id'])); |
| 48 | } |
| 49 | |
| 50 | if (isset($data['name'])) { |
| 51 | $template->setName($data['name']); |
| 52 | } |
| 53 | |
| 54 | if (isset($data['thumbnail'])) { |
| 55 | // Backward compatibility for importing templates exported from older versions |
| 56 | if (strpos($data['thumbnail'], 'data:image') === 0) { |
| 57 | $data['thumbnail_data'] = $data['thumbnail']; |
| 58 | } else { |
| 59 | $template->setThumbnail($data['thumbnail']); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (isset($data['thumbnail_data'])) { |
| 64 | $template->setThumbnailData($data['thumbnail_data']); |
| 65 | } |
| 66 | |
| 67 | if (isset($data['body']) && is_string($data['body'])) { |
| 68 | $decodedBody = json_decode($data['body'], true); |
| 69 | $template->setBody(is_array($decodedBody) ? $decodedBody : null); |
| 70 | } |
| 71 | |
| 72 | if (isset($data['categories'])) { |
| 73 | $template->setCategories($data['categories']); |
| 74 | } |
| 75 | |
| 76 | $this->entityManager->flush(); |
| 77 | return $template; |
| 78 | } |
| 79 | |
| 80 | public function cleanRecentlySent() { |
| 81 | // fetch 'RECENTLY_SENT_COUNT' of most recent template IDs in 'RECENTLY_SENT_CATEGORIES' |
| 82 | $recentIds = $this->doctrineRepository->createQueryBuilder('nt') |
| 83 | ->select('nt.id') |
| 84 | ->where('nt.categories = :categories') |
| 85 | ->setParameter('categories', self::RECENTLY_SENT_CATEGORIES) |
| 86 | ->orderBy('nt.id', 'DESC') |
| 87 | ->setMaxResults(self::RECENTLY_SENT_COUNT) |
| 88 | ->getQuery() |
| 89 | ->getResult(); |
| 90 | |
| 91 | // delete all 'RECENTLY_SENT_CATEGORIES' templates except the latest ones selected above |
| 92 | $this->entityManager->createQueryBuilder() |
| 93 | ->delete(NewsletterTemplateEntity::class, 'nt') |
| 94 | ->where('nt.categories = :categories') |
| 95 | ->andWhere('nt.id NOT IN (:recentIds)') |
| 96 | ->setParameter('categories', self::RECENTLY_SENT_CATEGORIES) |
| 97 | ->setParameter('recentIds', array_column($recentIds, 'id')) |
| 98 | ->getQuery() |
| 99 | ->execute(); |
| 100 | |
| 101 | // delete was done via DQL, make sure the entities are also detached from the entity manager |
| 102 | $this->detachAll(function (NewsletterTemplateEntity $entity) use ($recentIds) { |
| 103 | return $entity->getCategories() === self::RECENTLY_SENT_CATEGORIES && !in_array($entity->getId(), $recentIds, true); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | public function getRecentlySentCount(): int { |
| 108 | return (int)$this->doctrineRepository->createQueryBuilder('nt') |
| 109 | ->select('COUNT(nt.id)') |
| 110 | ->where('nt.categories = :categories') |
| 111 | ->setParameter('categories', self::RECENTLY_SENT_CATEGORIES) |
| 112 | ->getQuery() |
| 113 | ->getSingleScalarResult(); |
| 114 | } |
| 115 | |
| 116 | public function getIdsOfEditableTemplates(): array { |
| 117 | $result = $this->doctrineRepository->createQueryBuilder('nt') |
| 118 | ->select('nt.id') |
| 119 | ->where('nt.readonly = :readonly') |
| 120 | ->setParameter('readonly', false) |
| 121 | ->getQuery() |
| 122 | ->getArrayResult(); |
| 123 | return array_column($result, 'id'); |
| 124 | } |
| 125 | } |
| 126 |