woocommerce
/
packages
/
email-editor
/
src
/
Integrations
/
WooCommerce
/
Renderer
/
Blocks
/
class-coupon-code.php
woocommerce
/
packages
/
email-editor
/
src
/
Integrations
/
WooCommerce
/
Renderer
/
Blocks
Last commit date
class-abstract-product-block-renderer.php
9 months ago
class-coupon-code.php
2 months ago
class-product-button.php
1 month ago
class-product-collection.php
1 month ago
class-product-image.php
1 month ago
class-product-price.php
1 month ago
class-product-sale-badge.php
1 month ago
class-coupon-code.php
95 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks; |
| 10 | |
| 11 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 12 | use Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Abstract_Block_Renderer; |
| 13 | use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper; |
| 14 | |
| 15 | /** |
| 16 | * Renders the woocommerce/coupon-code block for email. |
| 17 | * |
| 18 | * For "existing" source, the block content passes through unchanged. |
| 19 | * For "createNew" source, the placeholder (XXXX-XXXXXX-XXXX) is replaced |
| 20 | * with a generated coupon code via the woocommerce_coupon_code_block_auto_generate filter. |
| 21 | */ |
| 22 | class Coupon_Code extends Abstract_Block_Renderer { |
| 23 | |
| 24 | const COUPON_CODE_PLACEHOLDER = 'XXXX-XXXXXX-XXXX'; |
| 25 | |
| 26 | /** |
| 27 | * Render the coupon code block content for email. |
| 28 | * |
| 29 | * @param string $block_content Block content from the standard WP render. |
| 30 | * @param array $parsed_block Parsed block data. |
| 31 | * @param Rendering_Context $rendering_context Rendering context with email-specific data. |
| 32 | * @return string |
| 33 | */ |
| 34 | protected function render_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string { |
| 35 | $attrs = $parsed_block['attrs'] ?? array(); |
| 36 | $source = $attrs['source'] ?? 'createNew'; |
| 37 | |
| 38 | if ( 'createNew' === $source ) { |
| 39 | /** |
| 40 | * Filters the auto-generated coupon code for the coupon-code block. |
| 41 | * |
| 42 | * Integrators (MailPoet, WooCommerce, third-party plugins) hook into this filter |
| 43 | * to generate a WooCommerce coupon at send time and return its code. |
| 44 | * |
| 45 | * @hook woocommerce_coupon_code_block_auto_generate |
| 46 | * @since 10.6.0 |
| 47 | * |
| 48 | * @param string $coupon_code The coupon code. Empty by default. |
| 49 | * @param array $attrs Block attributes (discountType, amount, expiryDay, etc.). |
| 50 | * @param Rendering_Context $rendering_context The rendering context with email-specific data |
| 51 | * (recipient email, user ID, etc.). |
| 52 | * @return string The generated coupon code. Return empty string to suppress the block output. |
| 53 | */ |
| 54 | $coupon_code = apply_filters( |
| 55 | 'woocommerce_coupon_code_block_auto_generate', |
| 56 | '', |
| 57 | $attrs, |
| 58 | $rendering_context |
| 59 | ); |
| 60 | |
| 61 | if ( empty( $coupon_code ) ) { |
| 62 | return ''; |
| 63 | } |
| 64 | |
| 65 | $block_content = str_replace( |
| 66 | self::COUPON_CODE_PLACEHOLDER, |
| 67 | esc_html( $coupon_code ), |
| 68 | $block_content |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | $align = $attrs['align'] ?? 'center'; |
| 73 | if ( ! in_array( $align, array( 'left', 'center', 'right' ), true ) ) { |
| 74 | $align = 'center'; |
| 75 | } |
| 76 | |
| 77 | $table_attrs = array( |
| 78 | 'style' => 'border-collapse: separate;', |
| 79 | 'width' => '100%', |
| 80 | ); |
| 81 | |
| 82 | $cell_attrs = array( |
| 83 | 'align' => $align, |
| 84 | 'style' => \WP_Style_Engine::compile_css( |
| 85 | array( |
| 86 | 'text-align' => $align, |
| 87 | ), |
| 88 | '' |
| 89 | ), |
| 90 | ); |
| 91 | |
| 92 | return Table_Wrapper_Helper::render_table_wrapper( $block_content, $table_attrs, $cell_attrs ); |
| 93 | } |
| 94 | } |
| 95 |