Skip to content

Commit 3796848

Browse files
authored
Merge pull request #973 from Codeinwp/refactor/inc_visibility_feat
feat: increase the visibility of rename/replace
2 parents 2963e8b + 3391017 commit 3796848

File tree

2 files changed

+149
-35
lines changed

2 files changed

+149
-35
lines changed

assets/js/modal-attachment.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
});

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 string[] $actions Array of row action links.
34+
* @param WP_Post $post The post object.
35+
* @return string[]
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',
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)