Skip to content

Commit

Permalink
Merge pull request #283 from wp-cli/try/wporg-api
Browse files Browse the repository at this point in the history
Use existing `WpOrgApi` helper class
  • Loading branch information
swissspidy authored Feb 9, 2025
2 parents e804145 + 002188b commit f114e2f
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,31 +1013,19 @@ private static function find_var( $var_name, $code ) {
* @return string|array String message on failure. An array of checksums on success.
*/
private static function get_core_checksums( $version, $locale, $insecure ) {
$query = http_build_query( compact( 'version', 'locale' ), '', '&' );
$url = "https://api.wordpress.org/core/checksums/1.0/?{$query}";
$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );

$headers = [ 'Accept' => 'application/json' ];
$options = [
'timeout' => 30,
'insecure' => $insecure,
];

$response = Utils\http_request( 'GET', $url, null, $headers, $options );

if ( ! $response->success || 200 !== (int) $response->status_code ) {
return "Checksum request '{$url}' failed (HTTP {$response->status_code}).";
try {
$checksums = $wp_org_api->get_core_checksums( $version, $locale );
} catch ( Exception $exception ) {
return $exception->getMessage();
}

$body = trim( $response->body );
$body = json_decode( $body, true );

if ( ! is_array( $body )
|| ! isset( $body['checksums'] )
|| ! is_array( $body['checksums'] ) ) {
if ( false === $checksums ) {
return "Checksums not available for WordPress {$version}/{$locale}.";
}

return $body['checksums'];
return $checksums;
}

/**
Expand Down Expand Up @@ -1437,9 +1425,11 @@ private function cleanup_extra_files( $version_from, $version_to, $locale, $inse
WP_CLI::warning( "{$old_checksums} Please cleanup files manually." );
return;
}

$new_checksums = self::get_core_checksums( $version_to, $locale ?: 'en_US', $insecure );
if ( ! is_array( $new_checksums ) ) {
WP_CLI::warning( "{$new_checksums} Please cleanup files manually." );

return;
}

Expand Down

0 comments on commit f114e2f

Please sign in to comment.