#61590 closed enhancement (fixed)
Make wp_delete_file return to be compliant both with PHP and WPCS
| Reported by: | anonymized_14808221 | Owned by: | SergeyBiryukov |
|---|---|---|---|
| Priority: | normal | Milestone: | 6.7 |
| Component: | Filesystem API | Version: | 6.6 |
| Severity: | normal | Keywords: | 2nd-opinion has-patch |
| Cc: | Focuses: |
Description
When you use unlink() you can check if it was successful via the return value of unlink().
But WPCS expects us to use wp_delete_file() which does not return, making it impossible to check if it was successful, without doing overhead checks if the file still exists.
I suggest returning the value of unlink in wp_delete_file so we can actually comply with WPCS while still using PHP features.
function wp_delete_file( $file ) { /** * Filters the path of the file to delete. * * @since 2.1.0 * * @param string $file Path to the file to delete. */ $delete = apply_filters( 'wp_delete_file', $file ); if ( ! empty( $delete ) ) { return @unlink( $delete ); } }
Change History (5)
This ticket was mentioned in PR #6993 on WordPress/wordpress-develop by @debarghyabanerjee.
2 years ago
#2
- Keywords has-patch added
Trac Ticket - Core-61590
## Summary
This Pull Request addresses the issue where using wp_delete_file() lacks a return value, making it challenging to verify the success of file deletion operations compared to unlink().
## Description
### Problem
The wp_delete_file() lacks a return value, making it challenging to verify the success of file deletion operations without doing overhead checks if the file still exists.
### Example
wp_delete_file( $file ) => returns void
### Solution
Since the wp_delete_file() internally uses the unlink function which returns a boolean value based on success or failure, to resolve this issue, the return value of unlink() is returned i.e., a boolean value is return to make the user aware if the deletion was successful or not. Additionally, if the file is not found, it returns false.
@debarghyabanerjee commented on PR #6993:
2 years ago
#5
Closing the PR since the issue has already been resolved.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
.. and probably also better error handling for when $delete is empty by simply removing the check for empty. It should return false in those cases.