Files
wp-healthcheck/includes/steps/90-database.php
Steve Hanlon 0d51fc3b59 Phase 3 v1: autocheck framework + six step automations
Infrastructure:
- WPH_Step::autocheck($session_state) returns an array of findings
  shaped {id, level: ok/warn/bad/info, label, value, detail}.
- WPH_Session stores results keyed by step id (persisted in the option-
  backed session).
- WPH_Step::has_autocheck() reflection check so the UI only renders the
  panel for steps that implement automation.
- 'Run checks' / 'Refresh' button per step, admin-post handler runs
  autocheck() and stashes the result on the session.
- Findings rendered as a coloured table on the step card; included
  verbatim in the Markdown report with status icons.

Step automations implemented:
- Step 1 (Backup): detection of 11 known backup plugins by slug;
  active/inactive state; UpdraftPlus last-backup timestamp.
- Step 2 (Environment): PHP version + EOL, WP version vs latest, disk
  usage, wp-config flags, file perms on wp-config/wp-content/uploads,
  error-log sizes.
- Step 4 (Plugins): WP.org API enrichment with 24h transient cache —
  last_updated, active_installs, abandonment flag, removed-from-repo
  flag, update-available count. Summary line at the top.
- Step 8 (Security): SSL cert expiry via stream_socket_client +
  openssl_x509_parse, administrator audit, xmlrpc reachability, login
  URL hardening detection.
- Step 9 (Database): spam comments, post revisions, autoload size (WP
  6.6+ value handling), top 3 largest tables.
- Step 11 (Small fixes): deactivated-but-installed plugin list,
  homepage alt-text scan.

Smoke-tested on testsite — all six steps return findings with
correctly-classified levels. Report regenerated with automated findings
section.
2026-06-11 16:02:34 +01:00

77 lines
3.1 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
return new class extends WPH_Step {
public function id(): string { return 'database'; }
public function title(): string { return 'Step 9 — Database'; }
public function sub_items(): array {
return [
'Run a database optimisation (via WP-CLI: wp db optimize, or via a plugin such as WP-Optimize)',
'Check for and remove any spam comments if comment moderation hasn\'t been keeping up',
'Check post revisions — if excessive (thousands), note for client discussion on whether a revision limit should be set',
];
}
public function autocheck(array $session_state): array {
/** @var wpdb $wpdb */
global $wpdb;
$f = [];
// Spam comments
$counts = wp_count_comments();
$f[] = $this->finding(
'spam_comments',
($counts->spam ?? 0) > 100 ? 'warn' : 'ok',
'Spam comments',
number_format((int) ($counts->spam ?? 0)),
($counts->trash ?? 0) ? number_format((int) $counts->trash) . ' in trash too.' : ''
);
// Post revisions
$revisions = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision'");
$level = $revisions > 5000 ? 'warn' : 'ok';
$f[] = $this->finding(
'revisions',
$level,
'Post revisions',
number_format($revisions),
$level === 'warn' ? 'Consider setting WP_POST_REVISIONS to a sane limit (e.g. 10).' : ''
);
// Autoload option size. WP 6.6+ uses 'on'/'auto'/'auto-on' alongside legacy 'yes'.
$autoload_in = "autoload IN ('yes', 'on', 'auto', 'auto-on')";
$autoload_bytes = (int) $wpdb->get_var("SELECT SUM(LENGTH(option_value)) FROM {$wpdb->options} WHERE $autoload_in");
$autoload_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->options} WHERE $autoload_in");
$autoload_level = $autoload_bytes > 5 * MB_IN_BYTES ? 'warn' : 'ok';
$f[] = $this->finding(
'autoload',
$autoload_level,
'Autoload options',
$autoload_count . ' rows · ' . size_format($autoload_bytes),
$autoload_level === 'warn' ? 'Large autoload payload slows every request — investigate which plugins are responsible.' : ''
);
// Top 3 largest tables. `rows` is reserved in MySQL 8 — alias as table_rows.
$tables = $wpdb->get_results("
SELECT TABLE_NAME AS name,
DATA_LENGTH + INDEX_LENGTH AS bytes,
TABLE_ROWS AS table_rows
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
ORDER BY bytes DESC
LIMIT 3
");
foreach ($tables as $i => $t) {
$f[] = $this->finding(
'big_table_' . $i,
'info',
($i === 0 ? 'Largest tables — ' : '') . $t->name,
size_format((int) $t->bytes),
number_format((int) $t->table_rows) . ' rows'
);
}
return $f;
}
};