Last active
March 6, 2024 13:28
-
-
Save xxsimoxx/30a8c3bf737d37e352310bd3e4e96e67 to your computer and use it in GitHub Desktop.
Functions 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
/** | |
* Get a list of ClassicPress released versions from api-v1.classicpress.net. | |
* | |
* @return array|false Array of CP versions or false on API failure. | |
*/ | |
function get_cp_versions() { | |
$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 a list of ClassicPress released versions from api.github.com. | |
* | |
* @return array|false URL for release. Empty string if no release found. False on API failure. | |
*/ | |
function get_migration_from_cp_version($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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment