| @@ -227,10 +227,9 @@ | ||
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | - * Prevents WP deleting files when there are still media using them | |
| 232 | - * Thanks to Bruno "Aesqe" Babic and its plugin file gallery in which I took all the ideas for this function | |
| 231 | + * Prevents WP deleting files when there are still media using them. | |
| 233 | 232 | * |
| 234 | 233 | * @since 0.9 |
| 235 | 234 | * |
| 236 | 235 | * @param string $file Path to the file to delete. |
| @@ -240,21 +239,22 @@ | ||
| 240 | 239 | global $wpdb; |
| 241 | 240 | |
| 242 | 241 | $uploadpath = wp_upload_dir(); |
| 243 | 242 | |
| 243 | + // Get the main attached file. | |
| 244 | + $attached_file = substr_replace( $file, '', 0, strlen( trailingslashit( $uploadpath['basedir'] ) ) ); | |
| 245 | + $attached_file = preg_replace( '#-\d+x\d+\.([a-z]+)$#', '.$1', $attached_file ); | |
| 246 | + | |
| 244 | 247 | $ids = $wpdb->get_col( |
| 245 | 248 | $wpdb->prepare( |
| 246 | 249 | "SELECT post_id FROM $wpdb->postmeta |
| 247 | 250 | WHERE meta_key = '_wp_attached_file' AND meta_value = %s", |
| 248 | - substr_replace( $file, '', 0, strlen( trailingslashit( $uploadpath['basedir'] ) ) ) | |
| 251 | + $attached_file | |
| 249 | 252 | ) |
| 250 | 253 | ); |
| 251 | 254 | |
| 252 | 255 | if ( ! empty( $ids ) ) { |
| 253 | - // Regenerate intermediate sizes if it's an image ( since we could not prevent WP deleting them before ). | |
| 254 | - require_once ABSPATH . 'wp-admin/includes/image.php'; // In case the file is deleted outside admin. | |
| 255 | - wp_update_attachment_metadata( $ids[0], wp_slash( wp_generate_attachment_metadata( $ids[0], $file ) ) ); // Directly uses update_post_meta, so expects slashed. | |
| 256 | - return ''; // Prevent deleting the main file. | |
| 256 | + return ''; // Prevent deleting the file. | |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | return $file; |
| 260 | 260 | } |