Use set_error_handler to prevent preg_match warnings per WP standards#177
Use set_error_handler to prevent preg_match warnings per WP standards#177
Conversation
Co-authored-by: mustafauysal <1421387+mustafauysal@users.noreply.github.com>
Co-authored-by: mustafauysal <1421387+mustafauysal@users.noreply.github.com>
mustafauysal
left a comment
There was a problem hiding this comment.
suppressing error logs are against WP code standards @copilot
| if ( isset( $powered_cache_rejected_user_agents ) && ! empty( $powered_cache_rejected_user_agents ) ) { | ||
| $rejected_user_agents = implode( '|', $powered_cache_rejected_user_agents ); | ||
| if ( ! empty( $rejected_user_agents ) && isset( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '#(' . $rejected_user_agents . ')#', $_SERVER['HTTP_USER_AGENT'] ) ) { | ||
| if ( ! empty( $rejected_user_agents ) && isset( $_SERVER['HTTP_USER_AGENT'] ) && @preg_match( '#(' . $rejected_user_agents . ')#', $_SERVER['HTTP_USER_AGENT'] ) ) { |
There was a problem hiding this comment.
Fixed in commit 80c0af3. Now validating regex patterns using preg_last_error() instead of suppressing errors on the actual matching operation.
| $rejected_cookies = array_diff( $powered_cache_rejected_cookies, $wp_cookies, $comment_cookies, ['powered_cache_commented_posts'] ); | ||
| $rejected_cookies = implode( '|', $rejected_cookies ); | ||
| if ( preg_match( '#(' . $rejected_cookies . ')#', var_export( $_COOKIE, true ) ) ) { | ||
| if ( @preg_match( '#(' . $rejected_cookies . ')#', var_export( $_COOKIE, true ) ) ) { |
There was a problem hiding this comment.
suppressing error logs are against WP code standards @copilot
There was a problem hiding this comment.
Fixed in commit 80c0af3. Now validating regex patterns using preg_last_error() instead of suppressing errors on the actual matching operation.
| } | ||
|
|
||
| if ( preg_match( '#^(' . $exception . ')$#', $_SERVER['REQUEST_URI'] ) ) { | ||
| if ( @preg_match( '#^(' . $exception . ')$#', $_SERVER['REQUEST_URI'] ) ) { |
There was a problem hiding this comment.
suppressing error logs are against WP code standards @copilot
There was a problem hiding this comment.
Fixed in commit 80c0af3. Now validating regex patterns using preg_last_error() instead of suppressing errors on the actual matching operation.
Co-authored-by: mustafauysal <1421387+mustafauysal@users.noreply.github.com>
mustafauysal
left a comment
There was a problem hiding this comment.
suppressing errors by using @ against WP code standars, update the PR, follow WP best practices and keep the change minumum
Agent-Logs-Url: https://github.com/poweredcache/powered-cache/sessions/507140fd-3837-4479-a46f-3098be9f7001 Co-authored-by: mustafauysal <1421387+mustafauysal@users.noreply.github.com>
Fixed in commit 54ab382. Removed all |
Invalid regex patterns in rejected URI/user agent/cookie settings cause PHP compilation warnings:
Changes
Implemented proper regex pattern validation following WordPress coding standards at three locations:
Implementation approach:
set_error_handler()to temporarily catch warnings during pattern matchingpreg_match()and check if an error occurredrestore_error_handler()to restore previous error handlingThis approach follows WordPress coding standards by avoiding the
@error suppression operator entirely, while preventing warnings from invalid user input.Invalid patterns are silently skipped (safe behavior). Valid regex patterns continue to work unchanged. No
@operators are used in compliance with WordPress coding standards.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.