Skip to content

Commit de322a2

Browse files
committed
chore: cleanup & add tests [wip]
1 parent 37f76ff commit de322a2

File tree

7 files changed

+336
-62
lines changed

7 files changed

+336
-62
lines changed

inc/media_rename/attachment_edit.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function enqueue_scripts( $hook ) {
5353

5454
$max_file_size = wp_max_upload_size();
5555
// translators: %s is the max file size in MB.
56-
$max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole' ), $max_file_size / 1024 / 1024 );
56+
$max_file_size_error = sprintf( __( 'File size is too large. Max file size is %sMB', 'optimole-wp' ), $max_file_size / 1024 / 1024 );
5757

5858
wp_enqueue_style( 'optml-attachment-edit', OPTML_URL . 'assets/css/single-attachment.css', [], OPTML_VERSION );
5959

@@ -68,7 +68,7 @@ public function enqueue_scripts( $hook ) {
6868
'mimeType' => $mime_type,
6969
'i18n' => [
7070
'maxFileSizeError' => $max_file_size_error,
71-
'replaceFileError' => __( 'Error replacing file', 'optimole' ),
71+
'replaceFileError' => __( 'Error replacing file', 'optimole-wp' ),
7272
],
7373
]
7474
);
@@ -100,13 +100,13 @@ public function add_attachment_fields( $form_fields, $post ) {
100100
}
101101

102102
$form_fields['optml_rename_file'] = [
103-
'label' => __( 'Rename attached file', 'optimole' ),
103+
'label' => __( 'Rename attached file', 'optimole-wp' ),
104104
'input' => 'html',
105105
'html' => $this->get_rename_field( $attachment ),
106106
];
107107

108108
$form_fields['optml_replace_file'] = [
109-
'label' => __( 'Replace file', 'optimole' ),
109+
'label' => __( 'Replace file', 'optimole-wp' ),
110110
'input' => 'html',
111111
'html' => $this->get_replace_field( $attachment ),
112112
];
@@ -146,7 +146,7 @@ private function get_rename_field( \Optml_Attachment_Model $attachment ) {
146146
$html .= '<span class="optml-file-ext">.' . esc_html( $file_ext ) . '</span>';
147147
$html .= '</div>';
148148

149-
$html .= '<button type="button" disabled class="button optml-btn primary" id="optml-rename-file-btn">' . __( 'Rename', 'optimole' ) . '</button>';
149+
$html .= '<button type="button" disabled class="button optml-btn primary" id="optml-rename-file-btn">' . __( 'Rename', 'optimole-wp' ) . '</button>';
150150
$html .= '</div>';
151151

152152
$html .= '<input type="hidden" name="optml_current_ext" value="' . esc_attr( $file_ext ) . '">';
@@ -169,17 +169,17 @@ private function get_replace_field( \Optml_Attachment_Model $attachment ) {
169169
$html = '<div class="optml-replace-section">';
170170
$html .= '<div class="optml-replace-input">';
171171
$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' ) . ' (' . implode( ',', $file_ext ) . ')</span>';
172+
$html .= '<span class="label-text">' . __( 'Click to select a file or drag & drop here', 'optimole-wp' ) . ' (' . implode( ',', $file_ext ) . ')</span>';
173173
$html .= '<div class="optml-replace-file-preview"></div>';
174174
$html .= '</label>';
175175

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

178178
$html .= '<div class="optml-replace-file-actions">';
179-
$html .= '<button type="button" class="button optml-btn primary" id="optml-replace-file-btn">' . __( 'Replace file', 'optimole' ) . '</button>';
180-
$html .= '<button type="button" class="button optml-btn destructive" id="optml-replace-clear-btn">' . __( 'Clear', 'optimole' ) . '</button>';
179+
$html .= '<button disabled type="button" class="button optml-btn primary" id="optml-replace-file-btn">' . __( 'Replace file', 'optimole-wp' ) . '</button>';
180+
$html .= '<button disabled type="button" class="button optml-btn destructive" id="optml-replace-clear-btn">' . __( 'Clear', 'optimole-wp' ) . '</button>';
181181
$html .= $this->get_svg_loader();
182-
$html .= '<p class="optml-description">' . __( 'This will replace the current file with the new one. This action cannot be undone.', 'optimole' ) . '</p>';
182+
$html .= '<p class="optml-description">' . __( 'This will replace the current file with the new one. This action cannot be undone.', 'optimole-wp' ) . '</p>';
183183
$html .= '</div>';
184184

185185
$html .= '<div class="optml-replace-file-error hidden"></div>';
@@ -197,9 +197,9 @@ private function get_replace_field( \Optml_Attachment_Model $attachment ) {
197197
private function get_footer_html() {
198198
$html = '';
199199
$html .= '<div class="optml-logo-contianer">';
200-
$html .= '<img src="' . OPTML_URL . 'assets/img/logo.svg" alt="' . __( 'Optimole logo', 'optimole' ) . '"/>';
200+
$html .= '<img src="' . OPTML_URL . 'assets/img/logo.svg" alt="' . __( 'Optimole logo', 'optimole-wp' ) . '"/>';
201201
// translators: %s is the 'Optimole'.
202-
$html .= '<span>' . sprintf( __( 'Powered by %s', 'optimole' ), '<strong>Optimole</strong>' ) . '</span>';
202+
$html .= '<span>' . sprintf( __( 'Powered by %s', 'optimole-wp' ), '<strong>Optimole</strong>' ) . '</span>';
203203
$html .= '</div>';
204204

205205
return $html;
@@ -218,6 +218,10 @@ public function prepare_attachment_filename( array $post_data, array $attachment
218218
return $post_data;
219219
}
220220

221+
if ( $post_data['post_type'] !== 'attachment' ) {
222+
return $post_data;
223+
}
224+
221225
if ( ! isset( $post_data['optml_rename_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $post_data['optml_rename_nonce'] ), 'optml_rename_media_nonce' ) ) {
222226
return $post_data;
223227
}
@@ -230,7 +234,7 @@ public function prepare_attachment_filename( array $post_data, array $attachment
230234
return $post_data;
231235
}
232236

233-
if ( strlen( $new_name ) < 3 || strlen( $new_name ) > 100 ) {
237+
if ( strlen( $new_name ) > 100 ) {
234238
return $post_data;
235239
}
236240

@@ -262,7 +266,11 @@ public function save_attachment_filename( $post_id ) {
262266
delete_post_meta( $post_id, '_optml_pending_rename' );
263267

264268
$renamer = new Optml_Attachment_Rename( $post_id, $new_filename );
265-
$renamer->rename();
269+
$status = $renamer->rename();
270+
271+
if ( is_wp_error( $status ) ) {
272+
wp_die( $status->get_error_message() );
273+
}
266274
}
267275

268276
/**
@@ -272,11 +280,11 @@ public function replace_file() {
272280
$id = sanitize_text_field( $_POST['attachment_id'] );
273281

274282
if ( ! current_user_can( 'edit_post', $id ) ) {
275-
wp_send_json_error( __( 'You are not allowed to replace this file', 'optimole' ) );
283+
wp_send_json_error( __( 'You are not allowed to replace this file', 'optimole-wp' ) );
276284
}
277285

278286
if ( ! isset( $_FILES['file'] ) ) {
279-
wp_send_json_error( __( 'No file uploaded', 'optimole' ) );
287+
wp_send_json_error( __( 'No file uploaded', 'optimole-wp' ) );
280288
}
281289

282290
$replacer = new Optml_Attachment_Replace( $id, $_FILES['file'] );
@@ -287,7 +295,7 @@ public function replace_file() {
287295

288296
$response = [
289297
'success' => ! $is_error,
290-
'message' => $is_error ? $replaced->get_error_message() : __( 'File replaced successfully', 'optimole' ),
298+
'message' => $is_error ? $replaced->get_error_message() : __( 'File replaced successfully', 'optimole-wp' ),
291299
];
292300

293301
wp_send_json( $response );

inc/media_rename/attachment_model.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,6 @@ public function get_attachment_id() {
132132
return $this->attachment_id;
133133
}
134134

135-
/**
136-
* Get filename.
137-
*
138-
* @return string
139-
*/
140-
public function setup_original_attached_file_name() {
141-
return $this->original_attached_file_name;
142-
}
143-
144135
/**
145136
* Get filename no extension.
146137
*

inc/media_rename/attachment_rename.php

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function __construct( int $attachment_id, string $new_filename ) {
3636
$this->attachment_id = $attachment_id;
3737
$this->attachment = new Optml_Attachment_Model( $attachment_id );
3838
$this->new_filename = $new_filename;
39+
40+
if ( ! function_exists( 'WP_Filesystem' ) ) {
41+
require_once ABSPATH . 'wp-admin/includes/file.php';
42+
}
43+
44+
WP_Filesystem();
3945
}
4046

4147
/**
@@ -45,7 +51,7 @@ public function __construct( int $attachment_id, string $new_filename ) {
4551
*/
4652
public function rename() {
4753
if ( empty( $this->new_filename ) || sanitize_file_name( $this->new_filename ) === $this->attachment->get_filename_no_ext() ) {
48-
return;
54+
return true;
4955
}
5056

5157
$extension = $this->attachment->get_extension();
@@ -57,18 +63,17 @@ public function rename() {
5763
$new_unique_filename = wp_unique_filename( $base_dir, $new_file_with_ext );
5864
$new_file_path = $base_dir . $new_unique_filename;
5965

60-
$this->init_filesystem();
6166
global $wp_filesystem;
6267

6368
// Bail if original file doesn't exist.
6469
if ( ! $wp_filesystem->exists( $file_path ) ) {
65-
return;
70+
return new WP_Error( 'optml_attachment_file_not_found', __( 'Error renaming file.', 'optimole-wp' ) );
6671
}
6772

6873
// Rename the file (move) - moves the original, not the scaled image.
6974
$moved = $wp_filesystem->move( $file_path, $new_file_path );
7075
if ( ! $moved ) {
71-
return new WP_Error( 'optml_attachment_rename_failed', __( 'Failed to rename the attachment.', 'optimole' ) );
76+
return new WP_Error( 'optml_attachment_rename_failed', __( 'Error renaming file.', 'optimole-wp' ) );
7277
}
7378

7479
// Move the scaled image if it exists.
@@ -84,21 +89,28 @@ public function rename() {
8489
}
8590

8691
// Update attachment metadata
87-
$this->update_attachment_metadata( $file_path, $new_file_path );
92+
$metadata_update = $this->update_attachment_metadata( $new_file_path );
8893

89-
$replacer = new Optml_Attachment_Db_Renamer();
90-
91-
$count = $replacer->replace( $this->attachment->get_guid(), $this->get_new_guid( $new_unique_filename ) );
94+
if ( $metadata_update === false ) {
95+
return new WP_Error( 'optml_attachment_metadata_update_failed', __( 'Error renaming file.', 'optimole-wp' ) );
96+
}
9297

93-
if ( $count > 0 ) {
94-
/**
95-
* Action triggered after the attachment file is renamed.
96-
*
97-
* @param int $attachment_id Attachment ID.
98-
* @param string $new_guid New GUID (new image URL).
99-
* @param string $old_guid Old GUID (old image URL).
100-
*/
101-
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $this->get_new_guid( $new_unique_filename ), $this->attachment->get_guid() );
98+
try {
99+
$replacer = new Optml_Attachment_Db_Renamer();
100+
$count = $replacer->replace( $this->attachment->get_guid(), $this->get_new_guid( $new_unique_filename ) );
101+
102+
if ( $count > 0 ) {
103+
/**
104+
* Action triggered after the attachment file is renamed.
105+
*
106+
* @param int $attachment_id Attachment ID.
107+
* @param string $new_guid New GUID (new image URL).
108+
* @param string $old_guid Old GUID (old image URL).
109+
*/
110+
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $this->get_new_guid( $new_unique_filename ), $this->attachment->get_guid() );
111+
}
112+
} catch ( Exception $e ) {
113+
return new WP_Error( 'optml_attachment_url_replace_failed', __( 'Error renaming file.', 'optimole-wp' ) );
102114
}
103115

104116
return true;
@@ -107,11 +119,10 @@ public function rename() {
107119
/**
108120
* Update attachment metadata.
109121
*
110-
* @param string $old_path Old path.
111122
* @param string $new_path New path.
112-
* @return void
123+
* @return bool
113124
*/
114-
private function update_attachment_metadata( $old_path, $new_path ) {
125+
private function update_attachment_metadata( $new_path ) {
115126
global $wp_filesystem;
116127

117128
$new_file_name_no_ext = pathinfo( $new_path, PATHINFO_FILENAME );
@@ -124,14 +135,14 @@ private function update_attachment_metadata( $old_path, $new_path ) {
124135
$attached_update = update_attached_file( $this->attachment_id, $new_path );
125136

126137
if ( ! $attached_update ) {
127-
return;
138+
return false;
128139
}
129140

130141
// Get current attachment metadata
131142
$metadata = $this->attachment->get_attachment_metadata();
132143

133144
if ( empty( $metadata ) ) {
134-
return;
145+
return false;
135146
}
136147

137148
// Update file path in metadata
@@ -149,6 +160,8 @@ private function update_attachment_metadata( $old_path, $new_path ) {
149160

150161
// Update image sizes if they exist
151162
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
163+
$already_moved_paths = [];
164+
152165
foreach ( $metadata['sizes'] as $size => $size_data ) {
153166
if ( ! isset( $size_data['file'] ) ) {
154167
continue;
@@ -161,17 +174,23 @@ private function update_attachment_metadata( $old_path, $new_path ) {
161174

162175
$move = $wp_filesystem->move( $old_size_file_path, $new_size_file_path );
163176

164-
if ( $move ) {
177+
if ( $move || in_array( $old_size_file_path, $already_moved_paths, true ) ) {
178+
$already_moved_paths[] = $old_size_file_path;
165179
$metadata['sizes'][ $size ]['file'] = $new_size_file;
180+
$already_moved_paths = array_unique( $already_moved_paths );
166181
}
167182
}
168183
}
169184

170-
wp_update_attachment_metadata( $this->attachment_id, $metadata );
185+
$metadata_update = wp_update_attachment_metadata( $this->attachment_id, $metadata );
186+
187+
if ( ! $metadata_update ) {
188+
return false;
189+
}
171190

172191
global $wpdb;
173192

174-
$wpdb->update(
193+
$update = $wpdb->update(
175194
$wpdb->posts,
176195
[
177196
'guid' => $this->get_new_guid( $original_image ),
@@ -180,19 +199,8 @@ private function update_attachment_metadata( $old_path, $new_path ) {
180199
'ID' => $this->attachment_id,
181200
]
182201
);
183-
}
184202

185-
/**
186-
* Initialize filesystem.
187-
*
188-
* @return void
189-
*/
190-
private function init_filesystem() {
191-
if ( ! function_exists( 'WP_Filesystem' ) ) {
192-
require_once ABSPATH . 'wp-admin/includes/file.php';
193-
}
194-
195-
WP_Filesystem();
203+
return $update !== false;
196204
}
197205

198206
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Test class for Optml_Attachment_Rename.
4+
*/
5+
6+
/**
7+
* Class Test_Attachment_Rename.
8+
*/
9+
trait Attachment_Edit_Utils {
10+
protected function delete_attachment( $id ) {
11+
wp_delete_attachment( $id, true );
12+
}
13+
14+
protected function create_attachment_get_id( $file ) {
15+
return self::factory()->attachment->create_upload_object( $file );
16+
}
17+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Test class for Optml_Attachment_Edit.
4+
*/
5+
6+
/**
7+
* Class Test_Attachment_Edit.
8+
*/
9+
class Test_Attachment_Edit extends WP_UnitTestCase {
10+
/**
11+
* Test instance
12+
*
13+
* @var Optml_Attachment_Edit
14+
*/
15+
private $instance;
16+
17+
/**
18+
* Setup test
19+
*/
20+
public function setUp(): void {
21+
parent::setUp();
22+
$this->instance = new Optml_Attachment_Edit();
23+
}
24+
25+
/**
26+
* Test prepare attachment filename
27+
*/
28+
public function test_prepare_attachment_filename() {
29+
$attachment = $this->factory->post->create_and_get( [
30+
'post_type' => 'attachment',
31+
'post_mime_type' => 'image/jpeg',
32+
] );
33+
34+
$post_data = [
35+
'ID' => $attachment->ID,
36+
'optml_rename_nonce' => wp_create_nonce( 'optml_rename_media_nonce' ),
37+
'optml_rename_file' => 'test-file'
38+
];
39+
40+
$result = $this->instance->prepare_attachment_filename( $post_data, (array) $attachment );
41+
42+
foreach ( $post_data as $key => $value ) {
43+
if ( $key == 'optml_rename_nonce' ) {
44+
continue;
45+
}
46+
$this->assertEquals( $value, $result[ $key ] );
47+
}
48+
49+
$this->assertEquals( 'test-file', get_post_meta( $attachment->ID, '_optml_pending_rename', true ) );
50+
}
51+
}

0 commit comments

Comments
 (0)