$versions WordPress.org version map, version => package URL. * @return string[] Version strings, newest first. */ public static function releases( array $versions ): array { $releases = array(); foreach ( array_keys( $versions ) as $version ) { $version = (string) $version; if ( '' === $version || in_array( strtolower( $version ), self::NOT_RELEASES, true ) ) { continue; } // Must begin with a digit, or version_compare() has nothing to work with. if ( 1 !== preg_match( '/^\d/', $version ) ) { continue; } $releases[] = $version; } $releases = array_values( array_unique( $releases ) ); usort( $releases, static function ( string $a, string $b ): int { return version_compare( $b, $a ); } ); return $releases; } /** * One page of a version list. * * @since 0.0.45 * @param string[] $releases Releases, newest first. * @param int $limit Page size; clamped to 1..MAX_LIMIT. * @param int $offset Zero-based offset; negatives treated as zero. * @return array{versions:string[],total:int,limit:int,offset:int,has_more:bool} */ public static function page( array $releases, int $limit = self::DEFAULT_LIMIT, int $offset = 0 ): array { $total = count( $releases ); $limit = max( 1, min( $limit, self::MAX_LIMIT ) ); $offset = max( 0, $offset ); $slice = array_slice( $releases, $offset, $limit ); return array( 'versions' => array_values( $slice ), 'total' => $total, 'limit' => $limit, 'offset' => $offset, 'has_more' => ( $offset + count( $slice ) ) < $total, ); } /** * How a candidate version relates to what is installed. * * Named rather than returned as a number because the caller's question is "is this a downgrade?" * and `-1` does not answer it legibly in a response payload. * * @since 0.0.45 * @param string $candidate Target version. * @param string $installed Currently installed version. * @return string `older`, `same`, `newer`, or `unknown` when either cannot be compared. */ public static function relation( string $candidate, string $installed ): string { if ( '' === $candidate || '' === $installed ) { return 'unknown'; } $compared = version_compare( $candidate, $installed ); if ( 0 === $compared ) { return 'same'; } return $compared < 0 ? 'older' : 'newer'; } /** * Whether a version appears in the published release list. * * Exact string match, not a `version_compare` equality: WordPress.org keys the package map by * the exact tag, and `1.0` and `1.0.0` are different keys there even though `version_compare` * calls them equal. Asking for a tag that does not exist must fail rather than resolve to a * neighbour. * * @since 0.0.45 * @param string $version Candidate. * @param string[] $releases Published releases. * @return bool */ public static function is_published( string $version, array $releases ): bool { return in_array( $version, $releases, true ); } }