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

475 lines 12.7 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.1
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 pdebug(1,"End of function wp_forecast_init ()");
173 }
174
175
176 //
177 // this function is called from your template
178 // to insert your weather data at the place you want it to be
179 // support to select language on a per call basis from Robert Lang
180 //
181 function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null)
182 {
183
184 pdebug(1,"Start of function wp_forecast_widget (".$wpfcid.")");
185
186 if ($wpfcid == "?")
187 $wpf_vars=get_wpf_opts("A");
188 else
189 $wpf_vars=get_wpf_opts($wpfcid);
190
191 if (!empty($language_override)) {
192 $wpf_vars['wpf_language']=$language_override;
193 }
194
195 if ($wpfcid == "?")
196 $weather=maybe_unserialize(wpf_get_option("wp-forecast-cacheA"));
197 else
198 $weather=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
199
200 show($wpfcid,$args,$wpf_vars);
201
202 pdebug(1,"End of function wp_forecast_widget ()");
203 }
204
205 //
206 // this is the wrapper function for displaying from sidebar.php
207 // and not as a widget. since the parameters are different we need this
208 //
209 function wp_forecast($wpfcid="A", $language_override=null)
210 {
211 pdebug(1,"Start of function wp_forecast ()");
212
213 wp_forecast_widget( array(), $wpfcid, $language_override);
214
215 pdebug(1,"End of function wp_forecast ()");
216 }
217
218 //
219 // a function to show a range of widgets at once
220 //
221 function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null)
222 {
223 global $wpf_maxwidgets;
224 $wcount=1;
225
226 // check min and max limit
227 if ($from < 0)
228 $from = 0;
229
230 if ($to > $wpf_maxwidgets)
231 $to = $wpf_maxwidgets;
232
233 // output table header
234 echo "<table><tr>";
235
236 // out put widgets in a table
237 for ($i=$from;$i<=$to;$i++) {
238
239 if ( $wcount % $numpercol == 1)
240 echo "<tr>";
241
242 echo "<td>";
243 wp_forecast( get_widget_id($i), $language_override);
244 echo "</td>";
245
246 if ( ($wcount % $numpercol == 0) and ($i< $to))
247 echo "</tr>";
248
249 $wcount += 1;
250 }
251
252 // output table footer
253 echo "</tr></table>";
254 }
255
256 //
257 // a function to show a set of widgets at once
258 //
259 function wp_forecast_set($wset, $numpercol=1, $language_override=null)
260 {
261 global $wpf_maxwidgets;
262 $wcount=1;
263 $wset_max= count($wset)-1;
264
265 // output table header
266 echo "<table><tr>";
267
268 // out put widgets in a table
269 for ($i=0;$i<=$wset_max;$i++) {
270
271 if ( $wcount % $numpercol == 1)
272 echo "<tr>";
273
274 echo "<td>";
275 wp_forecast( $wset[$i], $language_override);
276 echo "</td>";
277
278 if ( ($wcount % $numpercol == 0) and ($i< $wset_max))
279 echo "</tr>";
280
281 $wcount += 1;
282 }
283
284 // output table footer
285 echo "</tr></table>";
286 }
287
288 //
289 // returns the widget data as an array
290 //
291 function wp_forecast_data($wpfcid="A", $language_override=null)
292 {
293 pdebug(1,"Start of function wp_forecast_data ()");
294
295 $wpf_vars=get_wpf_opts($wpfcid);
296
297 if (!empty($language_override)) {
298 $wpf_vars['wpf_language']=$language_override;
299 }
300
301 extract($wpf_vars);
302 $w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
303
304 $weather_arr=array();
305
306 // read service dependent weather data
307 switch ($wpf_vars['service']) {
308 case "accu":
309 $weather_arr= accu_forecast_data($wpfcid,$language_override);
310 break;
311 case "bug":
312 $weather_arr= bug_forecast_data($wpfcid,$language_override);
313 break;
314 case "com":
315 // to be done
316 break;
317 case "google":
318 $weather_arr= google_forecast_data($wpfcid,$language_override);
319 break;
320 }
321
322 return $weather_arr;
323
324 pdebug(1,"End of function wp_forecast_data ()");
325
326 }
327
328
329 //
330 // set the choosen number of widgets, set at the widget page
331 //
332 function wpf_widget_setup() {
333 global $wpf_maxwidgets;
334
335 pdebug(1,"Start of function wpf_widget_setup ()");
336
337 $count = $newcount = wpf_get_option('wp-forecast-count');
338 if ( isset($_POST['wpf-count-submit']) ) {
339 $number = (int) $_POST['wp-forecast-count'];
340 if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets;
341 if ( $number < 1 ) $number = 1;
342 $newcount = $number;
343 }
344 if ( $count != $newcount ) {
345 $count = $newcount;
346 wpf_update_option('wp-forecast-count', $count);
347 // add missing option to database
348 wp_forecast_activate();
349 // init the new number of widgets
350 widget_wp_forecast_init($count);
351 }
352
353 pdebug(1,"End of function wpf_widget_setup ()");
354 }
355
356 //
357 // form snippet to set the number of wanted widgets from
358 // the widget page
359 //
360 function wpf_widget_page() {
361 global $wpf_maxwidgets;
362
363 pdebug(1,"Start of function wpf_widget_page ()");
364
365 $count = $newcount = wpf_get_option('wp-forecast-count');
366
367 // get locale
368 $locale = get_locale();
369 if ( empty($locale) )
370 $locale = 'en_US';
371 // load translation
372 if(function_exists('load_textdomain')) {
373 load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo");
374 }
375
376
377 $out = "<div class='wrap'><form method='POST'>";
378 $out .= "<h2>WP-Forecast Widgets</h2>";
379 $out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." ";
380 $out .= "<select id='wp-forecast-count' name='wp-forecast-count' value='".$count."'>";
381
382 for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) {
383 $out .= "<option value='$i' ";
384 if ($count==$i)
385 $out .= "selected='selected' ";
386 $out .= ">$i</option>";
387 }
388 $out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".esc_attr(__('Save'))." /></span></p></form></div>";
389 echo $out;
390
391 pdebug(1,"End of function wpf_widget_page ()");
392 }
393
394 function widget_wp_forecast_init()
395 {
396
397 global $wp_version,$wpf_maxwidgets;
398
399 pdebug(1,"Start of function widget_wp_forecast_init ()");
400
401 $count=(int) wpf_get_option('wp-forecast-count');
402
403 // check for widget support
404 if ( !function_exists('register_sidebar_widget') )
405 return;
406
407 // add fetch weather data to init the cache before any headers are sent
408 add_action('init','wp_forecast_init');
409 add_action('init','wp_forecast_admin_init');
410
411 // add css in header
412 add_action('wp_head', 'wp_forecast_css');
413
414 // javascript hinzufügen
415 wp_enqueue_script('wpf_update',
416 '/' . PLUGINDIR . '/wp-forecast/wpf_update.js',
417 array('jquery'), "9999");
418
419 for ($i=0;$i<=$wpf_maxwidgets;$i++) {
420 $wpfcid = get_widget_id( $i );
421
422 // register our widget and add a control
423 $name = sprintf(__('wp-forecast %s'), $wpfcid);
424 $id = "wp-forecast-$wpfcid";
425
426
427 // include widget class (new widget api)
428 require_once("class-wpf_widget.php");
429 // register class
430 add_action('widgets_init', create_function('', 'return register_widget("wpf_widget");'));
431
432 wp_unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:'');
433
434 wp_register_widget_control($id, $name, $i < $count ? 'wpf_admin_hint' : '',
435 array('width' => 300, 'height' => 150));
436
437 wp_unregister_widget_control($i >= $count ? 'wpf_admin_hint'.$wpfcid : '');
438 }
439
440 // add actions for setup the count of wanted wpf widgets
441 add_action('sidebar_admin_setup', 'wpf_widget_setup');
442 add_action('sidebar_admin_page', 'wpf_widget_page');
443
444 // add filters for transport method check
445 add_filter('use_fsockopen_transport','wpf_check_fsockopen');
446 add_filter('use_fopen_transport','wpf_check_fopen');
447 add_filter('use_streams_transport','wpf_check_streams');
448 add_filter('use_http_extension_transport','wpf_check_exthttp');
449 add_filter('use_curl_transport','wpf_check_curl');
450
451 pdebug(1,"End of function widget_wp_forecast_init ()");
452
453 }
454
455
456
457 // MAIN
458
459 pdebug(1,"Start of MAIN");
460
461 // activating deactivating the plugin
462 register_activation_hook(__FILE__,'wp_forecast_activate');
463 register_deactivation_hook(__FILE__,'wp_forecast_deactivate');
464
465 // add option page
466 add_action('admin_menu', 'wp_forecast_admin');
467 if ( function_exists("is_multisite") && is_multisite() && is_super_admin() )
468 add_action('admin_menu', 'wpmu_forecast_admin');
469
470 // Run our code later in case this loads prior to any required plugins.
471 add_action('plugins_loaded', 'widget_wp_forecast_init');
472
473 pdebug(1,"End of MAIN");
474 ?>
475