PluginProbe
wp-forecast / 3.3
wp-forecast v3.3
10.0 trunk 1.4 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3..5 3..8 3.0 3.1 3.2 3.3 3.4 3.6 All 86 releases
wp-forecast / wp-forecast.php

wp-forecast.php in wp-forecast 3.3, at wp-forecast.php

481 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.3
7 Author: Hans Matzen
8 Author URI: http://www.tuxlog.de
9 */
10
11 /*
12 Copyright 2006-2010 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("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
80 // include super admin admin dialog on multisite
81
82 // work around a bug in wp 3.0beta2-
83 if (! function_exists('wp_get_current_user'))
84 require_once(ABSPATH . '/wp-includes/pluggable.php');
85
86 global $blog_id;
87 if ( function_exists("is_multisite") && is_multisite() &&
88 function_exists('is_super_admin') && is_super_admin() ) {
89 pdebug(1,"I am a SuperAdmin. :-)");
90 require_once("wpf_sa_admin.php");
91 }
92
93
94 //
95 // set cache with weather data for current parameters
96 // a wrapper function called via the init hook
97 //
98
99 function wp_forecast_init()
100 {
101 pdebug(1,"Start of function wp_forecast_init ()");
102
103 // first of all check if we have to set a hard given
104 // transport method
105 if (isset($wp_forecast_pre_transport) &&
106 wpf_get_option("wp-forecast-pre-transport") != $wp_forecast_pre_transport )
107 {
108 pdebug(1,"Setting hard coded transport method to $wp_forecast_pre_transport");
109 wpf_update_option("wp-forecast-pre-transport",$wp_forecast_pre_transport);
110 }
111
112 $count=(int) wpf_get_option('wp-forecast-count');
113
114 $weather=array();
115
116 for ($i=0;$i<$count;$i++)
117 {
118 $wpfcid=get_widget_id($i);
119
120 $wpf_vars=get_wpf_opts($wpfcid);
121
122 if ($wpf_vars['expire'] < time())
123 {
124 switch ($wpf_vars['service'])
125 {
126 case "accu":
127 $w = accu_get_weather($wpf_vars['ACCU_BASE_URI'],
128 $wpf_vars['location'],
129 $wpf_vars['metric']);
130 // next line is beta for non utf8 charactes in weatherdata
131 $weather=accu_xml_parser(utf8_encode($w));
132 //$weather=accu_xml_parser($w);
133 break;
134
135 case "bug":
136 $w1 = bug_get_weather($wpf_vars['BUG_BASE_URI'],$wpf_vars['apikey1'],
137 $wpf_vars['location'],$wpf_vars['metric']);
138 $weather1=bug_xml_parser($w1);
139 $w2 = bug_get_weather($wpf_vars['BUG_FORC_URI'],$wpf_vars['apikey1'],
140 $wpf_vars['location'],$wpf_vars['metric']);
141 $weather2=bug_xml_parser($w2);
142 $weather = array_merge($weather2,$weather1);
143 break;
144
145 case "com":
146 // to be done
147 break;
148
149 case "google":
150 $w = google_get_weather($wpf_vars['GOOGLE_BASE_URI'],
151 $wpf_vars['location'],
152 substr($wpf_vars['wpf_language'],0,2));
153 $weather=google_xml_parser(utf8_encode($w));
154 break;
155 }
156
157 pdebug(1,"Fetched xml was:\n".$w);
158
159 // store weather to database and set expire time
160 // if the current data wasnt available use old data
161 if ( count($weather)>0)
162 {
163 wpf_update_option("wp-forecast-cache".$wpfcid, serialize($weather));
164 if ( empty($weather['failure']) or $weather['failure'] == "" )
165 wpf_update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']);
166 else
167 wpf_update_option("wp-forecast-expire".$wpfcid, 0);
168 }
169 }
170 }
171
172 // javascript hinzufügen fuer ajax widget
173 wp_enqueue_script('wpf_update',
174 '/' . PLUGINDIR . '/wp-forecast/wpf_update.js',
175 array('jquery'), "9999");
176
177 pdebug(1,"End of function wp_forecast_init ()");
178 }
179
180
181 //
182 // this function is called from your template
183 // to insert your weather data at the place you want it to be
184 // support to select language on a per call basis from Robert Lang
185 //
186 function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null)
187 {
188
189 pdebug(1,"Start of function wp_forecast_widget (".$wpfcid.")");
190
191 if ($wpfcid == "?")
192 $wpf_vars=get_wpf_opts("A");
193 else
194 $wpf_vars=get_wpf_opts($wpfcid);
195
196 if (!empty($language_override)) {
197 $wpf_vars['wpf_language']=$language_override;
198 }
199
200 if ($wpfcid == "?")
201 $weather=maybe_unserialize(wpf_get_option("wp-forecast-cacheA"));
202 else
203 $weather=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
204
205 show($wpfcid,$args,$wpf_vars);
206
207 pdebug(1,"End of function wp_forecast_widget ()");
208 }
209
210 //
211 // this is the wrapper function for displaying from sidebar.php
212 // and not as a widget. since the parameters are different we need this
213 //
214 function wp_forecast($wpfcid="A", $language_override=null)
215 {
216 pdebug(1,"Start of function wp_forecast ()");
217
218 wp_forecast_widget( array(), $wpfcid, $language_override);
219
220 pdebug(1,"End of function wp_forecast ()");
221 }
222
223 //
224 // a function to show a range of widgets at once
225 //
226 function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null)
227 {
228 global $wpf_maxwidgets;
229 $wcount=1;
230
231 // check min and max limit
232 if ($from < 0)
233 $from = 0;
234
235 if ($to > $wpf_maxwidgets)
236 $to = $wpf_maxwidgets;
237
238 // output table header
239 echo "<table><tr>";
240
241 // out put widgets in a table
242 for ($i=$from;$i<=$to;$i++) {
243
244 if ( $wcount % $numpercol == 1)
245 echo "<tr>";
246
247 echo "<td>";
248 wp_forecast( get_widget_id($i), $language_override);
249 echo "</td>";
250
251 if ( ($wcount % $numpercol == 0) and ($i< $to))
252 echo "</tr>";
253
254 $wcount += 1;
255 }
256
257 // output table footer
258 echo "</tr></table>";
259 }
260
261 //
262 // a function to show a set of widgets at once
263 //
264 function wp_forecast_set($wset, $numpercol=1, $language_override=null)
265 {
266 global $wpf_maxwidgets;
267 $wcount=1;
268 $wset_max= count($wset)-1;
269
270 // output table header
271 echo "<table><tr>";
272
273 // out put widgets in a table
274 for ($i=0;$i<=$wset_max;$i++) {
275
276 if ( $wcount % $numpercol == 1)
277 echo "<tr>";
278
279 echo "<td>";
280 wp_forecast( $wset[$i], $language_override);
281 echo "</td>";
282
283 if ( ($wcount % $numpercol == 0) and ($i< $wset_max))
284 echo "</tr>";
285
286 $wcount += 1;
287 }
288
289 // output table footer
290 echo "</tr></table>";
291 }
292
293 //
294 // returns the widget data as an array
295 //
296 function wp_forecast_data($wpfcid="A", $language_override=null)
297 {
298 pdebug(1,"Start of function wp_forecast_data ()");
299
300 $wpf_vars=get_wpf_opts($wpfcid);
301
302 if (!empty($language_override)) {
303 $wpf_vars['wpf_language']=$language_override;
304 }
305
306 extract($wpf_vars);
307 $w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
308
309 $weather_arr=array();
310
311 // read service dependent weather data
312 switch ($wpf_vars['service']) {
313 case "accu":
314 $weather_arr= accu_forecast_data($wpfcid,$language_override);
315 break;
316 case "bug":
317 $weather_arr= bug_forecast_data($wpfcid,$language_override);
318 break;
319 case "com":
320 // to be done
321 break;
322 case "google":
323 $weather_arr= google_forecast_data($wpfcid,$language_override);
324 break;
325 }
326
327 return $weather_arr;
328
329 pdebug(1,"End of function wp_forecast_data ()");
330
331 }
332
333
334 //
335 // set the choosen number of widgets, set at the widget page
336 //
337 function wpf_widget_setup() {
338 global $wpf_maxwidgets;
339
340 pdebug(1,"Start of function wpf_widget_setup ()");
341
342 $count = $newcount = wpf_get_option('wp-forecast-count');
343 if ( isset($_POST['wpf-count-submit']) ) {
344 $number = (int) $_POST['wp-forecast-count'];
345 if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets;
346 if ( $number < 1 ) $number = 1;
347 $newcount = $number;
348 }
349 if ( $count != $newcount ) {
350 $count = $newcount;
351 wpf_update_option('wp-forecast-count', $count);
352 // add missing option to database
353 wp_forecast_activate();
354 // init the new number of widgets
355 widget_wp_forecast_init($count);
356 }
357
358 pdebug(1,"End of function wpf_widget_setup ()");
359 }
360
361 //
362 // form snippet to set the number of wanted widgets from
363 // the widget page
364 //
365 function wpf_widget_page() {
366 global $wpf_maxwidgets;
367
368 pdebug(1,"Start of function wpf_widget_page ()");
369
370 $count = $newcount = wpf_get_option('wp-forecast-count');
371
372 // get locale
373 $locale = get_locale();
374 if ( empty($locale) )
375 $locale = 'en_US';
376 // load translation
377 if(function_exists('load_textdomain')) {
378 load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo");
379 }
380
381
382 $out = "<div class='wrap'><form method='POST'>";
383 $out .= "<h2>WP-Forecast Widgets</h2>";
384 $out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." ";
385 $out .= "<select id='wp-forecast-count' name='wp-forecast-count' value='".$count."'>";
386
387 for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) {
388 $out .= "<option value='$i' ";
389 if ($count==$i)
390 $out .= "selected='selected' ";
391 $out .= ">$i</option>";
392 }
393 $out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".esc_attr(__('Save'))." /></span></p></form></div>";
394 echo $out;
395
396 pdebug(1,"End of function wpf_widget_page ()");
397 }
398
399 function widget_wp_forecast_init()
400 {
401
402 global $wp_version,$wpf_maxwidgets;
403
404 pdebug(1,"Start of function widget_wp_forecast_init ()");
405
406 $count=(int) wpf_get_option('wp-forecast-count');
407
408 // check for widget support
409 if ( !function_exists('register_sidebar_widget') )
410 return;
411
412 // add fetch weather data to init the cache before any headers are sent
413 add_action('init','wp_forecast_init');
414 add_action('init','wp_forecast_admin_init');
415
416 // add css in header
417 add_action('wp_head', 'wp_forecast_css');
418
419 // javascript hinzufügen
420 //wp_enqueue_script('wpf_update',
421 // '/' . PLUGINDIR . '/wp-forecast/wpf_update.js',
422 // array('jquery'), "9999");
423
424 for ($i=0;$i<=$wpf_maxwidgets;$i++) {
425 $wpfcid = get_widget_id( $i );
426
427 // register our widget and add a control
428 $name = sprintf(__('wp-forecast %s'), $wpfcid);
429 $id = "wp-forecast-$wpfcid";
430
431
432 // include widget class (new widget api)
433 require_once("class-wpf_widget.php");
434 // register class
435 add_action('widgets_init', create_function('', 'return register_widget("wpf_widget");'));
436
437 wp_unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:'');
438
439 wp_register_widget_control($id, $name, $i < $count ? 'wpf_admin_hint' : '',
440 array('width' => 300, 'height' => 150));
441
442 wp_unregister_widget_control($i >= $count ? 'wpf_admin_hint'.$wpfcid : '');
443 }
444
445 // add actions for setup the count of wanted wpf widgets
446 add_action('sidebar_admin_setup', 'wpf_widget_setup');
447 add_action('sidebar_admin_page', 'wpf_widget_page');
448
449 // add filters for transport method check
450 add_filter('use_fsockopen_transport','wpf_check_fsockopen');
451 add_filter('use_fopen_transport','wpf_check_fopen');
452 add_filter('use_streams_transport','wpf_check_streams');
453 add_filter('use_http_extension_transport','wpf_check_exthttp');
454 add_filter('use_curl_transport','wpf_check_curl');
455
456 pdebug(1,"End of function widget_wp_forecast_init ()");
457
458 }
459
460
461
462 // MAIN
463
464 pdebug(1,"Start of MAIN");
465
466 // activating deactivating the plugin
467 register_activation_hook(__FILE__,'wp_forecast_activate');
468 register_deactivation_hook(__FILE__,'wp_forecast_deactivate');
469
470 // add option page
471 add_action('admin_menu', 'wp_forecast_admin');
472 if ( function_exists("is_multisite") && is_multisite() && is_super_admin() )
473 add_action('admin_menu', 'wpmu_forecast_admin');
474
475 // Run our code later in case this loads prior to any required plugins.
476 add_action('plugins_loaded', 'widget_wp_forecast_init');
477
478
479 pdebug(1,"End of MAIN");
480 ?>
481