| @@ -171,9 +171,10 @@ | ||
| 171 | 171 | } |
| 172 | 172 | if ( ! isset( $mime ) && empty( $mime ) ) { |
| 173 | 173 | $mime = 'image/webp'; |
| 174 | 174 | } |
| 175 | - $mime_types = str_replace( ",'" . $mime . "'", '', $mime_types ); | |
| 175 | + $mime = trim( $mime ); | |
| 176 | + $mime_types = str_replace( [ ", '" . $mime . "'", ",'" . $mime . "'" ], '', $mime_types ); | |
| 176 | 177 | $nextgen_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::' . strtoupper( $format ) . '_SUFFIX' ); |
| 177 | 178 | $files = $wpdb->get_results( |
| 178 | 179 | $wpdb->prepare( // WPCS: unprepared SQL ok. |
| 179 | 180 | " |
| @@ -179,15 +180,25 @@ | ||
| 179 | 180 | " |
| 180 | 181 | SELECT fi.file_id, fi.path |
| 181 | 182 | FROM $files_table as fi |
| 182 | 183 | INNER JOIN $folders_table AS fo |
| 183 | - ON ( fi.folder_id = fo.folder_id ) | |
| 184 | + ON ( fi.folder_id = fo.folder_id AND fo.active = 1 ) | |
| 184 | 185 | WHERE |
| 185 | 186 | fi.mime_type IN ( $mime_types ) |
| 186 | 187 | AND ( fi.status = 'success' OR fi.status = 'already_optimized' ) |
| 187 | - AND ( fi.data NOT LIKE %s OR fi.data IS NULL ) | |
| 188 | + AND fi.data NOT LIKE %s | |
| 189 | + AND fi.data NOT LIKE %s | |
| 188 | 190 | ORDER BY fi.file_id DESC", |
| 189 | - '%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%' | |
| 191 | + '%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%', | |
| 192 | + /** | |
| 193 | + * Second predicate: skip files the API permanently refused to convert (the next-gen file | |
| 194 | + * would be heavier than the original, or the file is already compressed). Those are stored | |
| 195 | + * as `<size><suffix>";a:3:{s:15:"permanent_error";b:1;s:7:"success";b:0;s:5:"error";…`. | |
| 196 | + * Transient failures serialize as a 2-element array without the `permanent_error` key, so | |
| 197 | + * they keep being retried. Matching serialized data with LIKE is fragile: the key order | |
| 198 | + * written in Optimization\Data\CustomFolders must not change. | |
| 199 | + */ | |
| 200 | + '%' . $wpdb->esc_like( $nextgen_suffix . '";a:3:{s:15:"permanent_error";b:1;' ) . '%' | |
| 190 | 201 | ) |
| 191 | 202 | ); |
| 192 | 203 | |
| 193 | 204 | $wpdb->flush(); |
| @@ -213,9 +224,16 @@ | ||
| 213 | 224 | $data['errors']['no_file_path'][] = $file_id; |
| 214 | 225 | continue; |
| 215 | 226 | } |
| 216 | 227 | |
| 217 | - $file_path = Imagify_Files_Scan::remove_placeholder( $file->path ); | |
| 228 | + $file_path = Imagify_Files_Scan::remove_placeholder( $file->path ); | |
| 229 | + | |
| 230 | + // Skip files whose extension already matches the target format | |
| 231 | + // (e.g. a .webp file stored with incorrect post_mime_type). | |
| 232 | + if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) { | |
| 233 | + continue; | |
| 234 | + } | |
| 235 | + | |
| 218 | 236 | $backup_path = Imagify_Custom_Folders::get_file_backup_path( $file_path ); |
| 219 | 237 | |
| 220 | 238 | if ( ! $this->filesystem->exists( $backup_path ) ) { |
| 221 | 239 | // No backup, no WebP. |