Skip to content

Commit bf6441d

Browse files
feat: increase the visibility of rename/replace
1 parent 387be28 commit bf6441d

File tree

2 files changed

+181
-35
lines changed

2 files changed

+181
-35
lines changed

assets/js/modal-attachment.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
jQuery(document).ready(function($) {
2+
if (!$('body').hasClass('upload-php')) {
3+
return;
4+
}
5+
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+
},
12+
13+
render: function() {
14+
originalAttachmentDetails.prototype.render.apply(this, arguments);
15+
16+
this.addReplaceRenameButton();
17+
18+
return this;
19+
},
20+
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+
}
48+
}
49+
});
50+
51+
if (wp.media.view.Attachment.Details.TwoColumn) {
52+
var originalTwoColumn = wp.media.view.Attachment.Details.TwoColumn;
53+
54+
wp.media.view.Attachment.Details.TwoColumn = originalTwoColumn.extend({
55+
initialize: function() {
56+
originalTwoColumn.prototype.initialize.apply(this, arguments);
57+
},
58+
59+
render: function() {
60+
originalTwoColumn.prototype.render.apply(this, arguments);
61+
62+
this.addReplaceRenameButton();
63+
64+
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+
}
94+
}
95+
});
96+
}
97+
98+
$(document).on('click', '.optml-replace-rename-link.thickbox', function() {
99+
tb_init('a.thickbox');
100+
});
101+
});

inc/media_rename/attachment_edit.php

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ public function init() {
2424
add_action( 'wp_ajax_optml_replace_file', [ $this, 'replace_file' ] );
2525

2626
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
27+
add_filter( 'media_row_actions', [ $this, 'add_replace_rename_action' ], 10, 2 );
28+
}
29+
30+
/**
31+
* Add Replace or Rename action in media library list view.
32+
*
33+
* @param array $actions The list of actions.
34+
* @param WP_Post $post The post object.
35+
* @return array
36+
*/
37+
public function add_replace_rename_action( $actions, $post ) {
38+
if ( get_post_type( $post->ID ) !== 'attachment' ) {
39+
return $actions;
40+
}
41+
42+
$edit_url = admin_url( 'post.php?post=' . $post->ID . '&action=edit' );
43+
$actions['replace_rename'] = sprintf(
44+
'<a href="%s" aria-label="%s">%s</a>',
45+
esc_url( $edit_url ),
46+
esc_attr__( 'Replace or Rename', 'optimole-wp' ),
47+
esc_html__( 'Replace or Rename', 'optimole-wp' )
48+
);
49+
50+
return $actions;
2751
}
2852

2953
/**
@@ -32,48 +56,69 @@ public function init() {
3256
* @param string $hook The hook.
3357
*/
3458
public function enqueue_scripts( $hook ) {
35-
if ( $hook !== 'post.php' ) {
59+
if ( $hook !== 'post.php' && $hook !== 'upload.php' ) {
3660
return;
3761
}
3862

39-
$id = (int) sanitize_text_field( $_GET['post'] );
63+
if ( $hook === 'post.php' ) {
64+
$id = (int) sanitize_text_field( $_GET['post'] );
4065

41-
if ( ! $id ) {
42-
return;
43-
}
66+
if ( ! $id ) {
67+
return;
68+
}
4469

45-
if ( ! current_user_can( 'edit_post', $id ) ) {
46-
return;
47-
}
70+
if ( ! current_user_can( 'edit_post', $id ) ) {
71+
return;
72+
}
4873

49-
if ( get_post_type( $id ) !== 'attachment' ) {
50-
return;
51-
}
74+
if ( get_post_type( $id ) !== 'attachment' ) {
75+
return;
76+
}
5277

53-
$mime_type = get_post_mime_type( $id );
54-
55-
$max_file_size = wp_max_upload_size();
56-
// translators: %s is the max file size in MB.
57-
$max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole-wp' ), $max_file_size / 1024 / 1024 );
58-
59-
wp_enqueue_style( 'optml-attachment-edit', OPTML_URL . 'assets/css/single-attachment.css', [], OPTML_VERSION );
60-
61-
wp_register_script( 'optml-attachment-edit', OPTML_URL . 'assets/js/single-attachment.js', [ 'jquery' ], OPTML_VERSION, true );
62-
wp_localize_script(
63-
'optml-attachment-edit',
64-
'OMAttachmentEdit',
65-
[
66-
'ajaxURL' => admin_url( 'admin-ajax.php' ),
67-
'maxFileSize' => $max_file_size,
68-
'attachmentId' => $id,
69-
'mimeType' => $mime_type,
70-
'i18n' => [
71-
'maxFileSizeError' => $max_file_size_error,
72-
'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
73-
],
74-
]
75-
);
76-
wp_enqueue_script( 'optml-attachment-edit' );
78+
$mime_type = get_post_mime_type( $id );
79+
80+
$max_file_size = wp_max_upload_size();
81+
// translators: %s is the max file size in MB.
82+
$max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole-wp' ), $max_file_size / 1024 / 1024 );
83+
84+
wp_enqueue_style( 'optml-attachment-edit', OPTML_URL . 'assets/css/single-attachment.css', [], OPTML_VERSION );
85+
86+
wp_register_script( 'optml-attachment-edit', OPTML_URL . 'assets/js/single-attachment.js', [ 'jquery' ], OPTML_VERSION, true );
87+
wp_localize_script(
88+
'optml-attachment-edit',
89+
'OMAttachmentEdit',
90+
[
91+
'ajaxURL' => admin_url( 'admin-ajax.php' ),
92+
'maxFileSize' => $max_file_size,
93+
'attachmentId' => $id,
94+
'mimeType' => $mime_type,
95+
'i18n' => [
96+
'maxFileSizeError' => $max_file_size_error,
97+
'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
98+
],
99+
]
100+
);
101+
wp_enqueue_script( 'optml-attachment-edit' );
102+
} elseif ( $hook === 'upload.php' ) {
103+
wp_enqueue_script(
104+
'optml-modal-attachment',
105+
OPTML_URL . 'assets/js/modal-attachment.js', // Your JavaScript file
106+
[ 'jquery', 'media-views', 'media-models' ],
107+
OPTML_VERSION,
108+
true
109+
);
110+
111+
wp_localize_script(
112+
'optml-modal-attachment',
113+
'OptimoleModalAttachment',
114+
[
115+
'editPostURL' => admin_url( 'post.php' ),
116+
'i18n' => [
117+
'replaceOrRename' => __( 'Replace or Rename', 'optimole-wp' ),
118+
],
119+
]
120+
);
121+
}
77122
}
78123

79124
/**

0 commit comments

Comments
 (0)