diff --git a/src/admin/class-meta-box.php b/src/admin/class-meta-box.php index ef40c66..67a3056 100644 --- a/src/admin/class-meta-box.php +++ b/src/admin/class-meta-box.php @@ -123,6 +123,12 @@ function cimo_get_media_type_label( $mimetype ) { $optimization_savings = number_format( 100 * ( $original_filesize - $converted_filesize ) / $original_filesize, 2 ); } + // If optimization savings is not a valid number or is negative, do not display it + if ( ! is_numeric( $optimization_savings ) || floatval( $optimization_savings ) < 0 ) { + echo '

' . esc_html__( 'Cimo did not optimize this attachment.', 'cimo-image-optimizer' ) . '

'; + return; + } + $kb_saved = cimo_format_filesize( $original_filesize - $converted_filesize, 1, true ); $optimization_savings_class = ( $optimization_savings > 0 ) ? 'cimo-optimization-savings-up' : 'cimo-optimization-savings-down'; @@ -132,6 +138,7 @@ function cimo_get_media_type_label( $mimetype ) { $converted_format_raw = isset( $cimo['convertedFormat'] ) ? $cimo['convertedFormat'] : ( isset( $post->post_mime_type ) ? $post->post_mime_type : '' ); $converted_format = $converted_format_raw ? cimo_convert_mimetype_to_format( $converted_format_raw ) : ''; $media_type_label = cimo_get_media_type_label( $converted_format_raw ); + $is_image_media = is_string( $converted_format_raw ) && strpos( strtolower( $converted_format_raw ), 'image/' ) === 0; $converttime = isset( $cimo['conversionTime'] ) ? floatval( $cimo['conversionTime'] ) : null; if ( $converttime !== null ) { if ( $converttime < 1000 ) { @@ -207,7 +214,9 @@ function cimo_get_media_type_label( $mimetype ) { echo '
  • '; echo '🏞️ ' . sprintf( /* translators: %s: bulk optimization count */ - esc_html__( '%s thumbnail(s) processed', 'cimo-image-optimizer' ), + $is_image_media + ? esc_html__( '%s thumbnail(s) processed', 'cimo-image-optimizer' ) + : esc_html__( '%s file(s) processed', 'cimo-image-optimizer' ), '' . esc_html( $bulk_optimization_count ) . '' ); echo '
  • '; diff --git a/src/admin/js/media-manager/sidebar-info.js b/src/admin/js/media-manager/sidebar-info.js index 65ca224..71b754f 100644 --- a/src/admin/js/media-manager/sidebar-info.js +++ b/src/admin/js/media-manager/sidebar-info.js @@ -118,6 +118,7 @@ function injectCimoMetadata( { const convertedFormatRaw = customMetadata.convertedFormat || model.get( 'mime' ) || '' const mediaTypeLabel = getMediaTypeLabel( convertedFormatRaw ) + const isImageMedia = typeof convertedFormatRaw === 'string' && convertedFormatRaw.startsWith( 'image/' ) let html = `
    @@ -135,6 +136,15 @@ function injectCimoMetadata( { ? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 ) : ( 100 * ( originalFilesize - convertedFilesize ) / originalFilesize ).toFixed( 2 ) + // If optimizationSavings is not a valid number or is negative, do not display it + if ( + optimizationSavings === 'NaN' || + ! Number.isFinite( Number( optimizationSavings ) ) || + Number( optimizationSavings ) < 0 + ) { + return + } + const kbSaved = formatFilesize( originalFilesize - convertedFilesize, 1, @@ -185,7 +195,7 @@ function injectCimoMetadata( { if ( isBulkOptimized ) { html += `
  • - 🏞️ ${ escape( Object.keys( customMetadata.bulk_optimization ).length.toString() ) } thumbnail(s) processed + 🏞️ ${ escape( Object.keys( customMetadata.bulk_optimization ).length.toString() ) } ${ escape( isImageMedia ? 'thumbnail(s) processed' : 'file(s) processed' ) }
  • ⚡️ Bulk optimized diff --git a/src/admin/js/page/admin-settings.js b/src/admin/js/page/admin-settings.js index 270f000..2f0642d 100644 --- a/src/admin/js/page/admin-settings.js +++ b/src/admin/js/page/admin-settings.js @@ -614,7 +614,7 @@ const AdminSettings = () => { - { __( 'Bulk Image Optimization', 'cimo-image-optimizer' ) } + { __( 'Bulk Optimization', 'cimo-image-optimizer' ) } { buildType === 'free' && (