| 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: 3.8 |
| 7 |
Author: Hans Matzen |
| 8 |
Author URI: http://www.tuxlog.de |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
Copyright 2006-2012 Hans Matzen |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
// |
| 31 |
// only use this in case of severe problems accessing the admin dialog |
| 32 |
// |
| 33 |
// preselected transport method for fetching the weather data |
| 34 |
// valid values are |
| 35 |
// curl - uses libcurl |
| 36 |
// fsockopen - uses fsockopen |
| 37 |
// streams - uses fopen with streams |
| 38 |
// exthttp - uses pecl http extension |
| 39 |
// this will override every setting from the admin dialog |
| 40 |
// you have to assure that the chosen transport is supported by the |
| 41 |
// wordpress class WP_Http; |
| 42 |
// |
| 43 |
static $wp_forecast_pre_transport=""; |
| 44 |
|
| 45 |
// |
| 46 |
// maximal number of widgets to use |
| 47 |
// |
| 48 |
$wpf_maxwidgets=20; |
| 49 |
|
| 50 |
// |
| 51 |
// set to 0 for no debugging information |
| 52 |
// set to 1 for call stack |
| 53 |
// set to 2 for call stack including xml parser |
| 54 |
// |
| 55 |
static $wpf_debug=0; |
| 56 |
|
| 57 |
|
| 58 |
/* ---------- no parameters to change after this point -------------------- */ |
| 59 |
|
| 60 |
// accuweather data functions |
| 61 |
require_once("func_accu.php"); |
| 62 |
// weatherbug data functions |
| 63 |
require_once("func_bug.php"); |
| 64 |
// google data functions |
| 65 |
require_once("func_google.php"); |
| 66 |
|
| 67 |
// generic functions |
| 68 |
require_once("funclib.php"); |
| 69 |
// include setup functions |
| 70 |
require_once("wpf_setup.php"); |
| 71 |
// include admin options page |
| 72 |
require_once("wp-forecast-admin.php"); |
| 73 |
// display functions |
| 74 |
require_once("wp-forecast-show.php"); |
| 75 |
// shortcodes |
| 76 |
require_once("shortcodes.php"); |
| 77 |
// support for wordpress autoupdate |
| 78 |
require_once("wpf_autoupdate.php"); |
| 79 |
// super admin dialog |
| 80 |
require_once("wpf_sa_admin.php"); |
| 81 |
|
| 82 |
|
| 83 |
global $blog_id; |
| 84 |
|
| 85 |
// |
| 86 |
// set cache with weather data for current parameters |
| 87 |
// a wrapper function called via the init hook |
| 88 |
// |
| 89 |
|
| 90 |
function wp_forecast_init() |
| 91 |
{ |
| 92 |
pdebug(1,"Start of function wp_forecast_init ()"); |
| 93 |
|
| 94 |
// first of all check if we have to set a hard given |
| 95 |
// transport method |
| 96 |
if (isset($wp_forecast_pre_transport) && |
| 97 |
wpf_get_option("wp-forecast-pre-transport") != $wp_forecast_pre_transport ) |
| 98 |
{ |
| 99 |
pdebug(1,"Setting hard coded transport method to $wp_forecast_pre_transport"); |
| 100 |
wpf_update_option("wp-forecast-pre-transport",$wp_forecast_pre_transport); |
| 101 |
} |
| 102 |
|
| 103 |
$count=(int) wpf_get_option('wp-forecast-count'); |
| 104 |
|
| 105 |
$weather=array(); |
| 106 |
|
| 107 |
for ($i=0;$i<$count;$i++) |
| 108 |
{ |
| 109 |
$wpfcid=get_widget_id($i); |
| 110 |
|
| 111 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 112 |
|
| 113 |
if ($wpf_vars['expire'] < time()) |
| 114 |
{ |
| 115 |
switch ($wpf_vars['service']) |
| 116 |
{ |
| 117 |
case "accu": |
| 118 |
$w = accu_get_weather($wpf_vars['ACCU_BASE_URI'], |
| 119 |
$wpf_vars['location'], |
| 120 |
$wpf_vars['metric']); |
| 121 |
// next line is beta for non utf8 charactes in weatherdata |
| 122 |
$weather=accu_xml_parser(utf8_encode($w)); |
| 123 |
//$weather=accu_xml_parser($w); |
| 124 |
break; |
| 125 |
|
| 126 |
case "bug": |
| 127 |
$w1 = bug_get_weather($wpf_vars['BUG_BASE_URI'],$wpf_vars['apikey1'], |
| 128 |
$wpf_vars['location'],$wpf_vars['metric']); |
| 129 |
$weather1=bug_xml_parser($w1); |
| 130 |
$w2 = bug_get_weather($wpf_vars['BUG_FORC_URI'],$wpf_vars['apikey1'], |
| 131 |
$wpf_vars['location'],$wpf_vars['metric']); |
| 132 |
$weather2=bug_xml_parser($w2); |
| 133 |
$weather = array_merge($weather2,$weather1); |
| 134 |
break; |
| 135 |
|
| 136 |
case "com": |
| 137 |
// to be done |
| 138 |
break; |
| 139 |
|
| 140 |
case "google": |
| 141 |
$w = google_get_weather($wpf_vars['GOOGLE_BASE_URI'], |
| 142 |
$wpf_vars['location'], |
| 143 |
substr($wpf_vars['wpf_language'],0,2)); |
| 144 |
$weather=google_xml_parser(utf8_encode($w)); |
| 145 |
break; |
| 146 |
} |
| 147 |
|
| 148 |
pdebug(1,"Fetched xml was:\n".$w); |
| 149 |
|
| 150 |
// store weather to database and set expire time |
| 151 |
// if the current data wasnt available use old data |
| 152 |
if ( count($weather)>0) |
| 153 |
{ |
| 154 |
wpf_update_option("wp-forecast-cache".$wpfcid, serialize($weather)); |
| 155 |
if ( empty($weather['failure']) or $weather['failure'] == "" ) |
| 156 |
wpf_update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']); |
| 157 |
else |
| 158 |
wpf_update_option("wp-forecast-expire".$wpfcid, 0); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// javascript hinzufügen fuer ajax widget |
| 164 |
wp_enqueue_script('wpf_update', |
| 165 |
'/' . PLUGINDIR . '/wp-forecast/wpf_update.js', |
| 166 |
array('jquery'), "9999"); |
| 167 |
|
| 168 |
pdebug(1,"End of function wp_forecast_init ()"); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
// |
| 173 |
// this function is called from your template |
| 174 |
// to insert your weather data at the place you want it to be |
| 175 |
// support to select language on a per call basis from Robert Lang |
| 176 |
// |
| 177 |
function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null) |
| 178 |
{ |
| 179 |
|
| 180 |
pdebug(1,"Start of function wp_forecast_widget (".$wpfcid.")"); |
| 181 |
|
| 182 |
if ($wpfcid == "?") |
| 183 |
$wpf_vars=get_wpf_opts("A"); |
| 184 |
else |
| 185 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 186 |
|
| 187 |
if (!empty($language_override)) { |
| 188 |
$wpf_vars['wpf_language']=$language_override; |
| 189 |
} |
| 190 |
|
| 191 |
if ($wpfcid == "?") |
| 192 |
$weather=maybe_unserialize(wpf_get_option("wp-forecast-cacheA")); |
| 193 |
else |
| 194 |
$weather=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid)); |
| 195 |
|
| 196 |
show($wpfcid,$args,$wpf_vars); |
| 197 |
|
| 198 |
pdebug(1,"End of function wp_forecast_widget ()"); |
| 199 |
} |
| 200 |
|
| 201 |
// |
| 202 |
// this is the wrapper function for displaying from sidebar.php |
| 203 |
// and not as a widget. since the parameters are different we need this |
| 204 |
// |
| 205 |
function wp_forecast($wpfcid="A", $language_override=null) |
| 206 |
{ |
| 207 |
pdebug(1,"Start of function wp_forecast ()"); |
| 208 |
|
| 209 |
wp_forecast_widget( array(), $wpfcid, $language_override); |
| 210 |
|
| 211 |
pdebug(1,"End of function wp_forecast ()"); |
| 212 |
} |
| 213 |
|
| 214 |
// |
| 215 |
// a function to show a range of widgets at once |
| 216 |
// |
| 217 |
function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null) |
| 218 |
{ |
| 219 |
global $wpf_maxwidgets; |
| 220 |
$wcount=1; |
| 221 |
|
| 222 |
// check min and max limit |
| 223 |
if ($from < 0) |
| 224 |
$from = 0; |
| 225 |
|
| 226 |
if ($to > $wpf_maxwidgets) |
| 227 |
$to = $wpf_maxwidgets; |
| 228 |
|
| 229 |
// output table header |
| 230 |
echo "<table><tr>"; |
| 231 |
|
| 232 |
// out put widgets in a table |
| 233 |
for ($i=$from;$i<=$to;$i++) { |
| 234 |
|
| 235 |
if ( $wcount % $numpercol == 1) |
| 236 |
echo "<tr>"; |
| 237 |
|
| 238 |
echo "<td>"; |
| 239 |
wp_forecast( get_widget_id($i), $language_override); |
| 240 |
echo "</td>"; |
| 241 |
|
| 242 |
if ( ($wcount % $numpercol == 0) and ($i< $to)) |
| 243 |
echo "</tr>"; |
| 244 |
|
| 245 |
$wcount += 1; |
| 246 |
} |
| 247 |
|
| 248 |
// output table footer |
| 249 |
echo "</tr></table>"; |
| 250 |
} |
| 251 |
|
| 252 |
// |
| 253 |
// a function to show a set of widgets at once |
| 254 |
// |
| 255 |
function wp_forecast_set($wset, $numpercol=1, $language_override=null) |
| 256 |
{ |
| 257 |
global $wpf_maxwidgets; |
| 258 |
$wcount=1; |
| 259 |
$wset_max= count($wset)-1; |
| 260 |
|
| 261 |
// output table header |
| 262 |
echo "<table><tr>"; |
| 263 |
|
| 264 |
// out put widgets in a table |
| 265 |
for ($i=0;$i<=$wset_max;$i++) { |
| 266 |
|
| 267 |
if ( $wcount % $numpercol == 1) |
| 268 |
echo "<tr>"; |
| 269 |
|
| 270 |
echo "<td>"; |
| 271 |
wp_forecast( $wset[$i], $language_override); |
| 272 |
echo "</td>"; |
| 273 |
|
| 274 |
if ( ($wcount % $numpercol == 0) and ($i< $wset_max)) |
| 275 |
echo "</tr>"; |
| 276 |
|
| 277 |
$wcount += 1; |
| 278 |
} |
| 279 |
|
| 280 |
// output table footer |
| 281 |
echo "</tr></table>"; |
| 282 |
} |
| 283 |
|
| 284 |
// |
| 285 |
// returns the widget data as an array |
| 286 |
// |
| 287 |
function wp_forecast_data($wpfcid="A", $language_override=null) |
| 288 |
{ |
| 289 |
pdebug(1,"Start of function wp_forecast_data ()"); |
| 290 |
|
| 291 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 292 |
|
| 293 |
if (!empty($language_override)) { |
| 294 |
$wpf_vars['wpf_language']=$language_override; |
| 295 |
} |
| 296 |
|
| 297 |
extract($wpf_vars); |
| 298 |
$w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid)); |
| 299 |
|
| 300 |
$weather_arr=array(); |
| 301 |
|
| 302 |
// read service dependent weather data |
| 303 |
switch ($wpf_vars['service']) { |
| 304 |
case "accu": |
| 305 |
$weather_arr= accu_forecast_data($wpfcid,$language_override); |
| 306 |
break; |
| 307 |
case "bug": |
| 308 |
$weather_arr= bug_forecast_data($wpfcid,$language_override); |
| 309 |
break; |
| 310 |
case "com": |
| 311 |
// to be done |
| 312 |
break; |
| 313 |
case "google": |
| 314 |
$weather_arr= google_forecast_data($wpfcid,$language_override); |
| 315 |
break; |
| 316 |
} |
| 317 |
|
| 318 |
return $weather_arr; |
| 319 |
|
| 320 |
pdebug(1,"End of function wp_forecast_data ()"); |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// |
| 326 |
// set the choosen number of widgets, set at the widget page |
| 327 |
// |
| 328 |
function wpf_widget_setup() { |
| 329 |
global $wpf_maxwidgets; |
| 330 |
|
| 331 |
pdebug(1,"Start of function wpf_widget_setup ()"); |
| 332 |
|
| 333 |
$count = $newcount = wpf_get_option('wp-forecast-count'); |
| 334 |
if ( isset($_POST['wpf-count-submit']) ) { |
| 335 |
$number = (int) $_POST['wp-forecast-count']; |
| 336 |
if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets; |
| 337 |
if ( $number < 1 ) $number = 1; |
| 338 |
$newcount = $number; |
| 339 |
} |
| 340 |
if ( $count != $newcount ) { |
| 341 |
$count = $newcount; |
| 342 |
wpf_update_option('wp-forecast-count', $count); |
| 343 |
// add missing option to database |
| 344 |
wp_forecast_activate(); |
| 345 |
// init the new number of widgets |
| 346 |
widget_wp_forecast_init($count); |
| 347 |
} |
| 348 |
|
| 349 |
pdebug(1,"End of function wpf_widget_setup ()"); |
| 350 |
} |
| 351 |
|
| 352 |
// |
| 353 |
// form snippet to set the number of wanted widgets from |
| 354 |
// the widget page |
| 355 |
// |
| 356 |
function wpf_widget_page() { |
| 357 |
global $wpf_maxwidgets; |
| 358 |
|
| 359 |
pdebug(1,"Start of function wpf_widget_page ()"); |
| 360 |
|
| 361 |
$count = $newcount = wpf_get_option('wp-forecast-count'); |
| 362 |
|
| 363 |
// get locale |
| 364 |
$locale = get_locale(); |
| 365 |
if ( empty($locale) ) |
| 366 |
$locale = 'en_US'; |
| 367 |
// load translation |
| 368 |
if(function_exists('load_textdomain')) { |
| 369 |
load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo"); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
$out = "<div class='wrap'><form method='POST' action='#'>"; |
| 374 |
$out .= "<h2>WP-Forecast Widgets</h2>"; |
| 375 |
$out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." "; |
| 376 |
$out .= "<select id='wp-forecast-count' name='wp-forecast-count'>"; |
| 377 |
|
| 378 |
for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) { |
| 379 |
$out .= "<option value='$i' "; |
| 380 |
if ($count==$i) |
| 381 |
$out .= "selected='selected' "; |
| 382 |
$out .= ">$i</option>"; |
| 383 |
} |
| 384 |
$out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".esc_attr(__('Save'))." /></span></p></form></div>"; |
| 385 |
echo $out; |
| 386 |
|
| 387 |
pdebug(1,"End of function wpf_widget_page ()"); |
| 388 |
} |
| 389 |
|
| 390 |
function widget_wp_forecast_init() |
| 391 |
{ |
| 392 |
|
| 393 |
global $wp_version,$wpf_maxwidgets; |
| 394 |
|
| 395 |
pdebug(1,"Start of function widget_wp_forecast_init ()"); |
| 396 |
|
| 397 |
$count=(int) wpf_get_option('wp-forecast-count'); |
| 398 |
|
| 399 |
// check for widget support |
| 400 |
if ( !function_exists('register_sidebar_widget') ) |
| 401 |
return; |
| 402 |
|
| 403 |
// add fetch weather data to init the cache before any headers are sent |
| 404 |
add_action('init','wp_forecast_init'); |
| 405 |
add_action('admin_init','wp_forecast_admin_init'); |
| 406 |
|
| 407 |
// add css in header |
| 408 |
add_action('wp_head', 'wp_forecast_css'); |
| 409 |
|
| 410 |
// javascript hinzufügen |
| 411 |
//wp_enqueue_script('wpf_update', |
| 412 |
// '/' . PLUGINDIR . '/wp-forecast/wpf_update.js', |
| 413 |
// array('jquery'), "9999"); |
| 414 |
|
| 415 |
for ($i=0;$i<=$wpf_maxwidgets;$i++) { |
| 416 |
$wpfcid = get_widget_id( $i ); |
| 417 |
|
| 418 |
// register our widget and add a control |
| 419 |
$name = sprintf(__('wp-forecast %s'), $wpfcid); |
| 420 |
$id = "wp-forecast-$wpfcid"; |
| 421 |
|
| 422 |
|
| 423 |
// include widget class (new widget api) |
| 424 |
require_once("class-wpf_widget.php"); |
| 425 |
// register class |
| 426 |
add_action('widgets_init', create_function('', 'return register_widget("wpf_widget");')); |
| 427 |
|
| 428 |
wp_unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:''); |
| 429 |
|
| 430 |
wp_register_widget_control($id, $name, $i < $count ? 'wpf_admin_hint' : '', |
| 431 |
array('width' => 300, 'height' => 150)); |
| 432 |
|
| 433 |
wp_unregister_widget_control($i >= $count ? 'wpf_admin_hint'.$wpfcid : ''); |
| 434 |
} |
| 435 |
|
| 436 |
// add actions for setup the count of wanted wpf widgets |
| 437 |
add_action('sidebar_admin_setup', 'wpf_widget_setup'); |
| 438 |
add_action('sidebar_admin_page', 'wpf_widget_page'); |
| 439 |
|
| 440 |
// add filters for transport method check |
| 441 |
add_filter('use_fsockopen_transport','wpf_check_fsockopen'); |
| 442 |
add_filter('use_fopen_transport','wpf_check_fopen'); |
| 443 |
add_filter('use_streams_transport','wpf_check_streams'); |
| 444 |
add_filter('use_http_extension_transport','wpf_check_exthttp'); |
| 445 |
add_filter('use_curl_transport','wpf_check_curl'); |
| 446 |
|
| 447 |
pdebug(1,"End of function widget_wp_forecast_init ()"); |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
// MAIN |
| 454 |
|
| 455 |
pdebug(1,"Start of MAIN"); |
| 456 |
|
| 457 |
// activating deactivating the plugin |
| 458 |
register_activation_hook(__FILE__,'wp_forecast_activate'); |
| 459 |
register_deactivation_hook(__FILE__,'wp_forecast_deactivate'); |
| 460 |
|
| 461 |
// add option page |
| 462 |
add_action('admin_menu', 'wp_forecast_admin'); |
| 463 |
|
| 464 |
// add super admin options page (check for super admin is done inside) |
| 465 |
add_action('admin_menu', 'wpmu_forecast_admin'); |
| 466 |
|
| 467 |
// Run our code later in case this loads prior to any required plugins. |
| 468 |
add_action('plugins_loaded', 'widget_wp_forecast_init'); |
| 469 |
|
| 470 |
|
| 471 |
pdebug(1,"End of MAIN"); |
| 472 |
?> |
| 473 |
|