@@ -163,17 +163,19 @@ 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+ $ this ->plugin ->components [ ' sync ' ]-> is_synced ( $ post_id ) &&
167+ ' dual_full ' !== $ this ->plugin ->settings -> find_setting ( ' offload ' )-> get_value ( )
168168 ) {
169169 continue ;
170170 }
171171
172172 // 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 );
173+ if ( ! $ this ->media ->is_cloudinary_url ( get_post_meta ( $ post_id , '_wp_attached_file ' , true ) ) ) {
174+ $ this ->sync ->delete_cloudinary_meta ( $ post_id );
175+ $ this ->sync ->set_signature_item ( $ post_id , 'file ' , '' );
176+ $ this ->media ->delete_post_meta ( $ post_id , Sync::META_KEYS ['public_id ' ] );
177+ $ this ->sync ->add_to_sync ( $ post_id );
178+ }
177179 }
178180 break ;
179181 }
@@ -218,11 +220,12 @@ public function setup() {
218220 /**
219221 * Upload an asset to Cloudinary.
220222 *
221- * @param int $attachment_id The attachment ID.
223+ * @param int $attachment_id The attachment ID.
224+ * @param string $suffix An optional suffix.
222225 *
223226 * @return array|\WP_Error
224227 */
225- public function upload_asset ( $ attachment_id ) {
228+ public function upload_asset ( $ attachment_id, $ suffix = null ) {
226229
227230 add_filter ( 'cloudinary_doing_upload ' , '__return_true ' );
228231
@@ -241,17 +244,10 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {
241244
242245 $ type = $ this ->sync ->get_sync_type ( $ attachment_id );
243246 $ options = $ this ->media ->get_upload_options ( $ attachment_id );
244- $ public_id = $ options ['public_id ' ];
245247 $ try_remote = 'cloud_name ' !== $ type ;
246248
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- }
249+ // Add suffix.
250+ $ options ['public_id ' ] .= $ suffix ;
255251
256252 // Run the upload Call.
257253 $ result = $ this ->connect ->api ->upload ( $ attachment_id , $ options , array (), $ try_remote );
@@ -262,16 +258,10 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {
262258
263259 // Check that this wasn't an existing.
264260 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- }
261+ // Add a suffix and try again.
262+ $ suffix = '_ ' . $ attachment_id . substr ( strrev ( uniqid () ), 0 , 5 );
263+
264+ return $ this ->upload_asset ( $ attachment_id , $ suffix );
275265 }
276266
277267 // Set folder Synced.
0 commit comments