Skip to content

Commit

Permalink
Merge pull request #138 from humanmade/backport-134-to-v6-branch
Browse files Browse the repository at this point in the history
[Backport v6-branch] [Backport v7-branch] Add unsupported version warning
  • Loading branch information
roborourke authored May 13, 2021
2 parents 4c997d9 + 9a7a377 commit 9b9317e
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"autoload": {
"files": [
"inc/namespace.php",
"inc/about/namespace.php"
"inc/about/namespace.php",
"inc/upgrades/namespace.php"
],
"classmap": [
"inc/"
Expand Down
33 changes: 33 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@
*/
function bootstrap() {
About\bootstrap();
Upgrades\bootstrap();
}

/**
* Get the major version of Altis in use.
*
* @return int|null Major version if available, or null if invalid or development version.
*/
function get_version() : ?int {
$data = get_composer_data();
$core = $data['altis/core'] ?? null;
if ( ! $core ) {
// Uh… I guess there's no Altis here then!
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 null;
}

$version = $core['version_normalized'];
$parts = explode( '.', $version, 2 );
if ( count( $parts ) < 2 ) {
// Invalid version, probably another development version.
return null;
}

$major = intval( $parts[0] );
return $major;
}

/**
Expand Down
248 changes: 248 additions & 0 deletions inc/upgrades/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php
/**
* Upgrade notice.
*
* @package altis/core
*/

namespace Altis\Upgrades;

use Altis;

/**
* Bootstrap the actions.
*/
function bootstrap() {
add_action( 'wp_network_dashboard_setup', __NAMESPACE__ . '\\register_widget' );
add_action( 'wp_dashboard_setup', __NAMESPACE__ . '\\register_widget' );
}

/**
* Register our dashboard widget.
*/
function register_widget() {
if ( ! current_user_can( 'edit_options' ) ) {
return;
}

if ( is_version_supported() ) {
return;
}

add_action( 'admin_head-index.php', function () {
?>
<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 () {
global $pagenow;
if ( $pagenow !== 'index.php' ) {
return;
}

render_widget();
} );
}

/**
* Get the supported version information from docs.altis-dxp.com.
*
* @param bool $skip_cache True to override the cache.
* @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_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();
$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 {
$release = get_current_version_info();
return $release['supported'] ?? true;
}

/**
* Render the unsupported version widget.
*
* @return void
*/
function render_widget() {
$version = Altis\get_version();
$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
// 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 9b9317e

Please sign in to comment.