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
CouponBlockValidator.php
198 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet\Coupons; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WooCommerce\Helper; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class CouponBlockValidator { |
| 12 | /** @var Helper */ |
| 13 | private $wcHelper; |
| 14 | |
| 15 | /** @var WPFunctions */ |
| 16 | private $wp; |
| 17 | |
| 18 | public function __construct( |
| 19 | Helper $wcHelper, |
| 20 | WPFunctions $wp |
| 21 | ) { |
| 22 | $this->wcHelper = $wcHelper; |
| 23 | $this->wp = $wp; |
| 24 | } |
| 25 | |
| 26 | public function validate(array $attrs, string $recipientEmail): array { |
| 27 | $discountType = $this->validateDiscountType($attrs['discountType'] ?? null); |
| 28 | $amount = $this->validateRequiredNumber($attrs['amount'] ?? null, 'amount'); |
| 29 | if ($discountType === 'percent' && $amount > 100) { |
| 30 | throw new CouponBlockValidationException('Percent coupon amount must be 100 or lower.'); |
| 31 | } |
| 32 | |
| 33 | $minimumAmount = $this->validateOptionalNumber($attrs['minimumAmount'] ?? null, 'minimumAmount'); |
| 34 | $maximumAmount = $this->validateOptionalNumber($attrs['maximumAmount'] ?? null, 'maximumAmount'); |
| 35 | if ($minimumAmount !== '' && $maximumAmount !== '' && $maximumAmount < $minimumAmount) { |
| 36 | throw new CouponBlockValidationException('Maximum amount must be greater than or equal to minimum amount.'); |
| 37 | } |
| 38 | |
| 39 | $emailRestrictions = $this->validateEmailRestrictions($attrs['emailRestrictions'] ?? []); |
| 40 | if (CouponBlockAttributeParser::toBoolean($attrs['restrictToSubscriber'] ?? false)) { |
| 41 | $recipientEmailRestrictions = $this->validateEmailRestrictions([$recipientEmail]); |
| 42 | if (empty($recipientEmailRestrictions)) { |
| 43 | throw new CouponBlockValidationException('Recipient email is required for recipient-restricted coupons.'); |
| 44 | } |
| 45 | $emailRestrictions[] = $recipientEmailRestrictions[0]; |
| 46 | } |
| 47 | |
| 48 | return [ |
| 49 | 'discountType' => $discountType, |
| 50 | 'amount' => $amount, |
| 51 | 'expiryDay' => $this->validateOptionalInteger($attrs['expiryDay'] ?? null, 'expiryDay'), |
| 52 | 'freeShipping' => CouponBlockAttributeParser::toBoolean($attrs['freeShipping'] ?? false), |
| 53 | 'individualUse' => CouponBlockAttributeParser::toBoolean($attrs['individualUse'] ?? false), |
| 54 | 'excludeSaleItems' => CouponBlockAttributeParser::toBoolean($attrs['excludeSaleItems'] ?? false), |
| 55 | 'usageLimit' => $this->validateOptionalInteger($attrs['usageLimit'] ?? null, 'usageLimit'), |
| 56 | 'usageLimitPerUser' => $this->validateOptionalInteger($attrs['usageLimitPerUser'] ?? null, 'usageLimitPerUser'), |
| 57 | 'minimumAmount' => $minimumAmount, |
| 58 | 'maximumAmount' => $maximumAmount, |
| 59 | 'productIds' => $this->validateProductIds($attrs['productIds'] ?? []), |
| 60 | 'excludedProductIds' => $this->validateProductIds($attrs['excludedProductIds'] ?? []), |
| 61 | 'productCategoryIds' => $this->validateProductCategoryIds($attrs['productCategoryIds'] ?? []), |
| 62 | 'excludedProductCategoryIds' => $this->validateProductCategoryIds($attrs['excludedProductCategoryIds'] ?? []), |
| 63 | 'emailRestrictions' => array_values(array_unique($emailRestrictions)), |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | private function validateDiscountType($discountType): string { |
| 68 | if (!is_string($discountType) || $discountType === '') { |
| 69 | throw new CouponBlockValidationException('Discount type is required.'); |
| 70 | } |
| 71 | |
| 72 | if (!in_array($discountType, array_keys($this->wcHelper->wcGetCouponTypes()), true)) { |
| 73 | throw new CouponBlockValidationException('Invalid discount type.'); |
| 74 | } |
| 75 | |
| 76 | return $discountType; |
| 77 | } |
| 78 | |
| 79 | private function validateRequiredNumber($value, string $field): float { |
| 80 | if ($value === null || $value === '' || !is_numeric($value)) { |
| 81 | throw new CouponBlockValidationException(sprintf('%s must be numeric.', $field)); |
| 82 | } |
| 83 | |
| 84 | $value = (float)$value; |
| 85 | if ($value < 0) { |
| 86 | throw new CouponBlockValidationException(sprintf('%s must be greater than or equal to 0.', $field)); |
| 87 | } |
| 88 | |
| 89 | return $value; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return float|'' |
| 94 | */ |
| 95 | private function validateOptionalNumber($value, string $field) { |
| 96 | if ($value === null || $value === '') { |
| 97 | return ''; |
| 98 | } |
| 99 | |
| 100 | return $this->validateRequiredNumber($value, $field); |
| 101 | } |
| 102 | |
| 103 | private function validateOptionalInteger($value, string $field): int { |
| 104 | if ($value === null || $value === '') { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | if (filter_var($value, FILTER_VALIDATE_INT) === false) { |
| 109 | throw new CouponBlockValidationException(sprintf('%s must be an integer.', $field)); |
| 110 | } |
| 111 | |
| 112 | $value = (int)$value; |
| 113 | if ($value < 0) { |
| 114 | throw new CouponBlockValidationException(sprintf('%s must be greater than or equal to 0.', $field)); |
| 115 | } |
| 116 | |
| 117 | return $value; |
| 118 | } |
| 119 | |
| 120 | private function validateProductIds($items): array { |
| 121 | $ids = $this->extractItemIds($items, 'product'); |
| 122 | foreach ($ids as $id) { |
| 123 | if (!$this->wcHelper->wcGetProduct($id)) { |
| 124 | throw new CouponBlockValidationException('Invalid product ID.'); |
| 125 | } |
| 126 | } |
| 127 | return $ids; |
| 128 | } |
| 129 | |
| 130 | private function validateProductCategoryIds($items): array { |
| 131 | $ids = $this->extractItemIds($items, 'product category'); |
| 132 | foreach ($ids as $id) { |
| 133 | $term = $this->wp->getTerm($id, 'product_cat'); |
| 134 | if (!$term || $this->wp->isWpError($term)) { |
| 135 | throw new CouponBlockValidationException('Invalid product category ID.'); |
| 136 | } |
| 137 | } |
| 138 | return $ids; |
| 139 | } |
| 140 | |
| 141 | private function extractItemIds($items, string $label): array { |
| 142 | if ($items === null || $items === '') { |
| 143 | return []; |
| 144 | } |
| 145 | if (!is_array($items)) { |
| 146 | throw new CouponBlockValidationException(sprintf('Invalid %s IDs.', $label)); |
| 147 | } |
| 148 | |
| 149 | $ids = []; |
| 150 | foreach ($items as $item) { |
| 151 | $id = null; |
| 152 | if (is_array($item)) { |
| 153 | $id = $item['id'] ?? null; |
| 154 | } elseif (is_object($item) && isset($item->id)) { |
| 155 | $id = $item->id; |
| 156 | } |
| 157 | |
| 158 | if (filter_var($id, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) === false) { |
| 159 | throw new CouponBlockValidationException(sprintf('Invalid %s ID.', $label)); |
| 160 | } |
| 161 | $ids[] = (int)$id; |
| 162 | } |
| 163 | |
| 164 | return array_values(array_unique($ids)); |
| 165 | } |
| 166 | |
| 167 | private function validateEmailRestrictions($raw): array { |
| 168 | if ($raw === null || $raw === '') { |
| 169 | return []; |
| 170 | } |
| 171 | |
| 172 | if (is_string($raw)) { |
| 173 | $raw = explode(',', $raw); |
| 174 | } |
| 175 | |
| 176 | if (!is_array($raw)) { |
| 177 | throw new CouponBlockValidationException('Invalid email restrictions.'); |
| 178 | } |
| 179 | |
| 180 | $emails = []; |
| 181 | foreach ($raw as $email) { |
| 182 | if (!is_string($email)) { |
| 183 | throw new CouponBlockValidationException('Invalid email restriction.'); |
| 184 | } |
| 185 | $email = strtolower(trim($this->wp->sanitizeEmail($email))); |
| 186 | if (!$email) { |
| 187 | continue; |
| 188 | } |
| 189 | if (!$this->wp->isEmail($email)) { |
| 190 | throw new CouponBlockValidationException('Invalid email restriction.'); |
| 191 | } |
| 192 | $emails[] = $email; |
| 193 | } |
| 194 | |
| 195 | return array_values(array_unique($emails)); |
| 196 | } |
| 197 | } |
| 198 |