Pattern.php
141 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet\Patterns; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Engine\Patterns\Abstract_Pattern; |
| 9 | use MailPoet\EmailEditor\Integrations\MailPoet\Coupons\CouponBlock; |
| 10 | use MailPoet\Util\CdnAssetUrl; |
| 11 | |
| 12 | abstract class Pattern extends Abstract_Pattern { |
| 13 | protected CdnAssetUrl $cdnAssetUrl; |
| 14 | protected $namespace = 'mailpoet'; |
| 15 | |
| 16 | public function __construct( |
| 17 | CdnAssetUrl $cdnAssetUrl |
| 18 | ) { |
| 19 | $this->cdnAssetUrl = $cdnAssetUrl; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Get the content to use when creating an email from this pattern. |
| 24 | * Override in subclasses to provide different insertion content than preview. |
| 25 | * By default, returns the same as get_content(). |
| 26 | */ |
| 27 | public function get_email_content(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 28 | return $this->get_content(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param string[] $imageNames CDN filenames for placeholder product images. |
| 33 | */ |
| 34 | protected function getProductPlaceholderBlocks(array $imageNames): string { |
| 35 | $blocks = ''; |
| 36 | foreach ($imageNames as $imageName) { |
| 37 | $blocks .= $this->getProductPlaceholderCard($imageName); |
| 38 | } |
| 39 | return $blocks; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Render placeholder products in a two-column grid. |
| 44 | * |
| 45 | * Uses 10px padding per column side (20px total gap) to match the WooCommerce |
| 46 | * email renderer's two-column grid (see class-product-collection.php). |
| 47 | * Pixel values are used instead of spacing presets for email client compatibility. |
| 48 | * |
| 49 | * @param string[] $imageNames CDN filenames for placeholder product images. Should contain an even number of items. |
| 50 | */ |
| 51 | protected function getProductPlaceholderColumns(array $imageNames): string { |
| 52 | $rows = array_chunk($imageNames, 2); |
| 53 | $blocks = ''; |
| 54 | |
| 55 | foreach ($rows as $row) { |
| 56 | $cols = ''; |
| 57 | foreach ($row as $index => $imageName) { |
| 58 | $isFirst = $index === 0; |
| 59 | $padding = $isFirst ? 'right' : 'left'; |
| 60 | $style = 'padding-' . $padding . ':10px'; |
| 61 | $cols .= ' |
| 62 | <!-- wp:column {"style":{"spacing":{"padding":{"' . $padding . '":"10px"}}}} --> |
| 63 | <div class="wp-block-column" style="' . $style . '">' . $this->getProductPlaceholderCard($imageName) . '</div> |
| 64 | <!-- /wp:column -->'; |
| 65 | } |
| 66 | |
| 67 | $blocks .= ' |
| 68 | <!-- wp:columns --> |
| 69 | <div class="wp-block-columns">' . $cols . '</div> |
| 70 | <!-- /wp:columns -->'; |
| 71 | } |
| 72 | |
| 73 | return $blocks; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get a product collection block for recommended products. |
| 78 | * |
| 79 | * @param string $collection WooCommerce collection slug (e.g., 'best-sellers', 'new-arrivals') |
| 80 | * or a fully namespaced collection (e.g., OrderProductCollectionProcessor::COLLECTION_ORDER_CROSS_SELLS, |
| 81 | * resolved per order at send time). The query authored here is the fallback when an |
| 82 | * order-aware collection cannot be resolved. |
| 83 | * @param string $orderBy Order by field (e.g., 'date', 'popularity'). |
| 84 | * @param int $perPage Number of products to show. |
| 85 | * @param int $columns Number of columns in the grid. |
| 86 | */ |
| 87 | protected function getRecommendedProductCollectionBlock(string $collection, string $orderBy = 'date', int $perPage = 4, int $columns = 2): string { |
| 88 | $collectionSlug = strpos($collection, '/') !== false ? $collection : 'woocommerce/product-collection/' . $collection; |
| 89 | return ' |
| 90 | <!-- wp:woocommerce/product-collection {"query":{"perPage":' . $perPage . ',"pages":1,"offset":0,"postType":"product","order":"desc","orderBy":"' . $orderBy . '","search":"","exclude":[],"inherit":false,"taxQuery":[],"isProductCollectionBlock":true,"featured":false,"woocommerceOnSale":false,"woocommerceStockStatus":["instock","onbackorder"],"woocommerceAttributes":[],"woocommerceHandPickedProducts":[],"filterable":false},"tagName":"div","displayLayout":{"type":"flex","columns":' . $columns . ',"shrinkColumns":true},"dimensions":{"widthType":"fill"},"collection":"' . $collectionSlug . '","hideControls":["inherit","attributes","keyword","order","default-order","featured","on-sale","stock-status","hand-picked","taxonomy","filterable","created","price-range"]} --> |
| 91 | <div class="wp-block-woocommerce-product-collection"><!-- wp:woocommerce/product-template --> |
| 92 | <!-- wp:woocommerce/product-image {"showSaleBadge":false,"imageSizing":"thumbnail","isDescendentOfQueryLoop":true,"style":{"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}}}} --> |
| 93 | <!-- wp:woocommerce/product-sale-badge {"align":"right"} /--> |
| 94 | <!-- /wp:woocommerce/product-image --> |
| 95 | |
| 96 | <!-- wp:post-title {"textAlign":"center","isLink":true,"style":{"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}},"typography":{"fontSize":"24px"}},"__woocommerceNamespace":"woocommerce/product-collection/product-title"} /--> |
| 97 | |
| 98 | <!-- wp:woocommerce/product-price {"isDescendentOfQueryLoop":true,"textAlign":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}}}} /--> |
| 99 | |
| 100 | <!-- wp:woocommerce/product-button {"textAlign":"center","isDescendentOfQueryLoop":true,"style":{"typography":{"fontSize":"16px"}}} /--> |
| 101 | <!-- /wp:woocommerce/product-template --> |
| 102 | </div> |
| 103 | <!-- /wp:woocommerce/product-collection --> |
| 104 | '; |
| 105 | } |
| 106 | |
| 107 | protected function getGeneratedCouponBlock(string $align, int $amount, int $expiryDay): string { |
| 108 | $attributes = CouponBlock::withCreateNewDefaults([ |
| 109 | 'align' => $align, |
| 110 | 'amount' => $amount, |
| 111 | 'expiryDay' => $expiryDay, |
| 112 | ]); |
| 113 | |
| 114 | return ' |
| 115 | <!-- wp:woocommerce/coupon-code ' . wp_json_encode($attributes, JSON_UNESCAPED_SLASHES) . ' --> |
| 116 | <div class="wp-block-woocommerce-coupon-code align' . esc_attr($align) . '"><strong>' . CouponBlock::SAFE_PLACEHOLDER . '</strong></div> |
| 117 | <!-- /wp:woocommerce/coupon-code --> |
| 118 | '; |
| 119 | } |
| 120 | |
| 121 | private function getProductPlaceholderCard(string $imageName): string { |
| 122 | $imageUrl = esc_url($this->cdnAssetUrl->generateCdnUrl('email-editor/' . $imageName)); |
| 123 | $imageAlt = esc_attr__('Product placeholder', 'mailpoet'); |
| 124 | $productName = __('Product name', 'mailpoet'); |
| 125 | |
| 126 | return ' |
| 127 | <!-- wp:image {"sizeSlug":"full","style":{"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}}}} --> |
| 128 | <figure class="wp-block-image size-full" style="padding-top:var(--wp--preset--spacing--10);padding-bottom:var(--wp--preset--spacing--10)"><img src="' . $imageUrl . '" alt="' . $imageAlt . '"/></figure> |
| 129 | <!-- /wp:image --> |
| 130 | |
| 131 | <!-- wp:heading {"textAlign":"center","style":{"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}},"typography":{"fontSize":"24px"}}} --> |
| 132 | <h2 class="wp-block-heading has-text-align-center" style="padding-top:var(--wp--preset--spacing--10);padding-bottom:var(--wp--preset--spacing--10);font-size:24px">' . $productName . '</h2> |
| 133 | <!-- /wp:heading --> |
| 134 | |
| 135 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"padding":{"top":"var:preset|spacing|10","bottom":"var:preset|spacing|10"}}}} --> |
| 136 | <p class="has-text-align-center" style="padding-top:var(--wp--preset--spacing--10);padding-bottom:var(--wp--preset--spacing--10);font-size:14px">$99.90</p> |
| 137 | <!-- /wp:paragraph --> |
| 138 | '; |
| 139 | } |
| 140 | } |
| 141 |