WC_Admin_Upload_Downloadable_Product::unique_filename
Change filename to append random text.
Method of the class: WC_Admin_Upload_Downloadable_Product{}
No Hooks.
Returns
String. Modified filename.
Usage
$WC_Admin_Upload_Downloadable_Product = new WC_Admin_Upload_Downloadable_Product(); $WC_Admin_Upload_Downloadable_Product->unique_filename( $full_filename, $ext );
- $full_filename(string) (required)
- Original filename with extension.
- $ext(string) (required)
- Extension.
WC_Admin_Upload_Downloadable_Product::unique_filename() WC Admin Upload Downloadable Product::unique filename code WC 10.3.5
public function unique_filename( $full_filename, $ext ) {
$ideal_random_char_length = 6; // Not going with a larger length because then downloaded filename will not be pretty.
$max_filename_length = 255; // Max file name length for most file systems.
$length_to_prepend = min( $ideal_random_char_length, $max_filename_length - strlen( $full_filename ) - 1 );
if ( 1 > $length_to_prepend ) {
return $full_filename;
}
$suffix = strtolower( wp_generate_password( $length_to_prepend, false, false ) );
$filename = $full_filename;
if ( strlen( $ext ) > 0 ) {
$filename = substr( $filename, 0, strlen( $filename ) - strlen( $ext ) );
}
$full_filename = str_replace(
$filename,
"$filename-$suffix",
$full_filename
);
return $full_filename;
}