options = get_option('aio_options', []); // 1. WebP Auto Conversion (Background Processing) if ( !empty($this->options['opt-perf-webp']) ) { add_filter('wp_generate_attachment_metadata', [$this, 'schedule_webp_conversion'], 10, 2); add_action('aio_convert_webp_cron', [$this, 'process_webp_conversion']); } // 2. Force Native Lazy Load if ( !empty($this->options['opt-perf-lazyload']) ) { add_filter( 'wp_lazy_loading_enabled', '__return_true' ); add_filter( 'the_content', [$this, 'force_lazy_load_content'], 99 ); } } /** * Schedule the conversion to run asynchronously via WP Cron. */ public function schedule_webp_conversion($meta, $attachment_id) { // Schedule event 5 seconds from now to avoid blocking the current upload request if (!wp_next_scheduled('aio_convert_webp_cron', [$attachment_id])) { wp_schedule_single_event(time() + 5, 'aio_convert_webp_cron', [$attachment_id]); } return $meta; } /** * Process the WebP conversion in the background. */ public function process_webp_conversion($attachment_id) { $delete_original = !empty($this->options['opt-perf-webp-delete-original']); aio_generate_webp_for_attachment($attachment_id, $delete_original); } /** * Fallback to add loading="lazy" to all img/iframe tags in content if missing. */ public function force_lazy_load_content($content) { if (empty($content) || !is_string($content)) return $content; // Add loading="lazy" to tags that don't have it $content = preg_replace_callback( '/<(img|iframe)[^>]+>/i', function($matches) { $tag = $matches[0]; if (stripos($tag, 'loading=') === false) { $tag = str_replace('<' . $matches[1], '<' . $matches[1] . ' loading="lazy"', $tag); } return $tag; }, $content ); return $content; } } // Init Module new AIO_Optimizer(); if (!function_exists('aio_generate_webp_for_attachment')) { function aio_generate_webp_for_attachment($attachment_id, $delete_original = false) { $file_path = get_attached_file($attachment_id); if (!$file_path || !file_exists($file_path)) return false; $mime_type = mime_content_type($file_path); if (!in_array($mime_type, ['image/jpeg', 'image/png'])) { return false; // Only convert JPEG and PNG } $path_info = pathinfo($file_path); $webp_path = $path_info['dirname'] . '/' . $path_info['filename'] . '.webp'; $success = false; // Try raw GD first for maximum control over alpha and truecolor (fixes blank/white image bug) if (function_exists('imagewebp')) { $image = null; if ($mime_type === 'image/jpeg' && function_exists('imagecreatefromjpeg')) { $image = @imagecreatefromjpeg($file_path); } elseif ($mime_type === 'image/png' && function_exists('imagecreatefrompng')) { $image = @imagecreatefrompng($file_path); } if ($image) { if (function_exists('imagepalettetotruecolor')) { @imagepalettetotruecolor($image); } if ($mime_type === 'image/png') { @imagealphablending($image, false); @imagesavealpha($image, true); } $success = @imagewebp($image, $webp_path, 85); @imagedestroy($image); } } // Fallback to WP Image Editor if GD failed or is missing if (!$success || !file_exists($webp_path) || filesize($webp_path) < 100) { $editor = wp_get_image_editor($file_path); if (!is_wp_error($editor)) { $editor->set_quality(85); $saved = $editor->save($webp_path, 'image/webp'); $success = !is_wp_error($saved); } } if ($success && file_exists($webp_path) && filesize($webp_path) > 100) { // Update attachment file and mime type in DB update_attached_file($attachment_id, $webp_path); global $wpdb; $wpdb->update( $wpdb->posts, ['post_mime_type' => 'image/webp'], ['ID' => $attachment_id], ['%s'], ['%d'] ); // Delete original file if option is enabled if ( $delete_original ) { @unlink($file_path); } return true; } return false; } }