PluginProbe
wp-forecast / 3.0
wp-forecast v3.0
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.0, at wp-forecast.php

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