mailpoet
/
lib
/
EmailEditor
/
Integrations
/
MailPoet
/
ProductCollection
/
ProductCollectionEmailRendererRegistrar.php
OrderProductCollectionProcessor.php
1 month ago
ProductCollectionEmailRendererRegistrar.php
1 month ago
index.php
1 month ago
ProductCollectionEmailRendererRegistrar.php
97 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet\ProductCollection; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Email_Editor_Container; |
| 9 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 10 | use Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Initializer as WooCommerceBlocksInitializer; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | /** |
| 14 | * Wires WooCommerce's email block renderers when WooCommerce registers the |
| 15 | * blocks but leaves render_email_callback unset. |
| 16 | * |
| 17 | * WooCommerce sets render_email_callback through the block_type_metadata_settings |
| 18 | * filter while blocks register. On some builds the product blocks are registered |
| 19 | * before that filter is in place, so the email renderer never gets attached. The |
| 20 | * email editor then falls back to WooCommerce's frontend renderer, which resolves |
| 21 | * cart-contents and order collections against the live store state and renders the |
| 22 | * product blocks empty during a real send. |
| 23 | * |
| 24 | * Mirrors CouponBlockGenerator::registerEmailRenderer for product blocks. |
| 25 | */ |
| 26 | class ProductCollectionEmailRendererRegistrar { |
| 27 | private const PRODUCT_BLOCK_NAMES = [ |
| 28 | 'woocommerce/product-collection', |
| 29 | 'woocommerce/product-image', |
| 30 | 'woocommerce/product-price', |
| 31 | 'woocommerce/product-button', |
| 32 | 'woocommerce/product-sale-badge', |
| 33 | ]; |
| 34 | |
| 35 | private WPFunctions $wp; |
| 36 | |
| 37 | public function __construct( |
| 38 | WPFunctions $wp |
| 39 | ) { |
| 40 | $this->wp = $wp; |
| 41 | } |
| 42 | |
| 43 | public function init(): void { |
| 44 | if (!class_exists(Rendering_Context::class) || !class_exists(Email_Editor_Container::class)) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $this->wp->addAction('woocommerce_email_editor_render_start', [$this, 'registerEmailRenderers']); |
| 49 | } |
| 50 | |
| 51 | public function registerEmailRenderers(): void { |
| 52 | if (!class_exists(\WP_Block_Type_Registry::class) || !class_exists(WooCommerceBlocksInitializer::class)) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $renderer = $this->getWooCommerceBlocksRenderer(); |
| 57 | if (!$renderer) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $renderEmailCallbackProperty = 'render_email_callback'; |
| 62 | $registry = \WP_Block_Type_Registry::get_instance(); |
| 63 | foreach (self::PRODUCT_BLOCK_NAMES as $blockName) { |
| 64 | $blockType = $registry->get_registered($blockName); |
| 65 | if (!$blockType) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | $currentCallback = get_object_vars($blockType)[$renderEmailCallbackProperty] ?? null; |
| 70 | if (!$this->needsEmailRenderer($currentCallback)) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | // @phpstan-ignore-next-line -- WooCommerce email editor reads this dynamic block setting. |
| 75 | $blockType->{$renderEmailCallbackProperty} = [$renderer, 'render_block']; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private function getWooCommerceBlocksRenderer(): ?WooCommerceBlocksInitializer { |
| 80 | try { |
| 81 | return Email_Editor_Container::container()->get(WooCommerceBlocksInitializer::class); |
| 82 | } catch (\Throwable $e) { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Only fill the gap when no renderer is wired yet. An existing callback, whether |
| 89 | * WooCommerce's own or a custom one from another integration, is left untouched. |
| 90 | * |
| 91 | * @param mixed $currentCallback |
| 92 | */ |
| 93 | public function needsEmailRenderer($currentCallback): bool { |
| 94 | return $currentCallback === null; |
| 95 | } |
| 96 | } |
| 97 |