Dear Reader,
A quick one here for those writing WordPress plugins. I’m experimenting with something here and thought I’d share. One thing that bugs me about writing plugins is when I make a new version, I have to go to several places and announce that there’s an update available. So I’m toying with the idea of having the plugin check every x days to see of there has been an update.
The way I do this is in the plugin’s option page (the only place I would ever do this) I put the following code at the bottom of this post.
Currently, this one is hard coded to check every 7 days. I’m thinking about changing that to like 3 and maybe making it an option so the user can set it to 0 and never have it execute.
I’m throwing it out here for others to review. My question is, it is ok to have a plug in ‘phone home’? I know I’m not collecting any stats but this could easily get out of hand if some reasonable guidelines aren’t set.
Until next time,
(l)(k)(bunny)
=C=
CODE:
<?PHP
/*
* Version Check code.
* This is a bit verbose. I'd like to cut it down a bit. I know this works
* in almost all cases. Some places may not have curl installed. I could do
* it in AJAX calling the plugin itself with a parameter and firing off a
* call but that's basically the same as what I'm doing here. So why bother.
*/
if (($notable_settings['last_version_check']+(86400*7))<mktime()) {
$elements = parse_url('https://blog.calevans.com/
plugin_version_checker.php?plugin=wp-notable');
$current_version = 0.00;
$line = '';
if ($fp = @fsockopen($elements['host'],80)) {
fputs($fp, sprintf("GET %s HTTP/1.0\r\n" . "Host: %s\r\n\r\n", $elements['path'] .
(isset ($elements['query']) ? '?'. $elements['query'] : ''), $elements['host']));
while (!feof($fp)) $line .= fgets($fp, 4096);
fclose($fp);
$line = urldecode(trim(strtr($line,"\n\r\t\0"," ")));
$work_array = explode(" ",$line);
/*
* This does not allow for any additional messages to be passed. It
* assumes that the last time coming in is the version #.
*/
$current_version = $work_array[count($work_array)-1];
update_option('notable_last_version_check',mktime());
} // if ($fp)
if ($version!=$current_version) {
?>
<div class="wrap" style="border:solid 1px red;">This is version <?=$version;?>. The current version
of the plugin is <?=$current_version;?>. Click <a href="http://www.calevans.com/view.php/page/notable"
target="_NEW" title="Opens a new window.">here</a> to go to the project page to find out more.
<?PHP
} else {
?>
<div class="wrap">This is plugin is up to date.<br />
<?PHP
} // if ($version!=$current_version)
} else {
?>
<div class="wrap" >
<?PHP
} // if ($notable_settings['last_version_check']!=(mktime()+(86400*7)))
?>
</div>