Make WordPress Core


Ignore:
Timestamp:
07/13/2024 05:01:47 AM (10 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r58684 r58715  
    76457645 *
    76467646 * @since 4.2.0
     7647 * @since 6.7.0 A return value was added.
    76477648 *
    76487649 * @param string $file The path to the file to delete.
     7650 * @return bool True on success, false on failure.
    76497651 */
    76507652function wp_delete_file( $file ) {
     
    76577659     */
    76587660    $delete = apply_filters( 'wp_delete_file', $file );
     7661
    76597662    if ( ! empty( $delete ) ) {
    7660         @unlink( $delete );
    7661     }
     7663        return @unlink( $delete );
     7664    }
     7665
     7666    return false;
    76627667}
    76637668
     
    76927697    }
    76937698
    7694     wp_delete_file( $file );
    7695 
    7696     return true;
     7699    return wp_delete_file( $file );
    76977700}
    76987701
Note: See TracChangeset for help on using the changeset viewer.