Skip to content

Commit 8ef0eca

Browse files
committed
fix issue when the offloaded images were marked as done on the legacy system but not on the new system.
Improve the logic the attachment cache logic
1 parent eaaf4f6 commit 8ef0eca

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

inc/media_offload.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ public function upload_and_update_existing_images( $image_ids ) {
813813
foreach ( $image_ids as $id ) {
814814
if ( self::is_uploaded_image( wp_get_attachment_metadata( $id )['file'] ) ) {
815815
// if this meta flag below failed at the initial update but the file meta above is updated it will cause an infinite query loop
816-
update_post_meta( $id, 'optimole_offload', 'true' );
816+
update_post_meta( $id, self::META_KEYS['offloaded'], 'true' );
817+
update_post_meta( $id, self::OM_OFFLOADED_FLAG, true );
817818
$success_up ++;
818819
continue;
819820
}

inc/traits/dam_offload_utils.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,46 @@ private function get_scaled_url( $url ) {
223223
private function is_scaled_url( $url ) {
224224
return strpos( $url, '-scaled.' ) !== false;
225225
}
226+
227+
/**
228+
* Check if the image has been offloaded completely.
229+
*
230+
* @param int $id The attachment ID.
231+
*
232+
* @return bool
233+
*/
234+
private function is_completed_offload( $id ) {
235+
// This is the flag that is used to mark the image as offloaded at the end.
236+
$completed = ! empty( get_post_meta( $id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) );
237+
if ( $completed ) {
238+
return true;
239+
}
240+
// In some rare cases the image is offloaded but the flag is not set so we can alternatively check this using the file path.
241+
$meta = wp_get_attachment_metadata( $id );
242+
if ( ! isset( $meta['file'] ) ) {
243+
return false;
244+
}
245+
if ( Optml_Media_Offload::is_uploaded_image( $meta['file'] ) ) {
246+
return true;
247+
}
248+
249+
return false;
250+
}
226251
/**
227252
* Get the attachment ID from URL.
228253
*
229-
* @param string $url The attachment URL.
254+
* @param string $input_url The attachment URL.
230255
*
231256
* @return int
232257
*/
233-
private function attachment_url_to_post_id( $url ) {
234-
$cached = Optml_Attachment_Cache::get_cached_attachment_id( $url );
258+
private function attachment_url_to_post_id( $input_url ) {
259+
$cached = Optml_Attachment_Cache::get_cached_attachment_id( $input_url );
235260

236261
if ( $cached !== false ) {
237262
return $cached;
238263
}
239264

240-
$url = $this->strip_image_size( $url );
265+
$url = $this->strip_image_size( $input_url );
241266

242267
$attachment_id = attachment_url_to_postid( $url );
243268

@@ -265,7 +290,7 @@ private function attachment_url_to_post_id( $url ) {
265290
$attachment_id = attachment_url_to_postid( $scaled_url );
266291
}
267292
}
268-
Optml_Attachment_Cache::set_cached_attachment_id( $url, $attachment_id );
293+
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
269294

270295
return $attachment_id;
271296
}

0 commit comments

Comments
 (0)