AbandonedCartContent.php
1 year ago
AutomatedLatestContentBlock.php
3 years ago
Button.php
1 year ago
Coupon.php
3 years ago
Divider.php
2 years ago
DynamicProductsBlock.php
11 months ago
Footer.php
2 months ago
Header.php
2 months ago
Image.php
3 years ago
Placeholder.php
4 years ago
Renderer.php
2 months ago
Social.php
2 months ago
Spacer.php
3 years ago
Text.php
2 months ago
index.php
3 years ago
AutomatedLatestContentBlock.php
76 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer\Blocks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\NewsletterPostEntity; |
| 10 | use MailPoet\Newsletter\AutomatedLatestContent; |
| 11 | use MailPoet\Newsletter\BlockPostQuery; |
| 12 | use MailPoet\Newsletter\NewsletterPostsRepository; |
| 13 | |
| 14 | class AutomatedLatestContentBlock { |
| 15 | /** |
| 16 | * Cache for rendered posts in newsletter. |
| 17 | * Used to prevent duplicate post in case a newsletter contains 2 ALC blocks |
| 18 | * @var array |
| 19 | */ |
| 20 | public $renderedPostsInNewsletter; |
| 21 | |
| 22 | /** @var AutomatedLatestContent */ |
| 23 | private $ALC; |
| 24 | |
| 25 | /** @var NewsletterPostsRepository */ |
| 26 | private $newsletterPostsRepository; |
| 27 | |
| 28 | public function __construct( |
| 29 | NewsletterPostsRepository $newsletterPostsRepository, |
| 30 | AutomatedLatestContent $ALC |
| 31 | ) { |
| 32 | $this->renderedPostsInNewsletter = []; |
| 33 | $this->ALC = $ALC; |
| 34 | $this->newsletterPostsRepository = $newsletterPostsRepository; |
| 35 | } |
| 36 | |
| 37 | public function render(NewsletterEntity $newsletter, $args) { |
| 38 | $newerThanTimestamp = false; |
| 39 | $newsletterId = false; |
| 40 | if ($newsletter->getType() === NewsletterEntity::TYPE_NOTIFICATION_HISTORY) { |
| 41 | $parent = $newsletter->getParent(); |
| 42 | if ($parent instanceof NewsletterEntity) { |
| 43 | $newsletterId = $parent->getId(); |
| 44 | |
| 45 | $lastPost = $this->newsletterPostsRepository->findOneBy(['newsletter' => $parent], ['createdAt' => 'desc']); |
| 46 | if ($lastPost instanceof NewsletterPostEntity) { |
| 47 | $newerThanTimestamp = $lastPost->getCreatedAt(); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | } |
| 52 | $postsToExclude = $this->getRenderedPosts((int)$newsletterId); |
| 53 | $query = new BlockPostQuery([ |
| 54 | 'args' => $args, |
| 55 | 'postsToExclude' => $postsToExclude, |
| 56 | 'newsletterId' => $newsletterId, |
| 57 | 'newerThanTimestamp' => $newerThanTimestamp, |
| 58 | 'dynamic' => true, |
| 59 | ]); |
| 60 | $aLCPosts = $this->ALC->getPosts($query); |
| 61 | foreach ($aLCPosts as $post) { |
| 62 | $postsToExclude[] = $post->ID; |
| 63 | } |
| 64 | $this->setRenderedPosts((int)$newsletterId, $postsToExclude); |
| 65 | return $this->ALC->transformPosts($args, $aLCPosts); |
| 66 | } |
| 67 | |
| 68 | private function getRenderedPosts(int $newsletterId) { |
| 69 | return $this->renderedPostsInNewsletter[$newsletterId] ?? []; |
| 70 | } |
| 71 | |
| 72 | private function setRenderedPosts(int $newsletterId, array $posts) { |
| 73 | return $this->renderedPostsInNewsletter[$newsletterId] = $posts; |
| 74 | } |
| 75 | } |
| 76 |