From 1ad7735355b126a574f9097a3de4ba19d421e3ec Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 16 Feb 2023 22:11:32 +0000 Subject: [PATCH] Filesystem API: Use `trailingslashit( $path )` instead of `$path . '/'`. Fixes a warning when `$path` already ends with a slash. Props: costdev, afragen, mukesh27. Fixes: #57516. git-svn-id: https://develop.svn.wordpress.org/trunk@55354 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-filesystem-direct.php | 17 +++++++++-------- .../includes/class-wp-filesystem-ftpext.php | 5 +++-- .../includes/class-wp-filesystem-ftpsockets.php | 5 +++-- .../includes/class-wp-filesystem-ssh2.php | 16 +++++++++------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/wp-admin/includes/class-wp-filesystem-direct.php b/src/wp-admin/includes/class-wp-filesystem-direct.php index e8be5506d2..f201def08f 100644 --- a/src/wp-admin/includes/class-wp-filesystem-direct.php +++ b/src/wp-admin/includes/class-wp-filesystem-direct.php @@ -629,7 +629,8 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return false; } - $ret = array(); + $path = trailingslashit( $path ); + $ret = array(); while ( false !== ( $entry = $dir->read() ) ) { $struc = array(); @@ -647,20 +648,20 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { continue; } - $struc['perms'] = $this->gethchmod( $path . '/' . $entry ); + $struc['perms'] = $this->gethchmod( $path . $entry ); $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); $struc['number'] = false; - $struc['owner'] = $this->owner( $path . '/' . $entry ); - $struc['group'] = $this->group( $path . '/' . $entry ); - $struc['size'] = $this->size( $path . '/' . $entry ); - $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); + $struc['owner'] = $this->owner( $path . $entry ); + $struc['group'] = $this->group( $path . $entry ); + $struc['size'] = $this->size( $path . $entry ); + $struc['lastmodunix'] = $this->mtime( $path . $entry ); $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] ); $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] ); - $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; + $struc['type'] = $this->is_dir( $path . $entry ) ? 'd' : 'f'; if ( 'd' === $struc['type'] ) { if ( $recursive ) { - $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); + $struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive ); } else { $struc['files'] = array(); } diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpext.php b/src/wp-admin/includes/class-wp-filesystem-ftpext.php index 8b59d0525e..975a4dd717 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpext.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpext.php @@ -761,12 +761,13 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $dirlist[ $entry['name'] ] = $entry; } - $ret = array(); + $path = trailingslashit( $path ); + $ret = array(); foreach ( (array) $dirlist as $struc ) { if ( 'd' === $struc['type'] ) { if ( $recursive ) { - $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); + $struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive ); } else { $struc['files'] = array(); } diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php index e64f47d44e..da771e4156 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php @@ -645,7 +645,8 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { return false; } - $ret = array(); + $path = trailingslashit( $path ); + $ret = array(); foreach ( $list as $struc ) { @@ -663,7 +664,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( 'd' === $struc['type'] ) { if ( $recursive ) { - $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); + $struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive ); } else { $struc['files'] = array(); } diff --git a/src/wp-admin/includes/class-wp-filesystem-ssh2.php b/src/wp-admin/includes/class-wp-filesystem-ssh2.php index 351fbf6fb5..80fb81b971 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ssh2.php +++ b/src/wp-admin/includes/class-wp-filesystem-ssh2.php @@ -774,6 +774,8 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { return false; } + $path = trailingslashit( $path ); + while ( false !== ( $entry = $dir->read() ) ) { $struc = array(); $struc['name'] = $entry; @@ -790,20 +792,20 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { continue; } - $struc['perms'] = $this->gethchmod( $path . '/' . $entry ); + $struc['perms'] = $this->gethchmod( $path . $entry ); $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); $struc['number'] = false; - $struc['owner'] = $this->owner( $path . '/' . $entry ); - $struc['group'] = $this->group( $path . '/' . $entry ); - $struc['size'] = $this->size( $path . '/' . $entry ); - $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); + $struc['owner'] = $this->owner( $path . $entry ); + $struc['group'] = $this->group( $path . $entry ); + $struc['size'] = $this->size( $path . $entry ); + $struc['lastmodunix'] = $this->mtime( $path . $entry ); $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] ); $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] ); - $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; + $struc['type'] = $this->is_dir( $path . $entry ) ? 'd' : 'f'; if ( 'd' === $struc['type'] ) { if ( $recursive ) { - $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); + $struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive ); } else { $struc['files'] = array(); }