| (Sunrise|Sunset)<\/td> | ([^>]*)<\/b><\/td><\/tr>/", $webpage_contents, $suns);
if (strtoupper($suns[1][0]) == 'SUNRISE'
|| strtoupper($suns[1][1]) == 'SUNSET') {
$sunset = true;
eval( '$wxInfo[\''.strtoupper($suns[1][0]). '\']=strtotime("'.$suns[2][0].'");' );
eval( '$wxInfo[\''.strtoupper($suns[1][1]). '\']=strtotime("'.$suns[2][1].'");' );
}
return $sunset;
}
///////////////////////////////////////////////////////////////////////
// Below here is the Noaa Query Code and the code to parse the output//
///////////////////////////////////////////////////////////////////////
function get_metar ( &$wxInfo) {
if ($wxInfo['CACHE_TYPE'] == 'FILE') {
$metar_cache= $wxInfo['CACHE_PATH']. "/metar_cache_".$wxInfo['STATION'].".txt";
if (file_exists($metar_cache)) {
$cache_age=(time()-filemtime($metar_cache));
}
if ($wxInfo['CACHE_DEBUG']) {
echo "Cache Debug\n";
echo " \n";
echo " - Cache Timeout: " . $wxInfo['CACHE_TIMEOUT'] . "
\n";
echo " - Cache File Age: " . $cache_age . "
\n";
}
// --- BEGIN BUG FIX ---
// Date: March 8th 2005
// Name: fread empty file bug
// Modification by: Garett Harnish
//
// Description
// -----------
// (reported by Kenny on Feb 27th, 2005)
// I have version 1.1.7 and works great but every so often I get
// this error message:
// Warning: fread(): Length parameter must be greater than 0
//
// Original Line: $metar=fread($fp, filesize($metar_cache));
//
// Cause: Unknown
// Fix: Ensure file has a size > 0 before reading else
// assume file is blank and acquire new metar data.
# Ensure File Exists
if (file_exists($metar_cache)) {
$metar_cache_size = filesize($metar_cache);}
if (isset($cache_age)
&& $cache_age < $wxInfo['CACHE_TIMEOUT']
&& $metar_cache_size > 0 )
{
$fp = fopen($metar_cache, "r");
$metar=fread($fp, $metar_cache_size);
fclose($fp);
if ($wxInfo['CACHE_DEBUG']) {
echo " ";
}
return $metar;
}
}
if ($wxInfo['CACHE_DEBUG']) {
echo " Fetching New Metar Data\n";
}
$host = 'weather.noaa.gov';
$location = "/pub/data/observations/metar/stations/".$wxInfo['STATION'].".TXT";
$request = "HTTP/1.1\r\n" .
"If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" .
"Pragma: no-cache\r\n".
"Cache-Control: no-cache\r\n";
@$fp = fsockopen($host, 80, $errno, $errstr, 60);
$request = "GET $location $request" .
"Host: $host\r\n" .
"Content-Type: text/html\r\n" .
"Connection: Close\r\n\r\n";
$metar = false;
if ($fp)
{
fputs($fp, $request);
$httpresponse = fgets($fp, 1024);
if ($wxInfo['CACHE_DEBUG'])
{
echo "NOAA Responded:";
echo "- ".$httpresponse."
";
}
if (strpos($httpresponse, '200 '))
{
do
{
$line = fgets($fp, 1024);
if ($wxInfo['CACHE_DEBUG']) echo "- ".$line."
";
} while ($line != "\r\n");
while ($line = fgets($fp, 1024))
{
$metar = $line;
if ($wxInfo['CACHE_DEBUG']) echo "- ".$line."
";
}
fclose($fp);
}
if ($wxInfo['CACHE_DEBUG']) echo " ";
}
else
{
if ($wxInfo['CACHE_DEBUG']) {
echo " Could Not Open Socket: ". $errstr . "(". $errno .")\n";
}
}
if ($wxInfo['CACHE_TYPE'] == 'FILE')
{
if ($fp = fopen($metar_cache, "w"))
{
fwrite($fp, $metar);
fclose($fp);
}
}
if ($wxInfo['CACHE_DEBUG'])
{
echo " ";
}
return $metar;
}
function process_metar(&$wxInfo,$visibility_unit,$wind_unit)
{
$metarParts = explode(' ',$wxInfo['METAR']);
$groupName = array('get_station','get_time','get_station_type','get_wind','get_var_wind','get_visibility','get_runway','get_conditions','get_cloud_cover','get_temperature','get_altimeter', 'get_remarks');
$metarPtr = 1; // get_station identity is ignored
$group = 1;
$part = $metarParts[$metarPtr];
while ($group < count($groupName) && strlen($part) > 0) {
$groupName[$group]($part,$metarPtr,$group,$wxInfo,$visibility_unit,$wind_unit); // $groupName is a function variable
$part = $metarParts[$metarPtr];
}
}
function get_station($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (strlen($part) == 4 and $group == 0) {
$group++;
$metarPtr++;
}
}
function get_time($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (substr($part,-1) == 'Z') $metarPtr++;
$group++;
// determine date
if (gmdate("d",time()) == substr($part,0,2)) {
$time = time();
} else {
$time = time() - 86400;
}
$wxInfo['TIME'] = gmmktime(intval(substr($part,2,2)), intval(substr($part,4,2)), 0, intval(gmdate("m", $time)), intval(gmdate("d", $time)), intval(gmdate("Y", $time)));
}
function get_station_type($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if ($part == 'AUTO' || $part == 'COR') $metarPtr++;
$group++;
}
function get_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (!function_exists('speed')) {
// --- BEGIN BUG FIX ---
// Date: Oct 28. 2004
// Name: Wind: Calm mph
// Modification by: Garett Harnish
//
// Description
// -----------
// I completely rewrote the speed function, not solely to fix the
// "Wind: Calm mph" bug in a more elegant manner, but to make it
// easier to read the function w/ its comments instead of having
// to look through a jumbled if-then-else construction. In any case,
// by attaching the wind units to the speed directly, we ensure that
// in the cases where a speed is displayed it will have it's units
// and when a speed isn't, units shan't be display.
function speed($part, $unit,$wind_unit) {
switch ($wind_unit.$unit) {
// Convert to miles per hour
case 'mphKT':
return round(1.1508 * $part) . " $wind_unit"; // from knots
case 'mphMPS':
return round(2.23694 * $part) . " $wind_unit"; // from meter/sec
case 'mphKMH':
return round(0.621371 * $part) . " $wind_unit"; // from km/h
// Convert to kilometers per hour
case 'km/hKT':
return round(1.852 * $part) . " $wind_unit"; // from knots
case 'km/hMPS':
return round($part * 3.6) . " $wind_unit"; // from meter/sec
case 'km/hKMH':
return $part . " $wind_unit"; // from km/h
// Convert to meters per second
case "m/sKT":
return round(0.514 * $part) . " $wind_unit"; // from knots
case "m/sMPS":
return $part . " $wind_unit"; // from meter/sec
case "m/sKMH":
return round(0.278 * $part) . " $wind_unit"; // from km/h
// Convert to knots
case 'ktKT':
return ($part - 0.0) . " $wind_unit"; // from knots
case 'ktMPS':
return round(0.86897*$part) . " $wind_unit"; // from meter/sec
case 'ktKMH':
return round(0.539956803*$part) . " $wind_unit"; // from km/h
default:
return "**INVALID DATA**";
}
}
// --- END BUG FIX ---
}
if (ereg('^([0-9G]{5,10}|VRB[0-9]{2,3})(KT|MPS|KMH)$',$part,$pieces)) {
$part = $pieces[1];
$unit = $pieces[2];
if ($part == '00000') {
$wxInfo['WIND'] = __('Calm', 'WeatherIcon'); // no wind
}
else {
ereg('([0-9]{3}|VRB)([0-9]{2,3})G?([0-9]{2,3})?',$part,$pieces);
if ($pieces[1] == 'VRB') {
$direction = __('Varies', 'WeatherIcon') ;
}
else {
$angle = (integer) $pieces[1];
$compass = array(__('N', 'WeatherIcon'),__('NNE', 'WeatherIcon'),__('NE', 'WeatherIcon'),__('ENE', 'WeatherIcon'),__('E', 'WeatherIcon'),__('ESE', 'WeatherIcon'),__('SE', 'WeatherIcon'),__('SSE', 'WeatherIcon'),__('S', 'WeatherIcon'),__('SSW', 'WeatherIcon'),__('SW', 'WeatherIcon'),__('WSW', 'WeatherIcon'),__('W', 'WeatherIcon'),__('WNW', 'WeatherIcon'),__('NW', 'WeatherIcon'),__('NNW', 'WeatherIcon'));
$direction = $compass[round($angle / 22.5) % 16];
}
if ($pieces[3] == 0) {
$gust = '';
}
else {
$gust = ', '. __('gusting to', 'WeatherIcon') .' ' . speed($pieces[3], $unit,$wind_unit);
}
$wxInfo['WIND'] = $direction . ' '.__('at', 'WeatherIcon').' ' . speed($pieces[2], $unit,$wind_unit) . $gust;
}
$metarPtr++;
}
$group++;
}
function get_var_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (ereg('([0-9]{3})V([0-9]{3})',$part,$pieces)) $metarPtr++;
$group++;
}
function get_visibility($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
static $integerMile = '';
// --- BEGIN BUG FIX ---
// Date: Feb 22, 2005
// Name: Reports Imperial Data as Metric
// Modification by: Garett Harnish
//
// Description
// -----------
// If plugin set to display information in metric, and data was
// provided in imperial, the plugin would display the imperial
// data as metric.
if (strlen($part) == 1) {
// visibility is limited to a whole mile plus a fraction part
if ($visibility_unit=='km') $integerMile= $part * 1.609344;
else $integerMile = $part . ' ';
$metarPtr++;
}
elseif (substr($part,-2) == 'SM') { // visibility is in miles
$part = substr($part,0,strlen($part)-2);
if (substr($part,0,1) == 'M') {
$prefix = __('less than', 'WeatherIcon') .' ';
$part = substr($part, 1);
}
else $prefix = '';
if ($visibility_unit=='km') {
str_replace(" ", "*", $part);
eval("\$distance = round($integerMile + ($part * 1.609344), 1);");
} else {
$distance = $integerMile . $part;
}
if ((($integerMile == '' && ereg('[/]',$part,$pieces)) || $part == '1') && $visibility_unit=='miles') {
$unit = ' mile';
}
else {
if ($visibility_unit=='miles') $unit = ' miles';
else $unit = ' km';
}
$wxInfo['VISIBILITY'] = $prefix . $distance . $unit;
$metarPtr++;
$group++;
}
// --- END BUG FIX ---
elseif (substr($part,-2) == 'KM') { // unknown (Reported by NFFN in Fiji)
$metarPtr++;
$group++;
}
elseif (ereg('^([0-9]{4})$',$part,$pieces)) { // visibility is in meters
if ($visibility_unit=='miles') $distance = round($part/ 1609.344, 1); // convert to miles
else $distance = $part/1000;
if ($distance > 5) $distance = round($distance);
if ($distance <= 1 && $visibility_unit=='miles') $unit = ' mile';
elseif ($distance <> 1 && $visibility_unit=='miles') $unit = ' miles';
if ($visibility_unit=='km') $unit = ' km';
$wxInfo['VISIBILITY'] = $distance . $unit;
$metarPtr++;
$group++;
}
elseif ($part == 'CAVOK') { // good weather
if ($visibility_unit=='miles') $wxInfo['VISIBILITY'] = __('greater than', 'WeatherIcon') .' 7 '.__('miles', 'WeatherIcon') ;
else $wxInfo['VISIBILITY'] =__('greater than', 'WeatherIcon') .' 10 km';
$wxInfo['CONDITIONS'] = '';
$wxInfo['CLOUDS'] = __('clear skies', 'WeatherIcon') ;
$wxInfo['CLOUDS_STATUS']='0';
$metarPtr++;
$group += 4; // can skip the next 3 groups
}
else {
$group++;
}
}
function get_runway($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (substr($part,0,1) == 'R') $metarPtr++;
else $group++;
}
function get_conditions($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
// --- BEGIN BUG FIX ---
// Date: Oct 18, 2004
// Name: Duplicate Conditions
// Modification by: Garett Harnish
//
// Description
// -----------
// This fix corrects a problem with mutliple reporting locations where
// condition data (ie: blowing snow) was not cleared from one location to
// the next. Changing the variable from a static to a global and placing
// code in the function WeatherIcon to clear the global on run fixed the
// problem
//
// Original Code
// -------------
// static $conditions = '';
global $conditions;
// --- END BUG FIX ---
$wxCode = array(
'VC' => __('nearby', 'WeatherIcon'),
'MI' => __('shallow', 'WeatherIcon'),
'PR' => __('partial', 'WeatherIcon'),
'BC' => __('patches of', 'WeatherIcon'),
'DR' => __('low drifting', 'WeatherIcon'),
'BL' => __('blowing', 'WeatherIcon'),
'SH' => __('showers', 'WeatherIcon'),
'TS' => __('thunderstorm', 'WeatherIcon'),
'FZ' => __('freezing', 'WeatherIcon'),
'DZ' => __('drizzle', 'WeatherIcon'),
'RA' => __('rain', 'WeatherIcon'),
'SN' => __('snow', 'WeatherIcon'),
'SG' => __('snow grains', 'WeatherIcon'),
'IC' => __('ice crystals', 'WeatherIcon'),
'PE' => __('ice pellets', 'WeatherIcon'),
'GR' => __('hail', 'WeatherIcon'),
'GS' => __('smallhail', 'WeatherIcon'),
'UP' => __('unknown', 'WeatherIcon'),
'BR' => __('mist', 'WeatherIcon'),
'FG' => __('fog', 'WeatherIcon'),
'FU' => __('smoke', 'WeatherIcon'),
'VA' => __('volcanic ash', 'WeatherIcon'),
'DU' => __('widespread dust', 'WeatherIcon'),
'SA' => __('sand', 'WeatherIcon'),
'HZ' => __('haze', 'WeatherIcon'),
'PY' => __('spray', 'WeatherIcon'),
'PO' => __('well-developed dust/sand whirls', 'WeatherIcon'),
'SQ' => __('squalls', 'WeatherIcon'),
'FC' => __('funnel cloud, tornado, or waterspout', 'WeatherIcon'),
'SS' => __('sandstorm/duststorm', 'WeatherIcon'));
static $wxIcon = array(
'TS' => '2',
'FZ' => '3',
'RA' => '4',
'SN' => '5',
'SG' => '5',
'IC' => '7',
'PE' => '7',
'FG' => '8',
'FU' => '9',
'SA' => '10',
'HZ' => '11',
'DZ' => '12');
if (ereg('^(-|\+|VC)?(TS|SH|FZ|BL|DR|MI|BC|PR|RA|DZ|SN|SG|GR|GS|PE|IC|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)+$',$part,$pieces)) {
if (strlen($conditions) == 0) {
$join = '';
$wxInfo['CONDITIONS_SEVERITY'] = '0';
}
else $join = ' & ';
if (substr($part,0,1) == '-') {
$wxInfo['CONDITIONS_SEVERITY'] = '1';
$prefix = __('light', 'WeatherIcon') .' ';
$part = substr($part,1);
}
elseif (substr($part,0,1) == '+') {
$wxInfo['CONDITIONS_SEVERITY'] = '2';
$prefix = __('heavy', 'WeatherIcon') .' ';
$part = substr($part,1);
}
else $prefix = ''; // moderate conditions have no descriptor
$conditions .= $join . $prefix;
// The 'showers' code 'SH' is moved behind the next 2-letter code to make the English translation read better.
if (substr($part,0,2) == 'SH') $part = substr($part,2,2) . substr($part,0,2). substr($part, 4);
while ($code = substr($part,0,2)) {
$conditions .= $wxCode[$code] . ' ';
// --- BEGIN BUG FIX ---
// Date: Oct 28. 2004
// Name: Fog, Haze, Smoke, and Sand don't set a CONDITION STATUS.
// Modification by: Garett Harnish
//
// Description
// -----------
// Changed the if/then statement to check the Icon Array for a matching
// code and then assigns a condition status to it if a match is found.
// This ensure if there is a condition status defined, it is assigned.
if (inarray($wxIcon, $code)) {
$wxInfo['CONDITIONS_STATUS'] = $wxIcon[$code];
}
// --- END BUG FIX ---
$part = substr($part,2);
}
$wxInfo['CONDITIONS'] = $conditions;
$metarPtr++;
}
else {
$wxInfo['CONDITIONS'] = $conditions;
$group++;
}
$wxInfo['CONDITIONS'] = $conditions;
if (ereg('^(-|\+|VC)?(SH|RA)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '1'; //showers rain
if (ereg('^(-|\+|VC)?(SH|SN)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '2'; //showers snow
}
function get_cloud_cover($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
$cloudCode = array(
'SKC' => __('clear skies', 'WeatherIcon'),
'CLR' => __('clear skies', 'WeatherIcon'),
'NSC' => __('clear skies', 'WeatherIcon'),
'FEW' => __('partly cloudy', 'WeatherIcon'),
'SCT' => __('scattered clouds', 'WeatherIcon'),
'BKN' => __('mostly cloudy', 'WeatherIcon'),
'OVC' => __('overcast', 'WeatherIcon'),
'VV' => __('vertical visibility', 'WeatherIcon') );
$cloudIcon = array(
'SKC' => '0',
'CLR' => '0',
'NSC' => '0',
'FEW' => '1',
'SCT' => '2',
'BKN' => '3',
'OVC' => __('overcast', 'WeatherIcon'),
'VV' => __('vertical visibility', 'WeatherIcon'));
if ($part == 'SKC' || $part == 'CLR' || $part == 'NSC') {
$wxInfo['CLOUDS'] = $cloudCode[$part];
$wxInfo['CLOUDS_STATUS'] = '0';
$metarPtr++;
$group++;
}
else {
if (ereg('^([A-Z]{2,3})([0-9]{3})',$part,$pieces)) {
// codes for CB and TCU are ignored
$wxInfo['CLOUDS'] = $cloudCode[$pieces[1]];
$wxInfo['CLOUDS_STATUS'] = $cloudIcon[$pieces[1]];
if ($pieces[1] == 'VV') {
$altitude = (integer) 100 * $pieces[2]; // units are feet
$wxInfo['CLOUDS'] .= " to $altitude ft";
}
$metarPtr++;
}
/** Fix for BKN/// error **/
elseif (ereg('^([A-Z]{2,3})', $part,$pieces)) {
// bad data (ignore)
$metarPtr++;
}
else {
$group++;
}
}
}
function get_temperature($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (!function_exists('get_heat_index')) {
function get_heat_index($tempF, $rh, &$wxInfo) {
// Calculate Heat Index based on temperature in F and relative humidity (65 = 65%)
if ($tempF > 79 && $rh > 39) {
$hiF = -42.379 + 2.04901523 * $tempF + 10.14333127 * $rh - 0.22475541 * $tempF * $rh;
$hiF += -0.00683783 * pow($tempF, 2) - 0.05481717 * pow($rh, 2);
$hiF += 0.00122874 * pow($tempF, 2) * $rh + 0.00085282 * $tempF * pow($rh, 2);
$hiF += -0.00000199 * pow($tempF, 2) * pow($rh, 2);
$hiF = round($hiF);
$hiC = round(($hiF - 32) / 1.8);
if ($wxInfo['TEMPERATURE']=='F') $wxInfo['HEAT INDEX'] = "$hiF°F";
else $wxInfo['HEAT INDEX'] = "$hiC°C";
}
}
}
if (!function_exists('get_wind_chill')) {
function get_wind_chill($tempF, &$wxInfo) {
// Calculate Wind Chill Temperature based on temperature in F and
// wind speed in miles per hour
if ($tempF < 51 && $wxInfo['WIND'] != __('Calm', 'WeatherIcon') ) {
$pieces = explode(' ', $wxInfo['WIND']);
$windspeed = (integer) $pieces[2]; // wind speed must be in miles per hour
if ($windspeed > 3) {
$chillF = 35.74 + 0.6215 * $tempF - 35.75 * pow($windspeed, 0.16) + 0.4275 * $tempF * pow($windspeed, 0.16);
$chillF = round($chillF);
$chillC = round(($chillF - 32) / 1.8);
if ($wxInfo['TEMPERATURE']=='F') $wxInfo['WIND CHILL'] = "$chillF°F";
else $wxInfo['WIND CHILL'] = "$chillC°C";
}
}
}
}
if (ereg('^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?$',$part,$pieces)) {
$tempC = (integer) strtr($pieces[1], 'M', '-');
$tempF = round(1.8 * $tempC + 32);
$wxInfo['TEMP_C'] = $tempC;
$wxInfo['TEMP_F'] = $tempF;
get_wind_chill($tempF, $wxInfo);
if (strlen($pieces[2]) != 0 && $pieces[2] != 'XX') {
$dewC = (integer) strtr($pieces[2], 'M', '-');
$dewF = round(1.8 * $dewC + 32);
$wxInfo['DEWPT_C'] = $dewC;
$wxInfo['DEWPT_F'] = $dewF;
$rh = round(100 * pow((112 - (0.1 * $tempC) + $dewC) / (112 + (0.9 * $tempC)), 8));
$wxInfo['HUMIDITY'] = $rh . '%';
get_heat_index($tempF, $rh, $wxInfo);
}
$metarPtr++;
$group++;
}
else {
$group++;
}
}
function get_altimeter($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (ereg('^(A|Q)([0-9]{4})',$part,$pieces)) {
if ($pieces[1] == 'A') {
$pressureIN = substr($pieces[2],0,2) . '.' . substr($pieces[2],2); // units are inches Hg
$pressureHPA = round($pressureIN / 0.02953); // convert to hectoPascals
}
else {
$pressureHPA = (integer) $pieces[2]; // units are hectoPascals
$pressureIN = round(0.02953 * $pressureHPA,2); // convert to inches Hg
}
$wxInfo['BAROMETER'] = "$pressureIN".__('"', 'WeatherIcon') ." Hg ($pressureHPA hPa)";
$metarPtr++;
$group++;
}
else {
$group++;
}
}
function get_remarks($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (ereg('VIRGA',$part,$pieces)) {
if (strlen($conditions) == 0) {
$join = '';
$wxInfo['CONDITIONS_SEVERITY'] = '0';
}
else $join = ' & ';
$conditions .= $join . __('virga', 'WeatherIcon');
// It's raining ... kinda
$wxInfo['CONDITIONS_STATUS'] = '4';
$wxInfo['CONDITIONS'] = $conditions;
$metarPtr++;
}
else {
$metarPtr++;
}
}
function inarray($array,$needle) {
foreach ($array as $key => $value) if (stristr($key, $needle)) return true;
return false;
}
/* And down here at the bottom we have the widget by Viper007Bond for WP-Dash */
function weathericon_css($ID) {
$options = get_option("weathericon_widget$ID");
return default_widget_css($ID) . "
#widget$ID {
width: 220px;
}
#content$ID {
width: 219px;
overflow: auto;
text-align: center;
padding-bottom: 1px;
}
#weatherconfig$ID input {
text-align: center;
}
";
}
// This handles the submit of the widget config options
function weathericon_updateconfig() {
global $wpdb, $widgettable;
if (isset($_POST['configweathericon'])) {
$ID = $_POST['configweathericon'];
$location = $_POST['weather_location'];
$locationlabel = $_POST['weather_locationlabel'];
if (isset($_POST['weather_labelheader']) && $locationlabel != '') {
$wpdb->query("UPDATE $widgettable SET name='$locationlabel' WHERE ID='$ID'");
update_option("weathericon_widget$ID", array('location' => $location, 'locationlabel' => $locationlabel, 'nameinheader' => TRUE));
} else {
$wpdb->query("UPDATE $widgettable SET name='WeatherIcon' WHERE ID='$ID'");
update_option("weathericon_widget$ID", array('location' => $location, 'locationlabel' => $locationlabel, 'nameinheader' => FALSE));
}
}
}
add_action('init', 'weathericon_updateconfig', 5);
// This spits out the widget config page
function weathericon_configure($ID) {
global $widgettable, $wpdb;
$options = get_option("weathericon_widget$ID");
$header = $wpdb->get_var("SELECT name FROM $widgettable WHERE ID='$ID'");
if ($header !== 'WeatherIcon') $headerchecked = 'checked="checked" ';
$output = '';
return $output;
}
// This outputs the main content of the widget -- the weather
function weathericon_content($ID) {
$options = get_option("weathericon_widget$ID");
// If no options exist for this particular box, die saying so
if (!$options['location']) return __('This WeatherIcon widget instance has not yet been configured.', 'WeatherIcon') . " " . __('Click here', 'WeatherIcon') . ' ' . __('to configure it.', 'WeatherIcon');
if ($options['locationlabel'] && $options['nameinheader'] != TRUE) $output .= 'Weather for ' . $options['locationlabel'] . ': ';
$output .= WeatherIcon('station=' . $options['location'] . '&before=&after= &debug=0&display=0');
return $output;
}
// This gives the widget to WP-Dash
function weathericon_available() {
if(function_exists('make_widget_available'))
make_widget_available('WeatherIcon', __('Displays current weather for specified location(s).', 'WeatherIcon'), 'weathericon_', 'weathericon_configure', 'configurable=true;spawn_multiple=true;');
}
add_action('init', 'weathericon_available');
// This removes the widget instance's config options from the database on close
function weathericon_deactivate($ID) {
global $wpdb;
delete_option("weathericon_widget$ID");
$wpdb->hide_errors();
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'rss$ID'");
}
}
?>
|