Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion php/sync/class-upload-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ public function edit_upload( $attachment_id ) {
* @param int $attachment_id The attachment ID.
* @param string|null $type Optional Sync type.
* @param string|null $suffix An optional suffix.
* @param bool $overwrite Whether to overwrite an existing Cloudinary asset.
*
* @return array|\WP_Error
*/
public function upload_asset( $attachment_id, $type = null, $suffix = null ) {
public function upload_asset( $attachment_id, $type = null, $suffix = null, $overwrite = false ) {

add_filter( 'cloudinary_doing_upload', '__return_true' );

Expand Down Expand Up @@ -340,6 +341,9 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {
remove_filter( 'wp_get_original_image_path', array( $this, 'filter_backup_original' ), 10 );
break;
default:
if ( $overwrite ) {
$options['overwrite'] = true;
}
$result = $this->connect->api->upload( $attachment_id, $options, array() );
break;
}
Expand All @@ -349,6 +353,11 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) {

// Check that this wasn't an existing.
if ( ! empty( $result['existing'] ) ) {
// If no public_id is recorded in WordPress, this asset in Cloudinary is from a
// failed previous upload. Overwrite it instead of creating a suffixed duplicate.
if ( empty( $suffix ) && ! $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) ) {
return $this->upload_asset( $attachment_id, $type, null, true );
}
// Add a suffix and try again.
$suffix = '_' . $attachment_id . substr( strrev( uniqid() ), 0, 5 );

Expand Down
Loading