Skip to content

Commit fc18717

Browse files
authored
Merge pull request #804 from Codeinwp/fix/offload-url-replacement
fix: offloading URL replacement not happening
2 parents 52267f5 + 4aec1f2 commit fc18717

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

inc/url_replacer.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
266266
return $this->get_dam_url( $image );
267267
}
268268

269+
$offloaded_id = $this->is_offloaded_url( $image->getSource() );
270+
271+
if ( $offloaded_id !== 0 ) {
272+
return $this->get_offloaded_url( $offloaded_id, $image->getUrl(), $image->getSource() );
273+
}
274+
269275
return $image->getUrl();
270276
}
271277

@@ -366,4 +372,46 @@ private function get_dam_url( Image $image ) {
366372
private function is_dam_url( $url ) {
367373
return is_string( $url ) && strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
368374
}
375+
376+
/**
377+
* Check if the URL is offloaded.
378+
*
379+
* @param string $source_url The source image URL.
380+
*
381+
* @return int
382+
*/
383+
private function is_offloaded_url( $source_url ) {
384+
$attachment_id = 0;
385+
386+
if ( strpos( $source_url, Optml_Media_Offload::KEYS['not_processed_flag'] ) !== false ) {
387+
$attachment_id = (int) Optml_Media_Offload::get_attachment_id_from_url( $source_url );
388+
} else {
389+
$attachment_id = $this->attachment_url_to_post_id( $source_url );
390+
}
391+
392+
if ( $attachment_id === 0 ) {
393+
return 0;
394+
}
395+
396+
if ( ! $this->is_completed_offload( $attachment_id ) ) {
397+
return 0;
398+
}
399+
400+
return (int) $attachment_id;
401+
}
402+
403+
/**
404+
* Get the offloaded URL for an image.
405+
*
406+
* @param int $id The attachment ID.
407+
* @param string $optimized_url The optimized image URL.
408+
* @param string $source_url The source image URL.
409+
*
410+
* @return string
411+
*/
412+
private function get_offloaded_url( $id, $optimized_url, $source_url ) {
413+
$suffix = wp_get_attachment_metadata( $id )['file'];
414+
415+
return str_replace( $source_url, ltrim( $suffix, '/' ), $optimized_url );
416+
}
369417
}

tests/test-media.php renamed to tests/Test_Media.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,11 @@ public function test_filter_saved_data() {
401401
$this->assertStringContainsString('300x300.jpg', $replaced['post_content'] );
402402
}
403403
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 );
404+
$attachment = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/'.self::$files[0].'.jpg' );
405+
$url = Optml_Media_Offload::get_original_url( $attachment );
406+
$this->assertEquals( $this->attachment_url_to_post_id( $url ), $attachment );
408407
$url = str_replace('example.org', 'www.example.org', $url);
409-
$this->assertEquals( $this->attachment_url_to_post_id( $url ), self::$sample_attachement );
408+
$this->assertEquals( $this->attachment_url_to_post_id( $url ), $attachment );
410409

411410
$scaled_url = Optml_Media_Offload::get_original_url( self::$sample_attachment_scaled );
412411

0 commit comments

Comments
 (0)