ArrayUtil.php
1 year ago
CallbackUtil.php
5 months ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
5 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
MetaDataUtil.php
2 months ago
NumberUtil.php
11 months ago
OrderUtil.php
7 months ago
PluginUtil.php
4 weeks ago
RestApiUtil.php
7 months ago
ShippingUtil.php
1 year ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
DiscountsUtil.php
44 lines
| 1 | <?php |
| 2 | /** |
| 3 | * DiscountsUtil class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Utilities; |
| 7 | |
| 8 | /** |
| 9 | * The DiscountsUtil class provides utilities to assist discounts calculation and validation. |
| 10 | */ |
| 11 | class DiscountsUtil { |
| 12 | |
| 13 | /** |
| 14 | * Checks if the given email address(es) matches the ones specified on the coupon. |
| 15 | * |
| 16 | * @param array $check_emails Array of customer email addresses. |
| 17 | * @param array $restrictions Array of allowed email addresses. |
| 18 | * |
| 19 | * @return bool |
| 20 | */ |
| 21 | public static function is_coupon_emails_allowed( $check_emails, $restrictions ) { |
| 22 | |
| 23 | foreach ( $check_emails as $check_email ) { |
| 24 | // With a direct match we return true. |
| 25 | if ( in_array( $check_email, $restrictions, true ) ) { |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | // Go through the allowed emails and return true if the email matches a wildcard. |
| 30 | foreach ( $restrictions as $restriction ) { |
| 31 | // Convert to PHP-regex syntax. |
| 32 | $regex = '/^' . str_replace( '*', '(.+)?', $restriction ) . '$/'; |
| 33 | preg_match( $regex, $check_email, $match ); |
| 34 | if ( ! empty( $match ) ) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // No matches, this one isn't allowed. |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 |