|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI; |
| 4 | + |
| 5 | +use WP_CLI; |
| 6 | + |
| 7 | +/** |
| 8 | + * Trait for upgraders that validates downloaded packages before installation. |
| 9 | + * |
| 10 | + * This trait adds package validation to WP_Upgrader subclasses to detect and |
| 11 | + * handle corrupted cache files and failed downloads. |
| 12 | + */ |
| 13 | +trait UpgraderWithValidation { |
| 14 | + |
| 15 | + /** |
| 16 | + * Downloads a package with validation. |
| 17 | + * |
| 18 | + * This method overrides WP_Upgrader::download_package() to add validation |
| 19 | + * of the downloaded file before it's used for installation. If validation |
| 20 | + * fails, the file is deleted and re-downloaded. |
| 21 | + * |
| 22 | + * @param string $package The URI of the package. |
| 23 | + * @param bool $check_signatures Whether to validate file signatures. Default false. |
| 24 | + * @param array $hook_extra Extra arguments to pass to hooked filters. Default empty array. |
| 25 | + * @return string|\WP_Error The full path to the downloaded package file, or a WP_Error object. |
| 26 | + */ |
| 27 | + public function download_package( $package, $check_signatures = false, $hook_extra = array() ) { |
| 28 | + // Call parent download_package to get the file (from cache or fresh download). |
| 29 | + $download = parent::download_package( $package, $check_signatures, $hook_extra ); |
| 30 | + |
| 31 | + // If download failed, return the error. |
| 32 | + if ( is_wp_error( $download ) ) { |
| 33 | + return $download; |
| 34 | + } |
| 35 | + |
| 36 | + // Validate the downloaded file. |
| 37 | + $validation = PackageValidator::validate( $download ); |
| 38 | + |
| 39 | + // If validation passed, return the file path. |
| 40 | + if ( true === $validation ) { |
| 41 | + return $download; |
| 42 | + } |
| 43 | + |
| 44 | + // Validation failed - log the issue and attempt recovery. |
| 45 | + WP_CLI::debug( |
| 46 | + sprintf( |
| 47 | + 'Package validation failed: %s', |
| 48 | + $validation->get_error_message() |
| 49 | + ), |
| 50 | + 'extension-command' |
| 51 | + ); |
| 52 | + |
| 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' ); |
| 78 | + } |
| 79 | + |
| 80 | + // Both attempts failed - return an error. |
| 81 | + return new \WP_Error( |
| 82 | + '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.' |
| 84 | + ); |
| 85 | + } |
| 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 | + } |
| 127 | +} |
0 commit comments