Skip to content

Commit

Permalink
Add support for external images replacement in editor if the domain i…
Browse files Browse the repository at this point in the history
…s allowed.
  • Loading branch information
selul committed Aug 8, 2024
1 parent 1a3d048 commit b09a584
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions inc/media_offload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion inc/traits/dam_offload_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
10 changes: 9 additions & 1 deletion tests/test-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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( '<img src="%s" />', $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' => [
Expand Down

0 comments on commit b09a584

Please sign in to comment.