+ $context = trailingslashit( $context );
+
+ if ( ! $method ) {
+
+ $temp_file_name = $context . 'temp-write-test-' . time();
+ $temp_handle = @fopen($temp_file_name, 'w');
+ if ( $temp_handle ) {
+
+ // Attempt to determine the file owner of the WordPress files, and that of newly created files
+ $wp_file_owner = $temp_file_owner = false;
+ if ( function_exists('fileowner') ) {
+ $wp_file_owner = @fileowner( __FILE__ );
+ $temp_file_owner = @fileowner( $temp_file_name );
+ }
+
+ if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) {
+ // WordPress is creating files as the same owner as the WordPress files,
+ // this means it's safe to modify & create new files via PHP.
+ $method = 'direct';
+ $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
+ } else if ( $allow_relaxed_file_ownership ) {
+ // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files
+ // safely in this directory. This mode doesn't create new files, only alter existing ones.
+ $method = 'direct';
+ $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
+ }
+
+ @fclose($temp_handle);
+ @unlink($temp_file_name);
+ }
+ }
+
+ if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2';