Skip to content

Commit f7eeecc

Browse files
authored
Merge pull request #913 from Codeinwp/fix/media-rename-mimetype
Fix/media rename mimetype
2 parents 822f0d3 + 5242329 commit f7eeecc

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

inc/media_rename/attachment_model.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class Optml_Attachment_Model {
7474
*/
7575
private $is_remote_attachment;
7676

77+
/**
78+
* The attachment mime type.
79+
*
80+
* @var string
81+
*/
82+
private $mime_type;
83+
7784
/**
7885
* The attachment metadata.
7986
*
@@ -91,11 +98,9 @@ class Optml_Attachment_Model {
9198
public function __construct( int $attachment_id ) {
9299
$this->attachment_id = $attachment_id;
93100
$this->attachment_metadata = wp_get_attachment_metadata( $this->attachment_id );
101+
$this->mime_type = get_post_mime_type( $attachment_id );
94102

95-
$mime_type = get_post_mime_type( $attachment_id );
96-
$is_image = strpos( $mime_type, 'image' ) !== false;
97-
98-
$this->main_attachment_url = $is_image ? wp_get_original_image_url( $attachment_id ) : wp_get_attachment_url( $attachment_id );
103+
$this->main_attachment_url = $this->is_image() ? wp_get_original_image_url( $attachment_id ) : wp_get_attachment_url( $attachment_id );
99104
$this->origianal_attached_file_path = $this->setup_original_attached_file();
100105
$this->dir_path = dirname( $this->origianal_attached_file_path );
101106
$this->is_scaled = isset( $this->attachment_metadata['original_image'] );
@@ -286,4 +291,18 @@ public function get_metadata_prefix_path() {
286291
public function can_be_renamed_or_replaced() {
287292
return ! $this->is_remote_attachment;
288293
}
294+
295+
/**
296+
* Get the attachment mime type.
297+
*/
298+
public function get_attachment_mimetype() {
299+
return $this->mime_type;
300+
}
301+
302+
/**
303+
* Check if the attachment is an image.
304+
*/
305+
public function is_image() {
306+
return strpos( $this->mime_type, 'image' ) !== false;
307+
}
289308
}

inc/media_rename/attachment_rename.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public function rename() {
9898
}
9999

100100
try {
101-
$replacer = new Optml_Attachment_Db_Renamer();
101+
$skip_sizes = ! $this->attachment->is_image();
102+
103+
$replacer = new Optml_Attachment_Db_Renamer( $skip_sizes );
102104
$old_url = $this->attachment->get_main_url();
103105
$new_url = $this->get_new_url( $new_unique_filename );
104106

@@ -145,6 +147,12 @@ private function update_attachment_metadata( $new_path ) {
145147
return false;
146148
}
147149

150+
// We should bail for non-image attachments here.
151+
// No need for additional metadata update.
152+
if ( ! $this->attachment->is_image() ) {
153+
return true;
154+
}
155+
148156
// Get current attachment metadata
149157
$metadata = $this->attachment->get_attachment_metadata();
150158

0 commit comments

Comments
 (0)