From 34595c598743429395439338e57617e123dd4a1a Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Fri, 28 Feb 2020 10:43:03 -0700
Subject: [PATCH] refresh patch

---
 .../includes/class-wp-filesystem-direct.php   | 34 ++++++++++++++++---
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/wp-admin/includes/class-wp-filesystem-direct.php b/src/wp-admin/includes/class-wp-filesystem-direct.php
index 1676a55658..53b0a084ea 100644
--- a/src/wp-admin/includes/class-wp-filesystem-direct.php
+++ b/src/wp-admin/includes/class-wp-filesystem-direct.php
@@ -333,8 +333,17 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
 		}
 		$file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise.
 
-		if ( 'f' == $type || $this->is_file( $file ) ) {
-			return @unlink( $file );
+		if ( 'f' == $type || $this->is_file( $file ) || $this->is_link( $file ) ) {
+			if ( 'WIN' === strotupper( substr( PHP_OS, 0, 3 ) ) &&
+					$this->is_link( $file ) && $this->is_dir( $file ) ) {
+				// on windows unlink()'ing a symlink that points to a directory fails.
+				// @link https://bugs.php.net/bug.php?id=52176
+				return @rmdir( $file );
+			}
+			else {
+				// untrailingslashit() needed to remove symlinks successfully.
+				return @unlink( untrailingslashit( $file ) );
+			}
 		}
 		if ( ! $recursive && $this->is_dir( $file ) ) {
 			return @rmdir( $file );
@@ -372,6 +381,19 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
 		return @file_exists( $file );
 	}
 
+	/**
+	 * Checks if resource is a symbolic link.
+	 *
+	 * @since 5.5.0
+	 *
+	 * @param string $file File path.
+	 * @return bool Whther `$file` is a symbolic link.
+	 */
+	public function is_link( $file ) {
+		// Strip trailing slashes, to avoid directory symlinks resolving.
+		return @is_link( untrailingslashit( $file ) );
+	}
+
 	/**
 	 * Checks if resource is a file.
 	 *
@@ -558,7 +580,11 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
 	 * }
 	 */
 	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
-		if ( $this->is_file( $path ) ) {
+		if ( $this->is_dir( $path ) && $this->is_link( $path ) ) {
+			// Directory is a symlink, therefore return no listing.
+			return array();
+		}
+		elseif ( $this->is_file( $path ) || $this->is_link( $path ) ) {
 			$limit_file = basename( $path );
 			$path       = dirname( $path );
 		} else {
@@ -603,7 +629,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
 			$struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
 			$struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
 
-			if ( 'd' == $struc['type'] ) {
+			if ( 'd' == $struc['type'] && ! $this->is_link( $path . '/' . $struc['name'] ) ) {
 				if ( $recursive ) {
 					$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
 				} else {
-- 
2.19.0.windows.1

