Skip to content

Commit ba82bc9

Browse files
authored
Merge pull request #485 from cloudinary/fix/suffix-fix
remove suffix version check. clean suffix
2 parents 5900a08 + d7fbe8e commit ba82bc9

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

php/class-media.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,13 @@ public function get_public_id( $attachment_id, $suffixed = false ) {
11061106
if ( $this->is_folder_synced( $attachment_id ) ) {
11071107
$public_id = $this->get_cloudinary_folder() . pathinfo( $public_id, PATHINFO_BASENAME );
11081108
}
1109-
if ( true === $suffixed ) {
1110-
$public_id .= $this->get_post_meta( $attachment_id, Sync::META_KEYS['suffix'], true );
1109+
if ( true === $suffixed && ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['suffix'], true ) ) ) {
1110+
$suffix = $this->get_post_meta( $attachment_id, Sync::META_KEYS['suffix'], true );
1111+
if ( false === strrpos( $public_id, $suffix ) ) {
1112+
$public_id .= $suffix;
1113+
$this->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
1114+
}
1115+
$this->delete_post_meta( $attachment_id, Sync::META_KEYS['suffix'] );
11111116
}
11121117
} else {
11131118
$public_id = $this->sync->generate_public_id( $attachment_id );

php/class-sync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function log_sync_result( $attachment_id, $type, $result ) {
203203
}
204204
if ( isset( $log[ $type ] ) ) {
205205

206-
$log[ $type ][ time() ] = $result;
206+
$log[ $type ][ '_' . time() ] = $result;
207207
if ( 5 < count( $log[ $type ] ) ) {
208208
array_shift( $log[ $type ] );
209209
}

php/sync/class-upload-sync.php

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)