| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('WP_Optimize_Dashboard_Minify')) : |
| 6 |
|
| 7 |
/** |
| 8 |
* Collects all data needed to render the File Optimisation / Minify dashboard card. |
| 9 |
*/ |
| 10 |
class WP_Optimize_Dashboard_Minify implements WP_Optimize_Dashboard_Interface { |
| 11 |
|
| 12 |
/** |
| 13 |
* @return WpoDashboardMinifyData |
| 14 |
*/ |
| 15 |
public function collect(): array { |
| 16 |
$config = wp_optimize_minify_config()->get(); |
| 17 |
if (!is_array($config)) { |
| 18 |
$config = wp_optimize_minify_config()->get_defaults(); |
| 19 |
} |
| 20 |
$is_premium = WP_Optimize::is_premium(); |
| 21 |
$active = (bool) $config['enabled']; |
| 22 |
|
| 23 |
$files = WP_Optimize_Minify_Cache_Functions::get_cached_files(); |
| 24 |
$js_count = count($files['js']); |
| 25 |
$css_count = count($files['css']); |
| 26 |
|
| 27 |
$raw_ts = (int) $config['last-cache-update']; |
| 28 |
$saved_bytes = $this->compute_minify_savings($files); |
| 29 |
|
| 30 |
return array( |
| 31 |
'active' => $active, |
| 32 |
'label' => __('File optimisation', 'wp-optimize'), |
| 33 |
'js_enabled' => (bool) $config['enable_js'], |
| 34 |
'css_enabled' => (bool) $config['enable_css'], |
| 35 |
'js_file_count' => $js_count, |
| 36 |
'css_file_count' => $css_count, |
| 37 |
'fonts_cached_locally' => $is_premium && (bool) $config['host_local_google_fonts'], |
| 38 |
'analytics_hosted_locally' => $is_premium && (bool) $config['enable_analytics'], |
| 39 |
'is_premium' => $is_premium, |
| 40 |
'last_rebuilt_ts' => $raw_ts, |
| 41 |
'last_rebuilt_human' => WP_Optimize_Utils::human_time_diff_or_never($raw_ts), |
| 42 |
'saved_bytes' => $saved_bytes, |
| 43 |
'saved_human' => $saved_bytes > 0 ? size_format($saved_bytes, 1) : '', |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Computes byte savings by comparing original vs minified sizes per cache bundle. |
| 49 |
* |
| 50 |
* Savings are summed per bundle (each .json log = one minified output file). |
| 51 |
* A bundle where the output is larger than its sources (e.g. already-minified vendor |
| 52 |
* files that gain processing overhead) contributes 0 — it does NOT cancel savings from |
| 53 |
* other bundles. External/CDN files are skipped; they have no local path to stat. |
| 54 |
* |
| 55 |
* @param array<string, mixed> $files Output of WP_Optimize_Minify_Cache_Functions::get_cached_files(). |
| 56 |
* @return int Total bytes saved, never negative. |
| 57 |
*/ |
| 58 |
private function compute_minify_savings(array $files): int { |
| 59 |
$home_path = untrailingslashit(get_home_path()); |
| 60 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 61 |
$cache_dir = $cache_path['cachedir']; |
| 62 |
$total = 0; |
| 63 |
|
| 64 |
foreach (array('js', 'css') as $ext) { |
| 65 |
if (empty($files[$ext]) || !is_array($files[$ext])) continue; |
| 66 |
foreach ($files[$ext] as $cached) { |
| 67 |
if (!is_array($cached)) continue; |
| 68 |
$log = isset($cached['log']) && $cached['log'] instanceof stdClass ? $cached['log'] : null; |
| 69 |
$file_url = isset($cached['file_url']) && is_string($cached['file_url']) ? $cached['file_url'] : ''; |
| 70 |
if (!$log instanceof stdClass || isset($log->error) || !isset($log->files) || '' === $file_url) continue; |
| 71 |
|
| 72 |
// Minified combined bundle size. |
| 73 |
$cache_file = $cache_dir . '/' . basename($file_url); |
| 74 |
$min_size = file_exists($cache_file) ? (int) filesize($cache_file) : 0; |
| 75 |
if (0 === $min_size) continue; |
| 76 |
|
| 77 |
// Sum original sizes for all local files in this bundle. |
| 78 |
$orig_size = 0; |
| 79 |
foreach ($log->files as $file_log) { |
| 80 |
if (!$file_log instanceof stdClass || empty($file_log->url) || empty($file_log->success)) continue; |
| 81 |
$local_path = $home_path . $file_log->url; |
| 82 |
if (file_exists($local_path)) { |
| 83 |
$orig_size += (int) filesize($local_path); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Only count bundles where minification actually reduced the size. |
| 88 |
// Pre-minified vendor files often end up slightly larger — ignore those. |
| 89 |
$total += max(0, $orig_size - $min_size); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $total; |
| 94 |
} |
| 95 |
} |
| 96 |
endif; |
| 97 |
|