From 184e7c15ed498503a2db9146c91ab0f6bf2d5047 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 9 Oct 2021 03:37:41 +0000 Subject: [PATCH] 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 #54166. See #51857. git-svn-id: https://develop.svn.wordpress.org/trunk@51899 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-upgrader.php | 4 +-- src/wp-admin/includes/file.php | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/class-wp-upgrader.php b/src/wp-admin/includes/class-wp-upgrader.php index ed9302feb2..0fab410240 100644 --- a/src/wp-admin/includes/class-wp-upgrader.php +++ b/src/wp-admin/includes/class-wp-upgrader.php @@ -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 ); diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 8c4797d113..50345c6331 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -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. *