Upgrade/Install: Introduce a move_dir() function.

This replaces the `copy_dir()` usage in `WP_Upgrader::install_package()` and aims to avoid PHP timeout issues when installing or updating large plugins on slower systems like Vagrant or the WP Docker test environment.

The new function attempts a native PHP `rename()` function first and falls back to the previous `copy_dir()`.

Follow-up to [51815], [51898].

Props afragen, aristath, peterwilsoncc, galbaras, noisysocks, pbiron.
Fixes . See .

git-svn-id: https://develop.svn.wordpress.org/trunk@51899 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-10-09 03:37:41 +00:00
parent 4b60c38a36
commit 184e7c15ed
2 changed files with 30 additions and 2 deletions
src/wp-admin/includes

@ -623,8 +623,8 @@ class WP_Upgrader {
}
}
// Copy new version of item into place.
$result = copy_dir( $source, $remote_destination );
// Move new version of item into place.
$result = move_dir( $source, $remote_destination );
if ( is_wp_error( $result ) ) {
if ( $args['clear_working'] ) {
$wp_filesystem->delete( $remote_source, true );

@ -1917,6 +1917,34 @@ function copy_dir( $from, $to, $skip_list = array() ) {
return true;
}
/**
* Moves a directory from one location to another via the rename() PHP function.
* If the renaming failed, falls back to copy_dir().
*
* Assumes that WP_Filesystem() has already been called and setup.
*
* @since 5.9.0
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*
* @param string $from Source directory.
* @param string $to Destination directory.
* @return true|WP_Error True on success, WP_Error on failure.
*/
function move_dir( $from, $to ) {
global $wp_filesystem;
$wp_filesystem->rmdir( $to );
if ( @rename( $from, $to ) ) {
return true;
}
$wp_filesystem->mkdir( $to );
$result = copy_dir( $from, $to );
return $result;
}
/**
* Initializes and connects the WordPress Filesystem Abstraction classes.
*