Skip to content

Commit

Permalink
Compatibility issue with WordPress 5.3 #406
Browse files Browse the repository at this point in the history
  • Loading branch information
Md. Alimuzzaman Alim committed Nov 8, 2019
1 parent 2ed064c commit 6ed9a6a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/classes/class-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public function init() {
add_filter( 'wp_stateless_bucket_link', array( $this, 'wp_stateless_bucket_link' ) );
}

if($this->get( 'sm.mode' ) === 'stateless'){
// Store attachment id in a static variable on 'intermediate_image_sizes_advanced' filter.
// Utility::store_can_delete_attachment();
if(is_wp_version_compatible('5.3-RC4-46673')){
add_filter( 'intermediate_image_sizes_advanced', array($this, 'store_can_delete_attachment'), 10, 3 );
}
}

add_filter( 'wp_stateless_file_name', array( $this, 'handle_root_dir' ), 10, 2 );

/**
Expand Down
42 changes: 40 additions & 2 deletions lib/classes/class-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class Utility {

static $can_delete_attachment = [];

/**
* ChromeLogger
*
Expand Down Expand Up @@ -263,7 +265,7 @@ public static function add_media( $metadata, $attachment_id, $force = false, $ar
// also skips full size image if already uploaded using that feature.
// and delete it in stateless mode as it already bin uploaded through intermediate_image_sizes_advanced filter.
if( !$img['is_thumb'] && $stateless_synced_full_size == $attachment_id ){
if(ud_get_stateless_media()->get( 'sm.mode' ) === 'stateless' && $args['no_thumb'] != true){
if(ud_get_stateless_media()->get( 'sm.mode' ) === 'stateless' && $args['no_thumb'] != true && \file_exists($img['path'])){
unlink($img['path']);
}
continue;
Expand Down Expand Up @@ -303,7 +305,7 @@ public static function add_media( $metadata, $attachment_id, $force = false, $ar

// Stateless mode: we don't need the local version.
// Except when uploading the full size image first.
if(ud_get_stateless_media()->get( 'sm.mode' ) === 'stateless' && $args['no_thumb'] != true){
if(self::can_delete_attachment($attachment_id, $args)){
unlink($img['path']);
}
}
Expand Down Expand Up @@ -539,6 +541,42 @@ public function add_webp_mime($t, $user){
return $t;
}

/**
* Store attachment id in a static variable on 'intermediate_image_sizes_advanced' filter.
* To indicate that we can now delete attachment from server now.
*
* @param array $new_sizes
* @param array $image_meta
* @param int $attachment_id
* @return array $new_sizes
*/
public static function store_can_delete_attachment( $new_sizes, $image_meta, $attachment_id ){
if( !in_array($attachment_id, self::$can_delete_attachment)){
self::$can_delete_attachment[] = $attachment_id;
}
return $new_sizes;
}

/**
* Check whether to delete attachment from server or not.
*
* @param int $attachment_id
* @return boolean
*/
public static function can_delete_attachment($attachment_id, $args){
if(
ud_get_stateless_media()->get( 'sm.mode' ) === 'stateless' &&
$args['no_thumb'] != true
){
// checks whether it's WP 5.3 and 'intermediate_image_sizes_advanced' is passed.
// To be sure that we don't delete full size image before thumbnails are generated.
if(is_wp_version_compatible('5.3-RC4-46673') && !in_array($attachment_id, self::$can_delete_attachment)){
return false;
}
return true;
}
return false;
}

}

Expand Down

0 comments on commit 6ed9a6a

Please sign in to comment.