-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathwp-version-resolver.php
More file actions
69 lines (54 loc) · 1.99 KB
/
wp-version-resolver.php
File metadata and controls
69 lines (54 loc) · 1.99 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
<?php
/**
* Resolve the WP version to use for the tests.
*/
use Composer\Semver\Semver;
const WP_VERSIONS_JSON_FILE = '/wp-cli-tests-wp-versions.json';
const WP_VERSIONS_JSON_URL = 'https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json';
$wp_version_env = getenv( 'WP_VERSION' );
$wp_versions_file_path = sys_get_temp_dir() . WP_VERSIONS_JSON_FILE;
if ( ! file_exists( $wp_versions_file_path ) ) {
$ch = curl_init( WP_VERSIONS_JSON_URL );
$fp = fopen( $wp_versions_file_path, 'w' );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_exec( $ch );
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated
curl_close( $ch );
}
fclose( $fp );
}
$wp_versions_json = json_decode( file_get_contents( $wp_versions_file_path ), true );
if ( empty( $wp_version_env ) || 'latest' === $wp_version_env ) {
$wp_version = array_search( 'latest', $wp_versions_json, true );
if ( empty( $wp_version ) ) {
$wp_version = $wp_version_env;
}
echo $wp_version;
exit( 0 );
}
$wp_version = '';
$constraint = '';
$wp_versions = array_keys( $wp_versions_json );
$version_count = count( explode( '.', $wp_version_env ) );
if ( 1 === $version_count ) {
$constraint = "^$wp_version_env"; // Get the latest minor version.
} elseif ( 2 === $version_count ) {
$constraint = "~$wp_version_env.0"; // Get the latest patch version.
} elseif ( 3 === $version_count ) {
$constraint = "=$wp_version_env"; // Get the exact version.
} else {
$constraint = $wp_version_env;
}
if ( ! class_exists( 'Composer\Semver\Semver' ) ) {
require_once __DIR__ . '/../vendor/autoload.php';
}
try {
$wp_satisfied_versions = Semver::satisfiedBy( $wp_versions, $constraint );
} catch ( Exception $e ) {
echo $wp_version_env;
exit( 0 );
}
$wp_version = end( $wp_satisfied_versions );
echo $wp_version ? : $wp_version_env;