Skip to content

Commit 2d444d1

Browse files
committed
fix: attempt to fix phpunit
1 parent 7d7c6a8 commit 2d444d1

14 files changed

+314
-71
lines changed

.github/workflows/test-php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
strategy:
2929
fail-fast: false
3030
matrix:
31-
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
31+
php-versions: [ '8.0' ]
3232
services:
3333
database:
3434
image: mysql:latest

assets/js/single-attachment.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
jQuery(document).ready(function($) {
3-
console.log(OMAttachmentEdit);
43
const existingFileName = $("#optml_rename_file").attr("placeholder");
54
const renameBtn = $("#optml-rename-file-btn");
65

inc/media_rename/attachment_db_renamer.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@ private function sql_handle_column( $table, $column, $old_url, $new_url ) {
210210
$old_path = $old_path_parts['path'];
211211
$old_file_info = pathinfo( $old_path );
212212

213-
if ( ! isset( $old_file_info['filename'] ) ) {
214-
return 0;
215-
}
216-
217213
$old_base = $old_file_info['filename'];
218214
$old_dir = dirname( $old_path );
219215
$old_domain = isset( $old_path_parts['host'] ) ? 'http' . ( isset( $old_path_parts['scheme'] ) && $old_path_parts['scheme'] === 'https' ? 's' : '' ) . '://' . $old_path_parts['host'] : '';
@@ -444,11 +440,6 @@ private function replace_image_urls( $content, $old_url, $new_url ) {
444440
$old_file_info = pathinfo( $old_path );
445441
$new_file_info = pathinfo( $new_path );
446442

447-
if ( ! isset( $old_file_info['filename'] ) || ! isset( $new_file_info['filename'] ) ) {
448-
// If we can't get the filenames, fallback to direct replacement
449-
return str_replace( $old_url, $new_url, $content );
450-
}
451-
452443
$old_base = $old_file_info['filename'];
453444
$new_base = $new_file_info['filename'];
454445
$old_ext = isset( $old_file_info['extension'] ) ? $old_file_info['extension'] : '';
@@ -491,7 +482,7 @@ function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old
491482

492483
$content = preg_replace_callback(
493484
$scaled_pattern,
494-
function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old_dir, $new_dir, $old_ext, $new_ext ) {
485+
function ( $matches ) use ( $new_base, $new_domain, $new_dir, $new_ext ) {
495486
return $new_domain . $new_dir . '/' . $new_base . '-scaled.' . $new_ext;
496487
},
497488
$content
@@ -517,7 +508,7 @@ function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old
517508

518509
$content = preg_replace_callback(
519510
$json_scaled_pattern,
520-
function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old_dir, $new_dir, $old_ext, $new_ext ) {
511+
function ( $matches ) use ( $new_base, $new_domain, $new_dir, $new_ext ) {
521512
return str_replace( '/', '\/', $new_domain . $new_dir . '/' . $new_base . '-scaled.' . $new_ext );
522513
},
523514
$content

inc/media_rename/attachment_edit.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Optml_Attachment_Edit {
1515
* @return void
1616
*/
1717
public function init() {
18-
add_action( 'attachment_fields_to_edit', [ $this, 'add_attachment_fields' ], 10, 2 );
18+
add_filter( 'attachment_fields_to_edit', [ $this, 'add_attachment_fields' ], 10, 2 );
1919
add_filter( 'attachment_fields_to_save', [ $this, 'prepare_attachment_filename' ], 10, 2 );
2020

2121
add_action( 'edit_attachment', [ $this, 'save_attachment_filename' ] );
@@ -35,7 +35,7 @@ public function enqueue_scripts( $hook ) {
3535
return;
3636
}
3737

38-
$id = sanitize_text_field( $_GET['post'] );
38+
$id = (int) sanitize_text_field( $_GET['post'] );
3939

4040
if ( ! $id ) {
4141
return;
@@ -218,7 +218,7 @@ public function prepare_attachment_filename( array $post_data, array $attachment
218218
return $post_data;
219219
}
220220

221-
if ( $post_data['post_type'] !== 'attachment' ) {
221+
if ( ! isset( $post_data['post_type'] ) || $post_data['post_type'] !== 'attachment' ) {
222222
return $post_data;
223223
}
224224

@@ -277,7 +277,7 @@ public function save_attachment_filename( $post_id ) {
277277
* Replace the file
278278
*/
279279
public function replace_file() {
280-
$id = sanitize_text_field( $_POST['attachment_id'] );
280+
$id = (int) sanitize_text_field( $_POST['attachment_id'] );
281281

282282
if ( ! current_user_can( 'edit_post', $id ) ) {
283283
wp_send_json_error( __( 'You are not allowed to replace this file', 'optimole-wp' ) );
@@ -305,10 +305,10 @@ public function replace_file() {
305305
* Bust cached assets
306306
*
307307
* @param int $attachment_id The attachment ID.
308-
* @param string $new_guid The new GUID.
309-
* @param string $old_guid The old GUID.
308+
* @param string $new_url The new attachment URL.
309+
* @param string $old_url The old attachment URL.
310310
*/
311-
public function bust_cached_assets( $attachment_id, $new_guid, $old_guid ) {
311+
public function bust_cached_assets( $attachment_id, $new_url, $old_url ) {
312312
if (
313313
class_exists( '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server' ) &&
314314
is_callable( [ '\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server', 'regenerate_styles' ] )

inc/media_rename/attachment_model.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class Optml_Attachment_Model {
6161
private $is_scaled = false;
6262

6363
/**
64-
* The attachment GUID (main image URL).
64+
* The attached file main URL.
6565
*
6666
* @var string
6767
*/
68-
private $guid;
68+
private $main_attachment_url;
6969

7070
/**
7171
* Whether the attachment is remote - offloaded, imported from DAM or legacy offloaded.
@@ -92,14 +92,15 @@ public function __construct( int $attachment_id ) {
9292
$this->attachment_id = $attachment_id;
9393
$this->attachment_metadata = wp_get_attachment_metadata( $this->attachment_id );
9494

95-
$post = get_post( $attachment_id );
95+
$mime_type = get_post_mime_type( $attachment_id );
96+
$is_image = strpos( $mime_type, 'image' ) !== false;
9697

97-
$this->guid = $post->guid;
98+
$this->main_attachment_url = $is_image ? wp_get_original_image_url( $attachment_id ) : wp_get_attachment_url( $attachment_id );
9899
$this->origianal_attached_file_path = $this->setup_original_attached_file();
99100
$this->dir_path = dirname( $this->origianal_attached_file_path );
100101
$this->is_scaled = isset( $this->attachment_metadata['original_image'] );
101102

102-
$filename = $this->is_scaled ?
103+
$filename = $this->is_scaled && isset( $this->attachment_metadata['original_image'] ) ?
103104
$this->attachment_metadata['original_image'] :
104105
basename( $this->origianal_attached_file_path );
105106

@@ -108,7 +109,7 @@ public function __construct( int $attachment_id ) {
108109
$file_parts = pathinfo( $filename );
109110

110111
$this->extension = isset( $file_parts['extension'] ) ? $file_parts['extension'] : '';
111-
$this->filename_no_ext = isset( $file_parts['filename'] ) ? $file_parts['filename'] : $filename;
112+
$this->filename_no_ext = $file_parts['filename'];
112113
$this->is_remote_attachment = $this->is_dam_imported_image( $this->attachment_id ) ||
113114
$this->is_legacy_offloaded_attachment( $this->attachment_id ) ||
114115
$this->is_new_offloaded_attachment( $this->attachment_id );
@@ -186,12 +187,12 @@ public function get_dir_path() {
186187
}
187188

188189
/**
189-
* Get guid.
190+
* Get the attachment file main url.
190191
*
191192
* @return string
192193
*/
193-
public function get_guid() {
194-
return $this->guid;
194+
public function get_main_url() {
195+
return $this->main_attachment_url;
195196
}
196197

197198
/**
@@ -237,7 +238,7 @@ public function get_all_image_sizes_urls() {
237238
}
238239

239240
foreach ( $attachment_metadata['sizes'] as $size => $size_data ) {
240-
$links[ $size ] = str_replace( $this->original_attached_file_name, $size_data['file'], $this->get_guid() );
241+
$links[ $size ] = str_replace( $this->original_attached_file_name, $size_data['file'], $this->get_main_url() );
241242
}
242243

243244
return $links;

inc/media_rename/attachment_rename.php

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

9898
try {
9999
$replacer = new Optml_Attachment_Db_Renamer();
100-
$count = $replacer->replace( $this->attachment->get_guid(), $this->get_new_guid( $new_unique_filename ) );
100+
$count = $replacer->replace( $this->attachment->get_main_url(), $this->get_new_url( $new_unique_filename ) );
101101

102102
if ( $count > 0 ) {
103103
/**
104104
* Action triggered after the attachment file is renamed.
105105
*
106106
* @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).
107+
* @param string $new_url New attachment URL.
108+
* @param string $old_url Old attachment URL.
109109
*/
110-
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $this->get_new_guid( $new_unique_filename ), $this->attachment->get_guid() );
110+
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $this->get_new_url( $new_unique_filename ), $this->attachment->get_main_url() );
111111
}
112112
} catch ( Exception $e ) {
113113
return new WP_Error( 'optml_attachment_url_replace_failed', __( 'Error renaming file.', 'optimole-wp' ) );
@@ -188,33 +188,21 @@ private function update_attachment_metadata( $new_path ) {
188188
return false;
189189
}
190190

191-
global $wpdb;
192-
193-
$update = $wpdb->update(
194-
$wpdb->posts,
195-
[
196-
'guid' => $this->get_new_guid( $original_image ),
197-
],
198-
[
199-
'ID' => $this->attachment_id,
200-
]
201-
);
202-
203-
return $update !== false;
191+
return true;
204192
}
205193

206194
/**
207-
* Get the new guid (main image URL).
195+
* Get the new main attached file URL.
208196
*
209197
* @return string
210198
*/
211-
private function get_new_guid( $filename ) {
212-
$guid = $this->attachment->get_guid();
199+
private function get_new_url( $filename ) {
200+
$url = $this->attachment->get_main_url();
213201

214202
return str_replace(
215-
basename( $guid ),
203+
basename( $url ),
216204
$filename,
217-
$guid
205+
$url
218206
);
219207
}
220208
}

inc/media_rename/attachment_replace.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private function handle_scaled_images() {
134134

135135
// Delete the old scaled version and replace scaled URLs with non-scaled URLs.
136136
if ( $old_scaled && ! $new_scaled ) {
137-
$main_file_url = $this->attachment->get_guid();
137+
$main_file_url = $this->attachment->get_main_url();
138138
$unscaled_file = $this->attachment->get_filename_with_ext();
139139
$old_scaled_file = $this->attachment->get_filename_with_ext( true );
140140
$old_scaled_url = str_replace( $unscaled_file, $old_scaled_file, $main_file_url );
@@ -166,12 +166,12 @@ private function replace_image_sizes_links( $new_sizes, $old_sizes_urls ) {
166166
foreach ( $old_sizes_urls as $size => $old_url ) {
167167
// If the size is not in the new sizes, we need to use the original URL.
168168
if ( ! isset( $new_sizes[ $size ], $new_sizes[ $size ]['file'] ) ) {
169-
$replacer->replace( $old_url, $this->attachment->get_guid() );
169+
$replacer->replace( $old_url, $this->attachment->get_main_url() );
170170
continue;
171171
}
172172

173173
// If the size is in the new sizes, we need to use the new URL.
174-
$new_url = str_replace( $this->attachment->get_filename_with_ext(), $new_sizes[ $size ]['file'], $this->attachment->get_guid() );
174+
$new_url = str_replace( $this->attachment->get_filename_with_ext(), $new_sizes[ $size ]['file'], $this->attachment->get_main_url() );
175175
$replacer->replace( $old_url, $new_url );
176176
}
177177
}

tests/assets/rename-scaled.jpg

149 KB
Loading

tests/assets/rename-unscaled.jpg

10.5 KB
Loading

tests/media_rename/test-attachment-edit.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ public function setUp(): void {
2626
* Test prepare attachment filename
2727
*/
2828
public function test_prepare_attachment_filename() {
29-
$attachment = $this->factory->post->create_and_get( [
29+
$attachment = self::factory()->post->create_and_get( [
3030
'post_type' => 'attachment',
3131
'post_mime_type' => 'image/jpeg',
3232
] );
3333

3434
$post_data = [
3535
'ID' => $attachment->ID,
3636
'optml_rename_nonce' => wp_create_nonce( 'optml_rename_media_nonce' ),
37-
'optml_rename_file' => 'test-file'
37+
'optml_rename_file' => 'test-file',
38+
'post_type' => 'attachment',
3839
];
3940

4041
$result = $this->instance->prepare_attachment_filename( $post_data, (array) $attachment );

0 commit comments

Comments
 (0)