'attachment', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_status' => 'inherit', ); $query = new WP_Query($args); $total_images = $query->found_posts; $converted_images = 0; $total_size = 0; $total_size_saved = 0; $format_distribution = array('webp' => 0, 'avif' => 0); foreach ($query->posts as $post) { $metadata = wp_get_attachment_metadata($post->ID); if (isset($metadata['filesize'])) { $total_size += $metadata['filesize']; } if (isset($metadata['iwac_conversions']) && !empty($metadata['iwac_conversions'])) { $converted_images++; foreach ($metadata['iwac_conversions'] as $format => $conversion_data) { $format_distribution[strtolower($format)]++; } } } return array( 'total_images' => $total_images, 'converted_images' => $converted_images, 'pending_images' => $total_images - $converted_images, 'conversion_rate' => $total_images > 0 ? round(($converted_images / $total_images) * 100) : 0, 'total_size' => $total_size, 'total_size_saved' => $total_size_saved, 'average_compression' => 0, 'format_distribution' => $format_distribution, ); } /** * Get recent conversions */ public function get_recent_conversions($limit = 10) { global $wpdb; $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'posts_per_page' => $limit, 'orderby' => 'modified', 'order' => 'DESC', 'post_status' => 'inherit', ); $query = new WP_Query($args); $conversions = array(); foreach ($query->posts as $post) { $metadata = wp_get_attachment_metadata($post->ID); if (isset($metadata['iwac_conversions']) && !empty($metadata['iwac_conversions'])) { $file = get_attached_file($post->ID); $file_size = filesize($file); foreach ($metadata['iwac_conversions'] as $format => $conversion_data) { $conversions[] = array( 'id' => $post->ID, 'file_name' => basename($file), 'original_size' => $file_size, 'format' => strtoupper($format), 'quality' => $conversion_data['quality'], 'converted_at' => $conversion_data['converted_at'], 'size_saved' => 0, // Calculate based on actual file sizes ); } } } return $conversions; } /** * Get format distribution chart data */ public function get_format_distribution() { $stats = $this->get_conversion_stats(); $distribution = $stats['format_distribution']; $total_conversions = array_sum($distribution); return array( 'webp_percent' => $total_conversions > 0 ? round(($distribution['webp'] / $total_conversions) * 100) : 0, 'avif_percent' => $total_conversions > 0 ? round(($distribution['avif'] / $total_conversions) * 100) : 0, 'webp_count' => $distribution['webp'], 'avif_count' => $distribution['avif'], ); } /** * Get storage saved */ public function get_storage_saved() { global $wpdb; $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_status' => 'inherit', ); $query = new WP_Query($args); $total_saved = 0; foreach ($query->posts as $post) { $metadata = wp_get_attachment_metadata($post->ID); if (isset($metadata['iwac_conversions']) && !empty($metadata['iwac_conversions'])) { // Get original file size $file = get_attached_file($post->ID); if (file_exists($file)) { $original_size = filesize($file); foreach ($metadata['iwac_conversions'] as $format => $conversion_data) { // Get converted file size $converted_path = dirname($file) . '/' . basename($file, '.' . pathinfo($file, PATHINFO_EXTENSION)) . '.' . $format; if (file_exists($converted_path)) { $converted_size = filesize($converted_path); $saved = $original_size - $converted_size; if ($saved > 0) { $total_saved += $saved; } } } } } } return $total_saved; } }