@@ -266,6 +266,12 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
266
266
return $ this ->get_dam_url ( $ image );
267
267
}
268
268
269
+ $ offloaded_id = $ this ->is_offloaded_url ( $ image ->getSource () );
270
+
271
+ if ( $ offloaded_id !== 0 ) {
272
+ return $ this ->get_offloaded_url ( $ offloaded_id , $ image ->getUrl (), $ image ->getSource () );
273
+ }
274
+
269
275
return $ image ->getUrl ();
270
276
}
271
277
@@ -366,4 +372,46 @@ private function get_dam_url( Image $image ) {
366
372
private function is_dam_url ( $ url ) {
367
373
return is_string ( $ url ) && strpos ( $ url , Optml_Dam::URL_DAM_FLAG ) !== false ;
368
374
}
375
+
376
+ /**
377
+ * Check if the URL is offloaded.
378
+ *
379
+ * @param string $source_url The source image URL.
380
+ *
381
+ * @return int
382
+ */
383
+ private function is_offloaded_url ( $ source_url ) {
384
+ $ attachment_id = 0 ;
385
+
386
+ if ( strpos ( $ source_url , Optml_Media_Offload::KEYS ['not_processed_flag ' ] ) !== false ) {
387
+ $ attachment_id = (int ) Optml_Media_Offload::get_attachment_id_from_url ( $ source_url );
388
+ } else {
389
+ $ attachment_id = $ this ->attachment_url_to_post_id ( $ source_url );
390
+ }
391
+
392
+ if ( $ attachment_id === 0 ) {
393
+ return 0 ;
394
+ }
395
+
396
+ if ( empty ( get_post_meta ( $ attachment_id , Optml_Media_Offload::OM_OFFLOADED_FLAG , true ) ) ) {
397
+ return 0 ;
398
+ }
399
+
400
+ return (int ) $ attachment_id ;
401
+ }
402
+
403
+ /**
404
+ * Get the offloaded URL for an image.
405
+ *
406
+ * @param int $id The attachment ID.
407
+ * @param string $optimized_url The optimized image URL.
408
+ * @param string $source_url The source image URL.
409
+ *
410
+ * @return string
411
+ */
412
+ private function get_offloaded_url ( $ id , $ optimized_url , $ source_url ) {
413
+ $ suffix = wp_get_attachment_metadata ( $ id )['file ' ];
414
+
415
+ return str_replace ( $ source_url , ltrim ( $ suffix , '/ ' ), $ optimized_url );
416
+ }
369
417
}
0 commit comments