Skip to content

Commit bd82983

Browse files
chore: copilot suggestions
1 parent bf6441d commit bd82983

File tree

2 files changed

+50
-82
lines changed

2 files changed

+50
-82
lines changed

assets/js/modal-attachment.js

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,65 @@ jQuery(document).ready(function($) {
33
return;
44
}
55

6-
var originalAttachmentDetails = wp.media.view.Attachment.Details;
7-
8-
wp.media.view.Attachment.Details = originalAttachmentDetails.extend({
9-
initialize: function() {
10-
originalAttachmentDetails.prototype.initialize.apply(this, arguments);
11-
},
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');
1213

13-
render: function() {
14-
originalAttachmentDetails.prototype.render.apply(this, arguments);
15-
16-
this.addReplaceRenameButton();
17-
18-
return this;
19-
},
14+
if (!$actions.length || $actions.find('.optml-replace-rename-link').length) {
15+
return;
16+
}
2017

21-
addReplaceRenameButton: function() {
22-
var self = this;
23-
24-
var $el = self.$el;
25-
var $actions = $el.find('.actions');
26-
27-
if ($actions.find('.optml-replace-rename-link').length) {
28-
return;
29-
}
30-
31-
var attachmentId = self.model.get('id');
32-
33-
if (attachmentId && $actions.length) {
34-
var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
35-
'&action=edit&TB_iframe=true&width=90%&height=90%';
36-
37-
var $editLink = $actions.find('a[href*="post.php"]');
38-
39-
if ($editLink.length) {
40-
$editLink.after(
41-
' <span class="links-separator">|</span>' +
42-
'<a href="' + editUrl + '" class="optml-replace-rename-link thickbox" title="' +
43-
OptimoleModalAttachment.i18n.replaceOrRename + '"> ' +
44-
OptimoleModalAttachment.i18n.replaceOrRename + '</a>'
45-
);
46-
}
47-
}
18+
var attachmentId = view.model.get('id');
19+
20+
if (!attachmentId) {
21+
return;
4822
}
49-
});
50-
51-
if (wp.media.view.Attachment.Details.TwoColumn) {
52-
var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn;
5323

54-
wp.media.view.Attachment.Details.TwoColumn = originalTwoColumn.extend({
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({
5546
initialize: function() {
56-
originalTwoColumn.prototype.initialize.apply(this, arguments);
47+
OriginalView.prototype.initialize.apply(this, arguments);
5748
},
5849

5950
render: function() {
60-
originalTwoColumn.prototype.render.apply(this, arguments);
61-
62-
this.addReplaceRenameButton();
63-
51+
OriginalView.prototype.render.apply(this, arguments);
52+
addReplaceRenameButton(this);
6453
return this;
65-
},
66-
67-
addReplaceRenameButton: function() {
68-
var self = this;
69-
70-
var $el = self.$el;
71-
var $actions = $el.find('.actions');
72-
73-
if ($actions.find('.optml-replace-rename-link').length) {
74-
return;
75-
}
76-
77-
var attachmentId = self.model.get('id');
78-
79-
if (attachmentId && $actions.length) {
80-
var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
81-
'&action=edit&TB_iframe=true&width=90%&height=90%';
82-
83-
var $editLink = $actions.find('a[href*="post.php"]');
84-
85-
if ($editLink.length) {
86-
$editLink.after(
87-
' <span class="links-separator">|</span>' +
88-
'<a href="' + editUrl + '" class="optml-replace-rename-link thickbox" title="' +
89-
OptimoleModalAttachment.i18n.replaceOrRename + '"> ' +
90-
OptimoleModalAttachment.i18n.replaceOrRename + '</a>'
91-
);
92-
}
93-
}
9454
}
9555
});
9656
}
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+
}
9765

9866
$(document).on('click', '.optml-replace-rename-link.thickbox', function() {
9967
tb_init('a.thickbox');

inc/media_rename/attachment_edit.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function init() {
3030
/**
3131
* Add Replace or Rename action in media library list view.
3232
*
33-
* @param array $actions The list of actions.
34-
* @param WP_Post $post The post object.
35-
* @return array
33+
* @param string[] $actions Array of row action links.
34+
* @param WP_Post $post The post object.
35+
* @return string[]
3636
*/
3737
public function add_replace_rename_action( $actions, $post ) {
3838
if ( get_post_type( $post->ID ) !== 'attachment' ) {

0 commit comments

Comments
 (0)