WP_Upgrader::fs_connect()publicWP 2.8.0

Connects to the filesystem.

Method of the class: WP_Upgrader{}

No Hooks.

Return

true|false|WP_Error. True if able to connect, false or a WP_Error otherwise.

Usage

$WP_Upgrader = new WP_Upgrader();
$WP_Upgrader->fs_connect( $directories, $allow_relaxed_file_ownership );
$directories(string[])
Array of directories. If any of these do not exist, a WP_Error object will be returned.
Default: empty array
$allow_relaxed_file_ownership(true|false)
Whether to allow relaxed file ownership.
Default: false

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 2.8.0 Introduced.

WP_Upgrader::fs_connect() code WP 6.5.2

public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
	global $wp_filesystem;

	$credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership );
	if ( false === $credentials ) {
		return false;
	}

	if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
		$error = true;
		if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
			$error = $wp_filesystem->errors;
		}
		// Failed to connect. Error and request again.
		$this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
		return false;
	}

	if ( ! is_object( $wp_filesystem ) ) {
		return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
	}

	if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
		return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
	}

	foreach ( (array) $directories as $dir ) {
		switch ( $dir ) {
			case ABSPATH:
				if ( ! $wp_filesystem->abspath() ) {
					return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] );
				}
				break;
			case WP_CONTENT_DIR:
				if ( ! $wp_filesystem->wp_content_dir() ) {
					return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
				}
				break;
			case WP_PLUGIN_DIR:
				if ( ! $wp_filesystem->wp_plugins_dir() ) {
					return new WP_Error( 'fs_no_plugins_dir', $this->strings['fs_no_plugins_dir'] );
				}
				break;
			case get_theme_root():
				if ( ! $wp_filesystem->wp_themes_dir() ) {
					return new WP_Error( 'fs_no_themes_dir', $this->strings['fs_no_themes_dir'] );
				}
				break;
			default:
				if ( ! $wp_filesystem->find_folder( $dir ) ) {
					return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
				}
				break;
		}
	}
	return true;
}