| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('Unauthorized Access'); |
| 3 |
|
| 4 |
// WP 7.0 Abilities API integration. Exposes the audit data this plugin |
| 5 |
// already collects (PHP version + EOL, config grade, directives, extensions) |
| 6 |
// as named server abilities so AI assistants and other plugins on the site |
| 7 |
// can introspect server health through a standard interface instead of |
| 8 |
// scraping the phpinfo screen. |
| 9 |
// |
| 10 |
// All abilities require manage_options because they expose server config — |
| 11 |
// the same gate every visible page of this plugin uses. |
| 12 |
|
| 13 |
class Phpinfo_WP_Abilities { |
| 14 |
|
| 15 |
public static function register(): void { |
| 16 |
// Abilities API ships in WP 7.0. On older cores the hook never fires, |
| 17 |
// so the rest of the plugin keeps working unchanged. |
| 18 |
add_action('wp_abilities_api_init', [self::class, 'register_abilities']); |
| 19 |
} |
| 20 |
|
| 21 |
public static function register_abilities(): void { |
| 22 |
if (!function_exists('wp_register_ability_category') || !function_exists('wp_register_ability')) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
wp_register_ability_category('phpinfowp/audit', [ |
| 27 |
'label' => __('Server Health Audit', 'phpinfo-wp'), |
| 28 |
'description' => __('Inspect PHP version, server configuration, extensions, and audit grade collected by phpinfo() WP.', 'phpinfo-wp'), |
| 29 |
]); |
| 30 |
|
| 31 |
wp_register_ability('phpinfowp/get-php-version', [ |
| 32 |
'label' => __('Get PHP Version & EOL Status', 'phpinfo-wp'), |
| 33 |
'description' => __('Returns the running PHP version, its end-of-life date, days remaining, and an ok/warning/eol status flag.', 'phpinfo-wp'), |
| 34 |
'category' => 'phpinfowp/audit', |
| 35 |
'input_schema' => ['type' => 'object', 'properties' => new stdClass()], |
| 36 |
'output_schema' => [ |
| 37 |
'type' => 'object', |
| 38 |
'properties' => [ |
| 39 |
'version' => ['type' => 'string'], |
| 40 |
'minor' => ['type' => 'string'], |
| 41 |
'eol' => ['type' => ['string', 'null']], |
| 42 |
'days' => ['type' => ['integer', 'null']], |
| 43 |
'status' => ['type' => 'string', 'enum' => ['ok', 'warning', 'eol', 'unknown']], |
| 44 |
], |
| 45 |
], |
| 46 |
'execute_callback' => [self::class, 'do_get_php_version'], |
| 47 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 48 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 49 |
]); |
| 50 |
|
| 51 |
wp_register_ability('phpinfowp/get-config-grade', [ |
| 52 |
'label' => __('Get Config Grade', 'phpinfo-wp'), |
| 53 |
'description' => __('Returns the overall A–F config grade, numeric score, and counts of passing/warning/failing checks.', 'phpinfo-wp'), |
| 54 |
'category' => 'phpinfowp/audit', |
| 55 |
'input_schema' => ['type' => 'object', 'properties' => new stdClass()], |
| 56 |
'output_schema' => [ |
| 57 |
'type' => 'object', |
| 58 |
'properties' => [ |
| 59 |
'grade' => ['type' => 'string'], |
| 60 |
'score' => ['type' => 'integer'], |
| 61 |
'passes' => ['type' => 'integer'], |
| 62 |
'warns' => ['type' => 'integer'], |
| 63 |
'fails' => ['type' => 'integer'], |
| 64 |
'total' => ['type' => 'integer'], |
| 65 |
], |
| 66 |
], |
| 67 |
'execute_callback' => [self::class, 'do_get_config_grade'], |
| 68 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 69 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 70 |
]); |
| 71 |
|
| 72 |
wp_register_ability('phpinfowp/get-config-issues', [ |
| 73 |
'label' => __('List Failing Config Checks', 'phpinfo-wp'), |
| 74 |
'description' => __('Returns each failing or warning config check with its directive key, current value, recommended value, and severity. Pro-only — free tier returns an empty list.', 'phpinfo-wp'), |
| 75 |
'category' => 'phpinfowp/audit', |
| 76 |
'input_schema' => ['type' => 'object', 'properties' => new stdClass()], |
| 77 |
'output_schema' => [ |
| 78 |
'type' => 'array', |
| 79 |
'items' => [ |
| 80 |
'type' => 'object', |
| 81 |
'properties' => [ |
| 82 |
'key' => ['type' => 'string'], |
| 83 |
'label' => ['type' => 'string'], |
| 84 |
'category' => ['type' => 'string'], |
| 85 |
'value' => ['type' => 'string'], |
| 86 |
'recommended' => ['type' => 'string'], |
| 87 |
'status' => ['type' => 'string', 'enum' => ['warn', 'fail']], |
| 88 |
'note' => ['type' => 'string'], |
| 89 |
], |
| 90 |
], |
| 91 |
], |
| 92 |
'execute_callback' => [self::class, 'do_get_config_issues'], |
| 93 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 94 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 95 |
]); |
| 96 |
|
| 97 |
wp_register_ability('phpinfowp/get-directive', [ |
| 98 |
'label' => __('Get PHP Directive Value', 'phpinfo-wp'), |
| 99 |
'description' => __('Returns the current value of a named php.ini directive (e.g. memory_limit, upload_max_filesize).', 'phpinfo-wp'), |
| 100 |
'category' => 'phpinfowp/audit', |
| 101 |
'input_schema' => [ |
| 102 |
'type' => 'object', |
| 103 |
'properties' => [ |
| 104 |
'name' => ['type' => 'string', 'description' => 'php.ini directive name'], |
| 105 |
], |
| 106 |
'required' => ['name'], |
| 107 |
], |
| 108 |
'output_schema' => [ |
| 109 |
'type' => 'object', |
| 110 |
'properties' => [ |
| 111 |
'name' => ['type' => 'string'], |
| 112 |
'value' => ['type' => ['string', 'null']], |
| 113 |
'set' => ['type' => 'boolean'], |
| 114 |
], |
| 115 |
], |
| 116 |
'execute_callback' => [self::class, 'do_get_directive'], |
| 117 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 118 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 119 |
]); |
| 120 |
|
| 121 |
wp_register_ability('phpinfowp/list-extensions', [ |
| 122 |
'label' => __('List Loaded PHP Extensions', 'phpinfo-wp'), |
| 123 |
'description' => __('Returns the names of all PHP extensions currently loaded on the server, sorted alphabetically.', 'phpinfo-wp'), |
| 124 |
'category' => 'phpinfowp/audit', |
| 125 |
'input_schema' => ['type' => 'object', 'properties' => new stdClass()], |
| 126 |
'output_schema' => [ |
| 127 |
'type' => 'array', |
| 128 |
'items' => ['type' => 'string'], |
| 129 |
], |
| 130 |
'execute_callback' => [self::class, 'do_list_extensions'], |
| 131 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 132 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 133 |
]); |
| 134 |
|
| 135 |
wp_register_ability('phpinfowp/get-audit-summary', [ |
| 136 |
'label' => __('Get Server Audit Summary', 'phpinfo-wp'), |
| 137 |
'description' => __('Returns a high-level rollup of server health: PHP version, EOL status, config grade, and counts of issues. The single best ability to call for a "how is my server doing" check.', 'phpinfo-wp'), |
| 138 |
'category' => 'phpinfowp/audit', |
| 139 |
'input_schema' => ['type' => 'object', 'properties' => new stdClass()], |
| 140 |
'output_schema' => [ |
| 141 |
'type' => 'object', |
| 142 |
'properties' => [ |
| 143 |
'php' => ['type' => 'object'], |
| 144 |
'config' => ['type' => 'object'], |
| 145 |
], |
| 146 |
], |
| 147 |
'execute_callback' => [self::class, 'do_get_audit_summary'], |
| 148 |
'permission_callback' => [self::class, 'can_read_audit'], |
| 149 |
'meta' => ['show_in_rest' => true, 'annotations' => ['readonly' => true]], |
| 150 |
]); |
| 151 |
} |
| 152 |
|
| 153 |
public static function can_read_audit($input = null) { |
| 154 |
return current_user_can('manage_options'); |
| 155 |
} |
| 156 |
|
| 157 |
public static function do_get_php_version($input = null): array { |
| 158 |
$s = Phpinfo_WP_EOL::status(); |
| 159 |
return [ |
| 160 |
'version' => PHP_VERSION, |
| 161 |
'minor' => $s['minor'] ?? '', |
| 162 |
'eol' => $s['eol'] ?? null, |
| 163 |
'days' => $s['days'] ?? null, |
| 164 |
'status' => $s['status']?? 'unknown', |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
public static function do_get_config_grade($input = null): array { |
| 169 |
return Phpinfo_WP_Config_Grader::summary(); |
| 170 |
} |
| 171 |
|
| 172 |
public static function do_get_config_issues($input = null): array { |
| 173 |
$run = Phpinfo_WP_Config_Grader::run(); |
| 174 |
if (empty($run['checks'])) return []; |
| 175 |
$issues = []; |
| 176 |
foreach ($run['checks'] as $c) { |
| 177 |
if ($c['status'] === 'pass') continue; |
| 178 |
$issues[] = [ |
| 179 |
'key' => (string) $c['key'], |
| 180 |
'label' => (string) $c['label'], |
| 181 |
'category' => (string) $c['category'], |
| 182 |
'value' => (string) $c['value'], |
| 183 |
'recommended' => (string) $c['good'], |
| 184 |
'status' => (string) $c['status'], |
| 185 |
'note' => (string) $c['note'], |
| 186 |
]; |
| 187 |
} |
| 188 |
return $issues; |
| 189 |
} |
| 190 |
|
| 191 |
public static function do_get_directive($input): array { |
| 192 |
$name = is_array($input) ? ($input['name'] ?? '') : (string) $input; |
| 193 |
$name = preg_replace('/[^a-zA-Z0-9_.]/', '', (string) $name); |
| 194 |
if ($name === '') { |
| 195 |
return ['name' => '', 'value' => null, 'set' => false]; |
| 196 |
} |
| 197 |
$raw = ini_get($name); |
| 198 |
return [ |
| 199 |
'name' => $name, |
| 200 |
'value' => $raw === false ? null : (string) $raw, |
| 201 |
'set' => $raw !== false && $raw !== '', |
| 202 |
]; |
| 203 |
} |
| 204 |
|
| 205 |
public static function do_list_extensions($input = null): array { |
| 206 |
$exts = get_loaded_extensions(); |
| 207 |
sort($exts, SORT_NATURAL | SORT_FLAG_CASE); |
| 208 |
return array_values($exts); |
| 209 |
} |
| 210 |
|
| 211 |
public static function do_get_audit_summary($input = null): array { |
| 212 |
return [ |
| 213 |
'php' => self::do_get_php_version(), |
| 214 |
'config' => self::do_get_config_grade(), |
| 215 |
]; |
| 216 |
} |
| 217 |
} |
| 218 |
|