portalzine revised this gist 16 hours ago. Go to revision
No changes
portalzine revised this gist 16 hours ago. Go to revision
1 file changed, 29 insertions
wordpress_dml_download_filename_fix.php(file created)
| @@ -0,0 +1,29 @@ | |||
| 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 | + | } | |
Newer
Older