| 1 |
jQuery(document).ready(function($) { |
| 2 |
if (!$('body').hasClass('upload-php')) { |
| 3 |
return; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Helper function to add Replace or Rename button to attachment actions |
| 8 |
* @param {Object} view - The attachment view instance |
| 9 |
*/ |
| 10 |
function addReplaceRenameButton(view) { |
| 11 |
var $el = view.$el; |
| 12 |
var $actions = $el.find('.actions'); |
| 13 |
|
| 14 |
if (!$actions.length || $actions.find('.optml-replace-rename-link').length) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
var attachmentId = view.model.get('id'); |
| 19 |
|
| 20 |
if (!attachmentId) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId + |
| 25 |
'&action=edit&TB_iframe=true&width=90%&height=90%'; |
| 26 |
|
| 27 |
var $editLink = $actions.find('a[href*="post.php"]'); |
| 28 |
|
| 29 |
if ($editLink.length) { |
| 30 |
$editLink.after( |
| 31 |
' <span class="links-separator">|</span>' + |
| 32 |
'<a href="' + editUrl + '" class="optml-replace-rename-link thickbox" title="' + |
| 33 |
OptimoleModalAttachment.i18n.replaceOrRename + '"> ' + |
| 34 |
OptimoleModalAttachment.i18n.replaceOrRename + '</a>' |
| 35 |
); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Extend a WordPress media view with Replace/Rename functionality |
| 41 |
* @param {Object} OriginalView - The original view to extend |
| 42 |
* @returns {Object} Extended view |
| 43 |
*/ |
| 44 |
function extendMediaView(OriginalView) { |
| 45 |
return OriginalView.extend({ |
| 46 |
initialize: function() { |
| 47 |
OriginalView.prototype.initialize.apply(this, arguments); |
| 48 |
}, |
| 49 |
|
| 50 |
render: function() { |
| 51 |
OriginalView.prototype.render.apply(this, arguments); |
| 52 |
addReplaceRenameButton(this); |
| 53 |
return this; |
| 54 |
} |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
var originalAttachmentDetails = wp.media.view.Attachment.Details; |
| 59 |
wp.media.view.Attachment.Details = extendMediaView(originalAttachmentDetails); |
| 60 |
|
| 61 |
if (wp.media.view.Attachment.Details.TwoColumn) { |
| 62 |
var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn; |
| 63 |
wp.media.view.Attachment.Details.TwoColumn = extendMediaView(originalTwoColumn); |
| 64 |
} |
| 65 |
|
| 66 |
$(document).on('click', '.optml-replace-rename-link.thickbox', function() { |
| 67 |
tb_init('a.thickbox'); |
| 68 |
}); |
| 69 |
}); |