| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
if (!class_exists('\\WP_List_Table', false)) { |
| 16 |
require_once ABSPATH.'wp-admin/includes/class-wp-list-table.php'; |
| 17 |
} |
| 18 |
|
| 19 |
class OPcacheView extends \WP_List_Table |
| 20 |
{ |
| 21 |
private $pt; |
| 22 |
|
| 23 |
public function __construct(Plugin $pt) |
| 24 |
{ |
| 25 |
parent::__construct( |
| 26 |
[ |
| 27 |
'singular' => 'opclist-info', |
| 28 |
'plural' => 'opclist-infos', |
| 29 |
'ajax' => false, |
| 30 |
'screen' => 'opclist-infos', |
| 31 |
] |
| 32 |
); |
| 33 |
|
| 34 |
$this->pt = $pt; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_doc($name) |
| 38 |
{ |
| 39 |
$name = str_replace('_', '-', $name); |
| 40 |
|
| 41 |
return 'https://www.php.net/manual/en/opcache.configuration.php#ini.'.$name; |
| 42 |
} |
| 43 |
|
| 44 |
public function get_config() |
| 45 |
{ |
| 46 |
static $data = false; |
| 47 |
|
| 48 |
try { |
| 49 |
if ($this->pt->opcache_function_exists('opcache_get_configuration') && (empty($data) || !\is_array($data))) { |
| 50 |
$data = @opcache_get_configuration(); |
| 51 |
} |
| 52 |
} catch (\Throwable $e) { |
| 53 |
nwdcx_throwable(__METHOD__, $e); |
| 54 |
} |
| 55 |
|
| 56 |
return $data; |
| 57 |
} |
| 58 |
|
| 59 |
private function get_status() |
| 60 |
{ |
| 61 |
$data = $this->pt->get_opcache_status(true); |
| 62 |
if (!empty($data) && \is_array($data)) { |
| 63 |
return $data; |
| 64 |
} |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
public function get_usage() |
| 70 |
{ |
| 71 |
$stats = [ |
| 72 |
'num_cached_scripts' => 0, |
| 73 |
'num_cached_keys' => 0, |
| 74 |
'max_cached_keys' => 0, |
| 75 |
'hits' => 0, |
| 76 |
'start_time' => 0, |
| 77 |
'last_restart_time' => 0, |
| 78 |
'oom_restarts' => 0, |
| 79 |
'hash_restarts' => 0, |
| 80 |
'manual_restarts' => 0, |
| 81 |
'misses' => 0, |
| 82 |
'blacklist_misses' => 0, |
| 83 |
'blacklist_miss_ratio' => 0, |
| 84 |
'opcache_hit_rate' => 0, |
| 85 |
'used_memory' => 0, |
| 86 |
'free_memory' => 0, |
| 87 |
'wasted_memory' => 0, |
| 88 |
'current_wasted_percentage' => 0, |
| 89 |
]; |
| 90 |
|
| 91 |
$data = $this->get_status(); |
| 92 |
if (!empty($data) && \is_array($data)) { |
| 93 |
if (isset($data['opcache_statistics']) && isset($data['memory_usage']) && \is_array($data['opcache_statistics']) && \is_array($data['memory_usage'])) { |
| 94 |
$stats = array_merge($data['opcache_statistics'], $data['memory_usage']); |
| 95 |
} elseif (!empty($data['file_cache_only'])) { |
| 96 |
$stats['file_cache_only'] = 1; |
| 97 |
$stats['file_cache'] = $data['file_cache']; |
| 98 |
$stats['statsfile'] = sprintf(esc_html__(_n('%1$s size of %2$s file', '%1$s size of %2$s files', $data['_numfile'], 'docket-cache')), $this->pt->normalize_size($data['_sizefile']), $data['_numfile']); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return (object) $stats; |
| 103 |
} |
| 104 |
|
| 105 |
private function get_files() |
| 106 |
{ |
| 107 |
$stats = []; |
| 108 |
$data = $this->get_status(); |
| 109 |
if (!empty($data) && \is_array($data)) { |
| 110 |
if (!empty($data['scripts']) && \is_array($data['scripts'])) { |
| 111 |
$sstr = !empty($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : ''; |
| 112 |
$sftr = !empty($_GET['sf']) ? sanitize_text_field($_GET['sf']) : 'obc'; |
| 113 |
$smtr = !empty($_GET['sm']) ? sanitize_text_field($_GET['sm']) : '1k'; |
| 114 |
|
| 115 |
$cnt = 0; |
| 116 |
$limit = 0; |
| 117 |
|
| 118 |
if ('all' !== $smtr) { |
| 119 |
$smtr = strtok($smtr, 'k').'000'; |
| 120 |
$limit = (int) $smtr; |
| 121 |
} |
| 122 |
|
| 123 |
foreach ($data['scripts'] as $script => $arr) { |
| 124 |
$cpath = wp_normalize_path($arr['full_path']); |
| 125 |
$script = wp_normalize_path($script); |
| 126 |
|
| 127 |
if ($this->pt->cf()->is_dcfalse('OPCVIEWER_SHOWALL') && !(0 === strpos($cpath, ABSPATH))) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
if (!empty($sstr) && false === strpos($cpath, $sstr)) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
if (!empty($sftr)) { |
| 136 |
$cfile = basename($cpath); |
| 137 |
$cdir = \dirname($cpath); |
| 138 |
|
| 139 |
if ('obc' === $sftr && (false === strpos($script, $this->pt->cache_path) || !$this->pt->is_docketcachedir($cdir) || !@preg_match('@^([a-z0-9]{12})\-([a-z0-9]{12})\.php(\.bin)?$@', $cfile))) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
if ('wpc' === $sftr && (false !== strpos($script, $this->pt->cache_path) && $this->pt->is_docketcachedir($cdir) && @preg_match('@^([a-z0-9]{12})\-([a-z0-9]{12})\.php(\.bin)?$@', $cfile))) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
if ('dfc' === $sftr && @is_file($cpath)) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$sort = $arr['hits']; |
| 153 |
if (!empty($_GET['orderby'])) { |
| 154 |
switch ($_GET['orderby']) { |
| 155 |
case 'file': |
| 156 |
$sort = $cpath; |
| 157 |
break; |
| 158 |
case 'mem': |
| 159 |
$sort = $arr['memory_consumption']; |
| 160 |
break; |
| 161 |
case 'stmp': |
| 162 |
$sort = $arr['last_used_timestamp']; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$fxfile = $this->pt->sanitize_rootpath($cpath); |
| 168 |
|
| 169 |
if (!empty($data['file_cache_only']) && !empty($data['file_cache'])) { |
| 170 |
$fxfile = preg_replace('@'.preg_quote(wp_normalize_path($data['file_cache'])).'([0-9a-zA-z]+)@', '[$1] ', $fxfile); |
| 171 |
} |
| 172 |
|
| 173 |
$stats[$sort.$script] = (object) [ |
| 174 |
'is_exists' => @is_file($cpath), |
| 175 |
'file' => $fxfile, |
| 176 |
'hits' => $arr['hits'], |
| 177 |
'mem' => $this->pt->normalize_size($arr['memory_consumption']), |
| 178 |
'stmp' => wp_date('Y-m-d H:i:s', $arr['last_used_timestamp']), |
| 179 |
]; |
| 180 |
|
| 181 |
++$cnt; |
| 182 |
if ($limit > 0 && $cnt >= $limit) { |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if (!empty($_GET['orderby']) && !empty($_GET['order'])) { |
| 188 |
$order = sanitize_text_field($_GET['order']); |
| 189 |
$orderby = sanitize_text_field($_GET['orderby']); |
| 190 |
|
| 191 |
if ('desc' === $order) { |
| 192 |
if ('file' !== $orderby) { |
| 193 |
krsort($stats, \SORT_NUMERIC); |
| 194 |
} else { |
| 195 |
krsort($stats, \SORT_STRING); |
| 196 |
} |
| 197 |
} else { |
| 198 |
if ('file' !== $orderby) { |
| 199 |
ksort($stats, \SORT_NUMERIC); |
| 200 |
} else { |
| 201 |
ksort($stats, \SORT_STRING); |
| 202 |
} |
| 203 |
} |
| 204 |
} else { |
| 205 |
krsort($stats, \SORT_NUMERIC); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
unset($data); |
| 211 |
|
| 212 |
return $stats; |
| 213 |
} |
| 214 |
|
| 215 |
public function prepare_items() |
| 216 |
{ |
| 217 |
$stats = $this->get_files(); |
| 218 |
|
| 219 |
$count = \count($stats); |
| 220 |
$per_page = 50; |
| 221 |
$offset = ($this->get_pagenum() - 1) * $per_page; |
| 222 |
|
| 223 |
$this->items = \array_slice($stats, $offset, $per_page); |
| 224 |
|
| 225 |
$this->set_pagination_args( |
| 226 |
[ |
| 227 |
'total_items' => $count, |
| 228 |
'per_page' => $per_page, |
| 229 |
'total_pages' => ceil($count / $per_page), |
| 230 |
] |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
public function get_columns() |
| 235 |
{ |
| 236 |
/* translators: %s = utc offset */ |
| 237 |
$lastused = sprintf(esc_html__('Last Used %s', 'docket-cache'), '('.$this->pt->get_utc_offset().')'); |
| 238 |
|
| 239 |
$cols = [ |
| 240 |
'opclist_file' => esc_html__('Cached Files', 'docket-cache'), |
| 241 |
'opclist_hits' => esc_html__('Cache Hits', 'docket-cache'), |
| 242 |
'opclist_mem' => esc_html__('Memory Usage', 'docket-cache'), |
| 243 |
'opclist_timestamp' => $lastused, |
| 244 |
]; |
| 245 |
|
| 246 |
if ($this->pt->is_opcache_filecache_only()) { |
| 247 |
unset($cols['opclist_hits']); |
| 248 |
$cols['opclist_mem'] = esc_html__('File Size', 'docket-cache'); |
| 249 |
} |
| 250 |
|
| 251 |
return $cols; |
| 252 |
} |
| 253 |
|
| 254 |
public function get_sortable_columns() |
| 255 |
{ |
| 256 |
$cols = [ |
| 257 |
'opclist_file' => ['file', false], |
| 258 |
'opclist_hits' => ['hits', false], |
| 259 |
'opclist_mem' => ['mem', false], |
| 260 |
'opclist_timestamp' => ['stmp', false], |
| 261 |
]; |
| 262 |
|
| 263 |
if ($this->pt->is_opcache_filecache_only()) { |
| 264 |
unset($cols['opclist_hits']); |
| 265 |
} |
| 266 |
|
| 267 |
return $cols; |
| 268 |
} |
| 269 |
|
| 270 |
public function get_table_classes() |
| 271 |
{ |
| 272 |
return ['widefat', 'striped', $this->_args['plural']]; |
| 273 |
} |
| 274 |
|
| 275 |
public function column_opclist_hits($stats) |
| 276 |
{ |
| 277 |
return '<code>'.$stats->hits.'</code>'; |
| 278 |
} |
| 279 |
|
| 280 |
public function column_opclist_mem($stats) |
| 281 |
{ |
| 282 |
return '<code>'.$stats->mem.'</code>'; |
| 283 |
} |
| 284 |
|
| 285 |
public function column_opclist_file($stats) |
| 286 |
{ |
| 287 |
if ($stats->is_exists) { |
| 288 |
return esc_html($stats->file); |
| 289 |
} |
| 290 |
|
| 291 |
return '<span class="text-red">'.esc_html($stats->file).'</span>'; |
| 292 |
} |
| 293 |
|
| 294 |
public function column_opclist_timestamp($stats) |
| 295 |
{ |
| 296 |
return esc_html($stats->stmp); |
| 297 |
} |
| 298 |
|
| 299 |
public function no_items() |
| 300 |
{ |
| 301 |
if (empty($_GET['s'])) { |
| 302 |
esc_html_e('OPcache items not available.', 'docket-cache'); |
| 303 |
} else { |
| 304 |
esc_html_e('No matching OPcache Cached Files.', 'docket-cache'); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|