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

454 lines 13.2 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.3
7 Author: Hans Matzen <webmaster at tuxlog.de>
8 Author URI: http://www.tuxlog.de
9 */
10
11 /* Copyright 2006,2007,2008 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 // maximal number of widgets to use
30 //
31 static $wpf_maxwidgets=20;
32
33 //
34 // set to 0 for no debugging information
35 // set to 1 for call stack
36 // set to 2 for call stack and variables ( not implemented yet)
37 //
38 static $wpf_debug=0;
39
40 // xml parser for accuweather xml
41 require_once("func_xml_parse.php");
42
43 // generic functions
44 require_once("funclib.php");
45 // include setup functions
46 require_once("setup.php");
47 // include admin options page
48 require_once("wp-forecast-admin.php");
49 // display functions
50 require_once("wp-forecast-show.php");
51
52 //
53 // set cache with weather data for current parameters
54 // a wrapper function called via the init hook
55 //
56
57 function wp_forecast_init()
58 {
59 global $wpf_debug;
60
61 if ($wpf_debug > 0)
62 pdebug("Start of function wp_forecast_init ()");
63
64 $count=(int) get_option('wp-forecast-count');
65 $weather=array();
66
67 for ($i=0;$i<$count;$i++) {
68 $wpfcid=get_widget_id($i);
69
70 $wpf_vars=get_wpf_opts($wpfcid);
71 $expire=get_option("wp-forecast-expire".$wpfcid);
72
73 if ($expire < time()) {
74 $w = get_weather($wpf_vars['BASE_URI'],$wpf_vars['location'],
75 $wpf_vars['metric']);
76
77 if ($wpf_debug > 0) {
78 pdebug("Fetched xml was:\n");
79 pdebug($w);
80 }
81
82 $weather=wpf_xml_parser($w);
83
84 // store weather to database and set expire time
85 // if the current data wasnt available use old data
86 if ( count($weather)>0) {
87 update_option("wp-forecast-cache".$wpfcid, arr2str($weather));
88 update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']);
89 }
90 }
91 }
92 if ($wpf_debug > 0)
93 pdebug("End of function wp_forecast_init ()");
94 }
95
96
97 //
98 // this function is called from your template
99 // to insert your weather data at the place you want it to be
100 // support to select language on a per call basis from Robert Lang
101 //
102 function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null)
103 {
104
105 global $wpf_debug;
106
107 if ($wpf_debug > 0)
108 pdebug("Start of function wp_forecast_widget ()");
109
110 $wpf_vars=get_wpf_opts($wpfcid);
111 if (!empty($language_override)) {
112 $wpf_vars['wpf_language']=$language_override;
113 }
114 $weather=str2arr(get_option("wp-forecast-cache".$wpfcid));
115 show($wpfcid,$weather,$args,$wpf_vars);
116
117 if ($wpf_debug > 0)
118 pdebug("End of function wp_forecast_widget ()");
119 }
120
121 //
122 // this is the wrapper function for displaying from sidebar.php
123 // and not as a widget. since the parameters are different we need this
124 //
125 function wp_forecast($wpfcid="A", $language_override=null)
126 {
127 global $wpf_debug;
128
129 if ($wpf_debug > 0)
130 pdebug("Start of function wp_forecast ()");
131
132 wp_forecast_widget( array(), $wpfcid, $language_override);
133
134 if ($wpf_debug > 0)
135 pdebug("End of function wp_forecast ()");
136 }
137
138 //
139 // a function to show a range of widgets at once
140 //
141 function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null)
142 {
143 global $wpf_maxwidgets;
144 $wcount=1;
145
146 // check min and max limit
147 if ($from < 0)
148 $from = 0;
149
150 if ($to > $wpf_maxwidgets)
151 $to = $wpf_maxwidgets;
152
153 // output table header
154 echo "<table><tr>";
155
156 // out put widgets in a table
157 for ($i=$from;$i<=$to;$i++) {
158
159 if ( $wcount % $numpercol == 1)
160 echo "<tr>";
161
162 echo "<td>";
163 wp_forecast( get_widget_id($i), $language_override);
164 echo "</td>";
165
166 if ( ($wcount % $numpercol == 0) and ($i< $to))
167 echo "</tr>";
168
169 $wcount += 1;
170 }
171
172 // output table footer
173 echo "</tr></table>";
174 }
175
176 //
177 // a function to show a set of widgets at once
178 //
179 function wp_forecast_set($wset, $numpercol=1, $language_override=null)
180 {
181 global $wpf_maxwidgets;
182 $wcount=1;
183 $wset_max= count($wset)-1;
184
185 // output table header
186 echo "<table><tr>";
187
188 // out put widgets in a table
189 for ($i=0;$i<=$wset_max;$i++) {
190
191 if ( $wcount % $numpercol == 1)
192 echo "<tr>";
193
194 echo "<td>";
195 wp_forecast( $wset[$i], $language_override);
196 echo "</td>";
197
198 if ( ($wcount % $numpercol == 0) and ($i< $wset_max))
199 echo "</tr>";
200
201 $wcount += 1;
202 }
203
204 // output table footer
205 echo "</tr></table>";
206 }
207
208 //
209 // returns the widget data as an array one line per item
210 //
211 function wp_forecast_data($wpfcid="A", $language_override=null)
212 {
213 global $wpf_debug;
214
215 if ($wpf_debug > 0)
216 pdebug("Start of function wp_forecast_data ()");
217
218 $wpf_vars=get_wpf_opts($wpfcid);
219 if (!empty($language_override)) {
220 $wpf_vars['wpf_language']=$language_override;
221 }
222
223 extract($wpf_vars);
224 $w=str2arr(get_option("wp-forecast-cache".$wpfcid));
225
226 // get translations
227 if(function_exists('load_textdomain')) {
228 global $l10n;
229 if (!isset($l10n["wp-forecast_".$wpf_language]))
230 load_textdomain("wp-forecast_".$wpf_language, ABSPATH . "wp-content/plugins/wp-forecast/lang/".$wpf_language.".mo");
231 }
232
233 $weather_arr=array();
234
235 // --------------------------------------------------------------
236 // calc values for current conditions
237
238 $weather_arr['acculink']= 'http://www.accuweather.com/world-index-forecast.asp?locCode=' . get_option('wp-forecast-location'.$wpfcid) . '&amp;metric=' . get_option("wp-forecast-metric".$wpfcid);
239 $weather_arr['location']=get_option("wp-forecast-locname".$wpfcid);
240 $weather_arr['locname']= $w["city"]." ".$w["state"];
241
242
243 $lt = time() - date("Z"); // this is the GMT
244 $ct = $lt + (3600 * ($w['gmtdiff'])); // local time
245 if ( $w['gmtdiffdls'] == 1)
246 $ct += 3600; // time with daylightsavings
247 $weather_arr['blogdate']=date_i18n($fc_date_format, $ct);
248 $weather_arr['blogtime']=date_i18n($fc_time_format, $ct);
249
250 $cts = $w['fc_obsdate_1']." ".$w['time'];
251 $ct = strtotime($cts);
252 $weather_arr['accudate']=date_i18n($fc_date_format, $ct);
253 $weather_arr['accutime']=date_i18n($fc_time_format, $ct);
254
255
256 $iconfile=find_icon($w["weathericon"]);
257 $weather_arr['icon']="icons/".$iconfile;
258
259 $weather_arr['shorttext']= __($w["weathericon"],"wp-forecast_".$wpf_language);
260
261 $weather_arr['temperature']=$w["temperature"]. "&deg;".$w['un_temp'];
262 $weather_arr['realfeel']=$w["realfeel"]."&deg;".$w['un_temp'];
263 $weather_arr['pressure']=$w["pressure"]." ".$w["un_pres"];
264 $weather_arr['humidity']=round($w["humidity"],0);
265 $weather_arr['windspeed']=windstr($metric,$w["windspeed"],$windunit);
266 $weather_arr['winddir']=$w["winddirection"];
267 $weather_arr['windgusts']=windstr($metric,$w["wgusts"],$windunit);
268 $sunarr = explode(" ",$w["sun"]);
269 $weather_arr['sunrise']=$sunarr[0];
270 $weather_arr['sunset']=$sunarr[1];
271 $weather_arr['copyright']="Copyright 2008 AccuWeather, Inc.";
272
273
274 // calc values for forecast
275 for ($i = 1; $i < 10; $i++) {
276 // daytime forecast
277 $weather_arr['fc_obsdate_'.$i]= date_i18n($fc_date_format, strtotime($w['fc_obsdate_'.$i]));
278 $iconfile=find_icon($w["fc_dt_icon_".$i]);
279 $weather_arr["fc_dt_icon_".$i]="icons/".$iconfile;
280 $weather_arr["fc_dt_desc_".$i]= __($w["fc_dt_icon_".$i],"wp-forecast_".$wpf_language);
281 $weather_arr["fc_dt_htemp_".$i]= $w["fc_dt_htemp_".$i]."&deg;".$w['un_temp'];
282 $wstr=windstr($metric,$w["fc_dt_windspeed_".$i],$windunit);
283 $weather_arr["fc_dt_windspeed_".$i]= $wstr;
284 $weather_arr["fc_dt_winddir_".$i]=translate_winddir($w["fc_dt_winddir_".$i],"wp-forecast_".$wpf_language);
285 $weather_arr["fc_dt_wgusts_".$i] = windstr($metric,$w["fc_dt_wgusts_".$i],$windunit);
286
287 // nighttime forecast
288 $iconfile=find_icon($w["fc_nt_icon_".$i]);
289 $weather_arr["fc_nt_icon_".$i]="icons/".$iconfile;
290 $weather_arr["fc_nt_desc_".$i]= __($w["fc_nt_icon_".$i],"wp-forecast_".$wpf_language);
291 $weather_arr["fc_nt_ltemp_".$i]= $w["fc_nt_ltemp_".$i]."&deg;".$w['un_temp'];
292 $wstr=windstr($metric,$w["fc_nt_windspeed_".$i],$windunit);
293 $weather_arr["fc_nt_windspeed_".$i]= $wstr;
294 $weather_arr["fc_nt_winddir_".$i]=translate_winddir($w["fc_nt_winddir_".$i],"wp-forecast_".$wpf_language);
295 $weather_arr["fc_nt_wgusts_".$i] = windstr($metric,$w["fc_nt_wgusts_".$i],$windunit);
296
297 // additional info
298 $weather_arr['failure']=$w['failure'];
299 $weather_arr['lat']=$w['lat'];
300 $weather_arr['lon']=$w['lon'];
301 }
302
303 if ($wpf_debug > 0)
304 pdebug("End of function wp_forecast_data ()");
305
306 return $weather_arr;
307 }
308
309
310 //
311 // set the choosen number of widgets, set at the widget page
312 //
313 function wpf_widget_setup() {
314 global $wpf_debug,$wpf_maxwidgets;
315
316 if ($wpf_debug > 0)
317 pdebug("Start of function wpf_widget_setup ()");
318
319 $count = $newcount = get_option('wp-forecast-count');
320 if ( isset($_POST['wpf-count-submit']) ) {
321 $number = (int) $_POST['wpf-number'];
322 if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets;
323 if ( $number < 1 ) $number = 1;
324 $newcount = $number;
325 }
326 if ( $count != $newcount ) {
327 $count = $newcount;
328 update_option('wp-forecast-count', $count);
329 // add missing option to database
330 wp_forecast_activate();
331 // init the new number of widgets
332 widget_wp_forecast_init($count);
333 }
334
335 if ($wpf_debug > 0)
336 pdebug("End of function wpf_widget_setup ()");
337 }
338
339 //
340 // form snippet to set the number of wanted widgets from
341 // the widget page
342 //
343 function wpf_widget_page() {
344 global $wpf_debug,$wpf_maxwidgets;
345
346 if ($wpf_debug > 0)
347 pdebug("Start of function wpf_widget_page ()");
348
349 $count = $newcount = get_option('wp-forecast-count');
350
351 // get locale
352 $locale = get_locale();
353 if ( empty($locale) )
354 $locale = 'en_US';
355 // load translation
356 if(function_exists('load_textdomain')) {
357 load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo");
358 }
359
360
361 $out = "<div class='wrap'><form method='POST'>";
362 $out .= "<h2>WP-Forecast Widgets</h2>";
363 $out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." ";
364 $out .= "<select id='wpf-number' name='wpf-number' value='".$count."'>";
365
366 for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) {
367 $out .= "<option value='$i' ";
368 if ($count==$i)
369 $out .= "selected='selected' ";
370 $out .= ">$i</option>";
371 }
372 $out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".attribute_escape(__('Save'))." /></span></p></form></div>";
373 echo $out;
374
375 if ($wpf_debug > 0)
376 pdebug("End of function wpf_widget_page ()");
377 }
378
379 function widget_wp_forecast_init()
380 {
381
382 global $wp_version,$wpf_debug,$wpf_maxwidgets;
383
384 if ($wpf_debug > 0)
385 pdebug("Start of function widget_wp_forecast_init ()");
386
387 $count=(int) get_option('wp-forecast-count');
388
389 // check for widget support
390 if ( !function_exists('register_sidebar_widget') )
391 return;
392
393 // add fetch weather data to init the cache before any headers are sent
394 add_action('init','wp_forecast_init');
395 add_action('init','wp_forecast_admin_init');
396
397 // add css in header
398 add_action('wp_head', 'wp_forecast_css');
399
400 for ($i=0;$i<=$wpf_maxwidgets;$i++) {
401 $wpfcid = get_widget_id( $i );
402
403 // register our widget and add a control
404 $name = sprintf(__('wp-forecast %s'), $wpfcid);
405 $id = "wp-forecast$wpfcid";
406
407 // register / unregister widget and control form
408 // the first part is to work around bug 4275 which was
409 // corrected with v2.2.1
410 if ($wp_version < "2.2.1")
411 register_sidebar_widget(array($id,$name),
412 $i < $count ? 'wp_forecast_widget' : '','',$wpfcid);
413 else
414 register_sidebar_widget(array($id,$name),
415 $i < $count ? 'wp_forecast_widget' : '',$wpfcid);
416
417 unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:'');
418
419 register_widget_control(array($id,$name),
420 $i < $count ? 'wpf_admin_hint' : ''
421 ,300,150,$wpfcid,1);
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 if ($wpf_debug > 0)
430 pdebug("End of function widget_wp_forecast_init ()");
431
432 }
433
434
435
436 // MAIN
437
438 if ($wpf_debug > 0)
439 pdebug("Start of MAIN");
440
441 // activating deactivating the plugin
442 register_activation_hook(__FILE__,'wp_forecast_activate');
443 register_deactivation_hook(__FILE__,'wp_forecast_deactivate');
444
445 // add option page
446 add_action('admin_menu', 'wp_forecast_admin');
447
448 // Run our code later in case this loads prior to any required plugins.
449 add_action('plugins_loaded', 'widget_wp_forecast_init');
450
451 if ($wpf_debug > 0)
452 pdebug("End of MAIN");
453 ?>
454