@@ -223,21 +223,46 @@ private function get_scaled_url( $url ) {
223
223
private function is_scaled_url ( $ url ) {
224
224
return strpos ( $ url , '-scaled. ' ) !== false ;
225
225
}
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
+ }
226
251
/**
227
252
* Get the attachment ID from URL.
228
253
*
229
- * @param string $url The attachment URL.
254
+ * @param string $input_url The attachment URL.
230
255
*
231
256
* @return int
232
257
*/
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 );
235
260
236
261
if ( $ cached !== false ) {
237
262
return $ cached ;
238
263
}
239
264
240
- $ url = $ this ->strip_image_size ( $ url );
265
+ $ url = $ this ->strip_image_size ( $ input_url );
241
266
242
267
$ attachment_id = attachment_url_to_postid ( $ url );
243
268
@@ -265,7 +290,7 @@ private function attachment_url_to_post_id( $url ) {
265
290
$ attachment_id = attachment_url_to_postid ( $ scaled_url );
266
291
}
267
292
}
268
- Optml_Attachment_Cache::set_cached_attachment_id ( $ url , $ attachment_id );
293
+ Optml_Attachment_Cache::set_cached_attachment_id ( $ input_url , $ attachment_id );
269
294
270
295
return $ attachment_id ;
271
296
}
0 commit comments