WordPress.org

Make WordPress Core

Ticket #7622: 7622.3.diff

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

Make attachment file path relative to the upload dir

  • wp-includes/post.php

     
    2727 */
    2828function get_attached_file( $attachment_id, $unfiltered = false ) {
    2929        $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
     30        // If the file is relative, prepend upload dir
     31        if ( 0 !== strpos($file, '/') ) {
     32                $uploads = wp_upload_dir();
     33                $file = $uploads['basedir'] . "/$file";
     34        }
     35               
    3036        if ( $unfiltered )
    3137                return $file;
    3238        return apply_filters( 'get_attached_file', $file, $attachment_id );
     
    5157
    5258        $file = apply_filters( 'update_attached_file', $file, $attachment_id );
    5359
     60        // Make the file path relative to the upload dir
     61        if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
     62                if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
     63                                $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
     64                                $file = ltrim($file, '/');
     65                }
     66        }
     67
    5468        return update_post_meta( $attachment_id, '_wp_attached_file', $file );
    5569}
    5670
     
    24502464        if ( !$post =& get_post( $post_id ) )
    24512465                return false;
    24522466
    2453         $url = get_the_guid( $post->ID );
     2467        $url = '';
     2468        if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
     2469                if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
     2470                        if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
     2471                                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
     2472                }
     2473        }
    24542474
    2455         if ( 'attachment' != $post->post_type || !$url )
     2475        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.
     2476                $url = get_the_guid( $post->ID );
     2477
     2478        if ( 'attachment' != $post->post_type || empty($url) )
    24562479                return false;
    24572480
    24582481        return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
  • wp-admin/includes/image.php

     
    9393                $metadata['height'] = $imagesize[1];
    9494                list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
    9595                $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
     96                // Make the file path relative to the upload dir
     97                if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
     98                        if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
     99                                $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
     100                                $file = ltrim($file, '/');
     101                        }
     102                }
    96103                $metadata['file'] = $file;
    97104
    98105                // make thumbnails and other intermediate sizes