@@ -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