Skip to content

Commit c5fa223

Browse files
authored
Merge pull request #802 from Codeinwp/fixes/offloading
Fixes/offloading
2 parents eaaf4f6 + 12cd420 commit c5fa223

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

inc/attachment_cache.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ class Optml_Attachment_Cache {
1111
* @var array
1212
*/
1313
private static $cache_map = [];
14-
14+
/**
15+
* Reset the memory cache.
16+
*/
17+
public static function reset() {
18+
self::$cache_map = [];
19+
}
1520
/**
1621
* Get the cached attachment ID.
1722
*

inc/media_offload.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,20 +540,13 @@ public function get_local_attachement_id_from_url( $url ) {
540540

541541
$size = 'full';
542542
$found_size = $this->parse_dimensions_from_filename( $url );
543-
$strip_url = $url;
544-
$scaled_url = $url;
543+
$url = $this->add_schema( $url );
545544
if ( $found_size[0] !== false && $found_size[1] !== false ) {
546-
$size = $found_size;
547-
$strip_url = str_replace( '-' . $found_size[0] . 'x' . $found_size[1], '', $url );
548-
$scaled_url = str_replace( '-' . $found_size[0] . 'x' . $found_size[1], '-scaled', $url );
549-
}
550-
$strip_url = $this->add_schema( $strip_url );
545+
$size = $found_size;
551546

552-
$attachment_id = attachment_url_to_postid( $strip_url );
553-
if ( $attachment_id === 0 ) {
554-
$scaled_url = $this->add_schema( $scaled_url );
555-
$attachment_id = attachment_url_to_postid( $scaled_url );
556547
}
548+
$url = $this->add_schema( $url );
549+
$attachment_id = $this->attachment_url_to_post_id( $url );
557550

558551
return [ 'attachment_id' => $attachment_id, 'size' => $size ];
559552
}
@@ -813,7 +806,8 @@ public function upload_and_update_existing_images( $image_ids ) {
813806
foreach ( $image_ids as $id ) {
814807
if ( self::is_uploaded_image( wp_get_attachment_metadata( $id )['file'] ) ) {
815808
// if this meta flag below failed at the initial update but the file meta above is updated it will cause an infinite query loop
816-
update_post_meta( $id, 'optimole_offload', 'true' );
809+
update_post_meta( $id, self::META_KEYS['offloaded'], 'true' );
810+
update_post_meta( $id, self::OM_OFFLOADED_FLAG, true );
817811
$success_up ++;
818812
continue;
819813
}

inc/traits/dam_offload_utils.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,46 @@ private function get_scaled_url( $url ) {
223223
private function is_scaled_url( $url ) {
224224
return strpos( $url, '-scaled.' ) !== false;
225225
}
226+
227+
/**
228+
* Check if the image has been offloaded completely.
229+
*
230+
* @param int $id The attachment ID.
231+
*
232+
* @return bool
233+
*/
234+
private function is_completed_offload( $id ) {
235+
// This is the flag that is used to mark the image as offloaded at the end.
236+
$completed = ! empty( get_post_meta( $id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) );
237+
if ( $completed ) {
238+
return true;
239+
}
240+
// In some rare cases the image is offloaded but the flag is not set so we can alternatively check this using the file path.
241+
$meta = wp_get_attachment_metadata( $id );
242+
if ( ! isset( $meta['file'] ) ) {
243+
return false;
244+
}
245+
if ( Optml_Media_Offload::is_uploaded_image( $meta['file'] ) ) {
246+
return true;
247+
}
248+
249+
return false;
250+
}
226251
/**
227252
* Get the attachment ID from URL.
228253
*
229-
* @param string $url The attachment URL.
254+
* @param string $input_url The attachment URL.
230255
*
231256
* @return int
232257
*/
233-
private function attachment_url_to_post_id( $url ) {
234-
$cached = Optml_Attachment_Cache::get_cached_attachment_id( $url );
258+
private function attachment_url_to_post_id( $input_url ) {
259+
$cached = Optml_Attachment_Cache::get_cached_attachment_id( $input_url );
235260

236261
if ( $cached !== false ) {
237262
return $cached;
238263
}
239264

240-
$url = $this->strip_image_size( $url );
265+
$url = $this->strip_image_size( $input_url );
241266

242267
$attachment_id = attachment_url_to_postid( $url );
243268

@@ -265,7 +290,7 @@ private function attachment_url_to_post_id( $url ) {
265290
$attachment_id = attachment_url_to_postid( $scaled_url );
266291
}
267292
}
268-
Optml_Attachment_Cache::set_cached_attachment_id( $url, $attachment_id );
293+
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
269294

270295
return $attachment_id;
271296
}

tests/test-media.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ public function test_replace_alternative_domain(){
418418
$this->assertEquals( $this->attachment_url_to_post_id( $scaled_url ), self::$sample_attachment_scaled );
419419
}
420420
public function test_replace_urls_in_editor_content() {
421+
Optml_Attachment_Cache::reset();
421422
// Sample attachment:
422423
$original_url = Optml_Media_Offload::get_original_url( self::$sample_attachement );
423424
$extension = pathinfo( $original_url, PATHINFO_EXTENSION );

0 commit comments

Comments
 (0)