Media: Account for Windows when normalizing file paths.

Previously, Windows paths in the `path_is_absolute` function resulted in incorrect URIs. This patch adjusts for forward slashes and adds tests for the `get_attached_file` function.
Props Whissi, SergeyBiryukov, desrosj, stevenlinx, birgire, davidbaumwald, costdev, peterwilsoncc, audrasjb, hellofromTonya, johnbillion.
Fixes #36308.


Built from https://develop.svn.wordpress.org/trunk@53934


git-svn-id: http://core.svn.wordpress.org/trunk@53493 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
antpb 2022-08-23 19:59:14 +00:00
parent 69fe3c1aff
commit 0406a3ffd7
3 changed files with 10 additions and 3 deletions

View File

@ -2083,6 +2083,7 @@ function wp_mkdir_p( $target ) {
* For example, '/foo/bar', or 'c:\windows'.
*
* @since 2.5.0
* @since 6.1.0 Allows normalized Windows paths (forward slashes).
*
* @param string $path File path.
* @return bool True if path is absolute, false is not absolute.
@ -2113,6 +2114,11 @@ function path_is_absolute( $path ) {
return true;
}
// Normalized Windows paths for local filesystem and network shares (forward slashes).
if ( preg_match( '#(^[a-zA-Z]+:/|^//[\w!@\#\$%\^\(\)\-\'{}\.~]{1,15})#', $path ) ) {
return true;
}
// A path starting with / or \ is absolute; anything else is relative.
return ( '/' === $path[0] || '\\' === $path[0] );
}

View File

@ -724,10 +724,11 @@ 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 ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
if ( $file ) {
$uploads = wp_get_upload_dir();
if ( false === $uploads['error'] ) {
$file = $uploads['basedir'] . "/$file";
$file = path_join( $uploads['basedir'], $file );
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-53933';
$wp_version = '6.1-alpha-53934';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.