WordPress Performance at Lightning Speed. Advanced optimization with Brotli compression, smart caching, and cat-like speed bursts that dash past the competition. - Brotli and Gzip compression with configurable levels - WP Rocket-inspired smart caching - Bandwidth monitoring - Automatic optimization - Performance status dashboard Includes build.sh and .distignore for WordPress-installable release ZIPs.
282 lines
9.9 KiB
PHP
282 lines
9.9 KiB
PHP
<?php
|
|
/**
|
|
* Analytics Engine for TigerStyle Dash
|
|
* Performance metrics and optimization analysis
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class TigerStyle_Dash_Analytics {
|
|
|
|
/**
|
|
* Plugin instance
|
|
*
|
|
* @var TigerStyle_Dash
|
|
*/
|
|
private $plugin;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param TigerStyle_Dash $plugin Main plugin instance
|
|
*/
|
|
public function __construct($plugin) {
|
|
$this->plugin = $plugin;
|
|
}
|
|
|
|
/**
|
|
* Analyze site performance
|
|
*/
|
|
public function analyze_site_performance() {
|
|
$analysis = array(
|
|
'timestamp' => time(),
|
|
'site_url' => home_url(),
|
|
'cache_stats' => $this->get_cache_analysis(),
|
|
'compression_opportunities' => $this->find_compression_opportunities(),
|
|
'performance_score' => $this->calculate_performance_score(),
|
|
'recommendations' => $this->get_recommendations()
|
|
);
|
|
|
|
return $analysis;
|
|
}
|
|
|
|
/**
|
|
* Get cache analysis
|
|
*/
|
|
private function get_cache_analysis() {
|
|
$cache = $this->plugin->get_cache();
|
|
$stats = $cache->get_cache_stats();
|
|
|
|
return array(
|
|
'total_cached_files' => $stats['total_files'],
|
|
'total_cache_size' => $this->format_bytes($stats['total_size']),
|
|
'cache_directory' => $stats['cache_dir'],
|
|
'cache_enabled' => get_option('tigerstyle_dash_cache_enabled', true)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Find compression opportunities
|
|
*/
|
|
private function find_compression_opportunities() {
|
|
// Analyze common WordPress assets that could benefit from compression
|
|
$opportunities = array();
|
|
|
|
// Check theme CSS files
|
|
$theme_dir = get_template_directory();
|
|
$css_files = glob($theme_dir . '/*.css');
|
|
$css_files = array_merge($css_files, glob($theme_dir . '/css/*.css'));
|
|
|
|
foreach ($css_files as $file) {
|
|
$size = filesize($file);
|
|
if ($size > 1024) { // Only analyze files larger than 1KB
|
|
$opportunities[] = array(
|
|
'file' => basename($file),
|
|
'type' => 'CSS',
|
|
'size' => $size,
|
|
'potential_savings' => $size * 0.7, // Estimate 70% compression
|
|
'recommendation' => 'Enable CSS compression and minification'
|
|
);
|
|
}
|
|
}
|
|
|
|
// Check JavaScript files
|
|
$js_files = glob($theme_dir . '/*.js');
|
|
$js_files = array_merge($js_files, glob($theme_dir . '/js/*.js'));
|
|
|
|
foreach ($js_files as $file) {
|
|
$size = filesize($file);
|
|
if ($size > 1024) {
|
|
$opportunities[] = array(
|
|
'file' => basename($file),
|
|
'type' => 'JavaScript',
|
|
'size' => $size,
|
|
'potential_savings' => $size * 0.6, // Estimate 60% compression
|
|
'recommendation' => 'Enable JavaScript compression and minification'
|
|
);
|
|
}
|
|
}
|
|
|
|
return $opportunities;
|
|
}
|
|
|
|
/**
|
|
* Calculate performance score
|
|
*/
|
|
private function calculate_performance_score() {
|
|
$score = 0;
|
|
$max_score = 100;
|
|
|
|
// Compression enabled (30 points)
|
|
if (get_option('tigerstyle_dash_compression_enabled', false)) {
|
|
$score += 30;
|
|
}
|
|
|
|
// Cache enabled (25 points)
|
|
if (get_option('tigerstyle_dash_cache_enabled', false)) {
|
|
$score += 25;
|
|
}
|
|
|
|
// Optimal compression method (20 points)
|
|
$method = get_option('tigerstyle_dash_compression_method', 'auto');
|
|
if ($method === 'auto' || $method === 'brotli') {
|
|
$score += 20;
|
|
} elseif ($method === 'gzip') {
|
|
$score += 15;
|
|
}
|
|
|
|
// Reasonable compression level (15 points)
|
|
$level = get_option('tigerstyle_dash_compression_level', 6);
|
|
if ($level >= 5 && $level <= 7) {
|
|
$score += 15;
|
|
} elseif ($level >= 3 && $level <= 9) {
|
|
$score += 10;
|
|
}
|
|
|
|
// Cache TTL optimization (10 points)
|
|
$ttl = get_option('tigerstyle_dash_cache_ttl', 3600);
|
|
if ($ttl >= 1800 && $ttl <= 7200) { // 30 min to 2 hours
|
|
$score += 10;
|
|
} elseif ($ttl >= 300 && $ttl <= 86400) { // 5 min to 24 hours
|
|
$score += 5;
|
|
}
|
|
|
|
return array(
|
|
'score' => $score,
|
|
'max_score' => $max_score,
|
|
'percentage' => round(($score / $max_score) * 100),
|
|
'grade' => $this->get_performance_grade($score, $max_score)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get performance grade
|
|
*/
|
|
private function get_performance_grade($score, $max_score) {
|
|
$percentage = ($score / $max_score) * 100;
|
|
|
|
if ($percentage >= 90) return 'A+';
|
|
if ($percentage >= 80) return 'A';
|
|
if ($percentage >= 70) return 'B';
|
|
if ($percentage >= 60) return 'C';
|
|
if ($percentage >= 50) return 'D';
|
|
return 'F';
|
|
}
|
|
|
|
/**
|
|
* Get optimization recommendations
|
|
*/
|
|
private function get_recommendations() {
|
|
$recommendations = array();
|
|
|
|
if (!get_option('tigerstyle_dash_compression_enabled', false)) {
|
|
$recommendations[] = array(
|
|
'priority' => 'high',
|
|
'title' => 'Enable Compression',
|
|
'description' => 'Activate TigerStyle Dash compression to reduce file sizes by 60-80%',
|
|
'action' => 'Enable compression in Dash settings'
|
|
);
|
|
}
|
|
|
|
if (!get_option('tigerstyle_dash_cache_enabled', false)) {
|
|
$recommendations[] = array(
|
|
'priority' => 'high',
|
|
'title' => 'Enable Smart Caching',
|
|
'description' => 'Turn on intelligent cache warming to pre-compress popular content',
|
|
'action' => 'Enable cache in Dash settings'
|
|
);
|
|
}
|
|
|
|
$method = get_option('tigerstyle_dash_compression_method', 'auto');
|
|
if ($method === 'gzip') {
|
|
$recommendations[] = array(
|
|
'priority' => 'medium',
|
|
'title' => 'Upgrade to Brotli Compression',
|
|
'description' => 'Switch to Auto mode for 15-25% better compression than Gzip',
|
|
'action' => 'Change compression method to Auto in settings'
|
|
);
|
|
}
|
|
|
|
$level = get_option('tigerstyle_dash_compression_level', 6);
|
|
if ($level < 5) {
|
|
$recommendations[] = array(
|
|
'priority' => 'low',
|
|
'title' => 'Optimize Compression Level',
|
|
'description' => 'Increase compression level to 6 for better size reduction',
|
|
'action' => 'Adjust compression level in settings'
|
|
);
|
|
} elseif ($level > 7) {
|
|
$recommendations[] = array(
|
|
'priority' => 'low',
|
|
'title' => 'Balance Compression vs Speed',
|
|
'description' => 'Consider reducing compression level to 6 for faster processing',
|
|
'action' => 'Adjust compression level in settings'
|
|
);
|
|
}
|
|
|
|
return $recommendations;
|
|
}
|
|
|
|
/**
|
|
* Format bytes into human readable format
|
|
*/
|
|
private function format_bytes($bytes, $precision = 2) {
|
|
$units = array('B', 'KB', 'MB', 'GB', 'TB');
|
|
|
|
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
|
|
$bytes /= 1024;
|
|
}
|
|
|
|
return round($bytes, $precision) . ' ' . $units[$i];
|
|
}
|
|
|
|
/**
|
|
* Track compression statistics
|
|
*/
|
|
public function track_compression($original_size, $compressed_size, $method) {
|
|
// Update running statistics
|
|
$files_compressed = get_option('tigerstyle_dash_files_compressed', 0) + 1;
|
|
$bandwidth_saved = get_option('tigerstyle_dash_bandwidth_saved', 0) + ($original_size - $compressed_size);
|
|
|
|
// Calculate running average compression ratio
|
|
$total_original = get_option('tigerstyle_dash_total_original_size', 0) + $original_size;
|
|
$total_compressed = get_option('tigerstyle_dash_total_compressed_size', 0) + $compressed_size;
|
|
$avg_ratio = $total_original > 0 ? round((($total_original - $total_compressed) / $total_original) * 100, 1) : 0;
|
|
|
|
// Update options
|
|
update_option('tigerstyle_dash_files_compressed', $files_compressed);
|
|
update_option('tigerstyle_dash_bandwidth_saved', $bandwidth_saved);
|
|
update_option('tigerstyle_dash_total_original_size', $total_original);
|
|
update_option('tigerstyle_dash_total_compressed_size', $total_compressed);
|
|
update_option('tigerstyle_dash_avg_compression_ratio', $avg_ratio);
|
|
|
|
// Log compression event for analysis
|
|
$this->log_compression_event($original_size, $compressed_size, $method);
|
|
}
|
|
|
|
/**
|
|
* Log compression event
|
|
*/
|
|
private function log_compression_event($original_size, $compressed_size, $method) {
|
|
// Simple logging - in production, this might use a more sophisticated system
|
|
$log_entry = array(
|
|
'timestamp' => time(),
|
|
'original_size' => $original_size,
|
|
'compressed_size' => $compressed_size,
|
|
'method' => $method,
|
|
'ratio' => round((($original_size - $compressed_size) / $original_size) * 100, 1)
|
|
);
|
|
|
|
// Store in transient (simple approach)
|
|
$recent_compressions = get_transient('tigerstyle_dash_recent_compressions') ?: array();
|
|
$recent_compressions[] = $log_entry;
|
|
|
|
// Keep only last 100 entries
|
|
$recent_compressions = array_slice($recent_compressions, -100);
|
|
|
|
set_transient('tigerstyle_dash_recent_compressions', $recent_compressions, WEEK_IN_SECONDS);
|
|
}
|
|
} |