#61590 closed enhancement (fixed)
Make wp_delete_file return to be compliant both with PHP and WPCS
Reported by: | bedas | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | 6.6 |
Component: | Filesystem API | Keywords: | 2nd-opinion has-patch |
Focuses: | Cc: |
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.
4 weeks 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.
#3
@
3 weeks ago
- Milestone changed from Awaiting Review to 6.7
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
@debarghyabanerjee commented on PR #6993:
3 weeks ago
#5
Closing the PR since the issue has already been resolved.
.. 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.
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 ); return @unlink( $delete ); }