Make WordPress Core

Changeset 8796


Ignore:
Timestamp:
09/02/2008 10:55:39 PM (17 years ago)
Author:
ryan
Message:

Make attachment file path relative to the upload dir. Don't use GUID to find attachement URL. Props DD32. see #7622

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r8744 r8796  
    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
  • trunk/wp-includes/post.php

    r8766 r8796  
    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;
     
    5157
    5258    $file = apply_filters( 'update_attached_file', $file, $attachment_id );
     59
     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    }
    5367
    5468    return update_post_meta( $attachment_id, '_wp_attached_file', $file );
     
    24512465        return false;
    24522466
    2453     $url = get_the_guid( $post->ID );
    2454 
    2455     if ( 'attachment' != $post->post_type || !$url )
     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    }
     2474
     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
Note: See TracChangeset for help on using the changeset viewer.