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

442 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.4
7 Author: Hans Matzen <webmaster at tuxlog.de>
8 Author URI: http://www.tuxlog.de
9 */
10
11 /* Copyright 2006-2009 Hans Matzen (email : webmaster at tuxlog.de)
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28
29 //
30 // only use this in case of severe problems accessing the admin dialog
31 //
32 // preselected transport method for fetching the weather data
33 // valid values are
34 // curl - uses libcurl
35 // fsockopen - uses fsockopen
36 // streams - uses fopen with streams
37 // exthttp - uses pecl http extension
38 // this will override every setting from the admin dialog
39 // you have to assure that the chosen transport is supported by the
40 // wordpress class WP_Http;
41 //
42 static $wp_forecast_pre_transport="";
43
44 //
45 // maximal number of widgets to use
46 //
47 static $wpf_maxwidgets=20;
48
49 //
50 // set to 0 for no debugging information
51 // set to 1 for call stack
52 // set to 2 for call stack including xml parser
53 //
54 static $wpf_debug=0;
55
56
57 /* ---------- no parameters to change after this point -------------------- */
58
59 // accuweather data functions
60 require_once("func_accu.php");
61 // weatherbug data functions
62 require_once("func_bug.php");
63
64 // generic functions
65 require_once("funclib.php");
66 // include setup functions
67 require_once("setup.php");
68 // include admin options page
69 require_once("wp-forecast-admin.php");
70 // display functions
71 require_once("wp-forecast-show.php");
72 // shortcodes
73 require_once("shortcodes.php");
74
75 //
76 // set cache with weather data for current parameters
77 // a wrapper function called via the init hook
78 //
79
80 function wp_forecast_init()
81 {
82 pdebug(1,"Start of function wp_forecast_init ()");
83
84 // first of all check if we have to set a hard given
85 // transport method
86 if ($wp_forecast_pre_transport!="" &&
87 get_option("wp-forecast-pre-transport") != $wp_forecast_pre_transport )
88 {
89 pdebug(1,"Setting hard coded transport method to $wp_forecast_pre_transport");
90 update_option("wp-forecast-pre-transport",$wp_forecast_pre_transport);
91 }
92
93 $count=(int) get_option('wp-forecast-count');
94
95 $weather=array();
96
97 for ($i=0;$i<$count;$i++)
98 {
99 $wpfcid=get_widget_id($i);
100
101 $wpf_vars=get_wpf_opts($wpfcid);
102
103 if ($wpf_vars['expire'] < time())
104 {
105 switch ($wpf_vars['service'])
106 {
107 case "accu":
108 $w = accu_get_weather($wpf_vars['ACCU_BASE_URI'],
109 $wpf_vars['location'],
110 $wpf_vars['metric']);
111 $weather=accu_xml_parser($w);
112 break;
113
114 case "bug":
115 $w1 = bug_get_weather($wpf_vars['BUG_BASE_URI'],$wpf_vars['apikey1'],
116 $wpf_vars['location'],$wpf_vars['metric']);
117 $weather1=bug_xml_parser($w1);
118 $w2 = bug_get_weather($wpf_vars['BUG_FORC_URI'],$wpf_vars['apikey1'],
119 $wpf_vars['location'],$wpf_vars['metric']);
120 $weather2=bug_xml_parser($w2);
121 $weather = array_merge($weather2,$weather1);
122 break;
123
124 case "com":
125 // to be done
126 break;
127 }
128
129 pdebug(1,"Fetched xml was:\n".$w);
130
131 // store weather to database and set expire time
132 // if the current data wasnt available use old data
133 if ( count($weather)>0)
134 {
135 update_option("wp-forecast-cache".$wpfcid, serialize($weather));
136 if ( empty($weather['failure']) or $weather['failure'] == "" )
137 update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']);
138 else
139 update_option("wp-forecast-expire".$wpfcid, 0);
140 }
141 }
142 }
143 pdebug(1,"End of function wp_forecast_init ()");
144 }
145
146
147 //
148 // this function is called from your template
149 // to insert your weather data at the place you want it to be
150 // support to select language on a per call basis from Robert Lang
151 //
152 function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null)
153 {
154
155 pdebug(1,"Start of function wp_forecast_widget ()");
156
157 $wpf_vars=get_wpf_opts($wpfcid);
158 if (!empty($language_override)) {
159 $wpf_vars['wpf_language']=$language_override;
160 }
161 $weather=unserialize(get_option("wp-forecast-cache".$wpfcid));
162 show($wpfcid,$args,$wpf_vars);
163
164 pdebug(1,"End of function wp_forecast_widget ()");
165 }
166
167
168
169 //
170 // this is the wrapper function for displaying from sidebar.php
171 // and not as a widget. since the parameters are different we need this
172 //
173 function wp_forecast($wpfcid="A", $language_override=null)
174 {
175 pdebug(1,"Start of function wp_forecast ()");
176
177 wp_forecast_widget( array(), $wpfcid, $language_override);
178
179 pdebug(1,"End of function wp_forecast ()");
180 }
181
182 //
183 // a function to show a range of widgets at once
184 //
185 function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null)
186 {
187 global $wpf_maxwidgets;
188 $wcount=1;
189
190 // check min and max limit
191 if ($from < 0)
192 $from = 0;
193
194 if ($to > $wpf_maxwidgets)
195 $to = $wpf_maxwidgets;
196
197 // output table header
198 echo "<table><tr>";
199
200 // out put widgets in a table
201 for ($i=$from;$i<=$to;$i++) {
202
203 if ( $wcount % $numpercol == 1)
204 echo "<tr>";
205
206 echo "<td>";
207 wp_forecast( get_widget_id($i), $language_override);
208 echo "</td>";
209
210 if ( ($wcount % $numpercol == 0) and ($i< $to))
211 echo "</tr>";
212
213 $wcount += 1;
214 }
215
216 // output table footer
217 echo "</tr></table>";
218 }
219
220 //
221 // a function to show a set of widgets at once
222 //
223 function wp_forecast_set($wset, $numpercol=1, $language_override=null)
224 {
225 global $wpf_maxwidgets;
226 $wcount=1;
227 $wset_max= count($wset)-1;
228
229 // output table header
230 echo "<table><tr>";
231
232 // out put widgets in a table
233 for ($i=0;$i<=$wset_max;$i++) {
234
235 if ( $wcount % $numpercol == 1)
236 echo "<tr>";
237
238 echo "<td>";
239 wp_forecast( $wset[$i], $language_override);
240 echo "</td>";
241
242 if ( ($wcount % $numpercol == 0) and ($i< $wset_max))
243 echo "</tr>";
244
245 $wcount += 1;
246 }
247
248 // output table footer
249 echo "</tr></table>";
250 }
251
252 //
253 // returns the widget data as an array
254 //
255 function wp_forecast_data($wpfcid="A", $language_override=null)
256 {
257 pdebug(1,"Start of function wp_forecast_data ()");
258
259 $wpf_vars=get_wpf_opts($wpfcid);
260
261 if (!empty($language_override)) {
262 $wpf_vars['wpf_language']=$language_override;
263 }
264
265 extract($wpf_vars);
266 $w=unserialize(get_option("wp-forecast-cache".$wpfcid));
267
268 $weather_arr=array();
269
270 // read service dependent weather data
271 switch ($wpf_vars['service']) {
272 case "accu":
273 $weather_arr= accu_forecast_data($wpfcid,$language_override);
274 break;
275 case "bug":
276 $weather_arr= bug_forecast_data($wpfcid,$language_override);
277 break;
278 case "com":
279 // to be done
280 break;
281 }
282
283 return $weather_arr;
284
285 pdebug(1,"End of function wp_forecast_data ()");
286
287 }
288
289
290 //
291 // set the choosen number of widgets, set at the widget page
292 //
293 function wpf_widget_setup() {
294 global $wpf_maxwidgets;
295
296 pdebug(1,"Start of function wpf_widget_setup ()");
297
298 $count = $newcount = get_option('wp-forecast-count');
299 if ( isset($_POST['wpf-count-submit']) ) {
300 $number = (int) $_POST['wpf-number'];
301 if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets;
302 if ( $number < 1 ) $number = 1;
303 $newcount = $number;
304 }
305 if ( $count != $newcount ) {
306 $count = $newcount;
307 update_option('wp-forecast-count', $count);
308 // add missing option to database
309 wp_forecast_activate();
310 // init the new number of widgets
311 widget_wp_forecast_init($count);
312 }
313
314 pdebug(1,"End of function wpf_widget_setup ()");
315 }
316
317 //
318 // form snippet to set the number of wanted widgets from
319 // the widget page
320 //
321 function wpf_widget_page() {
322 global $wpf_maxwidgets;
323
324 pdebug(1,"Start of function wpf_widget_page ()");
325
326 $count = $newcount = get_option('wp-forecast-count');
327
328 // get locale
329 $locale = get_locale();
330 if ( empty($locale) )
331 $locale = 'en_US';
332 // load translation
333 if(function_exists('load_textdomain')) {
334 load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo");
335 }
336
337
338 $out = "<div class='wrap'><form method='POST'>";
339 $out .= "<h2>WP-Forecast Widgets</h2>";
340 $out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." ";
341 $out .= "<select id='wpf-number' name='wpf-number' value='".$count."'>";
342
343 for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) {
344 $out .= "<option value='$i' ";
345 if ($count==$i)
346 $out .= "selected='selected' ";
347 $out .= ">$i</option>";
348 }
349 $out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".attribute_escape(__('Save'))." /></span></p></form></div>";
350 echo $out;
351
352 pdebug(1,"End of function wpf_widget_page ()");
353 }
354
355 function widget_wp_forecast_init()
356 {
357
358 global $wp_version,$wpf_maxwidgets;
359
360 pdebug(1,"Start of function widget_wp_forecast_init ()");
361
362 $count=(int) get_option('wp-forecast-count');
363
364 // check for widget support
365 if ( !function_exists('register_sidebar_widget') )
366 return;
367
368 // add fetch weather data to init the cache before any headers are sent
369 add_action('init','wp_forecast_init');
370 add_action('init','wp_forecast_admin_init');
371
372 // add css in header
373 add_action('wp_head', 'wp_forecast_css');
374
375 for ($i=0;$i<=$wpf_maxwidgets;$i++) {
376 $wpfcid = get_widget_id( $i );
377
378 // register our widget and add a control
379 $name = sprintf(__('wp-forecast %s'), $wpfcid);
380 $id = "wp-forecast-$wpfcid";
381
382
383 // register / unregister widget and control form
384 // the first part is to work around bug 4275 which was
385 // corrected with v2.2.1
386 if (version_compare($wp_version, '2.2.1', '<') )
387 register_sidebar_widget(array($id,$name),
388 $i < $count ? 'wp_forecast_widget' : '','',$wpfcid);
389 else if (version_compare($wp_version, '2.8', '<'))
390 {
391 register_sidebar_widget(array($id,$name),
392 $i < $count ? 'wp_forecast_widget' : '',$wpfcid);
393 }
394 else /* version 2.8 and up */
395 {
396 // include widget class (new widget api)
397 require_once("class-wpf_widget.php");
398 // register class
399 add_action('widgets_init', create_function('', 'return register_widget("wpf_widget");'));
400 }
401
402 unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:'');
403
404 register_widget_control(array($id,$name), $i < $count ? 'wpf_admin_hint' : ''
405 ,300,150,$wpfcid,2);
406
407 unregister_widget_control($i >= $count ? 'wpf_admin_hint'.$wpfcid : '');
408 }
409 // add actions for setup the count of wanted wpf widgets
410 add_action('sidebar_admin_setup', 'wpf_widget_setup');
411 add_action('sidebar_admin_page', 'wpf_widget_page');
412
413 // add filters for transport method check
414 add_filter('use_fsockopen_transport','wpf_check_fsockopen');
415 add_filter('use_fopen_transport','wpf_check_fopen');
416 add_filter('use_streams_transport','wpf_check_streams');
417 add_filter('use_http_extension_transport','wpf_check_exthttp');
418 add_filter('use_curl_transport','wpf_check_curl');
419
420 pdebug(1,"End of function widget_wp_forecast_init ()");
421
422 }
423
424
425
426 // MAIN
427
428 pdebug(1,"Start of MAIN");
429
430 // activating deactivating the plugin
431 register_activation_hook(__FILE__,'wp_forecast_activate');
432 register_deactivation_hook(__FILE__,'wp_forecast_deactivate');
433
434 // add option page
435 add_action('admin_menu', 'wp_forecast_admin');
436
437 // Run our code later in case this loads prior to any required plugins.
438 add_action('plugins_loaded', 'widget_wp_forecast_init');
439
440 pdebug(1,"End of MAIN");
441 ?>
442