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.
Built from https://develop.svn.wordpress.org/trunk@55219


git-svn-id: http://core.svn.wordpress.org/trunk@54752 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2023-02-04 01:08:17 +00:00
parent e7a506c636
commit 8cf4b7557f
2 changed files with 9 additions and 2 deletions

View File

@ -1955,6 +1955,8 @@ function copy_dir( $from, $to, $skip_list = array() ) {
* *
* Assumes that WP_Filesystem() has already been called and setup. * 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 * @since 6.2.0
* *
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. * @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 ); 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, * When using an environment with shared folders,
* there is a delay in updating the filesystem's cache. * there is a delay in updating the filesystem's cache.

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.2-alpha-55218'; $wp_version = '6.2-alpha-55219';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.