AbandonedCartContent.php
1 year ago
AutomatedLatestContentBlock.php
3 years ago
Button.php
2 years ago
Coupon.php
3 years ago
Divider.php
2 years ago
DynamicProductsBlock.php
1 year ago
Footer.php
3 months ago
Header.php
3 months ago
Image.php
3 years ago
Placeholder.php
4 years ago
Renderer.php
3 months ago
Social.php
3 months ago
Spacer.php
3 years ago
Text.php
3 months ago
index.php
3 years ago
AbandonedCartContent.php
81 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\AutomaticEmails\WooCommerce\Events\AbandonedCart; |
| 9 | use MailPoet\AutomaticEmails\WooCommerce\WooCommerce as WooCommerceEmail; |
| 10 | use MailPoet\Entities\NewsletterEntity; |
| 11 | use MailPoet\Entities\NewsletterOptionEntity; |
| 12 | use MailPoet\Entities\SendingQueueEntity; |
| 13 | |
| 14 | class AbandonedCartContent { |
| 15 | /** @var AutomatedLatestContentBlock */ |
| 16 | private $ALCBlock; |
| 17 | |
| 18 | public function __construct( |
| 19 | AutomatedLatestContentBlock $ALCBlock |
| 20 | ) { |
| 21 | $this->ALCBlock = $ALCBlock; |
| 22 | } |
| 23 | |
| 24 | public function render( |
| 25 | NewsletterEntity $newsletter, |
| 26 | array $args, |
| 27 | bool $preview = false, |
| 28 | ?SendingQueueEntity $sendingQueue = null |
| 29 | ): array { |
| 30 | if ( |
| 31 | !in_array( |
| 32 | $newsletter->getType(), |
| 33 | [ |
| 34 | NewsletterEntity::TYPE_AUTOMATIC, |
| 35 | NewsletterEntity::TYPE_AUTOMATION_TRANSACTIONAL, |
| 36 | NewsletterEntity::TYPE_AUTOMATION, |
| 37 | ], |
| 38 | true |
| 39 | ) |
| 40 | ) { |
| 41 | // Do not display the block if not an automatic email |
| 42 | return []; |
| 43 | } |
| 44 | $groupOption = $newsletter->getOptions()->filter(function (?NewsletterOptionEntity $newsletterOption = null) { |
| 45 | if (!$newsletterOption) return false; |
| 46 | $optionField = $newsletterOption->getOptionField(); |
| 47 | return $optionField && $optionField->getName() === 'group'; |
| 48 | })->first(); |
| 49 | $eventOption = $newsletter->getOptions()->filter(function (?NewsletterOptionEntity $newsletterOption = null) { |
| 50 | if (!$newsletterOption) return false; |
| 51 | $optionField = $newsletterOption->getOptionField(); |
| 52 | return $optionField && $optionField->getName() === 'event'; |
| 53 | })->first(); |
| 54 | if ( |
| 55 | !$groupOption |
| 56 | || $groupOption->getValue() !== WooCommerceEmail::SLUG |
| 57 | || !$eventOption |
| 58 | || $eventOption->getValue() !== AbandonedCart::SLUG |
| 59 | ) { |
| 60 | // Do not display the block if not an AbandonedCart email |
| 61 | return []; |
| 62 | } |
| 63 | if ($preview) { |
| 64 | // Display latest products for preview (no 'posts' argument specified) |
| 65 | return $this->ALCBlock->render($newsletter, $args); |
| 66 | } |
| 67 | if (!$sendingQueue) { |
| 68 | // Do not display the block if we're not sending an email |
| 69 | return []; |
| 70 | } |
| 71 | $meta = $sendingQueue->getMeta(); |
| 72 | if (empty($meta[AbandonedCart::TASK_META_NAME])) { |
| 73 | // Do not display the block if a cart is empty |
| 74 | return []; |
| 75 | } |
| 76 | $args['amount'] = 50; |
| 77 | $args['posts'] = $meta[AbandonedCart::TASK_META_NAME]; |
| 78 | return $this->ALCBlock->render($newsletter, $args); |
| 79 | } |
| 80 | } |
| 81 |