Skip to content

Commit

Permalink
Merge pull request #925 from matomo-org/system-report-cache
Browse files Browse the repository at this point in the history
Avoid incidental execution of system report unless it has not been executed in a week
  • Loading branch information
diosmosis authored Nov 1, 2023
2 parents 8b72e08 + c36c142 commit 60e53e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
52 changes: 34 additions & 18 deletions classes/WpMatomo/Admin/SystemReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class SystemReport {
*/
public $db_settings;
/**
* @var string the php binary used by Matomo
* @var string the php binary used by Matomo (use get_php_binary() to access)
*/
private $binary;

Expand All @@ -133,11 +133,6 @@ public function __construct( Settings $settings ) {
$this->logger = new Logger();
$this->db_settings = new \WpMatomo\Db\Settings();
$this->shell_exec_available = function_exists( 'shell_exec' );
if ( ! WpMatomo::is_safe_mode() ) {
Bootstrap::do_bootstrap();
$cli_php = new CliPhp();
$this->binary = $cli_php->findPhpBinary();
}
}

public function get_not_compatible_plugins() {
Expand Down Expand Up @@ -289,20 +284,31 @@ private function get_error_tables() {
}

public function errors_present() {
$matomo_tables = $this->get_error_tables();
$cache_key = 'matomo_system_report_has_errors';
$cache_value = get_transient( $cache_key );

$matomo_tables = apply_filters( 'matomo_systemreport_tables', $matomo_tables );
$matomo_tables = $this->add_errors_first( $matomo_tables );
if ( false === $cache_value ) {
// 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.
set_transient( $cache_key, 0, WEEK_IN_SECONDS );

foreach ( $matomo_tables as $report_table ) {
foreach ( $report_table['rows'] as $row ) {
if ( ! empty( $row['is_error'] ) || ! empty( $row['is_warning'] ) ) {
return true;
$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'] ) ) {
$cache_value = true;
}
}
}

set_transient( $cache_key, (int) $cache_value, WEEK_IN_SECONDS );
}

return false;
return 1 === (int) $cache_value;
}

public function show() {
Expand Down Expand Up @@ -442,9 +448,9 @@ private function get_phpcli_info() {

private function get_phpcli_output( $phpcli_params ) {
$output = '';
if ( $this->shell_exec_available && $this->binary ) {
if ( $this->shell_exec_available && $this->get_php_cli_binary() ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec
$output = trim( '' . @shell_exec( $this->binary . ' ' . $phpcli_params ) );
$output = trim( '' . @shell_exec( $this->get_php_cli_binary() . ' ' . $phpcli_params ) );
}

return $output;
Expand Down Expand Up @@ -1295,10 +1301,10 @@ private function get_server_info() {
'value' => $this->initial_error_reporting . ' After bootstrap: ' . @error_reporting(),
];

if ( ! empty( $this->binary ) ) {
if ( ! empty( $this->get_php_cli_binary() ) ) {
$rows[] = [
'name' => 'PHP Found Binary',
'value' => $this->binary,
'value' => $this->get_php_cli_binary(),
];
}
$rows[] = [
Expand Down Expand Up @@ -1927,4 +1933,14 @@ private function replace_hexadecimal_colors( $content ) {

return $content;
}

private function get_php_cli_binary() {
if ( ! $this->binary && ! WpMatomo::is_safe_mode() ) {
Bootstrap::do_bootstrap();
$cli_php = new CliPhp();
$this->binary = $cli_php->findPhpBinary();
}

return $this->binary;
}
}
11 changes: 7 additions & 4 deletions classes/WpMatomo/ErrorNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public function check_errors() {
if ( isset( $_GET['page'] ) && substr( sanitize_text_field( wp_unslash( $_GET['page'] ) ), 0, 7 ) === 'matomo-' ) {
$system_report = new \WpMatomo\Admin\SystemReport( $this->settings );
if ( ! get_user_meta( get_current_user_id(), self::OPTION_NAME_SYSTEM_REPORT_ERRORS_DISMISSED ) && $system_report->errors_present() ) {
echo '<div class="notice notice-warning is-dismissible" id="matomo-systemreporterrors">
<p>' . esc_html__( 'There are some errors in the', 'matomo' ) .
' <a href="' . esc_url( admin_url( 'admin.php?page=matomo-systemreport' ) ) . '">' . esc_html__( 'Matomo Diagnostics System report', 'matomo' ) . '</a> ' .
esc_html__( 'that may prevent the plugin for working normally.', 'matomo' ) . '</p></div>';
echo '<div class="notice notice-warning is-dismissible" id="matomo-systemreporterrors"><p>'
. sprintf(
esc_html__( 'There are some errors in the %1$sMatomo Diagnostics System report%2$s that may prevent the plugin for working normally.', 'matomo' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=matomo-systemreport' ) ) . '">',
'</a>'
)
. '</p></div>';
}
}
}
Expand Down

0 comments on commit 60e53e9

Please sign in to comment.