Commit e4fcfc68f10 for woocommerce
commit e4fcfc68f10493601ef9c94b5acd8cc35b79c890
Author: Luigi Teschio <gigitux@gmail.com>
Date: Sat Nov 21 02:58:57 2026 +0100
Match coupon allowed emails literally, with * as the only wildcard (#71219)
(cherry picked from commit 902beef0e56928e9a11e47764751b0103b4c8131)
Co-authored-by: Yuliyan Slavchev <yuliyan.slavchev@gmail.com>
diff --git a/plugins/woocommerce/src/Utilities/DiscountsUtil.php b/plugins/woocommerce/src/Utilities/DiscountsUtil.php
index 77683fd88eb..49a53930f85 100644
--- a/plugins/woocommerce/src/Utilities/DiscountsUtil.php
+++ b/plugins/woocommerce/src/Utilities/DiscountsUtil.php
@@ -28,8 +28,8 @@ class DiscountsUtil {
// Go through the allowed emails and return true if the email matches a wildcard.
foreach ( $restrictions as $restriction ) {
- // Convert to PHP-regex syntax.
- $regex = '/^' . str_replace( '*', '(.+)?', $restriction ) . '$/';
+ // Convert to PHP-regex syntax. Only the asterisk is a wildcard, everything else is matched literally.
+ $regex = '/^' . str_replace( '\*', '(.+)?', preg_quote( $restriction, '/' ) ) . '$/';
preg_match( $regex, $check_email, $match );
if ( ! empty( $match ) ) {
return true;
diff --git a/plugins/woocommerce/tests/php/src/Utilities/DiscountsUtilTest.php b/plugins/woocommerce/tests/php/src/Utilities/DiscountsUtilTest.php
index c44052e7307..234f6906751 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/DiscountsUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/DiscountsUtilTest.php
@@ -30,4 +30,23 @@ class DiscountsUtilTest extends \WC_Unit_Test_Case {
// Special characters in email.
$this->assertEquals( false, DiscountsUtil::is_coupon_emails_allowed( array( 'special@example.com' ), array( 'special+char@example.com' ) ) );
}
+
+ /**
+ * @testdox `is_coupon_emails_allowed` should treat only the asterisk as a wildcard and match every other character literally.
+ */
+ public function test_is_coupon_emails_allowed_matches_characters_literally() {
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'janexdoe@wc.local' ), array( 'jane.doe@wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'jane-doe@wc.local' ), array( 'jane.doe@wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'customer@staffxwc.local' ), array( '*@staff.wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'janevip@wc.local' ), array( 'jane+vip@wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'another@wc.local' ), array( 'a|b@wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'x@wc.local' ), array( 'a/b@wc.local' ) ) );
+
+ $this->assertTrue( DiscountsUtil::is_coupon_emails_allowed( array( 'jane+vip@wc.local' ), array( '*+vip@wc.local' ) ) );
+ $this->assertTrue( DiscountsUtil::is_coupon_emails_allowed( array( 'a|b@wc.local' ), array( 'a|*@wc.local' ) ) );
+ $this->assertTrue( DiscountsUtil::is_coupon_emails_allowed( array( 'a/b@wc.local' ), array( 'a/*@wc.local' ) ) );
+ $this->assertTrue( DiscountsUtil::is_coupon_emails_allowed( array( 'customer@staff.wc.local' ), array( '*@staff.wc.local' ) ) );
+ $this->assertTrue( DiscountsUtil::is_coupon_emails_allowed( array( 'jane+vip@wc.local' ), array( 'jane+*@wc.local' ) ) );
+ $this->assertFalse( DiscountsUtil::is_coupon_emails_allowed( array( 'janexvip@wc.local' ), array( 'jane+*@wc.local' ) ) );
+ }
}