diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 9a688b9866ce0..85e5cd0d4db97 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7879,7 +7879,12 @@ function wp_delete_file( $file ) { */ function wp_delete_file_from_directory( $file, $directory ) { if ( wp_is_stream( $file ) ) { - $real_file = $file; + if ( ! str_starts_with( strtolower( $file ), 'file:' ) ) { + + return false; + } + + $real_file = $file; $real_directory = $directory; } else { $real_file = realpath( wp_normalize_path( $file ) ); diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php new file mode 100644 index 0000000000000..9bb00e6b07258 --- /dev/null +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -0,0 +1,84 @@ +assertTrue( wp_delete_file_from_directory( $file, $directory ) ); + $this->assertFileDoesNotExist( $file ); + } + + /** + * Call with missing file. + * + * @ticket 59781 + */ + public function test_wp_delete_file_from_directory_no_file() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); + + $this->assertFalse( wp_delete_file_from_directory( $file, $directory ) ); + $this->assertFileDoesNotExist( $file ); + } + + /** + * Call with streamed file. + * + * @ticket 59781 + */ + public function test_wp_delete_file_from_directory_file_stream() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); + touch( $file ); + + $file_url = 'file://' . ( '/' === $file[0] ? $file : '/' . wp_normalize_path( $file ) ); + $directory_url = 'file://' . ( '/' === $directory[0] ? $directory : '/' . wp_normalize_path( $directory ) ); + + $this->assertTrue( wp_delete_file_from_directory( $file_url, $directory_url ) ); + $this->assertFileDoesNotExist( $file ); + } + + /** + * Call with unsupported stream file. + * + * @ticket 59781 + */ + public function test_wp_delete_file_from_directory_non_file_stream() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); + touch( $file ); + + $this->assertFalse( wp_delete_file_from_directory( 'php://temp', 'php://temp' ) ); + $this->assertFileExists( $file ); + @unlink( $file ); + } + + /** + * Call with incorrect directory. + * + * @ticket 59781 + */ + public function test_wp_delete_file_from_directory_no_folder() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ) . '/images'; + touch( $file ); + + $this->assertFalse( wp_delete_file_from_directory( $file, $directory ) ); + $this->assertFileExists( $file ); + @unlink( $file ); + } +}