-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathUpgraderWithValidation.php
More file actions
70 lines (60 loc) · 2.15 KB
/
UpgraderWithValidation.php
File metadata and controls
70 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace WP_CLI;
use WP_CLI;
/**
* Trait for upgraders that validates downloaded packages before installation.
*
* This trait adds package validation to WP_Upgrader subclasses to detect and
* handle corrupted cache files and failed downloads.
*/
trait UpgraderWithValidation {
/**
* Downloads a package with validation.
*
* This method overrides WP_Upgrader::download_package() to add validation
* of the downloaded file before it's used for installation. If validation
* fails, the corrupted file is deleted and an error is returned.
*
* @param string $package The URI of the package.
* @param bool $check_signatures Whether to validate file signatures. Default false.
* @param array $hook_extra Extra arguments to pass to hooked filters. Default empty array.
* @return string|\WP_Error The full path to the downloaded package file, or a WP_Error object.
*/
public function download_package( $package, $check_signatures = false, $hook_extra = array() ) {
// Call parent download_package to get the file (from cache or fresh download).
$download = parent::download_package( $package, $check_signatures, $hook_extra );
// If download failed, return the error.
if ( is_wp_error( $download ) ) {
return $download;
}
// Validate the downloaded file.
$validation = PackageValidator::validate( $download );
// If validation passed, return the file path.
if ( true === $validation ) {
return $download;
}
// Validation failed - log the issue and clean up.
WP_CLI::debug(
sprintf(
'Package validation failed: %s',
$validation->get_error_message()
),
'extension-command'
);
// Delete the corrupted file to prevent it from being reused.
if ( PackageValidator::delete_corrupted_file( $download ) ) {
WP_CLI::debug(
'Deleted corrupted package file from cache.',
'extension-command'
);
}
// Return a detailed error message.
return new \WP_Error(
'package_validation_failed',
sprintf(
'Downloaded package failed validation (%s). The corrupted file has been removed from cache. Please try the command again.',
$validation->get_error_message()
)
);
}
}