1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Allow UTF-8 Filenames on Windows |
---|
4 | Plugin URI: http://core.trac.wordpress.org/ticket/15955 |
---|
5 | Description: A workaround for correct uploading of files with UTF-8 names on Windows systems. |
---|
6 | Version: 0.1 |
---|
7 | Author: Sergey Biryukov |
---|
8 | Author URI: http://profiles.wordpress.org/sergeybiryukov/ |
---|
9 | */ |
---|
10 | |
---|
11 | function autfw_sanitize_file_name_for_windows($filename, $utf8 = false) { |
---|
12 | if ( seems_utf8($filename) == $utf8 ) |
---|
13 | return $filename; |
---|
14 | |
---|
15 | // On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096 |
---|
16 | if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) { |
---|
17 | $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' ); |
---|
18 | if ( function_exists( 'iconv' ) ) { |
---|
19 | if ( false == $utf8 ) |
---|
20 | $filename = iconv( get_option( 'blog_charset' ), $codepage . '//IGNORE', $filename ); |
---|
21 | else |
---|
22 | $filename = iconv( $codepage, get_option( 'blog_charset' ), $filename ); |
---|
23 | } elseif ( function_exists( 'mb_convert_encoding' ) ) { |
---|
24 | if ( false == $utf8 ) |
---|
25 | $filename = mb_convert_encoding( $filename, $codepage, get_option( 'blog_charset' ) ); |
---|
26 | else |
---|
27 | $filename = mb_convert_encoding( $filename, get_option( 'blog_charset' ), $codepage ); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | return $filename; |
---|
32 | } |
---|
33 | add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows'); |
---|
34 | add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows'); |
---|
35 | |
---|
36 | function autfw_handle_upload_input($file) { |
---|
37 | $file['name'] = autfw_sanitize_file_name_for_windows($file['name']); |
---|
38 | return $file; |
---|
39 | } |
---|
40 | add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input'); |
---|
41 | |
---|
42 | function autfw_handle_upload($file) { |
---|
43 | $file['file'] = autfw_sanitize_file_name_for_windows($file['file'], true); |
---|
44 | $file['url'] = autfw_sanitize_file_name_for_windows($file['url'], true); |
---|
45 | return $file; |
---|
46 | } |
---|
47 | add_filter('wp_handle_upload', 'autfw_handle_upload'); |
---|
48 | |
---|
49 | function autfw_update_attached_file($file) { |
---|
50 | return autfw_sanitize_file_name_for_windows($file, true); |
---|
51 | } |
---|
52 | add_filter('update_attached_file', 'autfw_update_attached_file'); |
---|
53 | |
---|
54 | function autfw_generate_attachment_metadata($metadata, $attachment_id) { |
---|
55 | $file = get_attached_file($attachment_id); |
---|
56 | |
---|
57 | remove_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata'); |
---|
58 | $metadata = wp_generate_attachment_metadata($attachment_id, $file); |
---|
59 | |
---|
60 | return autfw_update_attachment_metadata($metadata); |
---|
61 | } |
---|
62 | add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2); |
---|
63 | |
---|
64 | function autfw_update_attachment_metadata($metadata) { |
---|
65 | $metadata['file'] = autfw_sanitize_file_name_for_windows($metadata['file'], true); |
---|
66 | |
---|
67 | if ( !empty($metadata['sizes']) ) { |
---|
68 | foreach ( (array) $metadata['sizes'] as $size => $resized ) { |
---|
69 | $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); |
---|
70 | $metadata['sizes'][$size] = $resized; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return $metadata; |
---|
75 | } |
---|
76 | add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata'); |
---|
77 | |
---|
78 | function autfw_update_post_metadata($foo, $object_id, $meta_key, $meta_value) { |
---|
79 | if ( '_wp_attachment_backup_sizes' != $meta_key ) |
---|
80 | return null; |
---|
81 | |
---|
82 | foreach ( (array) $meta_value as $size => $resized ) { |
---|
83 | $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); |
---|
84 | $meta_value[$size] = $resized; |
---|
85 | } |
---|
86 | |
---|
87 | remove_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); |
---|
88 | update_metadata('post', $object_id, $meta_key, $meta_value); |
---|
89 | |
---|
90 | return true; |
---|
91 | } |
---|
92 | add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); |
---|
93 | ?> |
---|