From 5fa2d232eb27e48dab15e904e146a8dd98faa944 Mon Sep 17 00:00:00 2001 From: dmsnell Date: Sat, 30 Aug 2025 20:53:31 +0000 Subject: [PATCH] Compat: Replace warning-suppression in _wp_can_use_pcre_u() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch replaces the use of the problematic error-suppresssion operator with a specific error-handler to catch and report Unicode PCRE support without raising the related issues of error-suppression: notably conflating errors and failing to prevent completely the logging of the warnings. In this case, the WPCS rule against using error-suppression was actually helpful in pointing out the risk, but the code was left in place with an “ignore” comment to silence the violation; this patch addresses the risk and removes the need for the comment. Developed in https://github.com/WordPress/wordpress-develop/pull/9576 Discussed in https://core.trac.wordpress.org/ticket/63865 Follow-up to: [45611]. Props dmsnell. Fixes #63865. Built from https://develop.svn.wordpress.org/trunk@60694 git-svn-id: http://core.svn.wordpress.org/trunk@60030 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/compat.php | 24 ++++++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/wp-includes/compat.php b/wp-includes/compat.php index 84e611faee..864c410620 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -48,8 +48,28 @@ function _wp_can_use_pcre_u( $set = null ) { } if ( 'reset' === $utf8_pcre ) { - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support. - $utf8_pcre = @preg_match( '/^./u', 'a' ); + $utf8_pcre = true; + + set_error_handler( + function ( $errno, $errstr ) use ( &$utf8_pcre ) { + if ( str_starts_with( $errstr, 'preg_match():' ) ) { + $utf8_pcre = false; + return true; + } + + return false; + }, + E_WARNING + ); + + /* + * Attempt to compile a PCRE pattern with the PCRE_UTF8 flag. For + * systems lacking Unicode support this will trigger a warning + * during compilation, which the error handler will intercept. + */ + preg_match( '//u', '' ); + + restore_error_handler(); } return $utf8_pcre; diff --git a/wp-includes/version.php b/wp-includes/version.php index b23a19c182..5489863251 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.9-alpha-60693'; +$wp_version = '6.9-alpha-60694'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.