Skip to content

Commit 1d97b52

Browse files
committed
fix svg handling when image meta is non-existent.
fix offload replacement for alternative domains
1 parent 910230f commit 1d97b52

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

inc/tag_replacer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,10 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
490490
$sizes['width'] = $image_resized[6];
491491
$sizes['height'] = $image_resized[7];
492492
}
493-
493+
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
494+
if ( ! $sizes['width'] || ! $sizes['height'] ) {
495+
break;
496+
}
494497
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );
495498

496499
$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );

inc/traits/dam_offload_utils.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,24 @@ private function attachment_url_to_post_id( $url ) {
246246

247247
$attachment_id = attachment_url_to_postid( $scaled_url );
248248
}
249+
/*
250+
TODO: The logic is a mess, we need to refactor at some point.
251+
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.
252+
*/
253+
if ( $attachment_id === 0 ) {
254+
if ( strpos( $url, 'www.' ) !== false ) {
255+
$variant_url = str_replace( 'www.', '', $url );
256+
$attachment_id = attachment_url_to_postid( $variant_url );
257+
} else {
258+
$variant_url = str_replace( '://', '://www.', $url );
259+
$attachment_id = attachment_url_to_postid( $variant_url );
260+
}
261+
if ( $attachment_id === 0 && ! $this->is_scaled_url( $variant_url ) ) {
262+
$scaled_url = $this->get_scaled_url( $variant_url );
249263

264+
$attachment_id = attachment_url_to_postid( $scaled_url );
265+
}
266+
}
250267
Optml_Attachment_Cache::set_cached_attachment_id( $url, $attachment_id );
251268

252269
return $attachment_id;

tests/assets/sample.svg

Lines changed: 37 additions & 0 deletions
Loading

tests/test-media.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Test_Media extends WP_UnitTestCase {
1515
const IMG_TAGS = '<!-- wp:image {"id":5,"sizeSlug":"medium"} -->
1616
<figure class="wp-block-image size-large"><img src="https://example.i.optimole.com/BOxmgcE-_HsxvBf4/w:300/h:200/q:90/id:b19fed3de30366eb76682becf7645c7b/sample-test.jpg" alt="" class="wp-image-1339"/></figure>
1717
<!-- /wp:image --> ';
18-
18+
use Optml_Dam_Offload_Utils;
1919
public static $files = [
2020
'sample-test',
2121
'1PQ7p',
@@ -400,7 +400,23 @@ public function test_filter_saved_data() {
400400
$this->assertStringContainsString('/wp-content/uploads', $replaced['post_content'] );
401401
$this->assertStringContainsString('300x300.jpg', $replaced['post_content'] );
402402
}
403+
public function test_replace_alternative_domain(){
404+
405+
406+
$url = Optml_Media_Offload::get_original_url( self::$sample_attachement );
407+
$this->assertEquals( $this->attachment_url_to_post_id( $url ), self::$sample_attachement );
408+
$url = str_replace('example.org', 'www.example.org', $url);
409+
$this->assertEquals( $this->attachment_url_to_post_id( $url ), self::$sample_attachement );
403410

411+
$scaled_url = Optml_Media_Offload::get_original_url( self::$sample_attachment_scaled );
412+
413+
$this->assertEquals( $this->attachment_url_to_post_id( $scaled_url ), self::$sample_attachment_scaled );
414+
$scaled_url = str_replace('-scaled','',$scaled_url);
415+
416+
$this->assertEquals( $this->attachment_url_to_post_id( $scaled_url ), self::$sample_attachment_scaled );
417+
$scaled_url = str_replace('example.org', 'www.example.org', $scaled_url);
418+
$this->assertEquals( $this->attachment_url_to_post_id( $scaled_url ), self::$sample_attachment_scaled );
419+
}
404420
public function test_replace_urls_in_editor_content() {
405421
// Sample attachment:
406422
$original_url = Optml_Media_Offload::get_original_url( self::$sample_attachement );

tests/test-replacer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Test_Replacer extends WP_UnitTestCase {
124124

125125
public static $sample_post;
126126
public static $sample_attachement;
127+
public static $sample_svg;
127128

128129
public function setUp(): void {
129130

@@ -158,6 +159,7 @@ public function setUp(): void {
158159
]
159160
);
160161
self::$sample_attachement = self::factory()->attachment->create_upload_object( OPTML_PATH . 'assets/img/logo.png' );
162+
self::$sample_svg = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/sample.svg' );
161163

162164
}
163165

@@ -180,6 +182,12 @@ public function test_wc_json_replacement() {
180182

181183
}
182184

185+
public function test_image_svg() {
186+
187+
$attachement_url = wp_get_attachment_image_src( self::$sample_svg, 'thumbnail' );
188+
$this->assertStringContainsString( 'w:auto/h:auto', $attachement_url[0] );
189+
190+
}
183191
public function test_image_tags() {
184192
$settings = new Optml_Settings();
185193
$settings->update( 'limit_dimensions', 'disabled' );

0 commit comments

Comments
 (0)