Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines 7881 to +7885

$real_file = $file;
$real_directory = $directory;
} else {
$real_file = realpath( wp_normalize_path( $file ) );
Expand Down
84 changes: 84 additions & 0 deletions tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Tests for the wp_delete_file_from_directory function.
*
* @group functions
*
* @covers ::wp_delete_file_from_directory
*/
class Tests_Functions_wpDeleteFileFromDirectory extends WP_UnitTestCase {

/**
* Call with file and correct directory.
*
* @ticket 59781
*/
public function test_wp_delete_file_from_directory() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );
touch( $file );
Comment on lines +17 to +19

$this->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 );
}
}
Loading