Skip to content

Commit 759bdd0

Browse files
Copilotswissspidy
andcommitted
Add symlink handling and optimize remove_directory with cached parameter
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 2217d6a commit 759bdd0

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/Core_Command.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,9 +1749,17 @@ private function remove_old_files_from_list( $files ) {
17491749

17501750
// Handle both files and directories
17511751
if ( file_exists( $file_path ) ) {
1752-
if ( is_dir( $file_path ) ) {
1752+
if ( is_link( $file_path ) ) {
1753+
// Remove symbolic link without following it
1754+
if ( unlink( $file_path ) ) {
1755+
WP_CLI::log( "File removed: {$file}" );
1756+
++$count;
1757+
} else {
1758+
WP_CLI::debug( "Failed to remove file: {$file}", 'core' );
1759+
}
1760+
} elseif ( is_dir( $file_path ) ) {
17531761
// Remove directory recursively
1754-
if ( $this->remove_directory( $file_path ) ) {
1762+
if ( $this->remove_directory( $file_path, $abspath_realpath_trailing ) ) {
17551763
WP_CLI::log( "Directory removed: {$file}" );
17561764
++$count;
17571765
} else {
@@ -1773,17 +1781,17 @@ private function remove_old_files_from_list( $files ) {
17731781
* Recursively remove a directory and its contents.
17741782
*
17751783
* @param string $dir Directory path to remove.
1784+
* @param string $abspath_realpath_trailing Cached ABSPATH realpath with trailing slash for performance.
17761785
* @return bool True on success, false on failure.
17771786
*/
1778-
private function remove_directory( $dir ) {
1779-
$dir_realpath = realpath( $dir );
1780-
$abspath_realpath = realpath( ABSPATH );
1781-
if ( false === $dir_realpath || false === $abspath_realpath ) {
1782-
WP_CLI::debug( "Failed to resolve realpath for directory or ABSPATH: {$dir}", 'core' );
1787+
private function remove_directory( $dir, $abspath_realpath_trailing ) {
1788+
$dir_realpath = realpath( $dir );
1789+
if ( false === $dir_realpath ) {
1790+
WP_CLI::debug( "Failed to resolve realpath for directory: {$dir}", 'core' );
17831791
return false;
17841792
}
17851793
// Normalize paths with trailing slashes for accurate comparison
1786-
if ( 0 !== strpos( $dir_realpath, Utils\trailingslashit( $abspath_realpath ) ) ) {
1794+
if ( 0 !== strpos( $dir_realpath, $abspath_realpath_trailing ) ) {
17871795
WP_CLI::debug( "Attempted to remove directory outside of ABSPATH: {$dir_realpath}", 'core' );
17881796
return false;
17891797
}
@@ -1814,7 +1822,7 @@ private function remove_directory( $dir ) {
18141822
}
18151823

18161824
if ( is_dir( $path ) ) {
1817-
if ( ! $this->remove_directory( $path ) ) {
1825+
if ( ! $this->remove_directory( $path, $abspath_realpath_trailing ) ) {
18181826
WP_CLI::debug( "Failed to remove subdirectory: {$path}", 'core' );
18191827
return false;
18201828
}

0 commit comments

Comments
 (0)