From 874aa24b4e4437e14d336a8c3af32fa2039291ef Mon Sep 17 00:00:00 2001 From: Anthony Burchell Date: Fri, 28 Oct 2022 15:16:29 +0000 Subject: [PATCH] Media: Reverts `get_attached_file()` changes for normalized Windows paths. Based on feedback from network storage configurations there was a noticed slowdown due to the usage of the `path_join()` function. This needs more time to find a workaround. Follow-up to [53934]. Props mreishus, SergeyBiryukov, desrosj, mikeschroder. Reverts [53934]. See #56924. git-svn-id: https://develop.svn.wordpress.org/trunk@54712 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 6 -- src/wp-includes/post.php | 5 +- tests/phpunit/tests/functions.php | 11 ---- tests/phpunit/tests/post/getAttachedFile.php | 67 -------------------- 4 files changed, 2 insertions(+), 87 deletions(-) delete mode 100644 tests/phpunit/tests/post/getAttachedFile.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index edbf5e5c20..65b0cb03b7 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2079,7 +2079,6 @@ 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. @@ -2110,11 +2109,6 @@ 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] ); } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index a67d7a79a1..d7e7bfec99 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -724,11 +724,10 @@ 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 ) { + if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { $uploads = wp_get_upload_dir(); - if ( false === $uploads['error'] ) { - $file = path_join( $uploads['basedir'], $file ); + $file = $uploads['basedir'] . "/$file"; } } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 2a29fa2bd0..7e8c711bbd 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -105,13 +105,6 @@ class Tests_Functions extends WP_UnitTestCase { 'C:\\', 'C:\\WINDOWS', '\\\\sambashare\\foo', - 'c:/', - 'c://', - '//', - 'c:/FOO', - '//FOO', - 'C:/WWW/Sites/demo/htdocs/wordpress/wp-content/uploads/2016/03/example.jpg', - '//ComputerName/ShareName/SubfolderName/example.txt', ); foreach ( $absolute_paths as $path ) { $this->assertTrue( path_is_absolute( $path ), "path_is_absolute('$path') should return true" ); @@ -126,14 +119,10 @@ class Tests_Functions extends WP_UnitTestCase { '../foo', '../', '../foo.bar', - 'foo.bar', 'foo/bar', 'foo', 'FOO', '..\\WINDOWS', - '..//WINDOWS', - 'c:', - 'C:', ); foreach ( $relative_paths as $path ) { $this->assertFalse( path_is_absolute( $path ), "path_is_absolute('$path') should return false" ); diff --git a/tests/phpunit/tests/post/getAttachedFile.php b/tests/phpunit/tests/post/getAttachedFile.php deleted file mode 100644 index 09501b6c18..0000000000 --- a/tests/phpunit/tests/post/getAttachedFile.php +++ /dev/null @@ -1,67 +0,0 @@ -post->create_and_get( - array( - 'post_title' => 'example-page', - 'post_type' => 'post', - ) - ); - } - - /** - * @ticket 36308 - * - * @dataProvider data_get_attached_file_with_windows_paths - * - * @param string $file The file path to attach to the post. - * @param string $expected The expected attached file path. - * @param string $message The message when an assertion fails. - */ - public function test_get_attached_file_with_windows_paths( $file, $expected, $message ) { - $attachment = self::factory()->attachment->create_and_get( - array( - 'post_parent' => self::$post->ID, - 'file' => $file, - ) - ); - - $this->assertSame( $expected, get_attached_file( $attachment->ID ), $message ); - } - - /** - * Data provider with Windows paths. - * - * @return array - */ - public function data_get_attached_file_with_windows_paths() { - return array( - 'a local path' => array( - 'file' => 'C:/WWW/Sites/demo/htdocs/wordpress/wp-content/uploads/2016/03/example.jpg', - 'expected' => 'C:/WWW/Sites/demo/htdocs/wordpress/wp-content/uploads/2016/03/example.jpg', - 'message' => 'Windows local filesystem paths should be equal', - ), - 'a network share path' => array( - 'file' => '//ComputerName/ShareName/SubfolderName/example.txt', - 'expected' => '//ComputerName/ShareName/SubfolderName/example.txt', - 'message' => 'Network share paths should be equal', - ), - ); - } - -}