Skip to content

Commit f80b016

Browse files
Copilotswissspidy
andcommitted
Simplify cache validation retry logic
Instead of attempting automatic retry, delete corrupted file and ask user to retry. This is simpler and more reliable since WP-CLI cache manager handles re-downloads. Co-authored-by: swissspidy <[email protected]>
1 parent e6c6ab6 commit f80b016

File tree

1 file changed

+13
-70
lines changed

1 file changed

+13
-70
lines changed

src/WP_CLI/UpgraderWithValidation.php

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait UpgraderWithValidation {
1717
*
1818
* This method overrides WP_Upgrader::download_package() to add validation
1919
* of the downloaded file before it's used for installation. If validation
20-
* fails, the file is deleted and re-downloaded.
20+
* fails, the corrupted file is deleted and an error is returned.
2121
*
2222
* @param string $package The URI of the package.
2323
* @param bool $check_signatures Whether to validate file signatures. Default false.
@@ -41,7 +41,7 @@ public function download_package( $package, $check_signatures = false, $hook_ext
4141
return $download;
4242
}
4343

44-
// Validation failed - log the issue and attempt recovery.
44+
// Validation failed - log the issue and clean up.
4545
WP_CLI::debug(
4646
sprintf(
4747
'Package validation failed: %s',
@@ -50,78 +50,21 @@ public function download_package( $package, $check_signatures = false, $hook_ext
5050
'extension-command'
5151
);
5252

53-
// Delete the corrupted file.
54-
PackageValidator::delete_corrupted_file( $download );
55-
WP_CLI::debug(
56-
'Deleted corrupted package file, attempting fresh download...',
57-
'extension-command'
58-
);
59-
60-
// Try to download again by clearing any cache.
61-
// We need to bypass the cache, which we can do by using a modified package URL.
62-
// However, WP_Upgrader doesn't provide a direct way to do this.
63-
// Instead, we'll use a filter to modify the download behavior.
64-
$retry_download = $this->download_package_retry( $package, $check_signatures, $hook_extra );
65-
66-
// If retry succeeded, validate it again.
67-
if ( ! is_wp_error( $retry_download ) ) {
68-
$retry_validation = PackageValidator::validate( $retry_download );
69-
70-
if ( true === $retry_validation ) {
71-
WP_CLI::debug( 'Fresh download succeeded and validated.', 'extension-command' );
72-
return $retry_download;
73-
}
74-
75-
// Even the retry is corrupted - delete it and give up.
76-
PackageValidator::delete_corrupted_file( $retry_download );
77-
WP_CLI::debug( 'Retry download also failed validation.', 'extension-command' );
53+
// Delete the corrupted file to prevent it from being reused.
54+
if ( PackageValidator::delete_corrupted_file( $download ) ) {
55+
WP_CLI::debug(
56+
'Deleted corrupted package file from cache.',
57+
'extension-command'
58+
);
7859
}
7960

80-
// Both attempts failed - return an error.
61+
// Return a detailed error message.
8162
return new \WP_Error(
8263
'package_validation_failed',
83-
'Downloaded package failed validation. The file may be corrupted or the download URL may be returning an error instead of a valid zip file. Please check your network connection and try again.'
64+
sprintf(
65+
'Downloaded package failed validation (%s). The corrupted file has been removed from cache. Please try the command again.',
66+
$validation->get_error_message()
67+
)
8468
);
8569
}
86-
87-
/**
88-
* Retries downloading a package, bypassing cache.
89-
*
90-
* This is called when the initial download (which may have come from cache)
91-
* failed validation.
92-
*
93-
* @param string $package The URI of the package.
94-
* @param bool $check_signatures Whether to validate file signatures.
95-
* @param array $hook_extra Extra arguments to pass to hooked filters.
96-
* @return string|\WP_Error The full path to the downloaded package file, or a WP_Error object.
97-
*/
98-
private function download_package_retry( $package, $check_signatures, $hook_extra ) {
99-
// Add a filter to disable caching for this download.
100-
$disable_cache = function( $args, $url ) {
101-
// Disable caching by setting a short timeout and unique filename.
102-
$args['reject_cache'] = true;
103-
return $args;
104-
};
105-
106-
// WP HTTP API doesn't have reject_cache, so we'll use a different approach.
107-
// We'll hook into pre_http_request to force a fresh download.
108-
$force_fresh_download = function( $preempt, $args, $url ) use ( $package ) {
109-
// Only apply to our specific package URL.
110-
if ( $url !== $package ) {
111-
return $preempt;
112-
}
113-
// Return false to proceed with the request (not using preempt).
114-
// The cache is managed by WP-CLI's cache manager, not WP's HTTP API.
115-
return false;
116-
};
117-
118-
add_filter( 'pre_http_request', $force_fresh_download, 10, 3 );
119-
120-
// Attempt the download again.
121-
$result = parent::download_package( $package, $check_signatures, $hook_extra );
122-
123-
remove_filter( 'pre_http_request', $force_fresh_download, 10 );
124-
125-
return $result;
126-
}
12770
}

0 commit comments

Comments
 (0)