Skip to content

Commit b09a584

Browse files
committed
Add support for external images replacement in editor if the domain is allowed.
1 parent 1a3d048 commit b09a584

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

inc/media_offload.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,9 @@ public function replace_urls_in_editor_content( $content ) {
24552455

24562456
// No local attachment.
24572457
if ( $attachment['attachment_id'] === 0 ) {
2458+
if ( $this->can_replace_url( $url ) ) {
2459+
$to_replace[ $url ] = $this->get_optimized_image_url( $url, 'auto', 'auto' );
2460+
}
24582461
continue;
24592462
}
24602463

inc/traits/dam_offload_utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private function attachment_url_to_post_id( $input_url ) {
259259
$cached = Optml_Attachment_Cache::get_cached_attachment_id( $input_url );
260260

261261
if ( $cached !== false ) {
262-
return $cached;
262+
return (int) $cached;
263263
}
264264

265265
$url = $this->strip_image_size( $input_url );

tests/test-media.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function setUp() : void {
174174
$settings->update( 'service_data', [
175175
'cdn_key' => 'example',
176176
'cdn_secret' => 'test',
177-
'whitelist' => [ 'example.com', 'example.org' ],
177+
'whitelist' => [ 'example.com', 'example.org','external.com' ],
178178

179179
] );
180180
$settings->update( 'no_script', 'enabled' );
@@ -506,7 +506,15 @@ public function test_replace_urls_in_editor_content() {
506506
$this->assertStringNotContainsString( '-scaled.'.$extension, $replaced_content );
507507

508508
}
509+
public function test_replacement_editor_with_external_images(){
510+
Optml_Attachment_Cache::reset();
511+
$original_url = "https://external.com/image.png" ;
512+
$content_before_replace = sprintf( '<img src="%s" />', $original_url);
513+
$replaced_content = Optml_Media_Offload::instance()->replace_urls_in_editor_content( $content_before_replace );
509514

515+
$this->assertStringContainsString( $original_url, $replaced_content );
516+
$this->assertStringContainsString( 'i.optimole.com', $replaced_content );
517+
}
510518
public function test_alter_attachment_image_src() {
511519
$test_data = [
512520
'full' => [

0 commit comments

Comments
 (0)