diff --git a/includes/Images/ImageUploadListener.php b/includes/Images/ImageUploadListener.php index 206370e..b21d0bc 100644 --- a/includes/Images/ImageUploadListener.php +++ b/includes/Images/ImageUploadListener.php @@ -45,16 +45,16 @@ private function register_hooks() { * @return array|WP_Error The modified upload array or WP_Error on failure. */ public function handle_media_upload( $upload ) { - $optimized_image = $this->image_service->optimize_image( $upload['url'], $upload['file'] ); + $optimized_image_path = $this->image_service->optimize_image( $upload['url'], $upload['file'] ); - if ( is_wp_error( $optimized_image ) ) { + if ( is_wp_error( $optimized_image_path ) ) { return $upload; } if ( $this->delete_original ) { - $upload = $this->replace_original_with_webp( $upload, $optimized_image ); + $upload = $this->replace_original_with_webp( $upload, $optimized_image_path ); } else { - $this->add_webp_as_new_attachment( $optimized_image ); + $this->register_webp_as_new_media( $optimized_image_path ); } return $upload; @@ -68,24 +68,29 @@ public function handle_media_upload( $upload ) { * @return array The updated upload array. */ private function replace_original_with_webp( $upload, $webp_file_path ) { - // Update the upload array to use the WebP file - $upload['file'] = $webp_file_path; - $upload['url'] = trailingslashit( wp_upload_dir()['url'] ) . wp_basename( $webp_file_path ); - $upload['type'] = 'image/webp'; - // Delete the original file from disk - $this->delete_original_file( $upload['file'] ); + $original_file_path = $upload['file']; + + if ( $this->delete_original_file( $original_file_path ) ) { + // Update the upload array to use the WebP file + $upload['file'] = $webp_file_path; + $upload['url'] = trailingslashit( wp_upload_dir()['url'] ) . wp_basename( $webp_file_path ); + $upload['type'] = 'image/webp'; + } return $upload; } /** - * Adds the optimized WebP file as a new attachment in the Media Library. + * Registers the WebP file as a standalone media item in the Media Library. * * @param string $webp_file_path The path to the optimized WebP file. */ - private function add_webp_as_new_attachment( $webp_file_path ) { + private function register_webp_as_new_media( $webp_file_path ) { + $upload_dir = wp_upload_dir(); + $webp_url = trailingslashit( $upload_dir['url'] ) . wp_basename( $webp_file_path ); + // Prepare the attachment data $attachment_data = array( 'post_mime_type' => 'image/webp', 'post_title' => wp_basename( $webp_file_path ), @@ -93,12 +98,14 @@ private function add_webp_as_new_attachment( $webp_file_path ) { 'post_status' => 'inherit', ); + // Insert the WebP file as a new attachment $attachment_id = wp_insert_attachment( $attachment_data, $webp_file_path ); if ( is_wp_error( $attachment_id ) ) { return; } + // Generate and update attachment metadata require_once ABSPATH . 'wp-admin/includes/image.php'; $metadata = wp_generate_attachment_metadata( $attachment_id, $webp_file_path ); wp_update_attachment_metadata( $attachment_id, $metadata ); @@ -108,22 +115,15 @@ private function add_webp_as_new_attachment( $webp_file_path ) { * Deletes the original uploaded file from the filesystem. * * @param string $file_path The path to the original file. - * @return true|WP_Error True on success, WP_Error on failure. + * @return bool True on success, false on failure. */ private function delete_original_file( $file_path ) { if ( file_exists( $file_path ) ) { - if ( ! wp_delete_file( $file_path ) ) { - return new \WP_Error( - 'nfd_performance_error', - sprintf( - /* translators: %s: File path */ - __( 'Failed to delete original file: %s', 'wp-module-performance' ), - $file_path - ) - ); + if ( wp_delete_file( $file_path ) ) { + return true; } } - return true; // File deletion successful + return false; } }