Skip to content

Commit

Permalink
Add first pass at upgrade warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccue authored and roborourke committed May 13, 2021
1 parent 3e66be5 commit 850b943
Show file tree
Hide file tree
Showing 3 changed files with 210 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
30 changes: 30 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@
*/
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 false;
}

// Is this a dev version?
if ( substr( $core['version'], 0, 4 ) === 'dev-' ) {
return false;
}

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

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

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

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: 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
} );

add_action( 'all_admin_notices', function () {
global $pagenow;
if ( $pagenow !== 'index.php' ) {
return;
}

render_widget();
} );
}

/**
* Get the supported version information from altis-dxp.com
*
* @param bool $skip_cache True to override the cache.
* @return array Data from altis-dxp.com
*/
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/',
],
];
}

/**
* Is this site on the latest version of Altis?
*
* @return bool
*/
function is_version_latest() : bool {
$version = Altis\get_version();
$support = get_supported_version_info();
return $version === $support['latest']['version'];
}

/**
* Is this site on a supported version of Altis?
*
* @return bool
*/
function is_version_supported() : bool {
$version = Altis\get_version();
$support = get_supported_version_info();
return in_array( $version, $support['supported'], true );
}

/**
* Render the unsupported version widget.
*
* @return void
*/
function render_widget() {
$version = Altis\get_version();
$info = get_supported_version_info();

?>
<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>
<?php
}

0 comments on commit 850b943

Please sign in to comment.