wordpress_dml_download_filename_fix.php
· 1.3 KiB · PHP
Raw
add_filter( 'dlm_file_path', 'dlm_bypass_rocket_local_copy', 10, 3 );
function dlm_bypass_rocket_local_copy( $file_path, $download, $version ) {
if ( ! is_object( $download ) || ! is_object( $version ) ) return $file_path;
$new_filename = sanitize_title( $download->get_title() ) . '.' . (pathinfo( $file_path, PATHINFO_EXTENSION ) ?: 'pdf');
$temp_dir = wp_upload_dir()['basedir'] . '/dlm_tmp';
if ( ! file_exists( $temp_dir ) ) wp_mkdir_p( $temp_dir );
$local_target = $temp_dir . '/' . $new_filename;
if ( ! file_exists( $local_target ) ) {
@copy( $file_path, $local_target );
}
return file_exists( $local_target ) ? $local_target : $file_path;
}
// Fail-safe: Force headers over AJAX if local copy fails
add_filter( 'dlm_download_headers', 'dlm_force_headers_past_cache', 9999, 4 );
function dlm_force_headers_past_cache( $headers, $file_path, $download, $version ) {
if ( is_object( $download ) ) {
$clean_title = sanitize_title( $download->get_title() ) . '.' . (pathinfo( $file_path, PATHINFO_EXTENSION ) ?: 'pdf');
$encoded = rawurlencode( $clean_title );
$headers['Content-Disposition'] = "attachment; filename*=UTF-8''{$encoded};";
header("Content-Disposition: attachment; filename=\"{$clean_title}\"");
}
return $headers;
}
| 1 | add_filter( 'dlm_file_path', 'dlm_bypass_rocket_local_copy', 10, 3 ); |
| 2 | function dlm_bypass_rocket_local_copy( $file_path, $download, $version ) { |
| 3 | if ( ! is_object( $download ) || ! is_object( $version ) ) return $file_path; |
| 4 | |
| 5 | $new_filename = sanitize_title( $download->get_title() ) . '.' . (pathinfo( $file_path, PATHINFO_EXTENSION ) ?: 'pdf'); |
| 6 | $temp_dir = wp_upload_dir()['basedir'] . '/dlm_tmp'; |
| 7 | |
| 8 | if ( ! file_exists( $temp_dir ) ) wp_mkdir_p( $temp_dir ); |
| 9 | $local_target = $temp_dir . '/' . $new_filename; |
| 10 | |
| 11 | if ( ! file_exists( $local_target ) ) { |
| 12 | @copy( $file_path, $local_target ); |
| 13 | } |
| 14 | |
| 15 | return file_exists( $local_target ) ? $local_target : $file_path; |
| 16 | } |
| 17 | |
| 18 | // Fail-safe: Force headers over AJAX if local copy fails |
| 19 | add_filter( 'dlm_download_headers', 'dlm_force_headers_past_cache', 9999, 4 ); |
| 20 | function dlm_force_headers_past_cache( $headers, $file_path, $download, $version ) { |
| 21 | if ( is_object( $download ) ) { |
| 22 | $clean_title = sanitize_title( $download->get_title() ) . '.' . (pathinfo( $file_path, PATHINFO_EXTENSION ) ?: 'pdf'); |
| 23 | $encoded = rawurlencode( $clean_title ); |
| 24 | |
| 25 | $headers['Content-Disposition'] = "attachment; filename*=UTF-8''{$encoded};"; |
| 26 | header("Content-Disposition: attachment; filename=\"{$clean_title}\""); |
| 27 | } |
| 28 | return $headers; |
| 29 | } |
| 30 |