From bf6441d2ad6b63c8411c7ea9341bea2d9ff75ddc Mon Sep 17 00:00:00 2001
From: RaduCristianPopescu
<119046336+RaduCristianPopescu@users.noreply.github.com>
Date: Mon, 29 Sep 2025 17:10:06 +0300
Subject: [PATCH 1/3] feat: increase the visibility of rename/replace
---
assets/js/modal-attachment.js | 101 +++++++++++++++++++++++
inc/media_rename/attachment_edit.php | 115 +++++++++++++++++++--------
2 files changed, 181 insertions(+), 35 deletions(-)
create mode 100644 assets/js/modal-attachment.js
diff --git a/assets/js/modal-attachment.js b/assets/js/modal-attachment.js
new file mode 100644
index 000000000..4cfe57a53
--- /dev/null
+++ b/assets/js/modal-attachment.js
@@ -0,0 +1,101 @@
+jQuery(document).ready(function($) {
+ if (!$('body').hasClass('upload-php')) {
+ return;
+ }
+
+ var originalAttachmentDetails = wp.media.view.Attachment.Details;
+
+ wp.media.view.Attachment.Details = originalAttachmentDetails.extend({
+ initialize: function() {
+ originalAttachmentDetails.prototype.initialize.apply(this, arguments);
+ },
+
+ render: function() {
+ originalAttachmentDetails.prototype.render.apply(this, arguments);
+
+ this.addReplaceRenameButton();
+
+ return this;
+ },
+
+ addReplaceRenameButton: function() {
+ var self = this;
+
+ var $el = self.$el;
+ var $actions = $el.find('.actions');
+
+ if ($actions.find('.optml-replace-rename-link').length) {
+ return;
+ }
+
+ var attachmentId = self.model.get('id');
+
+ if (attachmentId && $actions.length) {
+ var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
+ '&action=edit&TB_iframe=true&width=90%&height=90%';
+
+ var $editLink = $actions.find('a[href*="post.php"]');
+
+ if ($editLink.length) {
+ $editLink.after(
+ ' |' +
+ ' ' +
+ OptimoleModalAttachment.i18n.replaceOrRename + ''
+ );
+ }
+ }
+ }
+ });
+
+ if (wp.media.view.Attachment.Details.TwoColumn) {
+ var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn;
+
+ wp.media.view.Attachment.Details.TwoColumn = originalTwoColumn.extend({
+ initialize: function() {
+ originalTwoColumn.prototype.initialize.apply(this, arguments);
+ },
+
+ render: function() {
+ originalTwoColumn.prototype.render.apply(this, arguments);
+
+ this.addReplaceRenameButton();
+
+ return this;
+ },
+
+ addReplaceRenameButton: function() {
+ var self = this;
+
+ var $el = self.$el;
+ var $actions = $el.find('.actions');
+
+ if ($actions.find('.optml-replace-rename-link').length) {
+ return;
+ }
+
+ var attachmentId = self.model.get('id');
+
+ if (attachmentId && $actions.length) {
+ var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
+ '&action=edit&TB_iframe=true&width=90%&height=90%';
+
+ var $editLink = $actions.find('a[href*="post.php"]');
+
+ if ($editLink.length) {
+ $editLink.after(
+ ' |' +
+ ' ' +
+ OptimoleModalAttachment.i18n.replaceOrRename + ''
+ );
+ }
+ }
+ }
+ });
+ }
+
+ $(document).on('click', '.optml-replace-rename-link.thickbox', function() {
+ tb_init('a.thickbox');
+ });
+});
\ No newline at end of file
diff --git a/inc/media_rename/attachment_edit.php b/inc/media_rename/attachment_edit.php
index 57b0fead5..c54d2b726 100644
--- a/inc/media_rename/attachment_edit.php
+++ b/inc/media_rename/attachment_edit.php
@@ -24,6 +24,30 @@ public function init() {
add_action( 'wp_ajax_optml_replace_file', [ $this, 'replace_file' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
+ add_filter( 'media_row_actions', [ $this, 'add_replace_rename_action' ], 10, 2 );
+ }
+
+ /**
+ * Add Replace or Rename action in media library list view.
+ *
+ * @param array $actions The list of actions.
+ * @param WP_Post $post The post object.
+ * @return array
+ */
+ public function add_replace_rename_action( $actions, $post ) {
+ if ( get_post_type( $post->ID ) !== 'attachment' ) {
+ return $actions;
+ }
+
+ $edit_url = admin_url( 'post.php?post=' . $post->ID . '&action=edit' );
+ $actions['replace_rename'] = sprintf(
+ '%s',
+ esc_url( $edit_url ),
+ esc_attr__( 'Replace or Rename', 'optimole-wp' ),
+ esc_html__( 'Replace or Rename', 'optimole-wp' )
+ );
+
+ return $actions;
}
/**
@@ -32,48 +56,69 @@ public function init() {
* @param string $hook The hook.
*/
public function enqueue_scripts( $hook ) {
- if ( $hook !== 'post.php' ) {
+ if ( $hook !== 'post.php' && $hook !== 'upload.php' ) {
return;
}
- $id = (int) sanitize_text_field( $_GET['post'] );
+ if ( $hook === 'post.php' ) {
+ $id = (int) sanitize_text_field( $_GET['post'] );
- if ( ! $id ) {
- return;
- }
+ if ( ! $id ) {
+ return;
+ }
- if ( ! current_user_can( 'edit_post', $id ) ) {
- return;
- }
+ if ( ! current_user_can( 'edit_post', $id ) ) {
+ return;
+ }
- if ( get_post_type( $id ) !== 'attachment' ) {
- return;
- }
+ if ( get_post_type( $id ) !== 'attachment' ) {
+ return;
+ }
- $mime_type = get_post_mime_type( $id );
-
- $max_file_size = wp_max_upload_size();
- // translators: %s is the max file size in MB.
- $max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole-wp' ), $max_file_size / 1024 / 1024 );
-
- wp_enqueue_style( 'optml-attachment-edit', OPTML_URL . 'assets/css/single-attachment.css', [], OPTML_VERSION );
-
- wp_register_script( 'optml-attachment-edit', OPTML_URL . 'assets/js/single-attachment.js', [ 'jquery' ], OPTML_VERSION, true );
- wp_localize_script(
- 'optml-attachment-edit',
- 'OMAttachmentEdit',
- [
- 'ajaxURL' => admin_url( 'admin-ajax.php' ),
- 'maxFileSize' => $max_file_size,
- 'attachmentId' => $id,
- 'mimeType' => $mime_type,
- 'i18n' => [
- 'maxFileSizeError' => $max_file_size_error,
- 'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
- ],
- ]
- );
- wp_enqueue_script( 'optml-attachment-edit' );
+ $mime_type = get_post_mime_type( $id );
+
+ $max_file_size = wp_max_upload_size();
+ // translators: %s is the max file size in MB.
+ $max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole-wp' ), $max_file_size / 1024 / 1024 );
+
+ wp_enqueue_style( 'optml-attachment-edit', OPTML_URL . 'assets/css/single-attachment.css', [], OPTML_VERSION );
+
+ wp_register_script( 'optml-attachment-edit', OPTML_URL . 'assets/js/single-attachment.js', [ 'jquery' ], OPTML_VERSION, true );
+ wp_localize_script(
+ 'optml-attachment-edit',
+ 'OMAttachmentEdit',
+ [
+ 'ajaxURL' => admin_url( 'admin-ajax.php' ),
+ 'maxFileSize' => $max_file_size,
+ 'attachmentId' => $id,
+ 'mimeType' => $mime_type,
+ 'i18n' => [
+ 'maxFileSizeError' => $max_file_size_error,
+ 'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
+ ],
+ ]
+ );
+ wp_enqueue_script( 'optml-attachment-edit' );
+ } elseif ( $hook === 'upload.php' ) {
+ wp_enqueue_script(
+ 'optml-modal-attachment',
+ OPTML_URL . 'assets/js/modal-attachment.js', // Your JavaScript file
+ [ 'jquery', 'media-views', 'media-models' ],
+ OPTML_VERSION,
+ true
+ );
+
+ wp_localize_script(
+ 'optml-modal-attachment',
+ 'OptimoleModalAttachment',
+ [
+ 'editPostURL' => admin_url( 'post.php' ),
+ 'i18n' => [
+ 'replaceOrRename' => __( 'Replace or Rename', 'optimole-wp' ),
+ ],
+ ]
+ );
+ }
}
/**
From bd82983a5cc7231d6adc3e9f6d7281053524f0f6 Mon Sep 17 00:00:00 2001
From: RaduCristianPopescu
<119046336+RaduCristianPopescu@users.noreply.github.com>
Date: Mon, 29 Sep 2025 17:22:16 +0300
Subject: [PATCH 2/3] chore: copilot suggestions
---
assets/js/modal-attachment.js | 126 ++++++++++-----------------
inc/media_rename/attachment_edit.php | 6 +-
2 files changed, 50 insertions(+), 82 deletions(-)
diff --git a/assets/js/modal-attachment.js b/assets/js/modal-attachment.js
index 4cfe57a53..c4471c28c 100644
--- a/assets/js/modal-attachment.js
+++ b/assets/js/modal-attachment.js
@@ -3,97 +3,65 @@ jQuery(document).ready(function($) {
return;
}
- var originalAttachmentDetails = wp.media.view.Attachment.Details;
-
- wp.media.view.Attachment.Details = originalAttachmentDetails.extend({
- initialize: function() {
- originalAttachmentDetails.prototype.initialize.apply(this, arguments);
- },
+ /**
+ * Helper function to add Replace or Rename button to attachment actions
+ * @param {Object} view - The attachment view instance
+ */
+ function addReplaceRenameButton(view) {
+ var $el = view.$el;
+ var $actions = $el.find('.actions');
- render: function() {
- originalAttachmentDetails.prototype.render.apply(this, arguments);
-
- this.addReplaceRenameButton();
-
- return this;
- },
+ if (!$actions.length || $actions.find('.optml-replace-rename-link').length) {
+ return;
+ }
- addReplaceRenameButton: function() {
- var self = this;
-
- var $el = self.$el;
- var $actions = $el.find('.actions');
-
- if ($actions.find('.optml-replace-rename-link').length) {
- return;
- }
-
- var attachmentId = self.model.get('id');
-
- if (attachmentId && $actions.length) {
- var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
- '&action=edit&TB_iframe=true&width=90%&height=90%';
-
- var $editLink = $actions.find('a[href*="post.php"]');
-
- if ($editLink.length) {
- $editLink.after(
- ' |' +
- ' ' +
- OptimoleModalAttachment.i18n.replaceOrRename + ''
- );
- }
- }
+ var attachmentId = view.model.get('id');
+
+ if (!attachmentId) {
+ return;
}
- });
-
- if (wp.media.view.Attachment.Details.TwoColumn) {
- var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn;
- wp.media.view.Attachment.Details.TwoColumn = originalTwoColumn.extend({
+ var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
+ '&action=edit&TB_iframe=true&width=90%&height=90%';
+
+ var $editLink = $actions.find('a[href*="post.php"]');
+
+ if ($editLink.length) {
+ $editLink.after(
+ ' |' +
+ ' ' +
+ OptimoleModalAttachment.i18n.replaceOrRename + ''
+ );
+ }
+ }
+
+ /**
+ * Extend a WordPress media view with Replace/Rename functionality
+ * @param {Object} OriginalView - The original view to extend
+ * @returns {Object} Extended view
+ */
+ function extendMediaView(OriginalView) {
+ return OriginalView.extend({
initialize: function() {
- originalTwoColumn.prototype.initialize.apply(this, arguments);
+ OriginalView.prototype.initialize.apply(this, arguments);
},
render: function() {
- originalTwoColumn.prototype.render.apply(this, arguments);
-
- this.addReplaceRenameButton();
-
+ OriginalView.prototype.render.apply(this, arguments);
+ addReplaceRenameButton(this);
return this;
- },
-
- addReplaceRenameButton: function() {
- var self = this;
-
- var $el = self.$el;
- var $actions = $el.find('.actions');
-
- if ($actions.find('.optml-replace-rename-link').length) {
- return;
- }
-
- var attachmentId = self.model.get('id');
-
- if (attachmentId && $actions.length) {
- var editUrl = OptimoleModalAttachment.editPostURL + '?post=' + attachmentId +
- '&action=edit&TB_iframe=true&width=90%&height=90%';
-
- var $editLink = $actions.find('a[href*="post.php"]');
-
- if ($editLink.length) {
- $editLink.after(
- ' |' +
- ' ' +
- OptimoleModalAttachment.i18n.replaceOrRename + ''
- );
- }
- }
}
});
}
+
+ var originalAttachmentDetails = wp.media.view.Attachment.Details;
+ wp.media.view.Attachment.Details = extendMediaView(originalAttachmentDetails);
+
+ if (wp.media.view.Attachment.Details.TwoColumn) {
+ var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn;
+ wp.media.view.Attachment.Details.TwoColumn = extendMediaView(originalTwoColumn);
+ }
$(document).on('click', '.optml-replace-rename-link.thickbox', function() {
tb_init('a.thickbox');
diff --git a/inc/media_rename/attachment_edit.php b/inc/media_rename/attachment_edit.php
index c54d2b726..d3c496916 100644
--- a/inc/media_rename/attachment_edit.php
+++ b/inc/media_rename/attachment_edit.php
@@ -30,9 +30,9 @@ public function init() {
/**
* Add Replace or Rename action in media library list view.
*
- * @param array $actions The list of actions.
- * @param WP_Post $post The post object.
- * @return array
+ * @param string[] $actions Array of row action links.
+ * @param WP_Post $post The post object.
+ * @return string[]
*/
public function add_replace_rename_action( $actions, $post ) {
if ( get_post_type( $post->ID ) !== 'attachment' ) {
From 339101731f4a6b5782ed4178f36bdce16ab4dce4 Mon Sep 17 00:00:00 2001
From: RaduCristianPopescu
<119046336+RaduCristianPopescu@users.noreply.github.com>
Date: Tue, 30 Sep 2025 11:46:32 +0300
Subject: [PATCH 3/3] chore: remove comment
---
inc/media_rename/attachment_edit.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/inc/media_rename/attachment_edit.php b/inc/media_rename/attachment_edit.php
index d3c496916..0663df3bf 100644
--- a/inc/media_rename/attachment_edit.php
+++ b/inc/media_rename/attachment_edit.php
@@ -102,7 +102,7 @@ public function enqueue_scripts( $hook ) {
} elseif ( $hook === 'upload.php' ) {
wp_enqueue_script(
'optml-modal-attachment',
- OPTML_URL . 'assets/js/modal-attachment.js', // Your JavaScript file
+ OPTML_URL . 'assets/js/modal-attachment.js',
[ 'jquery', 'media-views', 'media-models' ],
OPTML_VERSION,
true