Skip to content

Commit

Permalink
# [LOW] New version families are detected as development, not stable,…
Browse files Browse the repository at this point in the history
… versions
  • Loading branch information
nikosdion committed Jul 23, 2024
1 parent 2546c0a commit ba7521c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Panopticon Connector for WordPress 1.0.1
================================================================================
# [LOW] New version families are detected as development, not stable, versions

Panopticon Connector for WordPress 1.0.0
================================================================================

Expand Down
65 changes: 65 additions & 0 deletions includes/panopticon-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public function getUpdate(WP_REST_Request $request)
$needsUpdate = false;
}

$currentVersion = $this->normalizeVersion($currentVersion);
$latestVersion = $this->normalizeVersion($latestVersion);

// Return something sensible and predictable our code can query easily.
return new WP_REST_Response(
[
Expand Down Expand Up @@ -370,6 +373,68 @@ public function ensureCanUpdateCore(): bool
return current_user_can('update_core');
}

/**
* Normalize a WordPress version number to the SemVer specification.
*
* WordPress skips the patch version number if it's 0, i.e. they use 6.6 instead of 6.6.0. This is an invalid
* version number, causing the 3rd party version parsing library to mark it as a "development" release.
*
* This method detects version numbers like that, normalising them to the SemVer specification.
*
* @param string $versionString
* @return string
*/
private function normalizeVersion(string $versionString): string
{
/**
* WordPress fails to follow semantic versioning. They treat .0 version specifiers as optional. For example,
* they do version 6.6 not 6.6.0. The patch version specified is never optional! Semantic versions always have
* three parts. We are hereby fixing this.
*/
$parts = explode('.', $versionString);

if (count($parts) >= 3)
{
return $versionString;
}

$suffix = '';
$lastPart = $parts[count($parts) - 1];
$temp = explode(' ', $lastPart, 2);

if (count($temp) === 2)
{
$lastPart = $temp[0];
$suffix = $temp[1];
}
else
{
$temp = explode('-', $lastPart, 2);

if (count($temp) === 2)
{
$lastPart = $temp[0];
$suffix = $temp[1];
}
}

$parts[count($parts) - 1] = $lastPart;

for ($i = count($parts); $i < 3; $i++)
{
$parts[] = '0';
}

$versionString = implode('.', $parts);

if ($suffix)
{
$versionString .= '-' . $suffix;
}

return $versionString;
}

/**
* Detects the stability of a version string
*
Expand Down

0 comments on commit ba7521c

Please sign in to comment.