@@ -163,17 +163,18 @@ public function handle_bulk_actions( $location, $action, $post_ids ) {
163163
164164 // It's required to perform a new sync that Cloudinary and WordPress storage is set.
165165 if (
166- 'dual_full ' !== $ this ->plugin ->settings ->find_setting ( 'offload ' )->get_value () &&
167- $ this ->plugin ->components ['sync ' ]->is_synced ( $ post_id )
166+ 'dual_full ' !== $ this ->plugin ->settings ->find_setting ( 'offload ' )->get_value ()
168167 ) {
169168 continue ;
170169 }
171170
172171 // Clean up for previous syncs and start over.
173- $ this ->sync ->delete_cloudinary_meta ( $ post_id );
174- $ this ->sync ->set_signature_item ( $ post_id , 'file ' , '' );
175- $ this ->media ->delete_post_meta ( $ post_id , Sync::META_KEYS ['public_id ' ] );
176- $ this ->sync ->add_to_sync ( $ post_id );
172+ if ( ! $ this ->media ->is_cloudinary_url ( get_post_meta ( $ post_id , '_wp_attached_file ' , true ) ) ) {
173+ $ this ->sync ->delete_cloudinary_meta ( $ post_id );
174+ $ this ->sync ->set_signature_item ( $ post_id , 'file ' , '' );
175+ $ this ->media ->delete_post_meta ( $ post_id , Sync::META_KEYS ['public_id ' ] );
176+ $ this ->sync ->add_to_sync ( $ post_id );
177+ }
177178 }
178179 break ;
179180 }
@@ -218,11 +219,12 @@ public function setup() {
218219 /**
219220 * Upload an asset to Cloudinary.
220221 *
221- * @param int $attachment_id The attachment ID.
222+ * @param int $attachment_id The attachment ID.
223+ * @param string $suffix An optional suffix.
222224 *
223225 * @return array|\WP_Error
224226 */
225- public function upload_asset ( $ attachment_id ) {
227+ public function upload_asset ( $ attachment_id, $ suffix = null ) {
226228
227229 add_filter ( 'cloudinary_doing_upload ' , '__return_true ' );
228230
@@ -241,17 +243,10 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {
241243
242244 $ type = $ this ->sync ->get_sync_type ( $ attachment_id );
243245 $ options = $ this ->media ->get_upload_options ( $ attachment_id );
244- $ public_id = $ options ['public_id ' ];
245246 $ try_remote = 'cloud_name ' !== $ type ;
246247
247- // Add the suffix before uploading.
248- if ( $ this ->media ->get_public_id ( $ attachment_id ) === $ public_id ) {
249- // Only apply the saved suffix if the public_id is the same. This is to allow filtered ID's a change to have a suffix removed.
250- $ options ['public_id ' ] .= $ this ->media ->get_post_meta ( $ attachment_id , Sync::META_KEYS ['suffix ' ], true );
251- } else {
252- // If the public_id has been changed, remove the saved suffix.
253- $ this ->media ->delete_post_meta ( $ attachment_id , Sync::META_KEYS ['suffix ' ] );
254- }
248+ // Add suffix.
249+ $ options ['public_id ' ] .= $ suffix ;
255250
256251 // Run the upload Call.
257252 $ result = $ this ->connect ->api ->upload ( $ attachment_id , $ options , array (), $ try_remote );
@@ -262,16 +257,10 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {
262257
263258 // Check that this wasn't an existing.
264259 if ( ! empty ( $ result ['existing ' ] ) ) {
265- // Check to see if this is the same image.
266- $ version = $ this ->media ->get_cloudinary_version ( $ attachment_id );
267- if ( $ version !== $ result ['version ' ] ) {
268- // New image with the same name.
269- // Add a suffix and try again.
270- $ suffix = '_ ' . $ attachment_id . substr ( strrev ( uniqid () ), 0 , 5 );
271- $ this ->media ->update_post_meta ( $ attachment_id , Sync::META_KEYS ['suffix ' ], $ suffix );
272-
273- return $ this ->upload_asset ( $ attachment_id );
274- }
260+ // Add a suffix and try again.
261+ $ suffix = '_ ' . $ attachment_id . substr ( strrev ( uniqid () ), 0 , 5 );
262+
263+ return $ this ->upload_asset ( $ attachment_id , $ suffix );
275264 }
276265
277266 // Set folder Synced.
0 commit comments