Skip to content

Commit 2670de1

Browse files
authored
Merge pull request #46 from gambitph/feat/37-video-audio-bulk
feat: add video and audio bulk optimization
2 parents 0be6178 + 9bbf84b commit 2670de1

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/admin/class-meta-box.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ function cimo_get_media_type_label( $mimetype ) {
123123
$optimization_savings = number_format( 100 * ( $original_filesize - $converted_filesize ) / $original_filesize, 2 );
124124
}
125125

126+
// If optimization savings is not a valid number or is negative, do not display it
127+
if ( ! is_numeric( $optimization_savings ) || floatval( $optimization_savings ) < 0 ) {
128+
echo '<p>' . esc_html__( 'Cimo did not optimize this attachment.', 'cimo-image-optimizer' ) . '</p>';
129+
return;
130+
}
131+
126132
$kb_saved = cimo_format_filesize( $original_filesize - $converted_filesize, 1, true );
127133
$optimization_savings_class = ( $optimization_savings > 0 ) ? 'cimo-optimization-savings-up' : 'cimo-optimization-savings-down';
128134

@@ -132,6 +138,7 @@ function cimo_get_media_type_label( $mimetype ) {
132138
$converted_format_raw = isset( $cimo['convertedFormat'] ) ? $cimo['convertedFormat'] : ( isset( $post->post_mime_type ) ? $post->post_mime_type : '' );
133139
$converted_format = $converted_format_raw ? cimo_convert_mimetype_to_format( $converted_format_raw ) : '';
134140
$media_type_label = cimo_get_media_type_label( $converted_format_raw );
141+
$is_image_media = is_string( $converted_format_raw ) && strpos( strtolower( $converted_format_raw ), 'image/' ) === 0;
135142
$converttime = isset( $cimo['conversionTime'] ) ? floatval( $cimo['conversionTime'] ) : null;
136143
if ( $converttime !== null ) {
137144
if ( $converttime < 1000 ) {
@@ -207,7 +214,9 @@ function cimo_get_media_type_label( $mimetype ) {
207214
echo '<li class="cimo-bulk-optimization-number">';
208215
echo '🏞️ ' . sprintf(
209216
/* translators: %s: bulk optimization count */
210-
esc_html__( '%s thumbnail(s) processed', 'cimo-image-optimizer' ),
217+
$is_image_media
218+
? esc_html__( '%s thumbnail(s) processed', 'cimo-image-optimizer' )
219+
: esc_html__( '%s file(s) processed', 'cimo-image-optimizer' ),
211220
'<span class="cimo-value">' . esc_html( $bulk_optimization_count ) . '</span>'
212221
);
213222
echo '</li>';

src/admin/js/media-manager/sidebar-info.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function injectCimoMetadata( {
118118

119119
const convertedFormatRaw = customMetadata.convertedFormat || model.get( 'mime' ) || ''
120120
const mediaTypeLabel = getMediaTypeLabel( convertedFormatRaw )
121+
const isImageMedia = typeof convertedFormatRaw === 'string' && convertedFormatRaw.startsWith( 'image/' )
121122

122123
let html = `
123124
<div class="cimo-media-manager-metadata-title-container">
@@ -135,6 +136,15 @@ function injectCimoMetadata( {
135136
? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
136137
: ( 100 * ( originalFilesize - convertedFilesize ) / originalFilesize ).toFixed( 2 )
137138

139+
// If optimizationSavings is not a valid number or is negative, do not display it
140+
if (
141+
optimizationSavings === 'NaN' ||
142+
! Number.isFinite( Number( optimizationSavings ) ) ||
143+
Number( optimizationSavings ) < 0
144+
) {
145+
return
146+
}
147+
138148
const kbSaved = formatFilesize(
139149
originalFilesize - convertedFilesize,
140150
1,
@@ -185,7 +195,7 @@ function injectCimoMetadata( {
185195
if ( isBulkOptimized ) {
186196
html += `
187197
<li class="cimo-bulk-optimization-number">
188-
🏞️ <span class="cimo-value">${ escape( Object.keys( customMetadata.bulk_optimization ).length.toString() ) }</span> thumbnail(s) processed
198+
🏞️ <span class="cimo-value">${ escape( Object.keys( customMetadata.bulk_optimization ).length.toString() ) }</span> ${ escape( isImageMedia ? 'thumbnail(s) processed' : 'file(s) processed' ) }
189199
</li>
190200
<li class="cimo-bulk-optimization-number">
191201
⚡️ Bulk optimized

src/admin/js/page/admin-settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ const AdminSettings = () => {
707707
<span aria-hidden="true">
708708
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="lucide lucide-images-icon lucide-images"><path d="m22 11-1.296-1.296a2.4 2.4 0 0 0-3.408 0L11 16" /><path d="M4 8a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2" /><circle cx="13" cy="7" r="1" fill="currentColor" /><rect x="8" y="2" width="14" height="14" rx="2" /></svg>
709709
</span>
710-
{ __( 'Bulk Image Optimization', 'cimo-image-optimizer' ) }
710+
{ __( 'Bulk Optimization', 'cimo-image-optimizer' ) }
711711
</h2>
712712
{ buildType === 'free' && (
713713
<span

src/shared/converters/image-converter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class ImageConverter extends Converter {
318318
return {
319319
file,
320320
metadata: null,
321-
reason: 'resulting-image-bigger-than-input',
321+
reason: 'resulting-media-bigger-than-input',
322322
error: `Resulting image is bigger than the input (input: ${ file.size } bytes, output: ${ convertedBlob.size } bytes), skipping conversion.`,
323323
}
324324
}

0 commit comments

Comments
 (0)