CLI_Command::get_updates │ private │ WP-CLI 1.0
Returns update information.
Method of the class: CLI_Command{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->get_updates( $assoc_args );
- $assoc_args(required)
- .
CLI_Command::get_updates() CLI Command::get updates code WP-CLI 2.13.0-alpha
private function get_updates( $assoc_args ) {
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases?per_page=100';
$options = [
'timeout' => 30,
'insecure' => (bool) Utils\get_flag_value( $assoc_args, 'insecure', false ),
];
$headers = [
'Accept' => 'application/json',
];
$github_token = getenv( 'GITHUB_TOKEN' );
if ( false !== $github_token ) {
$headers['Authorization'] = 'token ' . $github_token;
}
$response = Utils\http_request( 'GET', $url, null, $headers, $options );
if ( ! $response->success || 200 !== $response->status_code ) {
WP_CLI::error( sprintf( 'Failed to get latest version (HTTP code %d).', $response->status_code ) );
}
$release_data = json_decode( $response->body );
$updates = [
'major' => false,
'minor' => false,
'patch' => false,
];
$updates_unavailable = [];
foreach ( $release_data as $release ) {
// Get rid of leading "v" if there is one set.
$release_version = $release->tag_name;
if ( 'v' === substr( $release_version, 0, 1 ) ) {
$release_version = ltrim( $release_version, 'v' );
}
$update_type = Utils\get_named_sem_ver( $release_version, WP_CLI_VERSION );
if ( ! $update_type ) {
continue;
}
// Release is older than one we already have on file.
if ( ! empty( $updates[ $update_type ] ) && ! Comparator::greaterThan( $release_version, $updates[ $update_type ]['version'] ) ) {
continue;
}
$package_url = null;
$manifest_data = null;
foreach ( $release->assets as $asset ) {
if ( ! isset( $asset->browser_download_url ) ) {
continue;
}
if ( substr( $asset->browser_download_url, - strlen( '.phar' ) ) === '.phar' ) {
$package_url = $asset->browser_download_url;
}
// The manifest.json file, if it exists, contains information about PHP version requirements and similar.
if ( substr( $asset->browser_download_url, - strlen( 'manifest.json' ) ) === 'manifest.json' ) {
$response = Utils\http_request( 'GET', $asset->browser_download_url, null, $headers, $options );
if ( $response->success ) {
$manifest_data = json_decode( $response->body );
}
}
}
if ( ! $package_url ) {
continue;
}
// Release requires a newer version of PHP.
if (
isset( $manifest_data->requires_php ) &&
! Comparator::greaterThanOrEqualTo( PHP_VERSION, $manifest_data->requires_php )
) {
$updates_unavailable[] = [
'version' => $release_version,
'update_type' => $update_type,
'package_url' => $release->assets[0]->browser_download_url,
'status' => 'unavailable',
'requires_php' => $manifest_data->requires_php,
];
} else {
$updates[ $update_type ] = [
'version' => $release_version,
'update_type' => $update_type,
'package_url' => $release->assets[0]->browser_download_url,
'status' => 'available',
'requires_php' => isset( $manifest_data->requires_php ) ? $manifest_data->requires_php : '',
];
}
}
foreach ( $updates as $type => $value ) {
if ( empty( $value ) ) {
unset( $updates[ $type ] );
}
}
foreach ( [ 'major', 'minor', 'patch' ] as $type ) {
if ( true === Utils\get_flag_value( $assoc_args, $type ) ) {
return ! empty( $updates[ $type ] ) ? [ $updates[ $type ] ] : false;
}
}
if ( empty( $updates ) && preg_match( '#-alpha-(.+)$#', WP_CLI_VERSION, $matches ) ) {
$version_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/NIGHTLY_VERSION';
$response = Utils\http_request( 'GET', $version_url, null, [], $options );
if ( ! $response->success || 200 !== $response->status_code ) {
WP_CLI::error( sprintf( 'Failed to get current nightly version (HTTP code %d)', $response->status_code ) );
}
$nightly_version = trim( $response->body );
if ( WP_CLI_VERSION !== $nightly_version ) {
$manifest_data = null;
// The manifest.json file, if it exists, contains information about PHP version requirements and similar.
$response = Utils\http_request( 'GET', 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.manifest.json', null, $headers, $options );
if ( $response->success ) {
$manifest_data = json_decode( $response->body );
}
// Release requires a newer version of PHP.
if (
isset( $manifest_data->requires_php ) &&
! Comparator::greaterThanOrEqualTo( PHP_VERSION, $manifest_data->requires_php )
) {
$updates_unavailable[] = [
'version' => $nightly_version,
'update_type' => 'nightly',
'package_url' => 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar',
'status' => 'unvailable',
'requires_php' => $manifest_data->requires_php,
];
} else {
$updates['nightly'] = [
'version' => $nightly_version,
'update_type' => 'nightly',
'package_url' => 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar',
'status' => 'available',
'requires_php' => isset( $manifest_data->requires_php ) ? $manifest_data->requires_php : '',
];
}
}
}
return array_merge( $updates_unavailable, array_values( $updates ) );
}