|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Resolve the WP version to use for the tests. |
| 4 | + */ |
| 5 | + |
| 6 | +use Composer\Semver\Semver; |
| 7 | + |
| 8 | +const WP_VERSIONS_JSON_FILE = '/wp-cli-tests-wp-versions.json'; |
| 9 | +const WP_VERSIONS_JSON_URL = 'https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json'; |
| 10 | + |
| 11 | +$wp_version_env = getenv( 'WP_VERSION' ); |
| 12 | +$wp_versions_file_path = sys_get_temp_dir() . WP_VERSIONS_JSON_FILE; |
| 13 | + |
| 14 | +if ( ! file_exists( $wp_versions_file_path ) ) { |
| 15 | + $ch = curl_init( WP_VERSIONS_JSON_URL ); |
| 16 | + $fp = fopen( $wp_versions_file_path, 'w' ); |
| 17 | + |
| 18 | + curl_setopt( $ch, CURLOPT_FILE, $fp ); |
| 19 | + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); |
| 20 | + |
| 21 | + curl_exec( $ch ); |
| 22 | + curl_close( $ch ); |
| 23 | + fclose( $fp ); |
| 24 | +} |
| 25 | + |
| 26 | +$wp_versions_json = json_decode( file_get_contents( sys_get_temp_dir() . WP_VERSIONS_JSON_FILE ), true ); |
| 27 | + |
| 28 | +if ( empty( $wp_version_env ) ) { |
| 29 | + echo $wp_version_env; |
| 30 | + exit( 0 ); |
| 31 | +} |
| 32 | + |
| 33 | +if ( empty( $wp_version_env ) || 'latest' === $wp_version_env ) { |
| 34 | + $wp_version = array_search( 'latest', $wp_versions_json, true ); |
| 35 | + |
| 36 | + if ( empty( $wp_version ) ) { |
| 37 | + $wp_version = $wp_version_env; |
| 38 | + } |
| 39 | + |
| 40 | + echo $wp_version; |
| 41 | + exit( 0 ); |
| 42 | +} |
| 43 | + |
| 44 | +$wp_version = ''; |
| 45 | +$constraint = ''; |
| 46 | +$wp_versions = array_keys( $wp_versions_json ); |
| 47 | +$version_count = count( explode( '.', $wp_version_env ) ); |
| 48 | + |
| 49 | +if ( 1 === $version_count ) { |
| 50 | + $constraint = ">=$wp_version_env"; // Get the latest minor version. |
| 51 | +} elseif ( 2 === $version_count ) { |
| 52 | + $constraint = "~$wp_version_env.0"; // Get the latest patch version. |
| 53 | +} elseif ( 3 === $version_count ) { |
| 54 | + $constraint = "=$wp_version_env"; // Get the exact version. |
| 55 | +} else { |
| 56 | + $constraint = $wp_version_env; |
| 57 | +} |
| 58 | + |
| 59 | +if ( ! class_exists( 'Composer\Semver\Semver' ) ) { |
| 60 | + require_once __DIR__ . '/../vendor/autoload.php'; |
| 61 | +} |
| 62 | + |
| 63 | +try { |
| 64 | + $wp_satisfied_versions = Semver::satisfiedBy( $wp_versions, $constraint ); |
| 65 | +} catch ( Exception $e ) { |
| 66 | + echo $wp_version_env; |
| 67 | + exit( 0 ); |
| 68 | +} |
| 69 | + |
| 70 | +$wp_version = end( $wp_satisfied_versions ); |
| 71 | +echo $wp_version ? : $wp_version_env; |
0 commit comments