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
Coupon.php
91 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer\Blocks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper; |
| 9 | use MailPoet\Newsletter\Renderer\StylesHelper; |
| 10 | use MailPoet\NewsletterProcessingException; |
| 11 | use MailPoet\WooCommerce\Helper; |
| 12 | |
| 13 | class Coupon { |
| 14 | const TYPE = 'coupon'; |
| 15 | |
| 16 | const CODE_PLACEHOLDER = 'XXXX-XXXXXXX-XXXX'; |
| 17 | |
| 18 | /*** @var Helper */ |
| 19 | private $helper; |
| 20 | |
| 21 | public function __construct( |
| 22 | Helper $helper |
| 23 | ) { |
| 24 | $this->helper = $helper; |
| 25 | } |
| 26 | |
| 27 | public function render($element, $columnBaseWidth) { |
| 28 | $couponCode = self::CODE_PLACEHOLDER; |
| 29 | if (!empty($element['couponId'])) { |
| 30 | try { |
| 31 | $couponCode = $this->helper->wcGetCouponCodeById((int)$element['couponId']); |
| 32 | } catch (\Exception $e) { |
| 33 | if (!$this->helper->isWooCommerceActive()) { |
| 34 | throw NewsletterProcessingException::create()->withMessage(__('WooCommerce is not active', 'mailpoet')); |
| 35 | } else { |
| 36 | throw NewsletterProcessingException::create()->withMessage($e->getMessage())->withCode($e->getCode()); |
| 37 | } |
| 38 | } |
| 39 | if (empty($couponCode)) { |
| 40 | throw NewsletterProcessingException::create()->withMessage(__('Couldn\'t find the coupon. Please update the email if the coupon was removed.', 'mailpoet')); |
| 41 | } |
| 42 | } |
| 43 | $element['styles']['block']['width'] = $this->calculateWidth($element, $columnBaseWidth); |
| 44 | $styles = 'display:inline-block;-webkit-text-size-adjust:none;mso-hide:all;text-decoration:none;text-align:center;' . StylesHelper::getBlockStyles($element, $exclude = ['textAlign']); |
| 45 | $styles = EHelper::escapeHtmlStyleAttr($styles); |
| 46 | $template = ' |
| 47 | <tr> |
| 48 | <td class="mailpoet_padded_vertical mailpoet_padded_side" valign="top"> |
| 49 | <div> |
| 50 | <table width="100%" cellpadding="0" cellspacing="0" border="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;"> |
| 51 | <tr> |
| 52 | <td class="mailpoet_coupon-container" style="text-align:' . $element['styles']['block']['textAlign'] . ';"><!--[if mso]> |
| 53 | <v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" |
| 54 | style="height:' . EHelper::escapeHtmlStyleAttr($element['styles']['block']['lineHeight']) . '; |
| 55 | width:' . EHelper::escapeHtmlStyleAttr($element['styles']['block']['width']) . '; |
| 56 | v-text-anchor:middle;" |
| 57 | arcsize="' . round((int)$element['styles']['block']['borderRadius'] / ((int)$element['styles']['block']['lineHeight'] ?: 1) * 100) . '%" |
| 58 | strokeweight="' . EHelper::escapeHtmlAttr($element['styles']['block']['borderWidth']) . '" |
| 59 | strokecolor="' . EHelper::escapeHtmlAttr($element['styles']['block']['borderColor']) . '" |
| 60 | fillcolor="' . EHelper::escapeHtmlAttr($element['styles']['block']['backgroundColor']) . '"> |
| 61 | <w:anchorlock/> |
| 62 | <center style="color:' . EHelper::escapeHtmlStyleAttr($element['styles']['block']['fontColor']) . '; |
| 63 | font-family:' . EHelper::escapeHtmlStyleAttr($element['styles']['block']['fontFamily']) . '; |
| 64 | font-size:' . EHelper::escapeHtmlStyleAttr($element['styles']['block']['fontSize']) . '; |
| 65 | font-weight:bold;">' . EHelper::escapeHtmlText($couponCode) . ' |
| 66 | </center> |
| 67 | </v:roundrect> |
| 68 | <![endif]--> |
| 69 | <!--[if !mso]><!-- --> |
| 70 | <div class="mailpoet_coupon" style="' . $styles . '">' . EHelper::escapeHtmlText($couponCode) . '</div> |
| 71 | <!--<![endif]--> |
| 72 | </td> |
| 73 | </tr> |
| 74 | </table> |
| 75 | </div> |
| 76 | </td> |
| 77 | </tr>'; |
| 78 | return $template; |
| 79 | } |
| 80 | |
| 81 | public function calculateWidth($element, $columnBaseWidth): string { |
| 82 | $columnWidth = $columnBaseWidth - (StylesHelper::$paddingWidth * 2); |
| 83 | $borderWidth = (int)$element['styles']['block']['borderWidth']; |
| 84 | $width = (int)$element['styles']['block']['width']; |
| 85 | $width = ($width > $columnWidth) ? |
| 86 | $columnWidth : |
| 87 | $width; |
| 88 | return ($width - (2 * $borderWidth) . 'px'); |
| 89 | } |
| 90 | } |
| 91 |