settings = $settings; $this->logger = $logger; $this->filesystem = $filesystem; } /** * Register hooks. * * @return void */ public function register() {} /** * Dashboard metrics. * * @return array */ public function metrics() { $cache = $this->filesystem->directory_stats( $this->filesystem->page_cache_dir() ); $asset = $this->filesystem->directory_stats( $this->filesystem->asset_cache_dir() ); $image = wp_parse_args( get_option( Image_Optimizer::STATS_OPTION, array() ), array( 'optimized_images' => 0, 'saved_bytes' => 0, 'webp_files' => 0, ) ); $logs = $this->logger->recent( 200 ); $hits = 0; $misses = 0; $purges = 0; $errors = 0; $last_logs = array_slice( $logs, 0, 10 ); foreach ( $logs as $log ) { $message = isset( $log['message'] ) ? strtolower( $log['message'] ) : ''; $level = isset( $log['level'] ) ? strtolower( $log['level'] ) : ''; if ( false !== strpos( $message, 'cache hit' ) ) { $hits++; } if ( false !== strpos( $message, 'cache miss' ) ) { $misses++; } if ( false !== strpos( $message, 'purged' ) ) { $purges++; } if ( 'error' === $level ) { $errors++; } } return array( 'cache_files' => $cache['files'], 'cache_size' => $cache['bytes'], 'asset_files' => $asset['files'], 'asset_size' => $asset['bytes'], 'image_stats' => $image, 'cache_hits' => $hits, 'cache_misses' => $misses, 'cache_purges' => $purges, 'errors' => $errors, 'optimization_score' => $this->score(), 'recommendations' => $this->recommendations(), 'logs' => $last_logs, ); } /** * Optimization score estimate. * * @return int */ private function score() { $checks = array( $this->settings->get( 'cache.enabled', 1 ), $this->settings->get( 'preload.enabled', 1 ), $this->settings->get( 'browser.enabled', 1 ), $this->settings->get( 'css.minify', 1 ), $this->settings->get( 'js.defer', 1 ), $this->settings->get( 'images.webp', 1 ), $this->settings->get( 'lazy.images', 1 ), $this->settings->get( 'compression.enabled', 1 ), $this->settings->get( 'automation.enabled', 1 ), $this->settings->get( 'logging.enabled', 1 ), ); return (int) round( ( array_sum( array_map( 'absint', $checks ) ) / count( $checks ) ) * 100 ); } /** * Recommendations and warnings. * * @return array */ private function recommendations() { $items = array(); if ( ! $this->settings->get( 'cache.enabled', 1 ) ) { $items[] = __( 'Enable intelligent page caching to reduce PHP execution and database load.', 'acelora-site-accelerator' ); } if ( ! $this->settings->get( 'preload.enabled', 1 ) ) { $items[] = __( 'Enable URL warmup so important URLs are requested after purges.', 'acelora-site-accelerator' ); } if ( ! $this->settings->get( 'images.webp', 1 ) ) { $items[] = __( 'Enable WebP generation to reduce image transfer size for modern browsers.', 'acelora-site-accelerator' ); } if ( ! $this->settings->get( 'cdn.enabled', 0 ) ) { $items[] = __( 'Connect a CDN for static assets if your visitors are geographically distributed.', 'acelora-site-accelerator' ); } if ( ! extension_loaded( 'zlib' ) ) { $items[] = __( 'The PHP zlib extension is not loaded. Server-level GZIP should be verified.', 'acelora-site-accelerator' ); } return $items; } }