This will display a update note message if a update is available, update checking will happen every 3 days. Update checking can be disabled via the 'Check for Updates' setting that is added to the plugin.
# =============================================================================
# UPDATE FUNCTIONALITY
function update__has_permission ( )
{
global $current_User;
if ( empty($current_User) || empty($current_User->ID) || (int)$current_User->Group->ID !== 1 || !$this->Settings->get('check_for_updates') )
return false;
return true;
}
function update__check ( )
{
$update_needed = false;
$update_message = $this->Settings->get('last_update_message');
$update_version = $this->Settings->get('last_update_version');
if ( !empty($update_message) && !empty($update_version) )
{ // We know there is an update
$update_needed = version_compare($update_version, $this->version);
if ( !$update_needed )
{ // We have been updated!
$this->Settings->set('next_update_check', strtotime('+3 days', time()));
$this->Settings->set('last_update_message', '');
$this->Settings->set('last_update_version', '');
$this->Settings->dbupdate();
}
}
elseif ( $this->update__has_permission() && time() >= $this->Settings->get('next_update_check') )
{ // Check for update
// Get update url
if ( empty($this->update_url) )
return NULL;
$update_url = $this->update_url;
// Get the update contents
$update_contents = @file_get_contents($update_url);
if ( empty($update_contents) )
{ // This server does not support updating
return NULL;
}
// Get the vars
$parts = explode('|', $update_contents);
for ( $i = 0, $n = sizeof($parts); $i < $n; ++$i )
{
$part = $parts[$i];
$split = strpos($part, ':');
if ( !$split ) continue;
$key = substr($part, 0, $split);
$value = substr($part, $split+1);
$var = 'update_'.$key;
$$var = $value;
}
if ( empty($update_version) )
{ // error
$this->msg(
'<strong>Failed to check for updates for the ['.$this->name.'] plugin.</strong>'.
' <span style="font-size:10px; font-style:italic;">'.
'(Could not get the version of the update)'.
'</span>',
'error'
);
return NULL;
}
if ( empty($update_message) )
$update_message = '';
else
$update_message = '<br />'.$update_message;
if ( empty($update_url) )
$update_url = $this->get_help_url();
// Compare
$update_needed = version_compare($update_version, $this->version);
// Update timer
$this->Settings->set('next_update_check', strtotime('+3 days', time()));
// Update check stuff
if ( $update_needed )
{
$update_message =
'<a href="'.$update_url.'"><strong>A newer version of the plugin ['.$this->name.'] is available.</strong></a >'.
'<br />'.
'<span style="font-size: 90%; font-style:italic;">'.
'(You are currently running v'.$this->version.', the new version is v'.$update_version.')'.
'</span>'.
$update_message;
$this->Settings->set('last_update_message', $update_message);
$this->Settings->set('last_update_version', $update_version);
$this->Settings->dbupdate();
}
}
if ( $update_needed && !empty($update_message) )
$this->msg($update_message, 'status');
return true;
}
function update__settings ( & $settings, & $params )
{
$settings['last_update_version'] = array(
'label' => 'Last Update Version',
'defaultvalue' => '',
'type' => 'text'
);
$settings['last_update_message'] = array(
'label' => 'Last Update Message',
'defaultvalue' => '',
'type' => 'text'
);
$settings['next_update_check'] = array(
'label' => 'Last Update',
'defaultvalue' => time(),
'type' => 'integer'
);
$settings['check_for_updates'] = array(
'label' => 'Check for Updates',
'defaultvalue' => '1',
'type' => 'checkbox',
);
if ( $params['for_editing'] )
{ // Editing changes
$settings['last_update_version']['no_edit'] = true;
$settings['last_update_version']['disabled'] = !$this->update__has_permission();
$settings['last_update_message']['no_edit'] = true;
$settings['last_update_message']['disabled'] = !$this->update__has_permission();
$settings['next_update_check']['no_edit'] = true;
$settings['next_update_check']['disabled'] = !$this->update__has_permission();
$settings['check_for_updates']['disabled'] = !$this->update__has_permission();
}
return true;
}
And change the end of your GetDefaultSetting function to
// EXTRA FUNCTIONALITY
$this->update__settings($r, $params);
// Return settings
return $r;
}
Then create a update.txt containing something like
version:1.0.0.1-final|message:This update brings many changes to your shit
And add the following vars (changed of course)
// Where should the user go for help
var $help_url = 'http://www.balupton.com/blogs/b2evo?title=sample_renderer_plugin';
// What is the author's url
var $author_url = 'http://www.balupton.com/';
// The update url, which is a txt file with format [verion:x.x.x.x-tag|message:a message to show for this update|url:http://....] message and url are optional
var $update_url = 'http://www.balupton.com/blogs/plugins/sample_renderer_plugin/update.txt';
Things to do:
1) Add "Perform Automatic Update to this Version" link.
You might be interested in [url=http://evocms-plugins.svn.sourceforge.net/viewvc/evocms-plugins/am_bopit_plugin/]this plugin[/url] which will add version checking for all installed plugins and enabled skins ;)
¥