PluginProbe
wp-forecast / 2.7
wp-forecast v2.7
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 2.7, at wp-forecast.php

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