Ticket #2794: 2794.diff

File 2794.diff, 9.8 KB (added by mdawaffe, 7 years ago)
Line 
1Index: wp-includes/default-filters.php
2===================================================================
3--- wp-includes/default-filters.php     (revision 3850)
4+++ wp-includes/default-filters.php     (working copy)
5@@ -114,6 +114,11 @@
6 // Misc filters
7 add_filter('option_ping_sites', 'privacy_ping_filter');
8 
9+// Attachments
10+add_filter('wp_thumbnail_attachment_image/jpeg', 'wp_image_thumbnail', 10, -1);
11+add_filter('wp_thumbnail_attachment_image/gif', 'wp_image_thumbnail', 10, -1);
12+add_filter('wp_thumbnail_attachment_image/png', 'wp_image_thumbnail', 10, -1);
13+
14 // Actions
15 add_action('wp_head', 'rsd_link');
16 add_action('publish_future_post', 'wp_publish_post', 10, 1);
17Index: wp-includes/functions-post.php
18===================================================================
19--- wp-includes/functions-post.php      (revision 3850)
20+++ wp-includes/functions-post.php      (working copy)
21@@ -355,15 +355,28 @@
22 
23        clean_post_cache($post_ID);
24 
25-       if ( $update) {
26+       if ( $update )
27                do_action('edit_attachment', $post_ID);
28-       } else {
29+       else
30                do_action('add_attachment', $post_ID);
31-       }
32 
33        return $post_ID;
34 }
35 
36+function wp_thumbnail_attachment( $post_ID ) {
37+       $attachment =& get_post( $post_ID );
38+       $meta = get_post_meta( $post_ID, '_wp_attachment_metadata', true );
39+       $file = get_post_meta( $post_ID, '_wp_attached_file', true );
40+
41+       $meta = apply_filters( 'wp_thumbnail_attachment_' . strtolower($attachment->post_mime_type), $meta, $post_ID, $file );
42+
43+       if ( !$meta || is_wp_error( $meta ) )
44+               add_post_meta( $post_ID, '_wp_attachment_metadata', array() );
45+       else
46+               add_post_meta( $post_ID, '_wp_attachment_metadata', $meta );
47+       return $meta;
48+}
49+
50 function wp_delete_attachment($postid) {
51        global $wpdb;
52        $postid = (int) $postid;
53Index: wp-admin/inline-uploading.php
54===================================================================
55--- wp-admin/inline-uploading.php       (revision 3850)
56+++ wp-admin/inline-uploading.php       (working copy)
57@@ -71,37 +71,19 @@
58        );
59 
60 // Save the data
61-$id = wp_insert_attachment($attachment, $file, $post);
62+$id = wp_insert_attachment( $attachment, $file, $post );
63 
64-if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
65-       // Generate the attachment's postmeta.
66-       $imagesize = getimagesize($file);
67-       $imagedata['width'] = $imagesize['0'];
68-       $imagedata['height'] = $imagesize['1'];
69-       list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
70-       $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
71-       $imagedata['file'] = $file;
72+$error = wp_thumbnail_attachment( $id );
73 
74-       add_post_meta($id, '_wp_attachment_metadata', $imagedata);
75-
76-       if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
77-               if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
78-                       $thumb = wp_create_thumbnail($file, 128);
79-               elseif ( $imagedata['height'] > 96 )
80-                       $thumb = wp_create_thumbnail($file, 96);
81-
82-               if ( @file_exists($thumb) ) {
83-                       $newdata = $imagedata;
84-                       $newdata['thumb'] = basename($thumb);
85-                       update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
86-               } else {
87-                       $error = $thumb;
88-               }
89-       }
90-} else {
91-       add_post_meta($id, '_wp_attachment_metadata', array());
92+if ( is_wp_error( $error ) ) {
93+       echo "<div class='error'>\n\t<ul>\n";
94+       foreach ( $error->get_error_messages() as $message )
95+                echo "\t\t<li>$message</li>\n";
96+       echo "\t</ul>\n\t<a href='" . basename(__FILE__) . "?post=$post&all=$all&action=view&start=0". "'>" . __('Continue') . "</a>\n</div>\n";
97+       die();
98 }
99 
100+
101 header("Location: " . basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
102 die();
103 
104Index: wp-admin/admin-functions.php
105===================================================================
106--- wp-admin/admin-functions.php        (revision 3850)
107+++ wp-admin/admin-functions.php        (working copy)
108@@ -771,98 +771,102 @@
109        return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC");
110 }
111 
112-function wp_create_thumbnail($file, $max_side, $effect = '') {
113+function wp_image_thumbnail( $meta, $post_ID, $file ) {
114+       $attachment =& get_post( $post_ID );
115 
116-               // 1 = GIF, 2 = JPEG, 3 = PNG
117+       if ( is_wp_error( $meta ) || isset($meta['thumb']) )
118+               return $meta;
119+       if ( !is_array($meta) )
120+               $meta = array();
121 
122-       if (file_exists($file)) {
123-               $type = getimagesize($file);
124+       if ( preg_match('!^image/!', $attachment->post_mime_type) ) {
125+               // Generate the attachment's postmeta.
126+               $imagesize = getimagesize($file);
127+               $meta['width'] = $imagesize['0'];
128+               $meta['height'] = $imagesize['1'];
129+               list($uwidth, $uheight) = get_udims( $meta['width'], $meta['height'] );
130+               $meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
131+               $meta['file'] = $file;
132 
133-               // if the associated function doesn't exist - then it's not
134-               // handle. duh. i hope.
135-
136-               if (!function_exists('imagegif') && $type[2] == 1) {
137-                       $error = __('Filetype not supported. Thumbnail not created.');
138+               if ( $meta['width'] * $meta['height'] < 3 * 1024 * 1024 ) {
139+                       if ( $meta['width'] > 128 && $meta['width'] >= $meta['height'] * 4 / 3 )
140+                               $thumb = wp_create_thumbnail( $file, 128 );
141+                       elseif ( $meta['height'] > 96 )
142+                               $thumb = wp_create_thumbnail( $file, 96 );
143+                       if ( @file_exists($thumb) )
144+                               $meta['thumb'] = basename($thumb);
145+                       else
146+                               return $thumb;
147                }
148-               elseif (!function_exists('imagejpeg') && $type[2] == 2) {
149-                       $error = __('Filetype not supported. Thumbnail not created.');
150-               }
151-               elseif (!function_exists('imagepng') && $type[2] == 3) {
152-                       $error = __('Filetype not supported. Thumbnail not created.');
153-               } else {
154+       }
155+       return $meta;
156+}
157 
158-                       // create the initial copy from the original file
159-                       if ($type[2] == 1) {
160-                               $image = imagecreatefromgif($file);
161-                       }
162-                       elseif ($type[2] == 2) {
163-                               $image = imagecreatefromjpeg($file);
164-                       }
165-                       elseif ($type[2] == 3) {
166-                               $image = imagecreatefrompng($file);
167-                       }
168+function wp_create_thumbnail( $file, $max_side, $effect = '' ) {
169+       if ( !file_exists($file) )
170+               return new WP_Error( 'thumbnail', __('File not found') );
171 
172-                       if (function_exists('imageantialias'))
173-                               imageantialias($image, TRUE);
174+       $image_attr = getimagesize($file);
175 
176-                       $image_attr = getimagesize($file);
177+       // If the associated function doesn't exist - then it's not handled.
178+       switch ( $image_attr[2] ) : // 1 = GIF, 2 = JPEG, 3 = PNG
179+       case 1:
180+               $image_func = 'imagegif';
181+               $image_create_func = 'imagecreatefromgif';
182+               break;
183+       case 2:
184+               $image_func = 'imagejpeg';
185+               $image_create_func = 'imagecreatefromjpeg';
186+               break;
187+       case 3:
188+               $image_func = 'imagepng';
189+               $image_create_func = 'imagecreatefrompng';
190+               break;
191+       default:
192+               return new WP_Error( 'thumbnail', __('Unknown filetype. Thumbnail not created.') );
193+               break;
194+       endswitch;
195 
196-                       // figure out the longest side
197+       if ( !function_exists($image_func) )
198+               return new WP_Error( 'thumbnail', __('Filetype not supported. Thumbnail not created.') );
199 
200-                       if ($image_attr[0] > $image_attr[1]) {
201-                               $image_width = $image_attr[0];
202-                               $image_height = $image_attr[1];
203-                               $image_new_width = $max_side;
204+       // create the initial copy from the original file
205+       $image = $image_create_func($file);
206 
207-                               $image_ratio = $image_width / $image_new_width;
208-                               $image_new_height = $image_height / $image_ratio;
209-                               //width is > height
210-                       } else {
211-                               $image_width = $image_attr[0];
212-                               $image_height = $image_attr[1];
213-                               $image_new_height = $max_side;
214+       if ( function_exists('imageantialias') )
215+               imageantialias($image, TRUE);
216 
217-                               $image_ratio = $image_height / $image_new_height;
218-                               $image_new_width = $image_width / $image_ratio;
219-                               //height > width
220-                       }
221+       // figure out the longest side
222+       if ( $image_attr[0] > $image_attr[1] ) { // width > height
223+               $image_width = $image_attr[0];
224+               $image_height = $image_attr[1];
225+               $image_new_width = $max_side;
226 
227-                       $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
228-                       @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
229+               $image_ratio = $image_width / $image_new_width;
230+               $image_new_height = $image_height / $image_ratio;
231+       } else { // height > width
232+               $image_width = $image_attr[0];
233+               $image_height = $image_attr[1];
234+               $image_new_height = $max_side;
235 
236-                       // If no filters change the filename, we'll do a default transformation.
237-                       if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) )
238-                               $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);
239+               $image_ratio = $image_height / $image_new_height;
240+               $image_new_width = $image_width / $image_ratio;
241+       }
242 
243-                       $thumbpath = str_replace(basename($file), $thumb, $file);
244+       $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
245+       @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
246 
247-                       // move the thumbnail to it's final destination
248-                       if ($type[2] == 1) {
249-                               if (!imagegif($thumbnail, $thumbpath)) {
250-                                       $error = __("Thumbnail path invalid");
251-                               }
252-                       }
253-                       elseif ($type[2] == 2) {
254-                               if (!imagejpeg($thumbnail, $thumbpath)) {
255-                                       $error = __("Thumbnail path invalid");
256-                               }
257-                       }
258-                       elseif ($type[2] == 3) {
259-                               if (!imagepng($thumbnail, $thumbpath)) {
260-                                       $error = __("Thumbnail path invalid");
261-                               }
262-                       }
263+       // If no filters change the filename, we'll do a default transformation.
264+       if ( basename($file) == $thumb = apply_filters( 'thumbnail_filename', basename($file) ) )
265+               $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);
266 
267-               }
268-       } else {
269-               $error = __('File not found');
270-       }
271+       $thumbpath = str_replace(basename($file), $thumb, $file);
272 
273-       if (!empty ($error)) {
274-               return $error;
275-       } else {
276-               return $thumbpath;
277-       }
278+       // move the thumbnail to it's final destination
279+       if ( !$image_func($thumbnail, $thumbpath) )
280+               return new WP_Error( 'thumbnail', __("Thumbnail path invalid") );
281+
282+       return $thumbpath;
283 }
284 
285 // Some postmeta stuff