Ticket #6813: media-6-23-1am.diff

File media-6-23-1am.diff, 2.2 KB (added by noel, 5 years ago)

Press this fixes for media.php dealing with wp_error object

Line 
1Index: includes/media.php
2===================================================================
3--- includes/media.php  (revision 8166)
4+++ includes/media.php  (working copy)
5@@ -115,41 +115,6 @@
6 
7 }
8 
9-
10-function media_sideload_image($file, $post_id, $desc = null) {
11-
12-       if (!empty($file) ) {
13-               // Upload File button was clicked
14-               
15-               $file_array['name'] = basename($file);
16-               $file_array['tmp_name'] = download_url($file);
17-               $desc = @$desc;
18-               
19-               $sideload = media_handle_sideload($file_array, $post_id, $desc);
20-
21-               $id = $sideload['id'];
22-               $src = $sideload['src'];
23-               
24-               unset($file_array['tmp_name']);
25-               unset($file_array);
26-               
27-               if ( is_wp_error($id) ) {
28-                       $errors['upload_error'] = $id;
29-                       $id = false;
30-               }
31-       }
32-       
33-       if ( !empty($src) && !strpos($src, '://') )
34-               
35-               $src = "http://$src";
36-               $alt = @$desc;
37-               
38-               if ( !empty($src) )
39-                       $html = "<img src='$src' alt='$alt' />";
40-                       return $html;
41-       
42-}
43-
44 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
45        $overrides = array('test_form'=>false);
46        $file = wp_handle_sideload($file_array, $overrides);
47@@ -186,10 +151,11 @@
48        $id = wp_insert_attachment($attachment, $file, $post_parent);
49        if ( !is_wp_error($id) ) {
50                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
51+               return $src;
52        }
53+       
54+       return $id;
55 
56-       return array('id' => $id, 'src' => $url);
57-
58 }
59 
60 
61@@ -348,6 +314,33 @@
62        return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
63 }
64 
65+function media_sideload_image($file, $post_id, $desc = null) {
66+       if (!empty($file) ) {
67+               $file_array['name'] = basename($file);
68+               $file_array['tmp_name'] = download_url($file);
69+               $desc = @$desc;
70+               
71+               $id = media_handle_sideload($file_array, $post_id, $desc);
72+               unset($file_array);
73+               
74+               if ( is_wp_error($id) ) {
75+                       $errors['upload_error'] = $id;
76+                       return $id;
77+               } else {
78+                       $src = $id;
79+               }
80+       }
81+       
82+       if (!empty($src) && !strpos($src, '://') ) {
83+               $src = "http://$src";
84+               $alt = @$desc;
85+       }
86+       if ( !empty($src) ) {
87+               $html = "<img src='$src' alt='$alt' />";
88+               return $html;
89+       }
90+}
91+
92 function media_upload_audio() {
93        if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
94                // Upload File button was clicked