diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index c2fe1228bf1d6..e99a93b80db71 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7644,8 +7644,10 @@ function wp_validate_boolean( $value ) { * Deletes a file. * * @since 4.2.0 + * @since 6.7.0 A return value was added. * * @param string $file The path to the file to delete. + * @return bool True on success, false on failure. */ function wp_delete_file( $file ) { /** @@ -7656,9 +7658,12 @@ function wp_delete_file( $file ) { * @param string $file Path to the file to delete. */ $delete = apply_filters( 'wp_delete_file', $file ); + if ( ! empty( $delete ) ) { - @unlink( $delete ); + return @unlink( $delete ); } + + return false; } /** @@ -7691,9 +7696,7 @@ function wp_delete_file_from_directory( $file, $directory ) { return false; } - wp_delete_file( $file ); - - return true; + return wp_delete_file( $file ); } /** diff --git a/tests/phpunit/tests/functions/wpDeleteFile.php b/tests/phpunit/tests/functions/wpDeleteFile.php new file mode 100644 index 0000000000000..33de72a9ed1d4 --- /dev/null +++ b/tests/phpunit/tests/functions/wpDeleteFile.php @@ -0,0 +1,38 @@ +assertTrue( wp_delete_file( $file ), 'File deletion failed.' ); + $this->assertFileDoesNotExist( $file, 'The file was not deleted.' ); + } + + /** + * @ticket 61590 + */ + public function test_wp_delete_file_with_empty_path() { + $this->assertFalse( wp_delete_file( '' ) ); + } + + /** + * @ticket 61590 + */ + public function test_wp_delete_file_with_file_that_does_not_exist() { + $file = DIR_TESTDATA . '/a_file_that_does_not_exist.txt'; + + $this->assertFileDoesNotExist( $file, "$file already existed as a file before testing." ); + $this->assertFalse( wp_delete_file( $file ), 'Attempting to delete a non-existent file should return false.' ); + } +}