Skip to content

Commit 02ce6f2

Browse files
committed
Refactor attachment ID retrieval logic in DAM URL handling
1 parent 6d05010 commit 02ce6f2

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

inc/traits/dam_offload_utils.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -288,43 +288,45 @@ private function attachment_url_to_post_id( $input_url ) {
288288
return (int) $cached;
289289
}
290290

291-
if ( $this->is_dam_url( $input_url ) ) {
291+
if ( Optml_Media_Offload::is_uploaded_image( $input_url ) ) {
292292
// The DAM are stored as attachments of format /id:<attachment_id>/<original_url>
293293
$pattern = '#/' . Optml_Media_Offload::KEYS['uploaded_flag'] . '([^/]+)#';
294294
if ( preg_match( $pattern, $input_url, $m ) ) {
295295
$attachment_id = $this->get_attachement_id_from_optimole_id( $m[1] );
296-
} else {
297-
$attachment_id = 0;
296+
if ( $attachment_id !== 0 ) {
297+
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
298+
299+
return $attachment_id;
300+
}
298301
}
299-
} else {
302+
}
300303

301-
$url = $this->strip_image_size( $input_url );
304+
$url = $this->strip_image_size( $input_url );
302305

303-
$attachment_id = attachment_url_to_postid( $url );
306+
$attachment_id = attachment_url_to_postid( $url );
304307

305-
if ( $attachment_id === 0 && ! $this->is_scaled_url( $url ) ) {
306-
$scaled_url = $this->get_scaled_url( $url );
308+
if ( $attachment_id === 0 && ! $this->is_scaled_url( $url ) ) {
309+
$scaled_url = $this->get_scaled_url( $url );
307310

308-
$attachment_id = attachment_url_to_postid( $scaled_url );
309-
}
311+
$attachment_id = attachment_url_to_postid( $scaled_url );
312+
}
310313

311-
/*
312-
* TODO: The logic is a mess, we need to refactor at some point.
313-
* Websites may transition between 'www' subdomains and apex domains, potentially breaking references to hosted images. This can cause issues when attempting to match attachment IDs if images are linked using outdated domains. The logic is checking for alternative domains and consider the use of 'scaled' prefixes in image URLs for large images, which might affect ID matching.
314-
*/
315-
if ( $attachment_id === 0 ) {
316-
if ( strpos( $url, 'www.' ) !== false ) {
317-
$variant_url = str_replace( 'www.', '', $url );
318-
$attachment_id = attachment_url_to_postid( $variant_url );
319-
} else {
320-
$variant_url = str_replace( '://', '://www.', $url );
321-
$attachment_id = attachment_url_to_postid( $variant_url );
322-
}
323-
if ( $attachment_id === 0 && ! $this->is_scaled_url( $variant_url ) ) {
324-
$scaled_url = $this->get_scaled_url( $variant_url );
314+
/*
315+
* TODO: The logic is a mess, we need to refactor at some point.
316+
* Websites may transition between 'www' subdomains and apex domains, potentially breaking references to hosted images. This can cause issues when attempting to match attachment IDs if images are linked using outdated domains. The logic is checking for alternative domains and consider the use of 'scaled' prefixes in image URLs for large images, which might affect ID matching.
317+
*/
318+
if ( $attachment_id === 0 ) {
319+
if ( strpos( $url, 'www.' ) !== false ) {
320+
$variant_url = str_replace( 'www.', '', $url );
321+
$attachment_id = attachment_url_to_postid( $variant_url );
322+
} else {
323+
$variant_url = str_replace( '://', '://www.', $url );
324+
$attachment_id = attachment_url_to_postid( $variant_url );
325+
}
326+
if ( $attachment_id === 0 && ! $this->is_scaled_url( $variant_url ) ) {
327+
$scaled_url = $this->get_scaled_url( $variant_url );
325328

326-
$attachment_id = attachment_url_to_postid( $scaled_url );
327-
}
329+
$attachment_id = attachment_url_to_postid( $scaled_url );
328330
}
329331
}
330332
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );

0 commit comments

Comments
 (0)