CouponBlock.php
3 months ago
CouponBlockAttributeParser.php
3 months ago
CouponBlockDetector.php
3 months ago
CouponBlockFailureTranslator.php
3 months ago
CouponBlockGenerationFailureCollector.php
3 months ago
CouponBlockGenerator.php
3 months ago
CouponBlockValidationException.php
3 months ago
CouponBlockValidator.php
3 months ago
EmailContextBuilder.php
1 month ago
index.php
3 months ago
CouponBlock.php
81 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet\Coupons; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | class CouponBlock { |
| 9 | const NAME = 'woocommerce/coupon-code'; |
| 10 | const SAFE_PLACEHOLDER = 'XXXX-XXXXXX-XXXX'; |
| 11 | |
| 12 | private const GENERATED_COUPON_ATTRIBUTES = [ |
| 13 | 'discountType', |
| 14 | 'amount', |
| 15 | 'expiryDay', |
| 16 | 'freeShipping', |
| 17 | 'minimumAmount', |
| 18 | 'maximumAmount', |
| 19 | 'individualUse', |
| 20 | 'excludeSaleItems', |
| 21 | 'productIds', |
| 22 | 'excludedProductIds', |
| 23 | 'productCategoryIds', |
| 24 | 'excludedProductCategoryIds', |
| 25 | 'emailRestrictions', |
| 26 | 'usageLimit', |
| 27 | 'usageLimitPerUser', |
| 28 | 'restrictToSubscriber', |
| 29 | ]; |
| 30 | |
| 31 | private const CREATE_NEW_DEFAULT_ATTRIBUTES = [ |
| 32 | 'source' => 'createNew', |
| 33 | 'discountType' => 'percent', |
| 34 | 'amount' => 10, |
| 35 | 'expiryDay' => 10, |
| 36 | 'freeShipping' => false, |
| 37 | 'minimumAmount' => '', |
| 38 | 'maximumAmount' => '', |
| 39 | 'individualUse' => false, |
| 40 | 'excludeSaleItems' => false, |
| 41 | 'productIds' => [], |
| 42 | 'excludedProductIds' => [], |
| 43 | 'productCategoryIds' => [], |
| 44 | 'excludedProductCategoryIds' => [], |
| 45 | 'emailRestrictions' => '', |
| 46 | 'usageLimit' => 0, |
| 47 | 'usageLimitPerUser' => 0, |
| 48 | 'restrictToSubscriber' => false, |
| 49 | ]; |
| 50 | |
| 51 | public static function isCreateNew(array $attrs, ?bool $containsGeneratedPlaceholder = null): bool { |
| 52 | if (array_key_exists('source', $attrs)) { |
| 53 | return $attrs['source'] === 'createNew'; |
| 54 | } |
| 55 | |
| 56 | if (!empty($attrs['couponCode'])) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ($containsGeneratedPlaceholder !== null) { |
| 61 | return $containsGeneratedPlaceholder || self::hasGeneratedCouponAttributes($attrs); |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | public static function withCreateNewDefaults(array $attrs): array { |
| 68 | return array_merge(self::CREATE_NEW_DEFAULT_ATTRIBUTES, $attrs); |
| 69 | } |
| 70 | |
| 71 | private static function hasGeneratedCouponAttributes(array $attrs): bool { |
| 72 | foreach (self::GENERATED_COUPON_ATTRIBUTES as $attribute) { |
| 73 | if (array_key_exists($attribute, $attrs)) { |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 |