Automattic\WooCommerce\Internal\Utilities
FilesystemUtil::initialize_wp_filesystem
Wrapper to initialize the WP filesystem with defined credentials if they are available.
Method of the class: FilesystemUtil{}
No Hooks.
Returns
true|false. True if the $wp_filesystem global was successfully initialized.
Usage
$result = FilesystemUtil::initialize_wp_filesystem(): bool;
FilesystemUtil::initialize_wp_filesystem() FilesystemUtil::initialize wp filesystem code WC 10.7.0
protected static function initialize_wp_filesystem(): bool {
global $wp_filesystem;
if ( $wp_filesystem instanceof WP_Filesystem_Base ) {
return true;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
$method = self::get_wp_filesystem_method_or_direct();
$initialized = false;
if ( 'direct' === $method ) {
$initialized = WP_Filesystem();
} elseif ( false !== $method ) {
$is_ftp = in_array( $method, array( 'ftpext', 'ftpsockets' ), true );
if ( $is_ftp && get_transient( self::FTP_INIT_FAILURE_TRANSIENT ) ) {
return false;
}
// See https://core.trac.wordpress.org/changeset/56341.
ob_start();
$credentials = request_filesystem_credentials( '' );
ob_end_clean();
$initialized = $credentials && WP_Filesystem( $credentials );
if ( $is_ftp ) {
if ( ! $initialized ) {
// A fixed cooldown is used instead of exponential backoff since this handles a non-critical
// edge case (broken FTP filesystem during logging) that most sites will never encounter.
set_transient( self::FTP_INIT_FAILURE_TRANSIENT, true, self::FTP_INIT_COOLDOWN_MINUTES * MINUTE_IN_SECONDS );
error_log( sprintf( 'WooCommerce: FTP filesystem connection failed. Please check your FTP credentials. Retrying in %d minutes.', self::FTP_INIT_COOLDOWN_MINUTES ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
} else {
delete_transient( self::FTP_INIT_FAILURE_TRANSIENT );
}
}
}
return is_null( $initialized ) ? false : $initialized;
}