Skip to content

Commit 6f34f25

Browse files
committed
fix(bulk): fix indefinite spinner and stuck counter in Generate Missing Next-Gen
Seven bugs caused the bulk next-gen generation flow to spin indefinitely: Bug 1 (CustomFolders): SQL JOIN did not filter on fo.active = 1, so files from inactive folders inflated the count with entries never processed. Bug 2 (CustomFolders): WHERE clause included OR fi.data IS NULL, admitting files with no optimization data that always fail generate_nextgen_versions(). Bug 3 (Bulk): no-backup error on the first context aborted the entire loop, silently skipping valid media library images. Bug 4 (WP): status condition included mt1.meta_key IS NULL, queuing images with _imagify_data but no _imagify_status row, which always fail with not_optimized. Bug 5 (AbstractProcess): when the API reported WebP larger than original, str_replace corrupted the storage key (full@imagify-webp -> full), so the @imagify-webp entry was never written and the image matched every subsequent NOT LIKE poll indefinitely. Bug 6 (WP, CustomFolders): mime-type exclusion silently failed because explode/str_replace left a leading space on the mime string; webp source files were included in the queue. Bug 7 (WP, CustomFolders): .webp files stored with wrong post_mime_type (e.g. image/jpeg) slipped past the SQL exclusion and were permanently stuck as remaining = 1. Closes #1041
1 parent dc72df5 commit 6f34f25

4 files changed

Lines changed: 23 additions & 16 deletions

File tree

classes/Bulk/Bulk.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,17 @@ public function run_generate_nextgen( array $contexts, array $formats ) {
242242
foreach ( $contexts as $context ) {
243243
foreach ( $formats as $format ) {
244244
$media = $this->get_bulk_instance( $context )->get_optimized_media_ids_without_format( $format );
245-
if ( ! $media['ids'] && $media['errors']['no_backup'] ) {
246-
// No backup, no next-gen.
247-
return [
248-
'success' => false,
249-
'message' => 'no-backup',
250-
];
251-
} elseif ( ! $media['ids'] && $media['errors']['no_file_path'] ) {
245+
if ( ! $media['ids'] && $media['errors']['no_file_path'] ) {
252246
// Error.
253247
return [
254248
'success' => false,
255249
'message' => __( 'The path to the selected files could not be retrieved.', 'imagify' ),
256250
];
257251
}
258252

259-
$medias[ $context ] = $media['ids'];
253+
if ( $media['ids'] ) {
254+
$medias[ $context ] = $media['ids'];
255+
}
260256
}
261257
}
262258

classes/Bulk/CustomFolders.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,20 @@ public function get_optimized_media_ids_without_format( $format ) {
114114
if ( ! isset( $mime ) && empty( $mime ) ) {
115115
$mime = 'image/webp';
116116
}
117-
$mime_types = str_replace( ",'" . $mime . "'", '', $mime_types );
117+
$mime = trim( $mime );
118+
$mime_types = str_replace( [ ", '" . $mime . "'", ",'" . $mime . "'" ], '', $mime_types );
118119
$nextgen_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::' . strtoupper( $format ) . '_SUFFIX' );
119120
$files = $wpdb->get_results(
120121
$wpdb->prepare( // WPCS: unprepared SQL ok.
121122
"
122123
SELECT fi.file_id, fi.path
123124
FROM $files_table as fi
124125
INNER JOIN $folders_table AS fo
125-
ON ( fi.folder_id = fo.folder_id )
126+
ON ( fi.folder_id = fo.folder_id AND fo.active = 1 )
126127
WHERE
127128
fi.mime_type IN ( $mime_types )
128129
AND ( fi.status = 'success' OR fi.status = 'already_optimized' )
129-
AND ( fi.data NOT LIKE %s OR fi.data IS NULL )
130+
AND fi.data NOT LIKE %s
130131
ORDER BY fi.file_id DESC",
131132
'%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%'
132133
)
@@ -159,6 +160,12 @@ public function get_optimized_media_ids_without_format( $format ) {
159160
$file_path = Imagify_Files_Scan::remove_placeholder( $file->path );
160161
$backup_path = Imagify_Custom_Folders::get_file_backup_path( $file_path );
161162

163+
// Skip files whose extension already matches the target format
164+
// (e.g. a .webp file stored with incorrect post_mime_type).
165+
if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) {
166+
continue;
167+
}
168+
162169
if ( ! $this->filesystem->exists( $backup_path ) ) {
163170
// No backup, no WebP.
164171
$data['errors']['no_backup'][] = $file_id;

classes/Bulk/WP.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ public function get_optimized_media_ids_without_format( $format ) {
206206
if ( ! isset( $mime ) && empty( $mime ) ) {
207207
$mime = 'image/webp';
208208
}
209-
$mime_types = str_replace( ",'" . $mime . "'", '', $mime_types );
209+
$mime = trim( $mime );
210+
$mime_types = str_replace( [ ", '" . $mime . "'", ",'" . $mime . "'" ], '', $mime_types );
210211
$statuses = Imagify_DB::get_post_statuses();
211212
$nodata_join = '';
212213
$nodata_where = '';
@@ -233,7 +234,7 @@ public function get_optimized_media_ids_without_format( $format ) {
233234
ON ( p.ID = mt2.post_id AND mt2.meta_key = '_imagify_data' )
234235
WHERE
235236
p.post_mime_type IN ( $mime_types )
236-
AND (mt1.meta_key IS NULL OR mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' )
237+
AND (mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' )
237238
AND mt2.meta_value NOT LIKE %s
238239
AND p.post_type = 'attachment'
239240
AND p.post_status IN ( $statuses )
@@ -296,6 +297,12 @@ public function get_optimized_media_ids_without_format( $format ) {
296297
continue;
297298
}
298299

300+
// Skip files whose extension already matches the target format
301+
// (e.g. a .webp file stored with incorrect post_mime_type).
302+
if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) {
303+
continue;
304+
}
305+
299306
$backup_path = get_imagify_attachment_backup_path( $file_path );
300307

301308
if ( ! $this->filesystem->exists( $backup_path ) ) {

classes/Optimization/Process/AbstractProcess.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,9 +1970,6 @@ public function update_size_optimization_data( $response, $size, $level ) {
19701970
*/
19711971
$data = (array) apply_filters( "imagify{$_unauthorized}_file_optimization_data", $data, $response, $size, $level, $this->get_data() );
19721972

1973-
if ( property_exists( $response, 'message' ) ) {
1974-
$size = str_replace( $this->format, '', $size );
1975-
}
19761973
// Store.
19771974
$this->get_data()->update_size_optimization_data( $size, $data );
19781975

0 commit comments

Comments
 (0)