Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #3: Ignore random errors in certain cases #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions App/Admin/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
*/
class Notices {

/**
* Array of patterns to match WP Rocket related errors in debug.log.
*
* @var array
*/
private $debug_log_patterns = [
'/plugins/wp-rocket/',
'wpr_rucss_used_css',
'wpr_rocket_cache',
'WP_Rocket',
];

/**
* Array of WP Rocket related error patterns to exclude.
*
* @var array
*/
private $debug_log_exclusion_patterns = [
'wp-rocket/inc/Dependencies/Database',
'WP_Rocket(.*)Table->create',
'wp-rocket/inc/Engine/Common/Database/Queries/AbstractQuery',
'Failed opening(.*?)wp-rocket/',
];

/**
Expand All @@ -19,6 +35,8 @@ class Notices {
* @return void
*/
public function debug_log_notice() : void {
$display_error_notice = true;

if ( ! defined( 'WP_DEBUG' ) || ! defined( 'WP_DEBUG_LOG' ) || false === WP_DEBUG || false === WP_DEBUG_LOG ) {
return;
}
Expand All @@ -30,9 +48,52 @@ public function debug_log_notice() : void {
}

$content = $file_system->get_contents( WP_CONTENT_DIR . '/debug.log' );
preg_match_all( '#^\[(?<timestamp>.*?)\] (?<error>.+?)(?=\n\[|\z)#ms', $content, $matches, PREG_SET_ORDER);

// Flag errors related to WP Rocket in log.
$wpr_related_errors = [];
$patterns = implode( '|', $this->debug_log_patterns );
if ( ! preg_match( '#' . $patterns . '#', $content ) ) {

foreach( $matches as $match ) {
if ( ! preg_match( '#' . $patterns . '#', $match['error'] ) ) {
continue;
}

// Store errors.
$wpr_related_errors[] = $match['error'];
}

// Bail if no WP Rocket related error.
if ( empty( $wpr_related_errors ) ) {
return;
}

/**
* Filters WP Rocket related errors to be excluded.
*
* @param array $exclusion_patterns Array of WP Rocket related error patterns to exclude.
*/
$exclusion_patterns = apply_filters( 'rocket_e2e_error_exclusions', $this->debug_log_exclusion_patterns );

// Validate filter return.
if ( ! is_array( $exclusion_patterns ) || empty( $exclusion_patterns ) ) {
$exclusion_patterns = $this->debug_log_exclusion_patterns;
}

$exclusion_patterns = implode( '|', $exclusion_patterns );

// Check if errors should be excluded from notice.
foreach( $wpr_related_errors as $errors ) {
if ( preg_match( '#' . $exclusion_patterns . '#', $errors ) ) {
$display_error_notice = false;

continue;
}

$display_error_notice = true;
}

if ( ! $display_error_notice ) {
return;
}

Expand Down