Skip to content

Commit 2524af0

Browse files
committed
fix: offloading URL replacement not happening
1 parent 52267f5 commit 2524af0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

inc/url_replacer.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
266266
return $this->get_dam_url( $image );
267267
}
268268

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+
269275
return $image->getUrl();
270276
}
271277

@@ -366,4 +372,46 @@ private function get_dam_url( Image $image ) {
366372
private function is_dam_url( $url ) {
367373
return is_string( $url ) && strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
368374
}
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+
}
369417
}

0 commit comments

Comments
 (0)