Commit 2982e6c0225 for woocommerce
commit 2982e6c0225a8e1cf1e2d851f23ffbeafbd1ac0a
Author: Bruna <bruna.filippozzi@automattic.com>
Date: Thu Apr 30 10:08:51 2026 +0200
Fix preg_match null deprecation in WC_Eval_Math (#64158)
WC_Eval_Math_Stack::last() can return null when the stack does not have
enough elements. Passing null to preg_match() triggers a deprecation
notice on PHP 8.1+. Use null coalescing to default to an empty string,
preserving existing behavior (no match).
Fixes woocommerce/woocommerce#52689
Co-authored-by: Bruna <bruberries@MacBook-Pro-9.local>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/fix-eval-math-preg-match-null-deprecation b/plugins/woocommerce/changelog/fix-eval-math-preg-match-null-deprecation
new file mode 100644
index 00000000000..8cab04dbd74
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-eval-math-preg-match-null-deprecation
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix PHP 8.1+ deprecation notice when passing null to preg_match in WC_Eval_Math.
diff --git a/plugins/woocommerce/includes/libraries/class-wc-eval-math.php b/plugins/woocommerce/includes/libraries/class-wc-eval-math.php
index 11d8509a19b..9b4aea4a2b9 100644
--- a/plugins/woocommerce/includes/libraries/class-wc-eval-math.php
+++ b/plugins/woocommerce/includes/libraries/class-wc-eval-math.php
@@ -159,7 +159,7 @@ if ( ! class_exists( 'WC_Eval_Math', false ) ) {
$output[] = $o2;
}
}
- if ( preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) ) { // did we just close a function?
+ if ( preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ) ?? '', $matches ) ) { // did we just close a function?
$fnn = $matches[1]; // get the function name
$arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
$output[] = $stack->pop(); // pop the function and push onto the output
@@ -186,7 +186,7 @@ if ( ! class_exists( 'WC_Eval_Math', false ) ) {
}
}
// make sure there was a function
- if ( ! preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) ) {
+ if ( ! preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ) ?? '', $matches ) ) {
return self::trigger( "unexpected ','" );
}
$stack->push( $stack->pop() + 1 ); // increment the argument count