| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: wp-forecast |
| 4 |
Plugin URI: http://www.tuxlog.de |
| 5 |
Description: wp-forecast is a highly customizable plugin for wordpress, showing weather-data from accuweather.com. |
| 6 |
Version: 2.0 |
| 7 |
Author: Hans Matzen <webmaster at tuxlog.de> |
| 8 |
Author URI: http://www.tuxlog.de |
| 9 |
*/ |
| 10 |
|
| 11 |
/* Copyright 2006,2007,2008 Hans Matzen (email : webmaster at tuxlog.de) |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License as published by |
| 15 |
the Free Software Foundation; either version 2 of the License, or |
| 16 |
(at your option) any later version. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
// |
| 29 |
// maximal number of widgets to use |
| 30 |
// |
| 31 |
static $wpf_maxwidgets=20; |
| 32 |
|
| 33 |
// |
| 34 |
// set to 0 for no debugging information |
| 35 |
// set to 1 for call stack |
| 36 |
// set to 2 for call stack and variables ( not implemented yet) |
| 37 |
// |
| 38 |
static $wpf_debug=0; |
| 39 |
|
| 40 |
// xml parser for accuweather xml |
| 41 |
require_once("func_xml_parse.php"); |
| 42 |
|
| 43 |
// generic functions |
| 44 |
require_once("funclib.php"); |
| 45 |
// include setup functions |
| 46 |
require_once("setup.php"); |
| 47 |
// include admin options page |
| 48 |
require_once("wp-forecast-admin.php"); |
| 49 |
// display functions |
| 50 |
require_once("wp-forecast-show.php"); |
| 51 |
|
| 52 |
// |
| 53 |
// set cache with weather data for current parameters |
| 54 |
// a wrapper function called via the init hook |
| 55 |
// |
| 56 |
|
| 57 |
function wp_forecast_init() |
| 58 |
{ |
| 59 |
global $wpf_debug; |
| 60 |
|
| 61 |
if ($wpf_debug > 0) |
| 62 |
pdebug("Start of function wp_forecast_init ()"); |
| 63 |
|
| 64 |
$count=(int) get_option('wp-forecast-count'); |
| 65 |
$weather=array(); |
| 66 |
|
| 67 |
for ($i=0;$i<$count;$i++) { |
| 68 |
$wpfcid=get_widget_id($i); |
| 69 |
|
| 70 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 71 |
$expire=get_option("wp-forecast-expire".$wpfcid); |
| 72 |
|
| 73 |
if ($expire < time()) { |
| 74 |
$w = get_weather($wpf_vars['BASE_URI'],$wpf_vars['location'], |
| 75 |
$wpf_vars['metric']); |
| 76 |
$weather=wpf_xml_parser($w); |
| 77 |
|
| 78 |
// store weather to database and set expire time |
| 79 |
// if the current data wasnt available use old data |
| 80 |
if ( count($weather)>0) { |
| 81 |
update_option("wp-forecast-cache".$wpfcid, arr2str($weather)); |
| 82 |
update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
if ($wpf_debug > 0) |
| 87 |
pdebug("End of function wp_forecast_init ()"); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
// |
| 92 |
// this function is called from your template |
| 93 |
// to insert your weather data at the place you want it to be |
| 94 |
// support to select language on a per call basis from Robert Lang |
| 95 |
// |
| 96 |
function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null) |
| 97 |
{ |
| 98 |
|
| 99 |
global $wpf_debug; |
| 100 |
|
| 101 |
if ($wpf_debug > 0) |
| 102 |
pdebug("Start of function wp_forecast_widget ()"); |
| 103 |
|
| 104 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 105 |
if (!empty($language_override)) { |
| 106 |
$wpf_vars['wpf_language']=$language_override; |
| 107 |
} |
| 108 |
$weather=str2arr(get_option("wp-forecast-cache".$wpfcid)); |
| 109 |
show($wpfcid,$weather,$args,$wpf_vars); |
| 110 |
|
| 111 |
if ($wpf_debug > 0) |
| 112 |
pdebug("End of function wp_forecast_widget ()"); |
| 113 |
} |
| 114 |
|
| 115 |
// |
| 116 |
// this is the wrapper function for displaying from sidebar.php |
| 117 |
// and not as a widget. since the parameters are different we need this |
| 118 |
// |
| 119 |
function wp_forecast($wpfcid="A", $language_override=null) |
| 120 |
{ |
| 121 |
global $wpf_debug; |
| 122 |
|
| 123 |
if ($wpf_debug > 0) |
| 124 |
pdebug("Start of function wp_forecast ()"); |
| 125 |
|
| 126 |
wp_forecast_widget( array(), $wpfcid, $language_override); |
| 127 |
|
| 128 |
if ($wpf_debug > 0) |
| 129 |
pdebug("End of function wp_forecast ()"); |
| 130 |
} |
| 131 |
|
| 132 |
// |
| 133 |
// a function to show a range of widgets at once |
| 134 |
// |
| 135 |
function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null) |
| 136 |
{ |
| 137 |
global $wpf_maxwidgets; |
| 138 |
$wcount=1; |
| 139 |
|
| 140 |
// check min and max limit |
| 141 |
if ($from < 0) |
| 142 |
$from = 0; |
| 143 |
|
| 144 |
if ($to > $wpf_maxwidgets) |
| 145 |
$to = $wpf_maxwidgets; |
| 146 |
|
| 147 |
// output table header |
| 148 |
echo "<table><tr>"; |
| 149 |
|
| 150 |
// out put widgets in a table |
| 151 |
for ($i=$from;$i<=$to;$i++) { |
| 152 |
|
| 153 |
if ( $wcount % $numpercol == 1) |
| 154 |
echo "<tr>"; |
| 155 |
|
| 156 |
echo "<td>"; |
| 157 |
wp_forecast( get_widget_id($i), $language_override); |
| 158 |
echo "</td>"; |
| 159 |
|
| 160 |
if ( ($wcount % $numpercol == 0) and ($i< $to)) |
| 161 |
echo "</tr>"; |
| 162 |
|
| 163 |
$wcount += 1; |
| 164 |
} |
| 165 |
|
| 166 |
// output table footer |
| 167 |
echo "</tr></table>"; |
| 168 |
} |
| 169 |
|
| 170 |
// |
| 171 |
// a function to show a set of widgets at once |
| 172 |
// |
| 173 |
function wp_forecast_set($wset, $numpercol=1, $language_override=null) |
| 174 |
{ |
| 175 |
global $wpf_maxwidgets; |
| 176 |
$wcount=1; |
| 177 |
$wset_max= count($wset)-1; |
| 178 |
|
| 179 |
// output table header |
| 180 |
echo "<table><tr>"; |
| 181 |
|
| 182 |
// out put widgets in a table |
| 183 |
for ($i=0;$i<=$wset_max;$i++) { |
| 184 |
|
| 185 |
if ( $wcount % $numpercol == 1) |
| 186 |
echo "<tr>"; |
| 187 |
|
| 188 |
echo "<td>"; |
| 189 |
wp_forecast( $wset[$i], $language_override); |
| 190 |
echo "</td>"; |
| 191 |
|
| 192 |
if ( ($wcount % $numpercol == 0) and ($i< $wset_max)) |
| 193 |
echo "</tr>"; |
| 194 |
|
| 195 |
$wcount += 1; |
| 196 |
} |
| 197 |
|
| 198 |
// output table footer |
| 199 |
echo "</tr></table>"; |
| 200 |
} |
| 201 |
|
| 202 |
// |
| 203 |
// set the choosen number of widgets, set at the widget page |
| 204 |
// |
| 205 |
function wpf_widget_setup() { |
| 206 |
global $wpf_debug,$wpf_maxwidgets; |
| 207 |
|
| 208 |
if ($wpf_debug > 0) |
| 209 |
pdebug("Start of function wpf_widget_setup ()"); |
| 210 |
|
| 211 |
$count = $newcount = get_option('wp-forecast-count'); |
| 212 |
if ( isset($_POST['wpf-count-submit']) ) { |
| 213 |
$number = (int) $_POST['wpf-number']; |
| 214 |
if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets; |
| 215 |
if ( $number < 1 ) $number = 1; |
| 216 |
$newcount = $number; |
| 217 |
} |
| 218 |
if ( $count != $newcount ) { |
| 219 |
$count = $newcount; |
| 220 |
update_option('wp-forecast-count', $count); |
| 221 |
// add missing option to database |
| 222 |
wp_forecast_activate(); |
| 223 |
// init the new number of widgets |
| 224 |
widget_wp_forecast_init($count); |
| 225 |
} |
| 226 |
|
| 227 |
if ($wpf_debug > 0) |
| 228 |
pdebug("End of function wpf_widget_setup ()"); |
| 229 |
} |
| 230 |
|
| 231 |
// |
| 232 |
// form snippet to set the number of wanted widgets from |
| 233 |
// the widget page |
| 234 |
// |
| 235 |
function wpf_widget_page() { |
| 236 |
global $wpf_debug,$wpf_maxwidgets; |
| 237 |
|
| 238 |
if ($wpf_debug > 0) |
| 239 |
pdebug("Start of function wpf_widget_page ()"); |
| 240 |
|
| 241 |
$count = $newcount = get_option('wp-forecast-count'); |
| 242 |
|
| 243 |
// get locale |
| 244 |
$locale = get_locale(); |
| 245 |
if ( empty($locale) ) |
| 246 |
$locale = 'en_US'; |
| 247 |
// load translation |
| 248 |
if(function_exists('load_textdomain')) { |
| 249 |
load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo"); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
$out = "<div class='wrap'><form method='POST'>"; |
| 254 |
$out .= "<h2>WP-Forecast Widgets</h2>"; |
| 255 |
$out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." "; |
| 256 |
$out .= "<select id='wpf-number' name='wpf-number' value='".$count."'>"; |
| 257 |
|
| 258 |
for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) { |
| 259 |
$out .= "<option value='$i' "; |
| 260 |
if ($count==$i) |
| 261 |
$out .= "selected='selected' "; |
| 262 |
$out .= ">$i</option>"; |
| 263 |
} |
| 264 |
$out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".attribute_escape(__('Save'))." /></span></p></form></div>"; |
| 265 |
echo $out; |
| 266 |
|
| 267 |
if ($wpf_debug > 0) |
| 268 |
pdebug("End of function wpf_widget_page ()"); |
| 269 |
} |
| 270 |
|
| 271 |
function widget_wp_forecast_init() |
| 272 |
{ |
| 273 |
|
| 274 |
global $wp_version,$wpf_debug,$wpf_maxwidgets; |
| 275 |
|
| 276 |
if ($wpf_debug > 0) |
| 277 |
pdebug("Start of function widget_wp_forecast_init ()"); |
| 278 |
|
| 279 |
$count=(int) get_option('wp-forecast-count'); |
| 280 |
|
| 281 |
// check for widget support |
| 282 |
if ( !function_exists('register_sidebar_widget') ) |
| 283 |
return; |
| 284 |
|
| 285 |
// add fetch weather data to init the cache before any headers are sent |
| 286 |
add_action('init','wp_forecast_init'); |
| 287 |
add_action('init','wp_forecast_admin_init'); |
| 288 |
|
| 289 |
// add css in header |
| 290 |
add_action('wp_head', 'wp_forecast_css'); |
| 291 |
|
| 292 |
for ($i=0;$i<=$wpf_maxwidgets;$i++) { |
| 293 |
$wpfcid = get_widget_id( $i ); |
| 294 |
|
| 295 |
// register our widget and add a control |
| 296 |
$name = sprintf(__('wp-forecast %s'), $wpfcid); |
| 297 |
$id = "wp-forecast$wpfcid"; |
| 298 |
|
| 299 |
// register / unregister widget and control form |
| 300 |
// the first part is to work around bug 4275 which was |
| 301 |
// corrected with v2.2.1 |
| 302 |
if ($wp_version < "2.2.1") |
| 303 |
register_sidebar_widget(array($id,$name), |
| 304 |
$i < $count ? 'wp_forecast_widget' : '','',$wpfcid); |
| 305 |
else |
| 306 |
register_sidebar_widget(array($id,$name), |
| 307 |
$i < $count ? 'wp_forecast_widget' : '',$wpfcid); |
| 308 |
|
| 309 |
register_widget_control(array($id,$name), |
| 310 |
$i < $count ? 'wpf_admin_hint' : '' |
| 311 |
,300,150,$wpfcid,1); |
| 312 |
} |
| 313 |
// add actions for setup the count of wanted wpf widgets |
| 314 |
add_action('sidebar_admin_setup', 'wpf_widget_setup'); |
| 315 |
add_action('sidebar_admin_page', 'wpf_widget_page'); |
| 316 |
|
| 317 |
if ($wpf_debug > 0) |
| 318 |
pdebug("End of function widget_wp_forecast_init ()"); |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
// MAIN |
| 325 |
|
| 326 |
if ($wpf_debug > 0) |
| 327 |
pdebug("Start of MAIN"); |
| 328 |
|
| 329 |
// activating deactivating the plugin |
| 330 |
register_activation_hook(__FILE__,'wp_forecast_activate'); |
| 331 |
register_deactivation_hook(__FILE__,'wp_forecast_deactivate'); |
| 332 |
|
| 333 |
// add option page |
| 334 |
add_action('admin_menu', 'wp_forecast_admin'); |
| 335 |
|
| 336 |
// Run our code later in case this loads prior to any required plugins. |
| 337 |
add_action('plugins_loaded', 'widget_wp_forecast_init'); |
| 338 |
|
| 339 |
if ($wpf_debug > 0) |
| 340 |
pdebug("End of MAIN"); |
| 341 |
?> |
| 342 |
|