From 166b55a12b3a06723c811527c819083ea358bb66 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 1 Aug 2023 16:08:16 +0000 Subject: [PATCH] Upgrade/Install: Pass stored credentials to `WP_Filesystem()` where appropriate. With the introduction of temporary backups of plugins and themes before updating, a new Site Health test was added to verify that plugin and theme temporary backup directories are writable or can be created. When using a non-direct filesystem, the Site Health test did not include the required credentials, leading to a fatal error as the connection was not initialized properly. This commit attemps to use the stored credentials if available, and displays a message otherwise. Includes a similar fix in a function that performs a cleanup of the temporary backup directory. Follow-up to [55720]. Props utsav72640, rajinsharwar, costdev, mukesh27, peterwilsoncc, audrasjb, SergeyBiryukov. Reviewed by audrasjb, SergeyBiryukov. Merges [56341] to the 6.3 branch. See #58940. git-svn-id: https://develop.svn.wordpress.org/branches/6.3@56342 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-site-health.php | 14 ++++++++++++-- src/wp-includes/update.php | 11 +++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index a198b496d7..eda8bfaa4b 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -2009,9 +2009,19 @@ class WP_Site_Health { 'test' => 'update_temp_backup_writable', ); - if ( ! $wp_filesystem ) { + if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; - WP_Filesystem(); + } + + ob_start(); + $credentials = request_filesystem_credentials( '' ); + ob_end_clean(); + + if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { + $result['status'] = 'recommended'; + $result['label'] = __( 'Could not access filesystem' ); + $result['description'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); + return $result; } $wp_content = $wp_filesystem->wp_content_dir(); diff --git a/src/wp-includes/update.php b/src/wp-includes/update.php index 1d2f55fbef..4b7fc9644d 100644 --- a/src/wp-includes/update.php +++ b/src/wp-includes/update.php @@ -1113,9 +1113,16 @@ function wp_delete_all_temp_backups() { function _wp_delete_all_temp_backups() { global $wp_filesystem; - if ( ! $wp_filesystem ) { + if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; - WP_Filesystem(); + } + + ob_start(); + $credentials = request_filesystem_credentials( '' ); + ob_end_clean(); + + if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { + return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) ); } if ( ! $wp_filesystem->wp_content_dir() ) {