Last active
March 8, 2024 15:31
-
-
Save xxsimoxx/84a45dd996777d44bb213cffc3f47b0d to your computer and use it in GitHub Desktop.
Class to get a list of all CP releases and to get the migration URL from a release.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ClassicPressReleases { | |
/** | |
* Get migration URL fetching created_at from api.github.com. | |
* | |
* @param string $version Version to retrive migration URL. | |
* | |
* @return string|bool URL for release. Empty string if no release found. False on API failure. | |
*/ | |
public static function getMigrationFromCPVersion($version) { | |
$response = wp_remote_get('https://api.github.com/repos/ClassicPress/ClassicPress-release/releases/tags/'.$version, ['timeout'=>3]); | |
if (is_wp_error($response) || empty($response)) { | |
return false; | |
} | |
$data = json_decode(wp_remote_retrieve_body($response), true); | |
if(isset($data['message']) && $data['message'] === 'Not Found') { | |
return ''; | |
} | |
$created_at = new \DateTime($data['created_at']); | |
$day = $created_at->format('Ymd'); | |
$exploded = explode('.', $version); | |
$major = $exploded[0]; | |
$url = 'https://github.com/ClassyBot/ClassicPress-v'.$major.'-nightly/releases/download/'.$version.'%2Bmigration.'.$day.'/ClassicPress-nightly-'.$version.'-migration.'.$day.'.zip'; | |
return $url; | |
} | |
/** | |
* Get release URL. | |
* | |
* @param string $version Version to retrive migration URL. | |
* | |
* @return string URL for release. | |
*/ | |
public static function getReleaseFromCPVersion($version) { | |
return 'https://github.com/ClassicPress/ClassicPress-release/archive/refs/tags/'.$version.'.zip'; | |
} | |
/** | |
* Get a list of ClassicPress released versions from api-v1.classicpress.net. | |
* | |
* @return array|bool Array of CP versions or false on API failure. | |
*/ | |
public static function getCPVersions() { | |
$response = wp_remote_get('https://api-v1.classicpress.net/v1/upgrade/index.php', ['timeout'=>3]); | |
if (is_wp_error($response) || empty($response)) { | |
return false; | |
} | |
$versions = json_decode(wp_remote_retrieve_body($response)); | |
// Get only stable releases | |
foreach ($versions as $key => $version) { | |
if(!str_contains($version, 'nightly') && !str_contains($version, 'rc') && !str_contains($version, 'alpha') && !str_contains($version, 'beta')) { | |
continue; | |
} | |
unset($versions[$key]); | |
} | |
// Strip .json from version | |
$versions = array_map( | |
function($v) { | |
return substr($v, 0, -5); | |
}, | |
$versions | |
); | |
// Sort using SemVer | |
usort($versions, 'version_compare'); | |
return array_values($versions); | |
} | |
/** | |
* Get previous release version. | |
* | |
* @param string $version Version to get previous release. | |
* @param array $versions Array of ClassicPress versions as | |
* returned by getCPVersions(). | |
* Used for caching. | |
* | |
* @return string|bool Previous version. False if not found. | |
*/ | |
public static function previous($version, $versions = []) { | |
if (empty($versions)) { | |
$versions = self::getCPVersions(); | |
} else { | |
usort($versions, 'version_compare'); | |
} | |
if(!in_array($version, $versions)) { | |
return false; | |
} | |
$pos = array_search($version, $versions, true); | |
if(!isset($versions[$pos - 1])) { | |
return false; | |
} | |
return $versions[$pos - 1];; | |
} | |
/** | |
* Get latest release version URL. | |
* | |
* @param string $major Major release. Default 2. | |
* @param array $versions Array of ClassicPress versions as | |
* returned by getCPVersions(). | |
* Used for caching. | |
* | |
* @return string|bool Latest elease URL. | |
*/ | |
public static function latest($major = '2', $versions = []) { | |
if (empty($versions)) { | |
$versions = self::getCPVersions(); | |
} else { | |
usort($versions, 'version_compare'); | |
} | |
foreach ($versions as $index => $version) { | |
$exploded = explode('.', $version); | |
if ($exploded[0] === $major) { | |
continue; | |
} | |
unset($versions[$index]); | |
} | |
return array_pop($versions); | |
} | |
/** | |
* Compare two versions. | |
* | |
* @param string $v1 Version to compare. | |
* @param string $v1 Version to compare. | |
* | |
* @return string Major, minor, patch or equal. | |
*/ | |
public static function compare($v1, $v2) { | |
if ($v1 === $v2) { | |
return 'equal'; | |
} | |
$v1_split = explode('.', $v1); | |
$v2_split = explode('.', $v2); | |
if ($v1_split[0] !== $v2_split[0]) { | |
return 'major'; | |
} | |
if ($v1_split[1] !== $v2_split[1]) { | |
return 'minor'; | |
} | |
return 'patch'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment