| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('Unauthorized Access'); |
| 3 |
|
| 4 |
if (!(defined('WP_CLI') && WP_CLI)) return; |
| 5 |
|
| 6 |
/** |
| 7 |
* Manage phpinfo() WP from the command line. |
| 8 |
*/ |
| 9 |
class Phpinfo_WP_CLI { |
| 10 |
|
| 11 |
/** |
| 12 |
* Show PHP / WP / EOL status summary. |
| 13 |
* |
| 14 |
* ## EXAMPLES |
| 15 |
* wp phpinfowp status |
| 16 |
*/ |
| 17 |
public function status($args, $assoc): void { |
| 18 |
$eol = Phpinfo_WP_EOL::status(); |
| 19 |
WP_CLI::log('PHP: ' . PHP_VERSION); |
| 20 |
WP_CLI::log('Minor: ' . $eol['minor']); |
| 21 |
WP_CLI::log('EOL: ' . ($eol['eol'] ?? 'unknown') . ' (' . $eol['status'] . ')'); |
| 22 |
if ($eol['days'] !== null) { |
| 23 |
WP_CLI::log('Days: ' . $eol['days']); |
| 24 |
} |
| 25 |
WP_CLI::log('WordPress: ' . get_bloginfo('version')); |
| 26 |
WP_CLI::log('Memory: ' . size_format(memory_get_usage(true)) . ' / ' . ini_get('memory_limit')); |
| 27 |
WP_CLI::log('License: ' . (Phpinfo_WP_License::is_valid() ? 'Active (Pro)' : 'Inactive')); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Run the Pro config grader and print the result. |
| 32 |
* |
| 33 |
* ## EXAMPLES |
| 34 |
* wp phpinfowp grade |
| 35 |
*/ |
| 36 |
public function grade($args, $assoc): void { |
| 37 |
if (!Phpinfo_WP_License::is_valid()) { |
| 38 |
WP_CLI::error('Pro license required.'); |
| 39 |
} |
| 40 |
$r = Phpinfo_WP_Config_Grader::run(); |
| 41 |
WP_CLI::log("Grade: {$r['grade']} ({$r['score']}/100)"); |
| 42 |
$failing = array_filter($r['checks'], function ($c) { |
| 43 |
return $c['status'] === 'fail'; |
| 44 |
}); |
| 45 |
if (!$failing) { |
| 46 |
WP_CLI::success('All checks passing.'); |
| 47 |
return; |
| 48 |
} |
| 49 |
$rows = []; |
| 50 |
foreach ($failing as $c) { |
| 51 |
$rows[] = ['directive' => $c['key'], 'current' => $c['value'], 'recommended' => $c['good']]; |
| 52 |
} |
| 53 |
WP_CLI\Utils\format_items('table', $rows, ['directive', 'current', 'recommended']); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Take a config snapshot. |
| 58 |
* |
| 59 |
* ## OPTIONS |
| 60 |
* [--label=<label>] |
| 61 |
* : Optional label. |
| 62 |
*/ |
| 63 |
public function snapshot($args, $assoc): void { |
| 64 |
if (!Phpinfo_WP_License::is_valid()) WP_CLI::error('Pro license required.'); |
| 65 |
$label = $assoc['label'] ?? 'CLI snapshot'; |
| 66 |
$id = Phpinfo_WP_Snapshots::take($label, 0); |
| 67 |
if ($id) WP_CLI::success("Snapshot #{$id} captured."); |
| 68 |
else WP_CLI::error('Failed to capture snapshot.'); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check SSL certificates for the site + configured domains. |
| 73 |
*/ |
| 74 |
public function ssl($args, $assoc): void { |
| 75 |
if (!Phpinfo_WP_License::is_valid()) WP_CLI::error('Pro license required.'); |
| 76 |
$results = Phpinfo_WP_SSL::check_all(); |
| 77 |
$rows = []; |
| 78 |
foreach ($results as $r) { |
| 79 |
$rows[] = [ |
| 80 |
'host' => $r['host'] ?? '—', |
| 81 |
'expiry' => $r['expiry'] ?? '—', |
| 82 |
'days' => $r['days'] ?? '—', |
| 83 |
'status' => $r['status'] ?? ($r['error'] ?? '—'), |
| 84 |
]; |
| 85 |
} |
| 86 |
WP_CLI\Utils\format_items('table', $rows, ['host', 'expiry', 'days', 'status']); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Run the compatibility scanner. |
| 91 |
* |
| 92 |
* ## OPTIONS |
| 93 |
* [--target=<version>] |
| 94 |
* : Target PHP version (default 8.2). |
| 95 |
*/ |
| 96 |
public function compat($args, $assoc): void { |
| 97 |
if (!Phpinfo_WP_License::is_valid()) WP_CLI::error('Pro license required.'); |
| 98 |
$target = $assoc['target'] ?? '8.2'; |
| 99 |
WP_CLI::log("Scanning against PHP {$target}..."); |
| 100 |
$r = Phpinfo_WP_Compat::scan($target); |
| 101 |
if (isset($r['error'])) WP_CLI::error($r['error']); |
| 102 |
WP_CLI::log("Files scanned: {$r['files']} ({$r['duration']}s)"); |
| 103 |
WP_CLI::log("Total issues: {$r['total']} across {$r['with_issues']} plugins/themes"); |
| 104 |
if ($r['total'] === 0) WP_CLI::success('No compatibility issues found.'); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Show database health summary. |
| 109 |
*/ |
| 110 |
public function db($args, $assoc): void { |
| 111 |
if (!Phpinfo_WP_License::is_valid()) WP_CLI::error('Pro license required.'); |
| 112 |
$server = Phpinfo_WP_DB_Health::server_info(); |
| 113 |
$autoload = Phpinfo_WP_DB_Health::autoload_size(); |
| 114 |
$size = Phpinfo_WP_DB_Health::db_size(); |
| 115 |
WP_CLI::log("Engine: {$server['engine']} {$server['version']} ({$server['status']})"); |
| 116 |
WP_CLI::log("EOL: " . ($server['eol'] ?? 'unknown')); |
| 117 |
WP_CLI::log("DB size: " . size_format($size['total']) . " ({$size['tables']} tables)"); |
| 118 |
WP_CLI::log("Autoload: " . size_format($autoload['bytes']) . " ({$autoload['count']} options, {$autoload['status']})"); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* List WP-Cron events with overdue/orphan status. |
| 123 |
*/ |
| 124 |
public function cron($args, $assoc): void { |
| 125 |
if (!Phpinfo_WP_License::is_valid()) WP_CLI::error('Pro license required.'); |
| 126 |
$events = Phpinfo_WP_Cron_Monitor::events(); |
| 127 |
$rows = []; |
| 128 |
foreach ($events as $e) { |
| 129 |
$rows[] = [ |
| 130 |
'hook' => $e['hook'], |
| 131 |
'next_run' => wp_date('Y-m-d H:i', $e['timestamp']), |
| 132 |
'schedule' => $e['schedule_label'], |
| 133 |
'status' => $e['overdue'] ? 'overdue' : ($e['has_callback'] ? 'ok' : 'orphan'), |
| 134 |
]; |
| 135 |
} |
| 136 |
WP_CLI\Utils\format_items('table', $rows, ['hook', 'next_run', 'schedule', 'status']); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
WP_CLI::add_command('phpinfowp', 'Phpinfo_WP_CLI'); |
| 141 |
|