From 9045798e950c73c2e7cb754e59bd0c17996d48f8 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 2 Sep 2008 22:55:39 +0000 Subject: [PATCH] Make attachment file path relative to the upload dir. Don't use GUID to find attachement URL. Props DD32. see #7622 git-svn-id: https://develop.svn.wordpress.org/trunk@8796 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/image.php | 7 +++++++ wp-includes/post.php | 27 +++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 09f4fc1d1d..26d0092046 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -93,6 +93,13 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $metadata['height'] = $imagesize[1]; list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']); $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; + // Make the file path relative to the upload dir + if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory + if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path + $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path + $file = ltrim($file, '/'); + } + } $metadata['file'] = $file; // make thumbnails and other intermediate sizes diff --git a/wp-includes/post.php b/wp-includes/post.php index a5c578e8f3..f5377f7fb1 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -27,6 +27,12 @@ */ function get_attached_file( $attachment_id, $unfiltered = false ) { $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); + // If the file is relative, prepend upload dir + if ( 0 !== strpos($file, '/') ) { + $uploads = wp_upload_dir(); + $file = $uploads['basedir'] . "/$file"; + } + if ( $unfiltered ) return $file; return apply_filters( 'get_attached_file', $file, $attachment_id ); @@ -51,6 +57,14 @@ function update_attached_file( $attachment_id, $file ) { $file = apply_filters( 'update_attached_file', $file, $attachment_id ); + // Make the file path relative to the upload dir + if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory + if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path + $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path + $file = ltrim($file, '/'); + } + } + return update_post_meta( $attachment_id, '_wp_attached_file', $file ); } @@ -2450,9 +2464,18 @@ function wp_get_attachment_url( $post_id = 0 ) { if ( !$post =& get_post( $post_id ) ) return false; - $url = get_the_guid( $post->ID ); + $url = ''; + if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file + if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory + if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location + $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location + } + } - if ( 'attachment' != $post->post_type || !$url ) + 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. + $url = get_the_guid( $post->ID ); + + if ( 'attachment' != $post->post_type || empty($url) ) return false; return apply_filters( 'wp_get_attachment_url', $url, $post->ID );