I’m trying to create an option in the WP modal media window to detect duplicate files and delete a newer file if an older duplicate is found. I have the following code working (in conjunction with the ‘attachment_fields_to_edit’ filter) to deselect a duplicate file and select the original file in the media modal. What I’d like to do is, when a user clicks the button, delete the original file (or at least hide it in the media library window so I can delete it later).
( function( $ ) {
var _AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay;
wp.media.view.Settings.AttachmentDisplay = _AttachmentDisplay.extend({
render: function() {
_AttachmentDisplay.prototype.render.apply(this, arguments);
currentselection = this.controller.state().get('selection').first().toJSON();
selection = this.controller.state().get('selection');
$('button.dmc').on('click', function(e){
e.preventDefault();
var id = $(e.currentTarget).data("id");
if(currentselection.id == id) {
currentattachment = wp.media.attachment(id);
selection.remove(currentattachment);
console.dir(wp.media.view.Attachment);
newattachment = wp.media.attachment($(e.currentTarget).data("original"));
selection.add(newattachment);
}
});
}
});
} )( jQuery );
The interface looks like the attached image.
I can see in media-views.js at line 5873 that there’s a deleteAttachment function bound to ‘click .delete-attachment’. How can I access this, given my current setup, by passing in an image ID or attachment object?