From 6ed9a6a9dc1ab8390b7dade3870dc078196494cf Mon Sep 17 00:00:00 2001 From: "Md. Alimuzzaman Alim" Date: Sat, 9 Nov 2019 02:50:05 +0600 Subject: [PATCH] Compatibility issue with WordPress 5.3 #406 --- lib/classes/class-bootstrap.php | 8 +++++++ lib/classes/class-utility.php | 42 +++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/classes/class-bootstrap.php b/lib/classes/class-bootstrap.php index 581aa035c..023af0249 100644 --- a/lib/classes/class-bootstrap.php +++ b/lib/classes/class-bootstrap.php @@ -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 ); /** diff --git a/lib/classes/class-utility.php b/lib/classes/class-utility.php index 9eab9446f..dbf1b7cf8 100644 --- a/lib/classes/class-utility.php +++ b/lib/classes/class-utility.php @@ -16,6 +16,8 @@ class Utility { + static $can_delete_attachment = []; + /** * ChromeLogger * @@ -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; @@ -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']); } } @@ -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; + } }