diff --git a/includes/dropins/page-cache.php b/includes/dropins/page-cache.php index 47d1192..2adf875 100644 --- a/includes/dropins/page-cache.php +++ b/includes/dropins/page-cache.php @@ -62,10 +62,26 @@ // Don't cache page with these user agents 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'] ) ) { - powered_cache_add_cache_miss_header( "Rejected user agent" ); + if ( ! empty( $rejected_user_agents ) && isset( $_SERVER['HTTP_USER_AGENT'] ) ) { + $pattern = '#(' . $rejected_user_agents . ')#'; + $error_occurred = false; - return; + set_error_handler( + function () use ( &$error_occurred ) { + $error_occurred = true; + }, + E_WARNING + ); + + $match = preg_match( $pattern, $_SERVER['HTTP_USER_AGENT'] ); + + restore_error_handler(); + + if ( ! $error_occurred && $match ) { + powered_cache_add_cache_miss_header( "Rejected user agent" ); + + return; + } } } @@ -150,7 +166,21 @@ if ( ! empty( $powered_cache_rejected_cookies ) ) { $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 ) ) ) { + $pattern = '#(' . $rejected_cookies . ')#'; + $error_occurred = false; + + set_error_handler( + function () use ( &$error_occurred ) { + $error_occurred = true; + }, + E_WARNING + ); + + $match = preg_match( $pattern, var_export( $_COOKIE, true ) ); + + restore_error_handler(); + + if ( ! $error_occurred && $match ) { powered_cache_add_cache_miss_header( "Rejected cookie" ); return; } @@ -173,7 +203,21 @@ continue; } - if ( preg_match( '#^(' . $exception . ')$#', $_SERVER['REQUEST_URI'] ) ) { + $pattern = '#^(' . $exception . ')$#'; + $error_occurred = false; + + set_error_handler( + function () use ( &$error_occurred ) { + $error_occurred = true; + }, + E_WARNING + ); + + $match = preg_match( $pattern, $_SERVER['REQUEST_URI'] ); + + restore_error_handler(); + + if ( ! $error_occurred && $match ) { powered_cache_add_cache_miss_header( "Rejected page" ); return;