@@ -19,7 +19,8 @@ public function init() {
19
19
add_filter ( 'attachment_fields_to_save ' , [ $ this , 'prepare_attachment_filename ' ], 10 , 2 );
20
20
21
21
add_action ( 'edit_attachment ' , [ $ this , 'save_attachment_filename ' ] );
22
- add_action ( 'optml_after_attachment_url_replace ' , [ $ this , 'bust_cached_assets ' ], 10 , 3 );
22
+ add_action ( 'optml_after_attachment_url_replace ' , [ $ this , 'bust_cache_on_rename ' ], 10 , 3 );
23
+ add_action ( 'optml_attachment_replaced ' , [ $ this , 'bust_cache_on_replace ' ] );
23
24
add_action ( 'wp_ajax_optml_replace_file ' , [ $ this , 'replace_file ' ] );
24
25
25
26
add_action ( 'admin_enqueue_scripts ' , [ $ this , 'enqueue_scripts ' ] );
@@ -62,11 +63,11 @@ public function enqueue_scripts( $hook ) {
62
63
'optml-attachment-edit ' ,
63
64
'OMAttachmentEdit ' ,
64
65
[
65
- 'ajaxURL ' => admin_url ( 'admin-ajax.php ' ),
66
- 'maxFileSize ' => $ max_file_size ,
66
+ 'ajaxURL ' => admin_url ( 'admin-ajax.php ' ),
67
+ 'maxFileSize ' => $ max_file_size ,
67
68
'attachmentId ' => $ id ,
68
- 'mimeType ' => $ mime_type ,
69
- 'i18n ' => [
69
+ 'mimeType ' => $ mime_type ,
70
+ 'i18n ' => [
70
71
'maxFileSizeError ' => $ max_file_size_error ,
71
72
'replaceFileError ' => __ ( 'Error replacing file ' , 'optimole-wp ' ),
72
73
],
@@ -80,6 +81,7 @@ public function enqueue_scripts( $hook ) {
80
81
*
81
82
* @param array $form_fields Array of form fields.
82
83
* @param WP_Post $post The post object.
84
+ *
83
85
* @return array Modified form fields.
84
86
*/
85
87
public function add_attachment_fields ( $ form_fields , $ post ) {
@@ -102,25 +104,25 @@ public function add_attachment_fields( $form_fields, $post ) {
102
104
$ form_fields ['optml_rename_file ' ] = [
103
105
'label ' => __ ( 'Rename attached file ' , 'optimole-wp ' ),
104
106
'input ' => 'html ' ,
105
- 'html ' => $ this ->get_rename_field ( $ attachment ),
107
+ 'html ' => $ this ->get_rename_field ( $ attachment ),
106
108
];
107
109
108
110
$ form_fields ['optml_replace_file ' ] = [
109
111
'label ' => __ ( 'Replace file ' , 'optimole-wp ' ),
110
112
'input ' => 'html ' ,
111
- 'html ' => $ this ->get_replace_field ( $ attachment ),
113
+ 'html ' => $ this ->get_replace_field ( $ attachment ),
112
114
];
113
115
114
116
$ form_fields ['optml_footer_row ' ] = [
115
117
'label ' => '' ,
116
118
'input ' => 'html ' ,
117
- 'html ' => $ this ->get_footer_html (),
119
+ 'html ' => $ this ->get_footer_html (),
118
120
];
119
121
120
122
$ form_fields ['optml_spacer_row ' ] = [
121
123
'label ' => '' ,
122
124
'input ' => 'html ' ,
123
- 'html ' => '<div></div> ' ,
125
+ 'html ' => '<div></div> ' ,
124
126
];
125
127
126
128
return $ form_fields ;
@@ -135,7 +137,7 @@ public function add_attachment_fields( $form_fields, $post ) {
135
137
*/
136
138
private function get_rename_field ( \Optml_Attachment_Model $ attachment ) {
137
139
$ file_name_no_ext = $ attachment ->get_filename_no_ext ();
138
- $ file_ext = $ attachment ->get_extension ();
140
+ $ file_ext = $ attachment ->get_extension ();
139
141
140
142
$ html = '' ;
141
143
@@ -166,12 +168,12 @@ private function get_rename_field( \Optml_Attachment_Model $attachment ) {
166
168
private function get_replace_field ( \Optml_Attachment_Model $ attachment ) {
167
169
$ file_ext = $ attachment ->get_extension ();
168
170
$ file_ext = in_array ( $ file_ext , [ 'jpg ' , 'jpeg ' ], true ) ? [ '.jpg ' , '.jpeg ' ] : [ '. ' . $ file_ext ];
169
- $ html = '<div class="optml-replace-section"> ' ;
170
- $ html .= '<div class="optml-replace-input"> ' ;
171
- $ html .= '<label for="optml-replace-file-field" id="optml-file-drop-area"> ' ;
172
- $ html .= '<span class="label-text"> ' . __ ( 'Click to select a file or drag & drop here ' , 'optimole-wp ' ) . ' ( ' . implode ( ', ' , $ file_ext ) . ')</span> ' ;
173
- $ html .= '<div class="optml-replace-file-preview"></div> ' ;
174
- $ html .= '</label> ' ;
171
+ $ html = '<div class="optml-replace-section"> ' ;
172
+ $ html .= '<div class="optml-replace-input"> ' ;
173
+ $ html .= '<label for="optml-replace-file-field" id="optml-file-drop-area"> ' ;
174
+ $ html .= '<span class="label-text"> ' . __ ( 'Click to select a file or drag & drop here ' , 'optimole-wp ' ) . ' ( ' . implode ( ', ' , $ file_ext ) . ')</span> ' ;
175
+ $ html .= '<div class="optml-replace-file-preview"></div> ' ;
176
+ $ html .= '</label> ' ;
175
177
176
178
$ html .= '<input type="file" class="hidden" id="optml-replace-file-field" name="optml-replace-file-field" accept=" ' . implode ( ', ' , $ file_ext ) . '"> ' ;
177
179
@@ -211,6 +213,7 @@ private function get_footer_html() {
211
213
*
212
214
* @param array $post_data Array of post data.
213
215
* @param array $attachment Array of attachment data.
216
+ *
214
217
* @return array Modified post data.
215
218
*/
216
219
public function prepare_attachment_filename ( array $ post_data , array $ attachment ) {
@@ -266,7 +269,7 @@ public function save_attachment_filename( $post_id ) {
266
269
delete_post_meta ( $ post_id , '_optml_pending_rename ' );
267
270
268
271
$ renamer = new Optml_Attachment_Rename ( $ post_id , $ new_filename );
269
- $ status = $ renamer ->rename ();
272
+ $ status = $ renamer ->rename ();
270
273
271
274
if ( is_wp_error ( $ status ) ) {
272
275
wp_die ( $ status ->get_error_message () );
@@ -302,13 +305,33 @@ public function replace_file() {
302
305
}
303
306
304
307
/**
305
- * Bust cached assets
308
+ * Bust cached assets when an attachment is renamed.
306
309
*
307
310
* @param int $attachment_id The attachment ID.
308
- * @param string $new_url The new attachment URL.
309
311
* @param string $old_url The old attachment URL.
312
+ * @param string $new_url The new attachment URL.
313
+ */
314
+ public function bust_cache_on_rename ( $ attachment_id , $ old_url , $ new_url ) {
315
+ $ this ->clear_cache ();
316
+ }
317
+
318
+ /**
319
+ * Bust cached assets when an attachment is replaced.
320
+ *
321
+ * @param int $attachment_id The attachment ID.
322
+ *
323
+ * @return void
324
+ */
325
+ public function bust_cache_on_replace ( $ attachment_id ) {
326
+ $ this ->clear_cache ();
327
+ }
328
+
329
+ /**
330
+ * Clear the cache for third-party plugins.
331
+ *
332
+ * @return void
310
333
*/
311
- public function bust_cached_assets ( $ attachment_id , $ new_url , $ old_url ) {
334
+ private function clear_cache ( ) {
312
335
if (
313
336
class_exists ( '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server ' ) &&
314
337
is_callable ( [ '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server ' , 'regenerate_styles ' ] )
0 commit comments