Skip to content

Commit 30f800e

Browse files
committed
chore: fix phpunit
1 parent 0c94b7b commit 30f800e

File tree

11 files changed

+420
-230
lines changed

11 files changed

+420
-230
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ build
1010
.DS_Store
1111
cc-test-reporter
1212
assets/build
13-
test-results
13+
test-results
14+
tests/assets/filestash

inc/media_rename/attachment_edit.php

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function init() {
1919
add_filter( 'attachment_fields_to_save', [ $this, 'prepare_attachment_filename' ], 10, 2 );
2020

2121
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' ] );
2324
add_action( 'wp_ajax_optml_replace_file', [ $this, 'replace_file' ] );
2425

2526
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
@@ -62,11 +63,11 @@ public function enqueue_scripts( $hook ) {
6263
'optml-attachment-edit',
6364
'OMAttachmentEdit',
6465
[
65-
'ajaxURL' => admin_url( 'admin-ajax.php' ),
66-
'maxFileSize' => $max_file_size,
66+
'ajaxURL' => admin_url( 'admin-ajax.php' ),
67+
'maxFileSize' => $max_file_size,
6768
'attachmentId' => $id,
68-
'mimeType' => $mime_type,
69-
'i18n' => [
69+
'mimeType' => $mime_type,
70+
'i18n' => [
7071
'maxFileSizeError' => $max_file_size_error,
7172
'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
7273
],
@@ -80,6 +81,7 @@ public function enqueue_scripts( $hook ) {
8081
*
8182
* @param array $form_fields Array of form fields.
8283
* @param WP_Post $post The post object.
84+
*
8385
* @return array Modified form fields.
8486
*/
8587
public function add_attachment_fields( $form_fields, $post ) {
@@ -102,25 +104,25 @@ public function add_attachment_fields( $form_fields, $post ) {
102104
$form_fields['optml_rename_file'] = [
103105
'label' => __( 'Rename attached file', 'optimole-wp' ),
104106
'input' => 'html',
105-
'html' => $this->get_rename_field( $attachment ),
107+
'html' => $this->get_rename_field( $attachment ),
106108
];
107109

108110
$form_fields['optml_replace_file'] = [
109111
'label' => __( 'Replace file', 'optimole-wp' ),
110112
'input' => 'html',
111-
'html' => $this->get_replace_field( $attachment ),
113+
'html' => $this->get_replace_field( $attachment ),
112114
];
113115

114116
$form_fields['optml_footer_row'] = [
115117
'label' => '',
116118
'input' => 'html',
117-
'html' => $this->get_footer_html(),
119+
'html' => $this->get_footer_html(),
118120
];
119121

120122
$form_fields['optml_spacer_row'] = [
121123
'label' => '',
122124
'input' => 'html',
123-
'html' => '<div></div>',
125+
'html' => '<div></div>',
124126
];
125127

126128
return $form_fields;
@@ -135,7 +137,7 @@ public function add_attachment_fields( $form_fields, $post ) {
135137
*/
136138
private function get_rename_field( \Optml_Attachment_Model $attachment ) {
137139
$file_name_no_ext = $attachment->get_filename_no_ext();
138-
$file_ext = $attachment->get_extension();
140+
$file_ext = $attachment->get_extension();
139141

140142
$html = '';
141143

@@ -166,12 +168,12 @@ private function get_rename_field( \Optml_Attachment_Model $attachment ) {
166168
private function get_replace_field( \Optml_Attachment_Model $attachment ) {
167169
$file_ext = $attachment->get_extension();
168170
$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>';
175177

176178
$html .= '<input type="file" class="hidden" id="optml-replace-file-field" name="optml-replace-file-field" accept="' . implode( ',', $file_ext ) . '">';
177179

@@ -211,6 +213,7 @@ private function get_footer_html() {
211213
*
212214
* @param array $post_data Array of post data.
213215
* @param array $attachment Array of attachment data.
216+
*
214217
* @return array Modified post data.
215218
*/
216219
public function prepare_attachment_filename( array $post_data, array $attachment ) {
@@ -266,7 +269,7 @@ public function save_attachment_filename( $post_id ) {
266269
delete_post_meta( $post_id, '_optml_pending_rename' );
267270

268271
$renamer = new Optml_Attachment_Rename( $post_id, $new_filename );
269-
$status = $renamer->rename();
272+
$status = $renamer->rename();
270273

271274
if ( is_wp_error( $status ) ) {
272275
wp_die( $status->get_error_message() );
@@ -302,13 +305,33 @@ public function replace_file() {
302305
}
303306

304307
/**
305-
* Bust cached assets
308+
* Bust cached assets when an attachment is renamed.
306309
*
307310
* @param int $attachment_id The attachment ID.
308-
* @param string $new_url The new attachment URL.
309311
* @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
310333
*/
311-
public function bust_cached_assets( $attachment_id, $new_url, $old_url ) {
334+
private function clear_cache() {
312335
if (
313336
class_exists( '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server' ) &&
314337
is_callable( [ '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server', 'regenerate_styles' ] )

inc/media_rename/attachment_rename.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,27 @@ public function rename() {
9797

9898
try {
9999
$replacer = new Optml_Attachment_Db_Renamer();
100-
$count = $replacer->replace( $this->attachment->get_main_url(), $this->get_new_url( $new_unique_filename ) );
100+
$old_url = $this->attachment->get_main_url();
101+
$new_url = $this->get_new_url( $new_unique_filename );
102+
103+
$count = $replacer->replace( $old_url, $new_url );
101104

102105
if ( $count > 0 ) {
103106
/**
104107
* Action triggered after the attachment file is renamed.
105108
*
106109
* @param int $attachment_id Attachment ID.
107-
* @param string $new_url New attachment URL.
108110
* @param string $old_url Old attachment URL.
111+
* @param string $new_url New attachment URL.
109112
*/
110-
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $this->get_new_url( $new_unique_filename ), $this->attachment->get_main_url() );
113+
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $old_url, $new_url );
111114
}
112115
} catch ( Exception $e ) {
113116
return new WP_Error( 'optml_attachment_url_replace_failed', __( 'Error renaming file.', 'optimole-wp' ) );
114117
}
115118

119+
do_action( 'optml_attachment_renamed', $this->attachment_id );
120+
116121
return true;
117122
}
118123

inc/media_rename/attachment_replace.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ class Optml_Attachment_Replace {
4343
*/
4444
public function __construct( $attachment_id, $file ) {
4545
$this->attachment_id = $attachment_id;
46-
$this->file = $file;
47-
$this->attachment = new Optml_Attachment_Model( $attachment_id );
46+
$this->file = $file;
47+
$this->attachment = new Optml_Attachment_Model( $attachment_id );
48+
49+
if ( ! function_exists( 'WP_Filesystem' ) ) {
50+
require_once ABSPATH . 'wp-admin/includes/file.php';
51+
}
52+
53+
WP_Filesystem();
4854
}
4955

5056
/**
@@ -57,7 +63,7 @@ public function replace() {
5763
return new WP_Error( 'file_error', __( 'Error uploading file.', 'optimole-wp' ) );
5864
}
5965

60-
$original_file = $this->attachment->get_source_file_path();
66+
$original_file = $this->attachment->get_source_file_path();
6167
$old_sizes_urls = $this->attachment->get_all_image_sizes_urls();
6268

6369
if ( ! file_exists( $original_file ) ) {
@@ -71,7 +77,6 @@ public function replace() {
7177
return new WP_Error( 'file_error', __( 'The uploaded file type does not match the original file type.', 'optimole-wp' ) );
7278
}
7379

74-
$this->init_filesystem();
7580
global $wp_filesystem;
7681

7782
if ( ! $wp_filesystem->move( $this->file['tmp_name'], $original_file, true ) ) {
@@ -93,6 +98,8 @@ public function replace() {
9398

9499
$this->handle_scaled_images();
95100

101+
do_action( 'optml_attachment_replaced', $this->attachment_id );
102+
96103
return true;
97104
}
98105

@@ -122,10 +129,10 @@ private function handle_scaled_images() {
122129

123130
$old_scaled = $this->attachment->is_scaled();
124131
$new_scaled = $this->new_attachment->is_scaled();
125-
$replacer = new Optml_Attachment_Db_Renamer( true );
132+
$replacer = new Optml_Attachment_Db_Renamer( true );
126133

127134
$new_file_path = $this->new_attachment->get_source_file_path();
128-
$file = apply_filters( 'update_attached_file', $new_file_path, $this->attachment_id );
135+
$file = apply_filters( 'update_attached_file', $new_file_path, $this->attachment_id );
129136

130137
// New is scaled, but old is not scaled. We don't replace anything.
131138
if ( $old_scaled === $new_scaled || ( ! $old_scaled && $new_scaled ) ) {
@@ -134,10 +141,10 @@ private function handle_scaled_images() {
134141

135142
// Delete the old scaled version and replace scaled URLs with non-scaled URLs.
136143
if ( $old_scaled && ! $new_scaled ) {
137-
$main_file_url = $this->attachment->get_main_url();
138-
$unscaled_file = $this->attachment->get_filename_with_ext();
144+
$main_file_url = $this->attachment->get_main_url();
145+
$unscaled_file = $this->attachment->get_filename_with_ext();
139146
$old_scaled_file = $this->attachment->get_filename_with_ext( true );
140-
$old_scaled_url = str_replace( $unscaled_file, $old_scaled_file, $main_file_url );
147+
$old_scaled_url = str_replace( $unscaled_file, $old_scaled_file, $main_file_url );
141148

142149
$replacer->replace( $old_scaled_url, $main_file_url );
143150

@@ -158,6 +165,7 @@ private function handle_scaled_images() {
158165
*
159166
* @param array $new_sizes New sizes.
160167
* @param array $old_sizes_urls Old sizes URLs.
168+
*
161169
* @return void
162170
*/
163171
private function replace_image_sizes_links( $new_sizes, $old_sizes_urls ) {
@@ -175,17 +183,4 @@ private function replace_image_sizes_links( $new_sizes, $old_sizes_urls ) {
175183
$replacer->replace( $old_url, $new_url );
176184
}
177185
}
178-
179-
/**
180-
* Initialize filesystem.
181-
*
182-
* @return void
183-
*/
184-
private function init_filesystem() {
185-
if ( ! function_exists( 'WP_Filesystem' ) ) {
186-
require_once ABSPATH . 'wp-admin/includes/file.php';
187-
}
188-
189-
WP_Filesystem();
190-
}
191186
}

tests/assets/large-1.jpg

255 KB
Loading

tests/assets/large-2.jpg

571 KB
Loading

tests/assets/small-1.jpg

9.31 KB
Loading

tests/assets/small-2.jpg

7.73 KB
Loading

tests/media_rename/attachment_edit_utils.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)