From 165d803380c561f39caeb0cbdc44f5c06c988cba Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Fri, 21 Feb 2025 20:23:34 +0000 Subject: [PATCH] Media: Support generating filenames without a suffix. Add support handling an empty string in the `$suffix` parameter that allows a file name to be generated with no suffix added. This makes it possible to avoid adding irrelevant suffixes in cases like converting image formats. Props azaozz, debarghyabanerjee, joedolson. See #62359. Fixes #62385. git-svn-id: https://develop.svn.wordpress.org/trunk@59855 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-image-editor.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index f9f58fcd50..da7f4082ce 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -433,6 +433,7 @@ abstract class WP_Image_Editor { * Builds an output filename based on current file, and adding proper suffix * * @since 3.5.0 + * @since 6.8.0 Passing an empty string as $suffix will now omit the suffix from the generated filename. * * @param string $suffix * @param string $dest_path @@ -440,9 +441,11 @@ abstract class WP_Image_Editor { * @return string filename */ public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { - // $suffix will be appended to the destination filename, just before the extension. - if ( ! $suffix ) { - $suffix = $this->get_suffix(); + // If not empty the $suffix will be appended to the destination filename, just before the extension. + if ( $suffix ) { + $suffix = '-' . $suffix; + } elseif ( '' !== $suffix ) { + $suffix = '-' . $this->get_suffix(); } $dir = pathinfo( $this->file, PATHINFO_DIRNAME ); @@ -462,7 +465,7 @@ abstract class WP_Image_Editor { } } - return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; + return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}"; } /**