Skip to content

Commit

Permalink
cache whether errors were found in the system report in a WP option s…
Browse files Browse the repository at this point in the history
…o the system report is not executed on every admin page view
  • Loading branch information
diosmosis committed Oct 31, 2023
1 parent d85d652 commit 7ba604b
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions classes/WpMatomo/Admin/SystemReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,39 @@ private function get_error_tables() {
}

public function errors_present() {
// TODO: cache this (use Matomo cache)
$secondsInADay = 86400;

$matomo_tables = $this->get_error_tables();
$cache_key = 'matomo_system_report_has_errors';
$cache_value = get_option( $cache_key );

$matomo_tables = apply_filters( 'matomo_systemreport_tables', $matomo_tables );
$matomo_tables = $this->add_errors_first( $matomo_tables );
if ( empty( $cache_value['last_updated'] )
|| $cache_value['last_updated'] + $secondsInADay < time()
|| ! isset( $cache_value['errors_found'] )
) {
// pre-record that there were no errors found. in case the system report fails to execute, this will
// allow the rest of Matomo for WordPress to continue to still be usable.
$cache_value = [ 'last_updated' => time(), 'errors_found' => 0 ];
update_option( $cache_key, $cache_value );

foreach ( $matomo_tables as $report_table ) {
foreach ( $report_table['rows'] as $row ) {
if ( ! empty( $row['is_error'] ) || ! empty( $row['is_warning'] ) ) {
return true;
$errors_found = false;
$matomo_tables = $this->get_error_tables();

$matomo_tables = apply_filters( 'matomo_systemreport_tables', $matomo_tables );
$matomo_tables = $this->add_errors_first( $matomo_tables );

foreach ( $matomo_tables as $report_table ) {
foreach ( $report_table['rows'] as $row ) {
if ( ! empty( $row['is_error'] ) || ! empty( $row['is_warning'] ) ) {
$errors_found = true;
}
}
}

$cache_value = [ 'last_updated' => time(), 'errors_found' => (int) $errors_found ];
update_option( $cache_key, $cache_value );
}

return false;
return $cache_value['errors_found'] == 1;
}

public function show() {
Expand Down

0 comments on commit 7ba604b

Please sign in to comment.