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
DynamicProductsBlock.php
126 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\Entities\NewsletterEntity; |
| 10 | use MailPoet\Entities\SendingQueueEntity; |
| 11 | use MailPoet\Newsletter\BlockPostQuery; |
| 12 | use MailPoet\Newsletter\DynamicProducts; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | class DynamicProductsBlock { |
| 16 | // For Order subject - products from the order |
| 17 | const ORDER_PRODUCTS_META_NAME = 'order_product_ids'; |
| 18 | |
| 19 | // For Order subject - cross-sell products |
| 20 | const ORDER_CROSS_SELL_PRODUCTS_META_NAME = 'order_cross_sell_product_ids'; |
| 21 | |
| 22 | /** |
| 23 | * Cache for rendered posts in newsletter. |
| 24 | * Used to prevent duplicate post in case a newsletter contains 2 DP blocks |
| 25 | * @var array |
| 26 | */ |
| 27 | public $renderedPostsInNewsletter; |
| 28 | |
| 29 | /** @var DynamicProducts */ |
| 30 | private $dynamicProducts; |
| 31 | |
| 32 | /** @var WPFunctions */ |
| 33 | private $wp; |
| 34 | |
| 35 | public function __construct( |
| 36 | DynamicProducts $dynamicProducts, |
| 37 | WPFunctions $wp |
| 38 | ) { |
| 39 | $this->renderedPostsInNewsletter = []; |
| 40 | $this->dynamicProducts = $dynamicProducts; |
| 41 | $this->wp = $wp; |
| 42 | } |
| 43 | |
| 44 | public function render(NewsletterEntity $newsletter, $args, $preview = false, ?SendingQueueEntity $sendingQueue = null) { |
| 45 | $newerThanTimestamp = false; |
| 46 | $newsletterId = $newsletter->getId(); |
| 47 | $postsToExclude = $this->getRenderedPosts((int)$newsletterId); |
| 48 | |
| 49 | // Check if we have specific product IDs from subject metadata |
| 50 | $productIds = []; |
| 51 | |
| 52 | if (!$preview && $sendingQueue) { |
| 53 | $meta = $sendingQueue->getMeta(); |
| 54 | |
| 55 | if (!empty($args['dynamicProductsType'])) { |
| 56 | switch ($args['dynamicProductsType']) { |
| 57 | case 'order': |
| 58 | // Check for OrderSubject products |
| 59 | if (!empty($meta[self::ORDER_PRODUCTS_META_NAME])) { |
| 60 | $productIds = $meta[self::ORDER_PRODUCTS_META_NAME]; |
| 61 | } |
| 62 | break; |
| 63 | case 'cross-sell': |
| 64 | // Check for OrderSubject cross-sells |
| 65 | if (!empty($meta[self::ORDER_CROSS_SELL_PRODUCTS_META_NAME])) { |
| 66 | $productIds = $meta[self::ORDER_CROSS_SELL_PRODUCTS_META_NAME]; |
| 67 | } |
| 68 | break; |
| 69 | case 'cart': |
| 70 | // Check for AbandonedCartSubject products |
| 71 | if (!empty($meta[AbandonedCart::TASK_META_NAME])) { |
| 72 | $productIds = $meta[AbandonedCart::TASK_META_NAME]; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Check for Premium dynamic products |
| 79 | $productIds = (array)$this->wp->applyFilters('mailpoet_dynamic_products_meta', $productIds, $sendingQueue); |
| 80 | $productIds = array_map(function($id) { |
| 81 | return is_numeric($id) ? (int)$id : 0; |
| 82 | }, $productIds); |
| 83 | } |
| 84 | |
| 85 | // Define query parameters |
| 86 | $queryArgs = [ |
| 87 | 'args' => $args, |
| 88 | 'contentType' => 'product', |
| 89 | 'postsToExclude' => $postsToExclude, |
| 90 | 'newsletterId' => $newsletterId, |
| 91 | 'newerThanTimestamp' => $newerThanTimestamp, |
| 92 | 'dynamic' => true, |
| 93 | ]; |
| 94 | |
| 95 | // If we have specific product IDs, add them to the query |
| 96 | if (!empty($productIds)) { |
| 97 | $queryArgs['includeProductIds'] = $productIds; |
| 98 | } else { |
| 99 | // Don't show any products if we don't have specific product IDs |
| 100 | // and the user didn't choose "selected" in the dynamic products type dropdown |
| 101 | if ($sendingQueue) { |
| 102 | if (($args['dynamicProductsType'] ?? null) !== 'selected') { |
| 103 | $queryArgs['includeProductIds'] = [0]; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $query = new BlockPostQuery($queryArgs); |
| 109 | $products = $this->dynamicProducts->getPosts($query); |
| 110 | |
| 111 | foreach ($products as $product) { |
| 112 | $postsToExclude[] = $product->get_id(); |
| 113 | } |
| 114 | $this->setRenderedPosts((int)$newsletterId, $postsToExclude); |
| 115 | return $this->dynamicProducts->transformPosts($args, $products); |
| 116 | } |
| 117 | |
| 118 | private function getRenderedPosts(int $newsletterId) { |
| 119 | return $this->renderedPostsInNewsletter[$newsletterId] ?? []; |
| 120 | } |
| 121 | |
| 122 | private function setRenderedPosts(int $newsletterId, array $posts) { |
| 123 | return $this->renderedPostsInNewsletter[$newsletterId] = $posts; |
| 124 | } |
| 125 | } |
| 126 |