| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('Unauthorized Access'); |
| 3 |
|
| 4 |
if (!Phpinfo_WP_License::is_valid()) { |
| 5 |
phpinfowp_render_feature_lock([ |
| 6 |
'feature' => 'Object Cache Monitor', |
| 7 |
'icon' => 'dashicons-database-view', |
| 8 |
'tagline' => 'Redis or Memcached hit rate, memory footprint, and key statistics from your persistent cache.', |
| 9 |
'previews' => [ |
| 10 |
'Backend: <strong>—</strong>', |
| 11 |
'Hit rate: <strong>— %</strong>', |
| 12 |
'Memory used: <strong>— MB</strong>', |
| 13 |
], |
| 14 |
]); |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
$message = ''; |
| 19 |
$msg_type = 'success'; |
| 20 |
|
| 21 |
if (isset($_POST['phpinfowp_objectcache_reset']) && check_admin_referer('phpinfowp_objectcache_nonce')) { |
| 22 |
$ok = Phpinfo_WP_Object_Cache::flush(); |
| 23 |
$message = $ok ? 'Object Cache cleared successfully.' : 'Could not clear Object Cache.'; |
| 24 |
$msg_type = $ok ? 'success' : 'error'; |
| 25 |
} |
| 26 |
|
| 27 |
$has_redis = Phpinfo_WP_Object_Cache::is_redis_extension_loaded(); |
| 28 |
$has_memcached = Phpinfo_WP_Object_Cache::is_memcached_extension_loaded(); |
| 29 |
$has_dropin = Phpinfo_WP_Object_Cache::has_dropin(); |
| 30 |
|
| 31 |
$s = Phpinfo_WP_Object_Cache::status(); |
| 32 |
?> |
| 33 |
|
| 34 |
<div class="phpinfowp-pro-page"> |
| 35 |
<div class="phpinfowp-page-header"> |
| 36 |
<div> |
| 37 |
<h1>Object Cache Diagnostics <span class="phpinfowp-pro-badge"><?php _e('PRO', 'phpinfo-wp'); ?></span></h1> |
| 38 |
<p class="phpinfowp-page-subtitle"><?php _e('Monitor persistent object cache drop-ins, connection speed, and hit/miss ratios.', 'phpinfo-wp'); ?></p> |
| 39 |
</div> |
| 40 |
</div> |
| 41 |
|
| 42 |
<?php if ($message): ?> |
| 43 |
<div class="notice notice-<?php echo $msg_type; ?> inline is-dismissible"><p><?php echo esc_html($message); ?></p></div> |
| 44 |
<?php endif; ?> |
| 45 |
|
| 46 |
<?php if (!$has_dropin): ?> |
| 47 |
<div class="notice notice-warning inline"> |
| 48 |
<p><strong><?php _e('Object Caching is not active.', 'phpinfo-wp'); ?></strong> The <code>wp-content/object-cache.php</code> drop-in is missing.</p> |
| 49 |
<?php if ($has_redis): ?> |
| 50 |
<p>💡 However, the <strong><?php _e('Redis', 'phpinfo-wp'); ?></strong> PHP extension is installed on your server! You should install a plugin like <a href="<?php echo esc_url(admin_url('plugin-install.php?tab=search&type=term&s=redis+object+cache')); ?>">Redis Object Cache</a> to instantly speed up database queries.</p> |
| 51 |
<?php elseif ($has_memcached): ?> |
| 52 |
<p>💡 However, the <strong><?php _e('Memcached', 'phpinfo-wp'); ?></strong> PHP extension is installed on your server! You can use it to speed up database queries.</p> |
| 53 |
<?php else: ?> |
| 54 |
<p><?php _e('Neither Redis nor Memcached PHP extensions are installed. Ask your host to enable one (Redis is recommended).', 'phpinfo-wp'); ?></p> |
| 55 |
<?php endif; ?> |
| 56 |
</div> |
| 57 |
<?php elseif (!$s): ?> |
| 58 |
<div class="notice notice-warning inline"> |
| 59 |
<p><strong><?php _e('Object Cache is active, but stats are unavailable.', 'phpinfo-wp'); ?></strong> The drop-in might not expose connection details or metrics.</p> |
| 60 |
</div> |
| 61 |
<?php else: ?> |
| 62 |
|
| 63 |
<!-- Hit rate card --> |
| 64 |
<div class="phpinfowp-opcache-grid"> |
| 65 |
<div class="phpinfowp-opcache-card phpinfowp-opcache-hitrate"> |
| 66 |
<div class="phpinfowp-grade-circle <?php echo esc_attr(Phpinfo_WP_Object_Cache::hit_rate_class($s['hit_rate'] ?? 0)); ?>"> |
| 67 |
<?php echo $s['hit_rate'] !== null ? esc_html($s['hit_rate']) . '%' : 'N/A'; ?> |
| 68 |
</div> |
| 69 |
<div> |
| 70 |
<h3 style="margin:0 0 4px"><?php _e('Hit Rate', 'phpinfo-wp'); ?></h3> |
| 71 |
<p style="margin:0;color:#666;font-size:13px"> |
| 72 |
<?php echo number_format($s['hits']); ?> hits / |
| 73 |
<?php echo number_format($s['misses']); ?> misses |
| 74 |
</p> |
| 75 |
<p style="margin:6px 0 0;font-size:13px;color:#444"> |
| 76 |
<strong><?php _e('Engine:', 'phpinfo-wp'); ?></strong> <?php echo esc_html(ucfirst($s['type'])); ?> |
| 77 |
</p> |
| 78 |
</div> |
| 79 |
</div> |
| 80 |
|
| 81 |
<div class="phpinfowp-opcache-card"> |
| 82 |
<h3 style="margin-top:0"><?php _e('Memory Usage', 'phpinfo-wp'); ?></h3> |
| 83 |
<?php if ($s['memory_total'] > 0): ?> |
| 84 |
<div class="phpinfowp-progress-bar-wrap"> |
| 85 |
<div class="phpinfowp-progress-bar" style="width:<?php echo min(100, $s['memory_pct']); ?>%;background:<?php echo $s['memory_pct'] > 85 ? '#d63638' : '#777BB3'; ?>"></div> |
| 86 |
</div> |
| 87 |
<?php endif; ?> |
| 88 |
<p style="margin:6px 0 0;font-size:13px;color:#444"> |
| 89 |
Used: <strong><?php echo esc_html(Phpinfo_WP_Object_Cache::format_bytes($s['memory_used'])); ?></strong> |
| 90 |
<?php if ($s['memory_total'] > 0): ?> |
| 91 |
/ Total: <?php echo esc_html(Phpinfo_WP_Object_Cache::format_bytes($s['memory_total'])); ?> |
| 92 |
<?php endif; ?> |
| 93 |
</p> |
| 94 |
<p style="margin:6px 0 0;font-size:12px;color:#666"> |
| 95 |
Connection: <?php echo esc_html($s['connection']); ?> |
| 96 |
</p> |
| 97 |
<?php if (strpos($s['connection'], 'TCP') !== false && $s['type'] === 'redis'): ?> |
| 98 |
<p style="margin:4px 0 0;font-size:12px;color:#dba617"> |
| 99 |
<em>Tip: UNIX sockets are ~20% faster than TCP.</em> |
| 100 |
</p> |
| 101 |
<?php endif; ?> |
| 102 |
</div> |
| 103 |
|
| 104 |
<div class="phpinfowp-opcache-card"> |
| 105 |
<h3 style="margin-top:0"><?php _e('Keys & Evictions', 'phpinfo-wp'); ?></h3> |
| 106 |
<p style="margin:6px 0 0;font-size:13px;color:#444"> |
| 107 |
<strong><?php _e('Keys in Memory:', 'phpinfo-wp'); ?></strong> <?php echo number_format($s['keys']); ?> |
| 108 |
</p> |
| 109 |
<p style="margin:6px 0 0;font-size:13px;color:#444"> |
| 110 |
<strong><?php _e('Evictions:', 'phpinfo-wp'); ?></strong> <?php echo number_format($s['evictions']); ?> |
| 111 |
</p> |
| 112 |
<?php if ($s['evictions'] > 0): ?> |
| 113 |
<p style="margin:6px 0 0;font-size:12px;color:#d63638"> |
| 114 |
Keys are being evicted. Consider increasing <code>maxmemory</code>. |
| 115 |
</p> |
| 116 |
<?php endif; ?> |
| 117 |
</div> |
| 118 |
</div> |
| 119 |
|
| 120 |
<!-- Reset button --> |
| 121 |
<form method="post" style="margin-top:20px" onsubmit="return confirm('Clear Object Cache? This will flush all database queries from memory.')"> |
| 122 |
<?php wp_nonce_field('phpinfowp_objectcache_nonce'); ?> |
| 123 |
<input type="hidden" name="phpinfowp_objectcache_reset" value="1"> |
| 124 |
<button type="submit" class="button button-secondary"><?php _e('Flush Cache', 'phpinfo-wp'); ?></button> |
| 125 |
<span style="margin-left:8px;font-size:12px;color:#666"><?php _e('Forces the cache to be entirely rebuilt.', 'phpinfo-wp'); ?></span> |
| 126 |
</form> |
| 127 |
|
| 128 |
<?php endif; ?> |
| 129 |
</div> |
| 130 |
|