Skip to content

Commit

Permalink
Fetch releases data from docs site
Browse files Browse the repository at this point in the history
  • Loading branch information
roborourke committed May 13, 2021
1 parent 850b943 commit d8d7b87
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 106 deletions.
9 changes: 6 additions & 3 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ function get_version() : ?int {
$core = $data['altis/core'] ?? null;
if ( ! $core ) {
// UhI 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] );
Expand Down
276 changes: 173 additions & 103 deletions inc/upgrades/namespace.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Upgrade notice.
*
* @package altis/core
*/

namespace Altis\Upgrades;

Expand All @@ -25,44 +30,50 @@ function register_widget() {
}

add_action( 'admin_head-index.php', function () {
?>
<style>
#altis-upgrade-warning {
position: relative;
margin: 40px 0 10px;
padding: 6px 10px;
border: 2px solid #df3232;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
background: #fff;
font-size: 13px;
line-height: 1.7;
}

#altis-upgrade-warning h1 {
background: #df3232;
color: #f0f0f0;
padding: 6px 10px;
margin: -6px -10px 0;
}

#altis-upgrade-warning h1 .dashicons {
color: #df3232;
color: #f0f0f0;
font-size: 23px;
height: 23px;
width: 23px;
line-height: 1.3;
}

#altis-upgrade-warning p {
font-size: 14px;
}

#altis-upgrade-warning .button .dashicons-external {
line-height: 43px;
}
</style>
<?php
?>
<style>
#altis-upgrade-warning {
position: relative;
margin: 40px 0 10px;
padding: 8px 12px;
border: 2px solid #df3232;
border-radius: 1px;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
background: #fff;
font-size: 13px;
line-height: 1.7;
}

#altis-upgrade-warning h1 {
background: #df3232;
color: #f0f0f0;
padding: 8px 12px;
margin: -8px -12px 0;
font-weight: bold;
}

#altis-upgrade-warning h1 .dashicons {
color: #df3232;
color: #f0f0f0;
font-size: 23px;
height: 23px;
width: 23px;
line-height: 1.3;
}

#altis-upgrade-warning p {
font-size: 14px;
}

#altis-upgrade-warning p:last-of-type {
margin-bottom: 4px;
}

#altis-upgrade-warning .button .dashicons-external {
line-height: 43px;
}
</style>
<?php
} );

add_action( 'all_admin_notices', function () {
Expand All @@ -76,47 +87,95 @@ function register_widget() {
}

/**
* Get the supported version information from altis-dxp.com
* Get the supported version information from docs.altis-dxp.com.
*
* @param bool $skip_cache True to override the cache.
* @return array Data from altis-dxp.com
* @return array|null Data from altis-dxp.com
*/
function get_supported_version_info( bool $skip_cache = false ) : ?array {
$releases = wp_cache_get( 'releases', 'altis-core' );
if ( ! empty( $releases ) && ! $skip_cache ) {
return $releases;
}

// Fetch releases data.
$response = wp_remote_get( 'https://docs.altis-dxp.com/releases.json' );

// Handle any error response.
if ( is_wp_error( $response ) ) {
/**
* The wp_remote_get error response.
*
* @var WP_Error $response
*/
trigger_error( $response->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;
}

/**
Expand All @@ -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];

?>
<div class="wrap">
<div id="altis-upgrade-warning">
<h1>
<span aria-hidden="true" class="dashicons dashicons-warning"></span>
<span class="screen-reader-text"><?php esc_html_e( 'Warning:', 'altis' ) ?></span>
<?php esc_html_e( 'Altis Upgrade Required', 'altis' ) ?>
</h1>
<p><?php
echo esc_html( sprintf(
__( 'You are running an unsupported version of Altis (Altis v%d).', 'altis' ),
$version
) );
?></p>

<p><?php
echo clean_html( sprintf(
__( 'The latest version of Altis is <a href="%s">Altis v%d</a>, released on %s.', 'altis' ),
$info['latest']['blog'],
$info['latest']['version'],
date_i18n( get_option( 'date_format' ), strtotime( $info['latest']['date'] ) )
), 'a' );
?></p>

<p><?php esc_html_e( 'Newer versions of Altis are faster and come with new features included in your existing licensing cost.', 'altis' ) ?></p>

<p class="button-container">
<a
class="button button-primary button-hero"
href="https://docs.altis-dxp.com/guides/upgrading/"
target="_blank"
rel="noopener noreferrer"
>
<?php
echo clean_html( sprintf(
'%1$s <span class="screen-reader-text">%2$s</span>',
__( 'Learn more about Altis upgrades' ),
/* translators: Accessibility text. */
__( '(opens in a new tab)' )
), 'span' );
?>
<span aria-hidden="true" class="dashicons dashicons-external"></span>
</a>
</p>
</div>
<div id="altis-upgrade-warning">
<h1>
<span aria-hidden="true" class="dashicons dashicons-warning"></span>
<span class="screen-reader-text"><?php esc_html_e( 'Warning:', 'altis' ) ?></span>
<?php esc_html_e( 'Altis Upgrade Required', 'altis' ) ?>
</h1>
<p>
<?php
echo esc_html( sprintf(
__( 'You are running an unsupported version of Altis (Altis v%d).', 'altis' ),
$version
) );
?>
</p>

<p>
<?php
// phpcs:ignore
echo clean_html( sprintf(
__( 'The latest version of Altis is <a href="%s">Altis v%d</a>, 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' );
?>
</p>

<p><?php esc_html_e( 'Newer versions of Altis are faster and come with new features included in your existing licensing cost.', 'altis' ) ?></p>

<p class="button-container">
<a
class="button button-primary button-hero"
href="https://docs.altis-dxp.com/guides/upgrading/"
target="_blank"
rel="noopener noreferrer"
>
<?php
// phpcs:ignore
echo clean_html( sprintf(
'%1$s <span class="screen-reader-text">%2$s</span>',
__( 'Learn more about Altis upgrades' ),
/* translators: Accessibility text. */
__( '(opens in a new tab)' )
), 'span' );
?>
<span aria-hidden="true" class="dashicons dashicons-external"></span>
</a>
</p>
</div>
</div>
<?php
}

0 comments on commit d8d7b87

Please sign in to comment.