From b09a584f57ffc52cf2020dc36e568e50e4f4c707 Mon Sep 17 00:00:00 2001 From: selul Date: Thu, 8 Aug 2024 17:27:20 +0300 Subject: [PATCH] Add support for external images replacement in editor if the domain is allowed. --- inc/media_offload.php | 3 +++ inc/traits/dam_offload_utils.php | 2 +- tests/test-media.php | 10 +++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/inc/media_offload.php b/inc/media_offload.php index 97cda880..57b0b9f0 100644 --- a/inc/media_offload.php +++ b/inc/media_offload.php @@ -2455,6 +2455,9 @@ public function replace_urls_in_editor_content( $content ) { // No local attachment. if ( $attachment['attachment_id'] === 0 ) { + if ( $this->can_replace_url( $url ) ) { + $to_replace[ $url ] = $this->get_optimized_image_url( $url, 'auto', 'auto' ); + } continue; } diff --git a/inc/traits/dam_offload_utils.php b/inc/traits/dam_offload_utils.php index a19374bf..e0027997 100644 --- a/inc/traits/dam_offload_utils.php +++ b/inc/traits/dam_offload_utils.php @@ -259,7 +259,7 @@ private function attachment_url_to_post_id( $input_url ) { $cached = Optml_Attachment_Cache::get_cached_attachment_id( $input_url ); if ( $cached !== false ) { - return $cached; + return (int) $cached; } $url = $this->strip_image_size( $input_url ); diff --git a/tests/test-media.php b/tests/test-media.php index 8d2ada1f..a3592433 100644 --- a/tests/test-media.php +++ b/tests/test-media.php @@ -174,7 +174,7 @@ public function setUp() : void { $settings->update( 'service_data', [ 'cdn_key' => 'example', 'cdn_secret' => 'test', - 'whitelist' => [ 'example.com', 'example.org' ], + 'whitelist' => [ 'example.com', 'example.org','external.com' ], ] ); $settings->update( 'no_script', 'enabled' ); @@ -506,7 +506,15 @@ public function test_replace_urls_in_editor_content() { $this->assertStringNotContainsString( '-scaled.'.$extension, $replaced_content ); } + public function test_replacement_editor_with_external_images(){ + Optml_Attachment_Cache::reset(); + $original_url = "https://external.com/image.png" ; + $content_before_replace = sprintf( '', $original_url); + $replaced_content = Optml_Media_Offload::instance()->replace_urls_in_editor_content( $content_before_replace ); + $this->assertStringContainsString( $original_url, $replaced_content ); + $this->assertStringContainsString( 'i.optimole.com', $replaced_content ); + } public function test_alter_attachment_image_src() { $test_data = [ 'full' => [