From d8e7eef76d96ee89578e81a3296abf3373e0f03b Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sat, 4 Feb 2023 01:06:57 +0000 Subject: [PATCH] Filesystem API: Update `move_dir()` to better handle the differences in the `WP_Filesystem::move()` methods. Changes `move_dir()` to attempt to delete the destination when overwriting, before calling `WP_Filesystem::move()`. Props: afragen, costdev, azaozz. Fixes: #57375. git-svn-id: https://develop.svn.wordpress.org/trunk@55219 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/file.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 6d22898b9c..13f04f05d4 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -1955,6 +1955,8 @@ function copy_dir( $from, $to, $skip_list = array() ) { * * Assumes that WP_Filesystem() has already been called and setup. * + * This function is not designed to merge directories, copy_dir() should be used instead. + * * @since 6.2.0 * * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. @@ -1979,7 +1981,12 @@ function move_dir( $from, $to, $overwrite = false ) { return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to ); } - if ( $wp_filesystem->move( $from, $to, $overwrite ) ) { + if ( $overwrite && $wp_filesystem->exists( $to ) && ! $wp_filesystem->delete( $to, true ) ) { + // Can't overwrite if the destination couldn't be deleted. + return WP_Error( 'destination_not_deleted_move_dir', __( 'The destination directory already exists and could not be removed.' ) ); + } + + if ( $wp_filesystem->move( $from, $to ) ) { /* * When using an environment with shared folders, * there is a delay in updating the filesystem's cache.