PluginProbe
phpinfo() WP – Site Health, PHP Compatibility & Server Audit / 7.2.6
phpinfo() WP – Site Health, PHP Compatibility & Server Audit v7.2.6
7.2.7 7.2.6 7.2.5 7.2.4 7.2.3 7.2.0 7.2.1 7.2.2 7.1.0 7.0.3 7.0.4 7.0.5 trunk 6.0 7.0.0 7.0.1 7.0.2
phpinfo-wp / includes / class-opcache.php

class-opcache.php in phpinfo() WP – Site Health, PHP Compatibility & Server Audit 7.2.6, at includes/class-opcache.php

71 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') or die('Unauthorized Access');
3
4 class Phpinfo_WP_OPcache {
5
6 private static function _pro(): bool { return Phpinfo_WP_License::is_valid(); }
7
8 public static function is_available(): bool {
9 return function_exists('opcache_get_status') && function_exists('opcache_get_configuration');
10 }
11
12 public static function status(): ?array {
13 if (!self::is_available()) return null;
14
15 $status = @opcache_get_status(false);
16 $config = @opcache_get_configuration();
17
18 if (!$status || !$config) return null;
19
20 $mem = $status['memory_usage'] ?? [];
21 $used = (int) ($mem['used_memory'] ?? 0);
22 $free = (int) ($mem['free_memory'] ?? 0);
23 $wasted = (int) ($mem['wasted_memory'] ?? 0);
24 $total = $used + $free + $wasted;
25 $hit_rate = null;
26
27 $stats = $status['opcache_statistics'] ?? [];
28 if (isset($stats['hits'], $stats['misses']) && ($stats['hits'] + $stats['misses']) > 0) {
29 $hit_rate = round($stats['hits'] / ($stats['hits'] + $stats['misses']) * 100, 2);
30 }
31
32 return [
33 'enabled' => (bool) ($status['opcache_enabled'] ?? false),
34 'full' => (bool) ($status['cache_full'] ?? false),
35 'hit_rate' => $hit_rate,
36 'hits' => (int) ($stats['hits'] ?? 0),
37 'misses' => (int) ($stats['misses'] ?? 0),
38 'cached_scripts' => (int) ($stats['num_cached_scripts'] ?? 0),
39 'max_scripts' => (int) ($config['directives']['opcache.max_accelerated_files'] ?? 0),
40 'memory_used' => $used,
41 'memory_free' => $free,
42 'memory_wasted' => $wasted,
43 'memory_total' => $total,
44 'memory_pct' => $total > 0 ? round($used / $total * 100, 1) : 0,
45 'wasted_pct' => (float) ($mem['current_wasted_percentage'] ?? 0),
46 'start_time' => (int) ($stats['start_time'] ?? 0),
47 'last_restart' => (int) ($stats['last_restart_time'] ?? 0),
48 'directives' => $config['directives'] ?? [],
49 ];
50 }
51
52 public static function reset(): bool {
53 if (!self::_pro()) return false;
54 if (!function_exists('opcache_reset')) return false;
55 return @opcache_reset();
56 }
57
58 public static function format_bytes(int $bytes): string {
59 if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB';
60 if ($bytes >= 1024) return round($bytes / 1024, 1) . ' KB';
61 return $bytes . ' B';
62 }
63
64 public static function hit_rate_class(float $rate): string {
65 if ($rate >= 90) return 'grade-a';
66 if ($rate >= 70) return 'grade-b';
67 if ($rate >= 50) return 'grade-c';
68 return 'grade-f';
69 }
70 }
71