Ticket #7622: 7622.3.diff

File 7622.3.diff, 2.9 KB (added by ryan, 5 years ago)

Make attachment file path relative to the upload dir

Line 
1Index: wp-includes/post.php
2===================================================================
3--- wp-includes/post.php        (revision 8795)
4+++ wp-includes/post.php        (working copy)
5@@ -27,6 +27,12 @@
6  */
7 function get_attached_file( $attachment_id, $unfiltered = false ) {
8        $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
9+       // If the file is relative, prepend upload dir
10+       if ( 0 !== strpos($file, '/') ) {
11+               $uploads = wp_upload_dir();
12+               $file = $uploads['basedir'] . "/$file";
13+       }
14+               
15        if ( $unfiltered )
16                return $file;
17        return apply_filters( 'get_attached_file', $file, $attachment_id );
18@@ -51,6 +57,14 @@
19 
20        $file = apply_filters( 'update_attached_file', $file, $attachment_id );
21 
22+       // Make the file path relative to the upload dir
23+       if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
24+               if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
25+                               $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
26+                               $file = ltrim($file, '/');
27+               }
28+       }
29+
30        return update_post_meta( $attachment_id, '_wp_attached_file', $file );
31 }
32 
33@@ -2450,9 +2464,18 @@
34        if ( !$post =& get_post( $post_id ) )
35                return false;
36 
37-       $url = get_the_guid( $post->ID );
38+       $url = '';
39+       if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
40+               if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
41+                       if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
42+                               $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
43+               }
44+       }
45 
46-       if ( 'attachment' != $post->post_type || !$url )
47+       if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
48+               $url = get_the_guid( $post->ID );
49+
50+       if ( 'attachment' != $post->post_type || empty($url) )
51                return false;
52 
53        return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
54Index: wp-admin/includes/image.php
55===================================================================
56--- wp-admin/includes/image.php (revision 8795)
57+++ wp-admin/includes/image.php (working copy)
58@@ -93,6 +93,13 @@
59                $metadata['height'] = $imagesize[1];
60                list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
61                $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
62+               // Make the file path relative to the upload dir
63+               if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
64+                       if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
65+                               $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
66+                               $file = ltrim($file, '/');
67+                       }
68+               }
69                $metadata['file'] = $file;
70 
71                // make thumbnails and other intermediate sizes