diff --git a/inc/namespace.php b/inc/namespace.php index b7a2777..e3e0e7f 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -27,19 +27,22 @@ function get_version() : ?int { $core = $data['altis/core'] ?? null; if ( ! $core ) { // Uh… I guess there's no Altis here then! - return false; + return null; } + // Cast to array for Composer v2 compat. + $core = (array) $core; + // Is this a dev version? if ( substr( $core['version'], 0, 4 ) === 'dev-' ) { - return false; + return null; } $version = $core['version_normalized']; $parts = explode( '.', $version, 2 ); if ( count( $parts ) < 2 ) { // Invalid version, probably another development version. - return false; + return null; } $major = intval( $parts[0] ); diff --git a/inc/upgrades/namespace.php b/inc/upgrades/namespace.php index d5f21df..a9124d1 100644 --- a/inc/upgrades/namespace.php +++ b/inc/upgrades/namespace.php @@ -1,4 +1,9 @@ - - + + get_error_message(), E_USER_WARNING ); + return null; + } + + // Parse JSON. + $releases = json_decode( wp_remote_retrieve_body( $response ), ARRAY_A ); + if ( json_last_error() !== JSON_ERROR_NONE ) { + trigger_error( json_last_error_msg(), E_USER_WARNING ); + return null; + } + + // Ensure releases are sorted by date. + array_multisort( + array_map( 'strtotime', array_column( $releases, 'date' ) ), + SORT_DESC, + $releases + ); + + wp_cache_set( 'releases', $releases, 'altis-core', WEEK_IN_SECONDS ); + + return $releases; +} + +/** + * Get data for current version of Altis. + * + * @return array|null */ -function get_supported_version_info() { - return [ - 'supported' => [ - 4, - 5, - 6, - 7, - ], - 'latest' => [ - 'version' => 7, - 'date' => '2021-05-04', - 'blog' => 'https://www.altis-dxp.com/foo/', - ], - ]; +function get_current_version_info() : ?array { + $releases = get_supported_version_info(); + if ( empty( $releases ) ) { + return null; + } + + $version = Altis\get_version(); + + foreach ( $releases as $release ) { + if ( $release['version'] === $version ) { + return $release; + } + } + + return null; } /** * Is this site on the latest version of Altis? * + * If we can't resolve the release data assume current version is latest. + * * @return bool */ function is_version_latest() : bool { $version = Altis\get_version(); - $support = get_supported_version_info(); - return $version === $support['latest']['version']; + $releases = get_supported_version_info(); + return $version === ( $releases[0]['version'] ?? $version ); } /** * Is this site on a supported version of Altis? * + * If version information cannot be resolved assume it is supported. + * * @return bool */ function is_version_supported() : bool { - $version = Altis\get_version(); - $support = get_supported_version_info(); - return in_array( $version, $support['supported'], true ); + $release = get_current_version_info(); + return $release['supported'] ?? true; } /** @@ -126,53 +185,64 @@ function is_version_supported() : bool { */ function render_widget() { $version = Altis\get_version(); - $info = get_supported_version_info(); + $releases = get_supported_version_info(); + if ( empty( $releases ) ) { + return; + } + + $latest = $releases[0]; ?>
-
-

- - - -

-

- -

Altis v%d, released on %s.', 'altis' ), - $info['latest']['blog'], - $info['latest']['version'], - date_i18n( get_option( 'date_format' ), strtotime( $info['latest']['date'] ) ) - ), 'a' ); - ?>

- -

- -

- - %2$s', - __( 'Learn more about Altis upgrades' ), - /* translators: Accessibility text. */ - __( '(opens in a new tab)' ) - ), 'span' ); - ?> - - -

-
+
+

+ + + +

+

+ +

+ +

+ Altis v%d, released on %s.', 'altis' ), + $latest['blog'] ?: "https://github.com/humanmade/altis/releases/tag/{$latest['tag']}", + $latest['version'], + date_i18n( get_option( 'date_format' ), strtotime( $latest['date'] ) ) + ), 'a' ); + ?> +

+ +

+ +

+ + %2$s', + __( 'Learn more about Altis upgrades' ), + /* translators: Accessibility text. */ + __( '(opens in a new tab)' ) + ), 'span' ); + ?> + + +

+