unique_filename( $full_filename, $ext ); // phpcs:enable WordPress.Security.NonceVerification.Missing } /** * Change filename to append random text. * * @param string $full_filename Original filename with extension. * @param string $ext Extension. * * @return string Modified filename. */ 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; } /** * Run a filter when uploading a downloadable product. */ public function woocommerce_media_upload_downloadable_product() { // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment do_action( 'media_upload_file' ); } }