| @@ -90,8 +90,10 @@ | ||
| 90 | 90 | // Get attachments optimization level. |
| 91 | 91 | 'optimization_levels' => '_imagify_optimization_level', |
| 92 | 92 | // Get attachments status. |
| 93 | 93 | 'statuses' => '_imagify_status', |
| 94 | + // Get attachments metadata, to detect a WP-scaled original. | |
| 95 | + 'metadata' => '_wp_attachment_metadata', | |
| 94 | 96 | ], |
| 95 | 97 | $ids |
| 96 | 98 | ); |
| 97 | 99 | |
| @@ -155,9 +157,10 @@ | ||
| 155 | 157 | if ( ! $file_path || ! $this->filesystem->exists( $file_path ) ) { |
| 156 | 158 | continue; |
| 157 | 159 | } |
| 158 | 160 | |
| 159 | - $attachment_backup_path = get_imagify_attachment_backup_path( $file_path ); | |
| 161 | + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null ); | |
| 162 | + $attachment_backup_path = get_imagify_attachment_backup_path( $original_path ); | |
| 160 | 163 | $attachment_status = isset( $metas['statuses'][ $id ] ) ? $metas['statuses'][ $id ] : false; |
| 161 | 164 | $attachment_optimization_level = isset( $metas['optimization_levels'][ $id ] ) ? $metas['optimization_levels'][ $id ] : false; |
| 162 | 165 | |
| 163 | 166 | // Don't try to re-optimize if there is no backup file. |
| @@ -171,8 +174,92 @@ | ||
| 171 | 174 | return $data; |
| 172 | 175 | } |
| 173 | 176 | |
| 174 | 177 | /** |
| 178 | + * Get all optimized media IDs that have a backup file available for restore. | |
| 179 | + * | |
| 180 | + * @return array A list of media IDs. | |
| 181 | + */ | |
| 182 | + public function get_optimized_media_ids(): array { | |
| 183 | + global $wpdb; | |
| 184 | + | |
| 185 | + $this->set_no_time_limit(); | |
| 186 | + | |
| 187 | + $mime_types = Imagify_DB::get_mime_types(); | |
| 188 | + $statuses = Imagify_DB::get_post_statuses(); | |
| 189 | + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); | |
| 190 | + $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( | |
| 191 | + [ | |
| 192 | + 'prepared' => true, | |
| 193 | + ] | |
| 194 | + ); | |
| 195 | + $ids = $wpdb->get_col( | |
| 196 | + $wpdb->prepare( // WPCS: unprepared SQL ok. | |
| 197 | + " | |
| 198 | + SELECT DISTINCT p.ID | |
| 199 | + FROM $wpdb->posts AS p | |
| 200 | + $nodata_join | |
| 201 | + INNER JOIN $wpdb->postmeta AS mt1 | |
| 202 | + ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' ) | |
| 203 | + WHERE | |
| 204 | + p.post_mime_type IN ( $mime_types ) | |
| 205 | + AND mt1.meta_value IN ( 'success', 'already_optimized' ) | |
| 206 | + AND p.post_type = 'attachment' | |
| 207 | + AND p.post_status IN ( $statuses ) | |
| 208 | + $nodata_where | |
| 209 | + ORDER BY p.ID DESC | |
| 210 | + LIMIT 0, %d", | |
| 211 | + imagify_get_unoptimized_attachment_limit() | |
| 212 | + ) | |
| 213 | + ); | |
| 214 | + | |
| 215 | + $wpdb->flush(); | |
| 216 | + unset( $mime_types, $statuses ); | |
| 217 | + $ids = array_filter( array_map( 'absint', $ids ) ); | |
| 218 | + | |
| 219 | + if ( ! $ids ) { | |
| 220 | + return []; | |
| 221 | + } | |
| 222 | + | |
| 223 | + $metas = Imagify_DB::get_metas( | |
| 224 | + [ | |
| 225 | + // Get attachments filename. | |
| 226 | + 'filenames' => '_wp_attached_file', | |
| 227 | + // Get attachments metadata, to detect a WP-scaled original. | |
| 228 | + 'metadata' => '_wp_attachment_metadata', | |
| 229 | + ], | |
| 230 | + $ids | |
| 231 | + ); | |
| 232 | + | |
| 233 | + $data = []; | |
| 234 | + | |
| 235 | + foreach ( $ids as $id ) { | |
| 236 | + if ( empty( $metas['filenames'][ $id ] ) ) { | |
| 237 | + // Problem. | |
| 238 | + continue; | |
| 239 | + } | |
| 240 | + | |
| 241 | + $file_path = get_imagify_attached_file( $metas['filenames'][ $id ] ); | |
| 242 | + | |
| 243 | + if ( ! $file_path ) { | |
| 244 | + continue; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null ); | |
| 248 | + $attachment_backup_path = get_imagify_attachment_backup_path( $original_path ); | |
| 249 | + | |
| 250 | + if ( ! $this->filesystem->exists( $attachment_backup_path ) ) { | |
| 251 | + // No backup, cannot restore. | |
| 252 | + continue; | |
| 253 | + } | |
| 254 | + | |
| 255 | + $data[] = $id; | |
| 256 | + } | |
| 257 | + | |
| 258 | + return $data; | |
| 259 | + } | |
| 260 | + | |
| 261 | + /** | |
| 175 | 262 | * Get ids of all optimized media without Next gen versions. |
| 176 | 263 | * |
| 177 | 264 | * @since 2.2 |
| 178 | 265 | * |
| @@ -205,9 +292,10 @@ | ||
| 205 | 292 | } |
| 206 | 293 | if ( ! isset( $mime ) && empty( $mime ) ) { |
| 207 | 294 | $mime = 'image/webp'; |
| 208 | 295 | } |
| 209 | - $mime_types = str_replace( ",'" . $mime . "'", '', $mime_types ); | |
| 296 | + $mime = trim( $mime ); | |
| 297 | + $mime_types = str_replace( [ ", '" . $mime . "'", ",'" . $mime . "'" ], '', $mime_types ); | |
| 210 | 298 | $statuses = Imagify_DB::get_post_statuses(); |
| 211 | 299 | $nodata_join = ''; |
| 212 | 300 | $nodata_where = ''; |
| 213 | 301 | if ( ! imagify_has_attachments_without_required_metadata() ) { |
| @@ -232,10 +320,11 @@ | ||
| 232 | 320 | LEFT JOIN $wpdb->postmeta AS mt2 |
| 233 | 321 | ON ( p.ID = mt2.post_id AND mt2.meta_key = '_imagify_data' ) |
| 234 | 322 | WHERE |
| 235 | 323 | 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' ) | |
| 324 | + AND ( mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' ) | |
| 237 | 325 | AND mt2.meta_value NOT LIKE %s |
| 326 | + AND mt2.meta_value NOT LIKE %s | |
| 238 | 327 | AND p.post_type = 'attachment' |
| 239 | 328 | AND p.post_status IN ( $statuses ) |
| 240 | 329 | $nodata_where |
| 241 | 330 | ORDER BY p.ID DESC |
| @@ -240,8 +329,17 @@ | ||
| 240 | 329 | $nodata_where |
| 241 | 330 | ORDER BY p.ID DESC |
| 242 | 331 | LIMIT 0, %d", |
| 243 | 332 | '%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%', |
| 333 | + /** | |
| 334 | + * Second predicate: skip media the API permanently refused to convert (the next-gen file | |
| 335 | + * would be heavier than the original, or the file is already compressed). Those are stored | |
| 336 | + * as `<size><suffix>";a:3:{s:15:"permanent_error";b:1;s:7:"success";b:0;s:5:"error";…`. | |
| 337 | + * Transient failures (network, quota, timeout) serialize as a 2-element array without the | |
| 338 | + * `permanent_error` key, so they keep being retried. Matching serialized data with LIKE is | |
| 339 | + * fragile: the key order written in Optimization\Data\WP must not change. | |
| 340 | + */ | |
| 341 | + '%' . $wpdb->esc_like( $nextgen_suffix . '";a:3:{s:15:"permanent_error";b:1;' ) . '%', | |
| 244 | 342 | imagify_get_unoptimized_attachment_limit() |
| 245 | 343 | ) |
| 246 | 344 | ); |
| 247 | 345 | |
| @@ -265,8 +363,10 @@ | ||
| 265 | 363 | $metas = Imagify_DB::get_metas( |
| 266 | 364 | [ |
| 267 | 365 | // Get attachments filename. |
| 268 | 366 | 'filenames' => '_wp_attached_file', |
| 367 | + // Get attachments metadata, to detect a WP-scaled original. | |
| 368 | + 'metadata' => '_wp_attachment_metadata', | |
| 269 | 369 | ], |
| 270 | 370 | $ids |
| 271 | 371 | ); |
| 272 | 372 | |
| @@ -295,10 +395,17 @@ | ||
| 295 | 395 | $data['errors']['no_file_path'][] = $id; |
| 296 | 396 | continue; |
| 297 | 397 | } |
| 298 | 398 | |
| 299 | - $backup_path = get_imagify_attachment_backup_path( $file_path ); | |
| 399 | + // Skip files whose extension already matches the target format | |
| 400 | + // (e.g. a .webp file stored with incorrect post_mime_type). | |
| 401 | + if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) { | |
| 402 | + continue; | |
| 403 | + } | |
| 300 | 404 | |
| 405 | + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null ); | |
| 406 | + $backup_path = get_imagify_attachment_backup_path( $original_path ); | |
| 407 | + | |
| 301 | 408 | if ( ! $this->filesystem->exists( $backup_path ) ) { |
| 302 | 409 | // No backup, no WebP. |
| 303 | 410 | $data['errors']['no_backup'][] = $id; |
| 304 | 411 | continue; |
| @@ -307,8 +414,35 @@ | ||
| 307 | 414 | $data['ids'][] = $id; |
| 308 | 415 | } |
| 309 | 416 | |
| 310 | 417 | return $data; |
| 418 | + } | |
| 419 | + | |
| 420 | + /** | |
| 421 | + * Get the original (pre-scaling) file path from batched `_wp_attachment_metadata`. | |
| 422 | + * | |
| 423 | + * When WordPress scales down an image on upload, `_wp_attached_file` (and therefore | |
| 424 | + * `$file_path`) points to the scaled copy, but Imagify backups are always stored at the | |
| 425 | + * path derived from the true original file (see `Imagify\Media\WP::get_raw_backup_path()`, | |
| 426 | + * which relies on `wp_get_original_image_path()`). This mirrors that derivation from data | |
| 427 | + * already fetched in a single batched query, instead of instantiating a media object (and | |
| 428 | + * firing its own uncached meta reads) for every attachment in the loop. | |
| 429 | + * | |
| 430 | + * @since 2.4 | |
| 431 | + * | |
| 432 | + * @param string $file_path Path derived from `_wp_attached_file`. | |
| 433 | + * @param mixed $metadata The unserialized `_wp_attachment_metadata` value for this attachment, | |
| 434 | + * or null/garbage if it couldn't be fetched or decoded. | |
| 435 | + * | |
| 436 | + * @return string The original-derived file path, or $file_path unchanged when there is no | |
| 437 | + * scaled original to account for. | |
| 438 | + */ | |
| 439 | + private function get_original_file_path_from_metadata( $file_path, $metadata ) { | |
| 440 | + if ( ! is_array( $metadata ) || empty( $metadata['original_image'] ) || ! is_string( $metadata['original_image'] ) ) { | |
| 441 | + return $file_path; | |
| 442 | + } | |
| 443 | + | |
| 444 | + return trailingslashit( dirname( $file_path ) ) . $metadata['original_image']; | |
| 311 | 445 | } |
| 312 | 446 | |
| 313 | 447 | /** |
| 314 | 448 | * Get the context data. |