From 8616ad913aea19270939968b8ba5d3a1c78d5278 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 13 Jul 2024 05:01:47 +0000 Subject: [PATCH] Filesystem API: Add a return value for `wp_delete_file()`. This addresses a discrepancy where using `unlink()` allows for checking if it was successful via the return value, but `wp_delete_file()` did not have a return value, making it impossible to verify the result without doing overhead checks if the file still exists. This also brings more consistency with the other `wp_delete_*()` functions, specifically: * `wp_delete_file_from_directory()` * `wp_delete_post()` * `wp_delete_post_revision()` * `wp_delete_attachment()` * `wp_delete_attachment_files()` * `wp_delete_comment()` * `wp_delete_nav_menu()` * `wp_delete_term()` * `wp_delete_site()` * `wp_delete_user()` Includes adding basic unit tests for `wp_delete_file()`. Follow-up to [31575]. Props bedas, debarghyabanerjee, mukesh27, SergeyBiryukov. Fixes #61590. git-svn-id: https://develop.svn.wordpress.org/trunk@58715 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 11 ++++-- .../phpunit/tests/functions/wpDeleteFile.php | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/functions/wpDeleteFile.php 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.' ); + } +}