Skip to content

Commit bbc3db7

Browse files
Copilotswissspidy
andcommitted
Add safeguards: prevent duplicate hooks, add error handling, clarify logic
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 94b42b8 commit bbc3db7

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ function ( $item ) {
527527

528528
// Check if file tracking is requested.
529529
$show_changed_files = Utils\get_flag_value( $assoc_args, 'show-changed-files', false );
530-
$skin = null;
530+
// Skin is only created if file tracking is needed; otherwise get_upgrader creates default skin.
531+
$skin = null;
531532

532533
// Only attempt to update if there is something to update.
533534
if ( ! empty( $items_to_update ) ) {

src/WP_CLI/ExtensionUpgraderSkin.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class ExtensionUpgraderSkin extends UpgraderSkin {
2828
*/
2929
private $track_files = false;
3030

31+
/**
32+
* Whether file tracking hooks have been set up.
33+
*
34+
* @var bool
35+
*/
36+
private $hooks_setup = false;
37+
3138
/**
3239
* Enable file tracking for opcache invalidation.
3340
*/
@@ -49,6 +56,13 @@ public function get_changed_files() {
4956
* Setup hooks to track file changes during upgrade.
5057
*/
5158
private function setup_file_tracking_hooks() {
59+
// Prevent duplicate hook registrations.
60+
if ( $this->hooks_setup ) {
61+
return;
62+
}
63+
64+
$this->hooks_setup = true;
65+
5266
// Hook into upgrader_post_install to capture the destination directory
5367
add_filter(
5468
'upgrader_post_install',
@@ -73,16 +87,22 @@ private function scan_directory_for_files( $dir ) {
7387
return;
7488
}
7589

76-
$iterator = new \RecursiveIteratorIterator(
77-
new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ),
78-
\RecursiveIteratorIterator::SELF_FIRST
79-
);
90+
try {
91+
$iterator = new \RecursiveIteratorIterator(
92+
new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ),
93+
\RecursiveIteratorIterator::SELF_FIRST
94+
);
8095

81-
foreach ( $iterator as $file ) {
82-
/** @var \SplFileInfo $file */
83-
if ( $file->isFile() && 'php' === $file->getExtension() ) {
84-
$this->changed_files[] = $file->getPathname();
96+
foreach ( $iterator as $file ) {
97+
/** @var \SplFileInfo $file */
98+
if ( $file->isFile() && 'php' === $file->getExtension() ) {
99+
$this->changed_files[] = $file->getPathname();
100+
}
85101
}
102+
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Intentionally ignoring errors to prevent update failure.
103+
} catch ( \Exception $e ) {
104+
// Silently handle filesystem errors to prevent update process from failing.
105+
// The changed files list may be incomplete, but the update should still succeed.
86106
}
87107
}
88108

0 commit comments

Comments
 (0)