Skip to content

Commit 66018df

Browse files
Copilotswissspidy
andcommitted
Simplify remove_old_files_from_list by merging validation and removal into a single pass
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent ce9a726 commit 66018df

File tree

1 file changed

+28
-45
lines changed

1 file changed

+28
-45
lines changed

src/Core_Command.php

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,6 @@ private function get_old_files_list() {
19561956
private function remove_old_files_from_list( $files ) {
19571957
$count = 0;
19581958

1959-
// Cache ABSPATH realpath for performance
19601959
$abspath_realpath = realpath( ABSPATH );
19611960
if ( false === $abspath_realpath ) {
19621961
WP_CLI::debug( 'Failed to resolve ABSPATH realpath', 'core' );
@@ -1965,72 +1964,56 @@ private function remove_old_files_from_list( $files ) {
19651964
$abspath_realpath_trailing = Utils\trailingslashit( $abspath_realpath );
19661965

19671966
foreach ( $files as $file ) {
1968-
// wp-content should be considered user data
1967+
// wp-content should be considered user data.
19691968
if ( 0 === stripos( $file, 'wp-content' ) ) {
19701969
continue;
19711970
}
19721971

19731972
$file_path = ABSPATH . $file;
19741973

1975-
// Short-circuit early: skip expensive realpath validation if the path
1976-
// doesn't exist and isn't a (potentially broken) symlink.
1974+
// Skip entries that don't exist and aren't (broken) symlinks.
19771975
if ( ! file_exists( $file_path ) && ! is_link( $file_path ) ) {
19781976
continue;
19791977
}
19801978

1981-
// For symlinks, validate the symlink itself is within ABSPATH (not where it points)
1982-
// For other files, validate the real path is within ABSPATH
1979+
// Symlinks: validate and remove without following the link.
19831980
if ( is_link( $file_path ) ) {
1984-
// Normalize the path to handle any .. sequences
19851981
$normalized_path = realpath( dirname( $file_path ) );
1986-
if ( false === $normalized_path ) {
1987-
WP_CLI::debug( "Skipping symbolic link with invalid parent directory: {$file}", 'core' );
1988-
continue;
1989-
}
1990-
// Ensure the normalized parent directory is within ABSPATH
1991-
if ( 0 !== strpos( Utils\trailingslashit( $normalized_path ), $abspath_realpath_trailing ) && rtrim( $abspath_realpath_trailing, '/' ) !== $normalized_path ) {
1982+
if ( false === $normalized_path
1983+
|| ( 0 !== strpos( Utils\trailingslashit( $normalized_path ), $abspath_realpath_trailing )
1984+
&& rtrim( $abspath_realpath_trailing, '/' ) !== $normalized_path )
1985+
) {
19921986
WP_CLI::debug( "Skipping symbolic link outside of ABSPATH: {$file}", 'core' );
19931987
continue;
19941988
}
1995-
} else {
1996-
// Validate the path is within ABSPATH
1997-
$file_realpath = realpath( $file_path );
1998-
if ( false === $file_realpath ) {
1999-
// Skip files with invalid paths
2000-
WP_CLI::debug( "Skipping file with invalid path: {$file}", 'core' );
2001-
continue;
1989+
if ( unlink( $file_path ) ) {
1990+
WP_CLI::log( "Symbolic link removed: {$file}" );
1991+
++$count;
1992+
} else {
1993+
WP_CLI::debug( "Failed to remove symbolic link: {$file}", 'core' );
20021994
}
1995+
continue;
1996+
}
20031997

2004-
if ( 0 !== strpos( $file_realpath, $abspath_realpath_trailing ) ) {
2005-
WP_CLI::debug( "Skipping file outside of ABSPATH: {$file}", 'core' );
2006-
continue;
2007-
}
1998+
// Regular files/directories: validate real path is within ABSPATH.
1999+
$file_realpath = realpath( $file_path );
2000+
if ( false === $file_realpath || 0 !== strpos( $file_realpath, $abspath_realpath_trailing ) ) {
2001+
WP_CLI::debug( "Skipping file outside of ABSPATH: {$file}", 'core' );
2002+
continue;
20082003
}
20092004

2010-
// Handle both files and directories
2011-
if ( file_exists( $file_path ) ) {
2012-
if ( is_link( $file_path ) ) {
2013-
// Remove symbolic link without following it
2014-
if ( unlink( $file_path ) ) {
2015-
WP_CLI::log( "Symbolic link removed: {$file}" );
2016-
++$count;
2017-
} else {
2018-
WP_CLI::debug( "Failed to remove symbolic link: {$file}", 'core' );
2019-
}
2020-
} elseif ( is_dir( $file_path ) ) {
2021-
// Remove directory recursively
2022-
if ( $this->remove_directory( $file_path, $abspath_realpath_trailing ) ) {
2023-
WP_CLI::log( "Directory removed: {$file}" );
2024-
++$count;
2025-
} else {
2026-
WP_CLI::debug( "Failed to remove directory: {$file}", 'core' );
2027-
}
2028-
} elseif ( unlink( $file_path ) ) {
2029-
WP_CLI::log( "File removed: {$file}" );
2005+
if ( is_dir( $file_path ) ) {
2006+
if ( $this->remove_directory( $file_path, $abspath_realpath_trailing ) ) {
2007+
WP_CLI::log( "Directory removed: {$file}" );
20302008
++$count;
20312009
} else {
2032-
WP_CLI::debug( "Failed to remove file: {$file}", 'core' );
2010+
WP_CLI::debug( "Failed to remove directory: {$file}", 'core' );
20332011
}
2012+
} elseif ( unlink( $file_path ) ) {
2013+
WP_CLI::log( "File removed: {$file}" );
2014+
++$count;
2015+
} else {
2016+
WP_CLI::debug( "Failed to remove file: {$file}", 'core' );
20342017
}
20352018
}
20362019

0 commit comments

Comments
 (0)