初始化并连接WordPress文件系统抽象类。

原型

WP_Filesystem( array|false $args = false, string|false $context = false, bool $allow_relaxed_file_ownership = false )

描述

此功能将包括所选的传输和尝试连接。

参数

$args (array|false) (Optional)  连接参数,它们直接传递给WP_Filesystem _ *()类。

$context (string|false) (Optional)  get_filesystem_method()的上下文。

$allow_relaxed_file_ownership (bool) (Optional)  是否允许Group / World可写。

返回值

(null|bool)  失败时是假的,成功时是真的。

源文件

路径:wp-admin/includes/file.php

<?php
...
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) {
	global $wp_filesystem;
	require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
	$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
	if ( ! $method )
		return false;
	if ( ! class_exists( "WP_Filesystem_$method" ) ) {
		/**
		 * Filters the path for a specific filesystem method class file.
		 *
		 * @since 2.6.0
		 *
		 * @see get_filesystem_method()
		 *
		 * @param string $path   Path to the specific filesystem method class file.
		 * @param string $method The filesystem method to use.
		 */
		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
		if ( ! file_exists($abstraction_file) )
			return;
		require_once($abstraction_file);
	}
	$method = "WP_Filesystem_$method";
	$wp_filesystem = new $method($args);
	//Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default.
	if ( ! defined('FS_CONNECT_TIMEOUT') )
		define('FS_CONNECT_TIMEOUT', 30);
	if ( ! defined('FS_TIMEOUT') )
		define('FS_TIMEOUT', 30);
	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
		return false;
	if ( !$wp_filesystem->connect() )
		return false; //There was an error connecting to the server.
	// Set the permission constants if not already set.
	if ( ! defined('FS_CHMOD_DIR') )
		define('FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
	if ( ! defined('FS_CHMOD_FILE') )
		define('FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
	return true;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_filesystem/