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

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