| @@ -32,39 +32,48 @@ | ||
| 32 | 32 | ); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Summary of deleteFileByUrl | |
| 37 | - * @param mixed $file_url | |
| 36 | + * Delete a local attachment only when its stored path belongs to Fluent Boards. | |
| 37 | + * | |
| 38 | + * @param mixed $attachment | |
| 39 | + * @param int|null $boardId | |
| 38 | 40 | * @return bool |
| 39 | 41 | */ |
| 40 | - public function deleteFileByUrl($file_url) | |
| 42 | + public function deleteAttachmentFile($attachment, $boardId = null) | |
| 41 | 43 | { |
| 42 | - $exists = Attachment::where('full_url', $file_url)->exists(); | |
| 43 | - if($exists) { | |
| 44 | - return; | |
| 44 | + if ( | |
| 45 | + !$attachment || | |
| 46 | + $attachment->attachment_type === 'url' || | |
| 47 | + (!empty($attachment->driver) && $attachment->driver !== 'local') || | |
| 48 | + empty($attachment->file_path) | |
| 49 | + ) { | |
| 50 | + return false; | |
| 45 | 51 | } |
| 46 | - // Convert the URL to the local file path | |
| 47 | - $upload_dir = wp_upload_dir(); | |
| 48 | - $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_url); | |
| 49 | 52 | |
| 50 | - // Check if the file exists | |
| 51 | - if (file_exists($file_path)) { | |
| 52 | - // Delete the file | |
| 53 | - $deleted = wp_delete_file($file_path); | |
| 53 | + $storedFilename = rawurldecode((string) $attachment->file_path); | |
| 54 | + $isBareFilename = $storedFilename !== '' | |
| 55 | + && strpos($storedFilename, '/') === false | |
| 56 | + && strpos($storedFilename, '\\') === false; | |
| 57 | + $filePath = FileSystem::resolveLocalAttachmentPath($attachment->file_path, $boardId); | |
| 54 | 58 | |
| 55 | - // Optionally, you can also remove the file from the media library | |
| 56 | - // Note: This won't delete the file physically, but it will remove it from the media library | |
| 57 | - $attachment_id = attachment_url_to_postid($file_url); | |
| 58 | - if ($attachment_id) { | |
| 59 | - wp_delete_attachment($attachment_id, true); | |
| 59 | + if (!$filePath) { | |
| 60 | + return false; | |
| 61 | + } | |
| 62 | + | |
| 63 | + // Legacy filenames need the board-qualified URL to distinguish same-named files across boards. | |
| 64 | + if ($isBareFilename) { | |
| 65 | + if ( | |
| 66 | + empty($attachment->full_url) || | |
| 67 | + Attachment::where('full_url', $attachment->full_url)->exists() | |
| 68 | + ) { | |
| 69 | + return false; | |
| 60 | 70 | } |
| 71 | + } elseif (Attachment::where('file_path', $attachment->file_path)->exists()) { | |
| 72 | + return false; | |
| 73 | + } | |
| 61 | 74 | |
| 62 | - // Return true if the file was successfully deleted | |
| 63 | - return $deleted; | |
| 64 | - } | |
| 65 | - // Return false if the file does not exist | |
| 66 | - return false; | |
| 75 | + return (bool) wp_delete_file($filePath); | |
| 67 | 76 | } |
| 68 | 77 | |
| 69 | 78 | |
| 70 | 79 | /** |