From 1227fc51195f6e53273681aa3a3c739e1178d6fa Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 11 Oct 2021 15:07:52 +0000 Subject: [PATCH] Upgrade/Install: Restore or clean up the temporary plugin or theme backup on shutdown. This allows these actions to run ''after'' the main process, without affecting the update. Actions running on `shutdown` are immune to PHP timeouts, so in case the failure was due to a PHP timeout, we'll still be able to properly restore the previous version. Follow-up to [51815], [51898], [51899]. Props aristath, peterwilsoncc. See #54166. git-svn-id: https://develop.svn.wordpress.org/trunk@51902 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-upgrader.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/class-wp-upgrader.php b/src/wp-admin/includes/class-wp-upgrader.php index 0fab410240..225a7d4c69 100644 --- a/src/wp-admin/includes/class-wp-upgrader.php +++ b/src/wp-admin/includes/class-wp-upgrader.php @@ -843,7 +843,18 @@ class WP_Upgrader { $this->skin->set_result( $result ); if ( is_wp_error( $result ) ) { if ( ! empty( $options['hook_extra']['temp_backup'] ) ) { - $this->restore_temp_backup( $options['hook_extra']['temp_backup'] ); + /* + * Restore the backup on shutdown. + * Actions running on `shutdown` are immune to PHP timeouts, + * so in case the failure was due to a PHP timeout, + * we'll still be able to properly restore the previous version. + */ + add_action( + 'shutdown', + function() use ( $options ) { + $this->restore_temp_backup( $options['hook_extra']['temp_backup'] ); + } + ); } $this->skin->error( $result ); @@ -859,7 +870,13 @@ class WP_Upgrader { // Clean up the backup kept in the temp-backup directory. if ( ! empty( $options['hook_extra']['temp_backup'] ) ) { - $this->delete_temp_backup( $options['hook_extra']['temp_backup'] ); + // Delete the backup on `shutdown` to avoid a PHP timeout. + add_action( + 'shutdown', + function() use ( $options ) { + $this->delete_temp_backup( $options['hook_extra']['temp_backup'] ); + } + ); } if ( ! $options['is_multi'] ) {