PluginProbe
wp-forecast / 9.6
wp-forecast v9.6
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
← All changes | wp-forecast.php +446 -371 3.09.6 View file →
@@ -1,461 +1,536 @@
1 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 Open-Meteo or OpenWeathermMap.
6 + * Version: 9.6
7 + * Author: Hans Matzen
8 + * Author URI: http://www.tuxlog.de
9 + * License: GPLv2
10 + * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 + * Domain Path: lang
12 + * Text Domain: wp-forecast
13 + *
14 + * @package wp-forecast
15 + */
16 +
17 +/**
18 + * Copyright 2006-2025 Hans Matzen
19 + *
20 + * This program is free software; you can redistribute it and/or modify
21 + * it under the terms of the GNU General Public License as published by
22 + * the Free Software Foundation; either version 2 of the License, or
23 + * (at your option) any later version.
24 + *
25 + * This program is distributed in the hope that it will be useful,
26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 + * GNU General Public License for more details.
29 + *
30 + * You should have received a copy of the GNU General Public License
31 + * along with this program; if not, write to the Free Software
32 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 + */
34 +
35 +//
36 +// maximal number of widgets to use.
37 +//
38 +$wpf_maxwidgets = 8;
39 +
2 40 /*
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.0
7 -Author: Hans Matzen
8 -Author URI: http://www.tuxlog.de
9 -*/
41 + ---------- no parameters to change after this point --------------------
42 + */
43 +// define path to wp-forecast plugin.
44 +define( 'WPF_PATH', plugin_dir_path( __FILE__ ) );
10 45
11 -/*
12 - Copyright 2006-2010 Hans Matzen
46 +// OpenWeathermap functions.
47 +require_once 'func-openweathermap.php';
48 +require_once 'func-openweathermap-v2.php';
49 +// Open-Meteo functions.
50 +require_once 'func-openmeteo.php';
13 51
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.
52 +// OpenUV data functions.
53 +require_once 'func-openuv.php';
54 +// ipstack functions.
55 +require_once 'func-ipstack.php';
18 56
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.
57 +// generic functions.
58 +require_once 'funclib.php';
59 +// include setup functions.
60 +require_once 'wpf-setup.php';
61 +// include admin options page.
62 +require_once 'wp-forecast-admin.php';
63 +// display functions.
64 +require_once 'wp-forecast-show.php';
65 +// shortcodes.
66 +require_once 'shortcodes.php';
67 +// support for WordPress autoupdate.
68 +require_once 'wpf-autoupdate.php';
69 +// virtual page class.
70 +require_once 'class-wpf-virtualpage.php';
23 71
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 72
73 +global $blog_id;
29 74
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="";
75 +/**
76 + * Set cache with weather data for current parameters
77 + * a wrapper function called via the init hook.
78 + */
79 +function wp_forecast_init() {
80 + $count = (int) wpf_get_option( 'wp-forecast-count' );
44 81
45 -//
46 -// maximal number of widgets to use
47 -//
48 -static $wpf_maxwidgets=20;
82 + $weather = array();
49 83
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;
84 + for ( $i = 0;$i < $count;$i++ ) {
85 + $wpfcid = get_widget_id( $i );
56 86
87 + $wpf_vars = get_wpf_opts( $wpfcid );
57 88
58 -/* ---------- no parameters to change after this point -------------------- */
89 + // use visitors location for weather and UV.
90 + if ( isset( $wpf_vars['visitorlocation'] ) && $wpf_vars['visitorlocation'] ) {
91 + // get users ip address.
92 + $vip = ipstack_get_visitor_ip();
59 93
60 -// accuweather data functions
61 -require_once("func_accu.php");
62 -// weatherbug data functions
63 -require_once("func_bug.php");
94 + // get location information about ip address.
95 + $vloc = ipstack_get_data( wpf_get_option( 'wp-forecast-ipstackapikey' ), $vip );
64 96
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");
97 + // find matching locations from open-meteo.
98 + // search locations for openmeteo.
99 + $openmeteo_loc = get_loclist( $wpf_vars['OPENMETEO_LOC_URI'], ( ! isset( $vloc['city'] ) ? '' : $vloc['city'] ) );
77 100
78 -// include super admin admin dialog on multisite
101 + // guess the locations which might be correct matching the country.
102 + $locations = array();
79 103
80 -// work around a bug in wp 3.0beta2-
81 -if (! function_exists('wp_get_current_user'))
82 - require_once(ABSPATH . '/wp-includes/pluggable.php');
104 + // prefilter list of found locations to reduce runtime.
105 + foreach ( $openmeteo_loc as $al ) {
106 + if ( strpos( $al['country'], $vloc['country_name'] ) !== false ) {
107 + $locations[] = $al;
108 + }
109 + }
83 110
84 -if ( function_exists("is_multisite") && is_multisite() &&
85 - function_exists('is_super_admin') && is_super_admin() ) {
86 - pdebug(1,"I am a SuperAdmin. :-)");
87 - require_once("wpf_sa_admin.php");
88 -}
111 + if ( count( $locations ) == 1 ) {
112 + // if there is only one location in the array take this one.
113 + $wpf_vars['location'] = $locations[0]['name'];
114 + $wpf_vars['locname'] = $locations[0]['name'];
115 + $wpf_vars['loclatitude'] = $locations[0]['latitude'];
116 + $wpf_vars['loclongitude'] = $locations[0]['longitude'];
117 + } else {
118 + // get the weather data of all the weather locations in the array and find the best match on lat and lon.
119 + $ldist = 99000.0;
120 + $lat1 = ( ! isset( $vloc['latitude'] ) ? '' : $vloc['latitude'] );
121 + $lon1 = ( ! isset( $vloc['longitude'] ) ? '' : $vloc['longitude'] );
122 + $tloc = '';
123 + $tname = '';
124 + foreach ( $locations as $ll ) {
125 + $lat2 = $ll['latitude'];
126 + $lon2 = $ll['longitude'];
127 + $cdist = distanceCalculation( $lat1, $lon1, $lat2, $lon2 );
89 128
129 + if ( $cdist < $ldist ) {
130 + $ldist = $cdist;
131 + $tloc = $ll['name'];
132 + $tname = $ll['name'];
133 + $tlat = $ll['latitude'];
134 + $tlon = $ll['longitude'];
135 + }
136 + }
90 137
91 -//
92 -// set cache with weather data for current parameters
93 -// a wrapper function called via the init hook
94 -//
138 + if ( '' != $tloc ) {
139 + $wpf_vars['locname'] = $tname;
140 + $wpf_vars['location'] = $tloc;
141 + $wpf_vars['loclatitude'] = $tlan;
142 + $wpf_vars['loclongitude'] = $tlon;
143 + } else {
144 + // nothing found set default values.
145 + $wpf_vars['location'] = 'Berlin Alexanderplatz';
146 + $wpf_vars['locname'] = 'Berlin Alexanderplatz';
147 + $wpf_vars['loclatitude'] = '52.521918';
148 + $wpf_vars['loclongitude'] = '13.413215';
149 + }
150 + }
95 151
96 -function wp_forecast_init()
97 -{
98 - pdebug(1,"Start of function wp_forecast_init ()");
152 + // make sure weather data is updated.
153 + $wpf_vars['expire'] = 0;
99 154
100 - // first of all check if we have to set a hard given
101 - // transport method
102 - if (isset($wp_forecast_pre_transport) &&
103 - wpf_get_option("wp-forecast-pre-transport") != $wp_forecast_pre_transport )
104 - {
105 - pdebug(1,"Setting hard coded transport method to $wp_forecast_pre_transport");
106 - wpf_update_option("wp-forecast-pre-transport",$wp_forecast_pre_transport);
107 - }
155 + // update wpf_vars in case we changed lat and lon.
156 + wpf_update_option( 'wp-forecast-opts' . $wpfcid, serialize( $wpf_vars ) );
157 + }
108 158
109 - $count=(int) wpf_get_option('wp-forecast-count');
110 -
111 - $weather=array();
112 -
113 - for ($i=0;$i<$count;$i++)
114 - {
115 - $wpfcid=get_widget_id($i);
116 -
117 - $wpf_vars=get_wpf_opts($wpfcid);
159 + // check if we have to fetch the weather data or if we can use the cache.
160 + if ( $wpf_vars['expire'] < time() ) {
161 + switch ( $wpf_vars['service'] ) {
162 + case 'openweathermap':
163 + $w = openweathermap_get_weather_v2( $wpf_vars['OPENWEATHERMAP_BASE_URI'], $wpf_vars['apikey1'], $wpf_vars['loclatitude'], $wpf_vars['loclongitude'], $wpf_vars['metric'] );
164 + $weather = openweathermap_get_data_v2( $w, $wpf_vars );
165 + break;
118 166
119 - if ($wpf_vars['expire'] < time())
120 - {
121 - switch ($wpf_vars['service'])
122 - {
123 - case "accu":
124 - $w = accu_get_weather($wpf_vars['ACCU_BASE_URI'],
125 - $wpf_vars['location'],
126 - $wpf_vars['metric']);
127 - // next line is beta for non utf8 charactes in weatherdata
128 - $weather=accu_xml_parser(utf8_encode($w));
129 - //$weather=accu_xml_parser($w);
130 - break;
131 -
132 - case "bug":
133 - $w1 = bug_get_weather($wpf_vars['BUG_BASE_URI'],$wpf_vars['apikey1'],
134 - $wpf_vars['location'],$wpf_vars['metric']);
135 - $weather1=bug_xml_parser($w1);
136 - $w2 = bug_get_weather($wpf_vars['BUG_FORC_URI'],$wpf_vars['apikey1'],
137 - $wpf_vars['location'],$wpf_vars['metric']);
138 - $weather2=bug_xml_parser($w2);
139 - $weather = array_merge($weather2,$weather1);
140 - break;
167 + case 'openweathermap3':
168 + $w = openweathermap_get_weather( $wpf_vars['OPENWEATHERMAP_BASE_URI_V3'], $wpf_vars['apikey1'], $wpf_vars['loclatitude'], $wpf_vars['loclongitude'], $wpf_vars['metric'] );
169 + $weather = openweathermap_get_data( $w, $wpf_vars );
170 + break;
171 + case 'openmeteo':
172 + $w = openmeteo_get_weather( $wpf_vars['OPENMETEO_BASE_URI'], $wpf_vars['loclatitude'], $wpf_vars['loclongitude'], $wpf_vars['metric'] );
173 + $weather = openmeteo_get_data( $w, $wpf_vars );
174 + break;
175 + }
141 176
142 - case "com":
143 - // to be done
144 - break;
145 - }
177 + // get OpenUV data if wanted.
178 + if ( isset( $wpf_vars['ouv_apikey'] ) && trim( $wpf_vars['ouv_apikey'] ) != '' ) {
179 + $ouv = openuv_get_data( $wpf_vars['ouv_apikey'], $weather['lat'], $weather['lon'] );
180 + if ( is_wp_error( $ouv ) ) {
181 + $weather['openuv'] = '';
182 + $weather['failure'] = $ouv->get_error_message();
183 + } else {
184 + $weather['openuv'] = $ouv;
185 + }
186 + }
146 187
147 - pdebug(1,"Fetched xml was:\n".$w);
188 + // store weather to database and set expire time.
189 + // if the current data wasnt available use old data.
190 + if ( count( $weather ) > 0 ) {
191 + wpf_update_option( 'wp-forecast-cache' . $wpfcid, serialize( $weather ) );
192 + if ( empty( $weather['failure'] ) || '' == $weather['failure'] ) {
193 + wpf_update_option( 'wp-forecast-expire' . $wpfcid, time() + $wpf_vars['refresh'] );
194 + } else {
195 + wpf_update_option( 'wp-forecast-expire' . $wpfcid, 0 );
196 + }
197 + }
198 + }
199 + }
148 200
149 - // store weather to database and set expire time
150 - // if the current data wasnt available use old data
151 - if ( count($weather)>0)
152 - {
153 - wpf_update_option("wp-forecast-cache".$wpfcid, serialize($weather));
154 - if ( empty($weather['failure']) or $weather['failure'] == "" )
155 - wpf_update_option("wp-forecast-expire".$wpfcid, time()+$wpf_vars['refresh']);
156 - else
157 - wpf_update_option("wp-forecast-expire".$wpfcid, 0);
158 - }
201 + // javascript hinzufügen fuer ajax widget.
202 + if ( ! is_admin() && get_option( 'wpf_sem_ajaxload' ) > 0 ) {
203 + wp_enqueue_script( 'wpf_update', plugins_url( 'wpf_update.js', __FILE__ ), array( 'jquery' ), '9999' );
159 204 }
160 - }
205 + // javascript hinzufügen für suche im admin dialog.
206 + if ( is_admin() ) {
207 + wp_enqueue_script( 'wp-color-picker' );
208 + wp_enqueue_script( 'wp-forecast-search', plugins_url( 'wp-forecast-admin.js', __FILE__ ), array( 'jquery' ), '9999' );
209 + }
161 210
162 - pdebug(1,"End of function wp_forecast_init ()");
211 + // add css attribute display to the list of safe css styles for pulldown widget.
212 + add_filter(
213 + 'safe_style_css',
214 + function( $styles ) {
215 + $styles[] = 'display';
216 + return $styles;
217 + }
218 + );
163 219 }
164 220
165 221
166 -//
167 -// this function is called from your template
168 -// to insert your weather data at the place you want it to be
169 -// support to select language on a per call basis from Robert Lang
170 -//
171 -function wp_forecast_widget($args=array(),$wpfcid="A", $language_override=null)
172 -{
222 +/**
223 + * This function is called from your template
224 + * to insert your weather data at the place you want it to be
225 + * support to select language on a per call basis from Robert Lang.
226 + *
227 + * @param array $args Default parameters.
228 + * @param string $wpfcid The Widget ID.
229 + * @param string $language_override ISO Code of the language.
230 + */
231 +function wp_forecast_widget( $args = array(), $wpfcid = 'A', $language_override = null ) {
232 + if ( '?' == $wpfcid ) {
233 + $wpf_vars = get_wpf_opts( 'A' );
234 + } else {
235 + $wpf_vars = get_wpf_opts( $wpfcid );
236 + }
173 237
174 - pdebug(1,"Start of function wp_forecast_widget (".$wpfcid.")");
175 -
176 - if ($wpfcid == "?")
177 - $wpf_vars=get_wpf_opts("A");
178 - else
179 - $wpf_vars=get_wpf_opts($wpfcid);
238 + if ( ! empty( $language_override ) ) {
239 + $wpf_vars['wpf_language'] = $language_override;
240 + }
180 241
181 - if (!empty($language_override)) {
182 - $wpf_vars['wpf_language']=$language_override;
183 - }
242 + if ( '?' == $wpfcid ) {
243 + $weather = maybe_unserialize( wpf_get_option( 'wp-forecast-cacheA' ) );
244 + } else {
245 + $weather = maybe_unserialize( wpf_get_option( 'wp-forecast-cache' . $wpfcid ) );
246 + }
184 247
185 - if ($wpfcid == "?")
186 - $weather=maybe_unserialize(wpf_get_option("wp-forecast-cacheA"));
187 - else
188 - $weather=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
248 + show( $wpfcid, $args, $wpf_vars );
249 +}
189 250
190 - show($wpfcid,$args,$wpf_vars);
251 +/**
252 + * This is the wrapper function for displaying from sidebar.php
253 + * and not as a widget. since the parameters are different we need this.
254 + *
255 + * @param string $wpfcid The Widget ID.
256 + * @param string $language_override ISO Code of the language.
257 + */
258 +function wp_forecast( $wpfcid = 'A', $language_override = null ) {
259 + wp_forecast_widget( array(), $wpfcid, $language_override );
260 +}
191 261
192 - pdebug(1,"End of function wp_forecast_widget ()");
193 -}
262 +/**
263 + * A function to show a range of widgets at once.
264 + *
265 + * @param int $from Lowest widget number to display.
266 + * @param int $to Highest widget number to display.
267 + * @param int $numpercol Number of widgets per column.
268 + * @param string $language_override ISO Code of the language.
269 + */
270 +function wp_forecast_range( $from = 0, $to = 0, $numpercol = 1, $language_override = null ) {
271 + global $wpf_maxwidgets;
272 + $wcount = 1;
194 273
195 -//
196 -// this is the wrapper function for displaying from sidebar.php
197 -// and not as a widget. since the parameters are different we need this
198 -//
199 -function wp_forecast($wpfcid="A", $language_override=null)
200 -{
201 - pdebug(1,"Start of function wp_forecast ()");
202 -
203 - wp_forecast_widget( array(), $wpfcid, $language_override);
274 + // check min and max limit.
275 + if ( $from < 0 ) {
276 + $from = 0;
277 + }
204 278
205 - pdebug(1,"End of function wp_forecast ()");
279 + if ( $to > $wpf_maxwidgets ) {
280 + $to = $wpf_maxwidgets;
281 + }
282 +
283 + // output table header.
284 + echo '<table><tr>';
285 +
286 + // out put widgets in a table.
287 + for ( $i = $from;$i <= $to;$i++ ) {
288 +
289 + if ( 1 == $wcount % $numpercol ) {
290 + echo '<tr>';
291 + }
292 +
293 + echo '<td>';
294 + wp_forecast( get_widget_id( $i ), $language_override );
295 + echo '</td>';
296 +
297 + if ( ( 0 == $wcount % $numpercol ) && ( $i < $to ) ) {
298 + echo '</tr>';
299 + }
300 +
301 + ++$wcount;
302 + }
303 +
304 + // output table footer.
305 + echo '</tr></table>';
206 306 }
207 307
208 -//
209 -// a function to show a range of widgets at once
210 -//
211 -function wp_forecast_range($from=0, $to=0, $numpercol=1, $language_override=null)
212 -{
213 - global $wpf_maxwidgets;
214 - $wcount=1;
308 +/**
309 + * A function to show a set of widgets at once.
310 + *
311 + * @param array $wset Array with numbers of widgets to display.
312 + * @param int $numpercol Number of widgets per column.
313 + * @param string $language_override ISO Code of the language.
314 + */
315 +function wp_forecast_set( $wset, $numpercol = 1, $language_override = null ) {
316 + global $wpf_maxwidgets;
317 + $wcount = 1;
318 + $wset_max = count( $wset ) - 1;
215 319
216 - // check min and max limit
217 - if ($from < 0)
218 - $from = 0;
219 -
220 - if ($to > $wpf_maxwidgets)
221 - $to = $wpf_maxwidgets;
222 -
223 - // output table header
224 - echo "<table><tr>";
320 + // output table header.
321 + echo '<table><tr>';
225 322
226 - // out put widgets in a table
227 - for ($i=$from;$i<=$to;$i++) {
323 + // out put widgets in a table.
324 + for ( $i = 0;$i <= $wset_max;$i++ ) {
228 325
229 - if ( $wcount % $numpercol == 1)
230 - echo "<tr>";
326 + if ( 1 == $wcount % $numpercol ) {
327 + echo '<tr>';
328 + }
231 329
232 - echo "<td>";
233 - wp_forecast( get_widget_id($i), $language_override);
234 - echo "</td>";
330 + echo '<td>';
331 + wp_forecast( $wset[ $i ], $language_override );
332 + echo '</td>';
235 333
236 - if ( ($wcount % $numpercol == 0) and ($i< $to))
237 - echo "</tr>";
334 + if ( ( 0 == $wcount % $numpercol ) && ( $i < $wset_max ) ) {
335 + echo '</tr>';
336 + }
238 337
239 - $wcount += 1;
240 - }
241 -
242 - // output table footer
243 - echo "</tr></table>";
338 + ++$wcount;
339 + }
340 +
341 + // output table footer.
342 + echo '</tr></table>';
244 343 }
245 344
246 -//
247 -// a function to show a set of widgets at once
248 -//
249 -function wp_forecast_set($wset, $numpercol=1, $language_override=null)
250 -{
251 - global $wpf_maxwidgets;
252 - $wcount=1;
253 - $wset_max= count($wset)-1;
345 +/**
346 + * Returns the widget data as an array.
347 + *
348 + * @param string $wpfcid The Widget ID.
349 + * @param string $language_override ISO Code of the language.
350 + */
351 +function wp_forecast_data( $wpfcid = 'A', $language_override = null ) {
352 + $wpf_vars = get_wpf_opts( $wpfcid );
254 353
255 - // output table header
256 - echo "<table><tr>";
354 + if ( ! empty( $language_override ) ) {
355 + $wpf_vars['wpf_language'] = $language_override;
356 + }
257 357
258 - // out put widgets in a table
259 - for ($i=0;$i<=$wset_max;$i++) {
358 + $w = maybe_unserialize( wpf_get_option( 'wp-forecast-cache' . $wpfcid ) );
260 359
261 - if ( $wcount % $numpercol == 1)
262 - echo "<tr>";
360 + $weather_arr = array();
263 361
264 - echo "<td>";
265 - wp_forecast( $wset[$i], $language_override);
266 - echo "</td>";
362 + // read service dependent weather data.
363 + switch ( $wpf_vars['service'] ) {
364 + case 'openweathermap':
365 + $weather_arr = openweathermap_forecast_data_v2( $wpfcid, $language_override );
366 + break;
367 + case 'openweathermap3':
368 + $weather_arr = openweathermap_forecast_data( $wpfcid, $language_override );
369 + break;
370 + case 'openmeteo':
371 + $weather_arr = openmeteo_forecast_data( $wpfcid, $language_override );
372 + break;
373 + }
267 374
268 - if ( ($wcount % $numpercol == 0) and ($i< $wset_max))
269 - echo "</tr>";
375 + // add openuv data to weather_arr.
376 + if ( is_array( $w ) && array_key_exists( 'openuv', $w ) ) {
377 + $weather_arr['openuv'] = $w['openuv'];
378 + } else {
379 + $weather_arr['openuv'] = array();
380 + }
270 381
271 - $wcount += 1;
272 - }
273 -
274 - // output table footer
275 - echo "</tr></table>";
382 + return $weather_arr;
276 383 }
277 384
278 -//
279 -// returns the widget data as an array
280 -//
281 -function wp_forecast_data($wpfcid="A", $language_override=null)
282 -{
283 - pdebug(1,"Start of function wp_forecast_data ()");
284 -
285 - $wpf_vars=get_wpf_opts($wpfcid);
286 385
287 - if (!empty($language_override)) {
288 - $wpf_vars['wpf_language']=$language_override;
289 - }
386 +/**
387 + * Register the wp-forecast widget
388 + */
389 +function reg_wpf_widget() {
390 + return register_widget( 'wpf_widget' );
391 +}
392 +/**
393 + * Register the UV Widget
394 + */
395 +function reg_wpf_uv_widget() {
396 + return register_widget( 'WpfUvWidget' );
397 +}
290 398
291 - extract($wpf_vars);
292 - $w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid));
399 +/**
400 + * Init the wp-forecast widgets.
401 + */
402 +function widget_wp_forecast_init() {
403 + global $wp_version,$wpf_maxwidgets, $wpf_lang_dict;
293 404
294 - $weather_arr=array();
405 + // include widget class.
406 + require_once 'class-wpf-widget.php';
407 + require_once 'class-wpfuvwidget.php';
295 408
296 - // read service dependent weather data
297 - switch ($wpf_vars['service']) {
298 - case "accu":
299 - $weather_arr= accu_forecast_data($wpfcid,$language_override);
300 - break;
301 - case "bug":
302 - $weather_arr= bug_forecast_data($wpfcid,$language_override);
303 - break;
304 - case "com":
305 - // to be done
306 - break;
307 - }
409 + $count = (int) wpf_get_option( 'wp-forecast-count' );
308 410
309 - return $weather_arr;
310 -
311 - pdebug(1,"End of function wp_forecast_data ()");
312 -
313 -}
411 + // check for widget support.
412 + if ( ! function_exists( 'register_sidebar_widget' ) ) {
413 + return;
414 + }
314 415
416 + // add fetch weather data to init the cache before any headers are sent.
417 + add_action( 'init', 'wp_forecast_init' );
418 + add_action( 'admin_init', 'wp_forecast_admin_init' );
315 419
316 -//
317 -// set the choosen number of widgets, set at the widget page
318 -//
319 -function wpf_widget_setup() {
320 - global $wpf_maxwidgets;
420 + // add css.
421 + add_action( 'wp_enqueue_scripts', 'wp_forecast_css' );
321 422
322 - pdebug(1,"Start of function wpf_widget_setup ()");
323 -
324 - $count = $newcount = wpf_get_option('wp-forecast-count');
325 - if ( isset($_POST['wpf-count-submit']) ) {
326 - $number = (int) $_POST['wp-forecast-count'];
327 - if ( $number > $wpf_maxwidgets ) $number = $wpf_maxwidgets;
328 - if ( $number < 1 ) $number = 1;
329 - $newcount = $number;
330 - }
331 - if ( $count != $newcount ) {
332 - $count = $newcount;
333 - wpf_update_option('wp-forecast-count', $count);
334 - // add missing option to database
335 - wp_forecast_activate();
336 - // init the new number of widgets
337 - widget_wp_forecast_init($count);
338 - }
423 + for ( $i = 0;$i <= $wpf_maxwidgets;$i++ ) {
424 + $wpfcid = get_widget_id( $i );
339 425
340 - pdebug(1,"End of function wpf_widget_setup ()");
341 -}
426 + // register our widget and add a control.
427 + // translators: %s is for the Widget ID.
428 + $name = sprintf( __( 'wp-forecast %s' ), $wpfcid );
429 + $id = "wp-forecast-$wpfcid";
342 430
343 -//
344 -// form snippet to set the number of wanted widgets from
345 -// the widget page
346 -//
347 -function wpf_widget_page() {
348 - global $wpf_maxwidgets;
349 -
350 - pdebug(1,"Start of function wpf_widget_page ()");
351 -
352 - $count = $newcount = wpf_get_option('wp-forecast-count');
353 -
354 - // get locale
355 - $locale = get_locale();
356 - if ( empty($locale) )
357 - $locale = 'en_US';
358 - // load translation
359 - if(function_exists('load_textdomain')) {
360 - load_textdomain("wp-forecast_".$locale,ABSPATH . "wp-content/plugins/wp-forecast/lang/".$locale.".mo");
361 - }
431 + // translators: %s is for the widget ID.
432 + $uvname = sprintf( __( 'wp-forecast-uv %s' ), $wpfcid );
433 + $uvid = "wp-forecast-uv-$wpfcid";
362 434
435 + // add widget.
436 + add_action( 'widgets_init', 'reg_wpf_widget' );
437 + add_action( 'widgets_init', 'reg_wpf_uv_widget' );
363 438
364 - $out = "<div class='wrap'><form method='POST'>";
365 - $out .= "<h2>WP-Forecast Widgets</h2>";
366 - $out .= "<p style='line-height: 30px;'>".__('How many wp-forecast widgets would you like?',"wp-forecast_".$locale)." ";
367 - $out .= "<select id='wp-forecast-count' name='wp-forecast-count' value='".$count."'>";
439 + wp_unregister_sidebar_widget( $i >= $count ? 'wp_forecast_widget' . $wpfcid : '' );
368 440
369 - for ( $i = 1; $i <= $wpf_maxwidgets; ++$i ) {
370 - $out .= "<option value='$i' ";
371 - if ($count==$i)
372 - $out .= "selected='selected' ";
373 - $out .= ">$i</option>";
374 - }
375 - $out .= "</select> <span class='submit'><input type='submit' name='wpf-count-submit' id='wpf-count-submit' value=".esc_attr(__('Save'))." /></span></p></form></div>";
376 - echo $out;
441 + wp_register_widget_control(
442 + $id,
443 + $name,
444 + $i < $count ? 'wpf_admin_hint' : '',
445 + array(
446 + 'width' => 300,
447 + 'height' => 150,
448 + )
449 + );
377 450
378 - pdebug(1,"End of function wpf_widget_page ()");
451 + wp_unregister_widget_control( $i >= $count ? 'wpf_admin_hint' . $wpfcid : '' );
452 + }
379 453 }
380 454
381 -function widget_wp_forecast_init()
382 -{
455 +/**
456 + * Filters the REQUEST_URI for virtual page slug
457 + * to show weather in an iframe.
458 + */
459 +function wpf_filter_url() {
460 + $url = ( isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' );
461 + $url = trim( parse_url( $url, PHP_URL_PATH ), '/' );
383 462
384 - global $wp_version,$wpf_maxwidgets;
463 + // determine if it is a wpf virtual page call.
464 + if ( strpos( $url, 'wp-forecast-direct' ) !== false ) {
465 + $wpfcid = ( isset( $_GET['wpfcid'] ) ? sanitize_text_field( wp_unslash( $_GET['wpfcid'] ) ) : 'A' );
466 + $language_override = ( isset( $_GET['language_override'] ) ? sanitize_text_field( wp_unslash( $_GET['language_override'] ) ) : '' );
467 + $header = ( isset( $_GET['header'] ) ? sanitize_text_field( wp_unslash( $_GET['header'] ) ) : '' );
468 + $selector = ( isset( $_GET['selector'] ) ? sanitize_text_field( wp_unslash( $_GET['selector'] ) ) : '' );
385 469
386 - pdebug(1,"Start of function widget_wp_forecast_init ()");
387 -
388 - $count=(int) wpf_get_option('wp-forecast-count');
470 + if ( '1' == $selector ) {
471 + $selector = '?';
472 + }
473 + $args = array();
474 + $wpf_vars = get_wpf_opts( $wpfcid );
389 475
390 - // check for widget support
391 - if ( !function_exists('register_sidebar_widget') )
392 - return;
476 + if ( ! empty( $language_override ) ) {
477 + $wpf_vars['wpf_language'] = $language_override;
478 + }
393 479
394 - // add fetch weather data to init the cache before any headers are sent
395 - add_action('init','wp_forecast_init');
396 - add_action('init','wp_forecast_admin_init');
397 -
398 - // add css in header
399 - add_action('wp_head', 'wp_forecast_css');
400 -
401 - // javascript hinzufügen
402 - wp_enqueue_script('wpf_update',
403 - '/' . PLUGINDIR . '/wp-forecast/wpf_update.js',
404 - array('jquery'), "9999");
480 + $weather = maybe_unserialize( wpf_get_option( 'wp-forecast-cache' . $wpfcid ) );
405 481
406 - for ($i=0;$i<=$wpf_maxwidgets;$i++) {
407 - $wpfcid = get_widget_id( $i );
482 + // only show weather html without page or header.
483 + if ( ! $header || 0 == $header ) {
484 + show( $selector . $wpfcid, $args, $wpf_vars );
485 + exit;
486 + }
408 487
409 - // register our widget and add a control
410 - $name = sprintf(__('wp-forecast %s'), $wpfcid);
411 - $id = "wp-forecast-$wpfcid";
412 -
413 -
414 - // include widget class (new widget api)
415 - require_once("class-wpf_widget.php");
416 - // register class
417 - add_action('widgets_init', create_function('', 'return register_widget("wpf_widget");'));
418 -
419 - wp_unregister_sidebar_widget($i >= $count ? 'wp_forecast_widget'.$wpfcid:'');
420 -
421 - wp_register_widget_control($id, $name, $i < $count ? 'wpf_admin_hint' : '',
422 - array('width' => 300, 'height' => 150));
423 -
424 - wp_unregister_widget_control($i >= $count ? 'wpf_admin_hint'.$wpfcid : '');
425 - }
488 + if ( 1 == $header ) {
489 + echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
490 + echo '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="' . esc_attr( str_replace( '_', '-', $wpf_vars['wpf_language'] ) ) . '">' . "\n";
491 + echo "<head><title>wp-forecast iframe</title>\n";
492 + echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />' . "\n";
493 + echo '<style>' . wp_kses( wp_forecast_get_nowp_css(), wpf_allowed_tags() ) . '</style>' . "\n";
494 + echo "</head>\n<body>\n";
426 495
427 - // add actions for setup the count of wanted wpf widgets
428 - add_action('sidebar_admin_setup', 'wpf_widget_setup');
429 - add_action('sidebar_admin_page', 'wpf_widget_page');
496 + show( $selector . $wpfcid, $args, $wpf_vars );
430 497
431 - // add filters for transport method check
432 - add_filter('use_fsockopen_transport','wpf_check_fsockopen');
433 - add_filter('use_fopen_transport','wpf_check_fopen');
434 - add_filter('use_streams_transport','wpf_check_streams');
435 - add_filter('use_http_extension_transport','wpf_check_exthttp');
436 - add_filter('use_curl_transport','wpf_check_curl');
498 + echo "</body></html>\n";
499 + exit;
500 + }
437 501
438 - pdebug(1,"End of function widget_wp_forecast_init ()");
502 + if ( 2 == $header ) {
503 + // create weather html.
504 + // add css.
505 + add_action( 'wp_enqueue_scripts', 'wp_forecast_css_nowp' );
506 + ob_start();
507 + show( $selector . $wpfcid, $args, $wpf_vars );
508 + $out = ob_get_contents();
509 + ob_end_clean();
439 510
511 + // create virtual page and display it.
512 + $args = array(
513 + 'slug' => 'wp-forecast-direct',
514 + 'title' => 'wp-forecast weather',
515 + 'content' => $out,
516 + );
517 + $pg4 = new Wpf_VirtualPage( $args );
518 + }
519 + }
440 520 }
441 521
442 522
523 +// MAIN.
443 524
444 -// MAIN
525 +// activating deactivating the plugin.
526 +register_activation_hook( __FILE__, 'wp_forecast_activate' );
527 +register_deactivation_hook( __FILE__, 'wp_forecast_deactivate' );
445 528
446 -pdebug(1,"Start of MAIN");
529 +// add option page.
530 +add_action( 'admin_menu', 'wp_forecast_admin' );
447 531
448 -// activating deactivating the plugin
449 -register_activation_hook(__FILE__,'wp_forecast_activate');
450 -register_deactivation_hook(__FILE__,'wp_forecast_deactivate');
451 -
452 -// add option page
453 -add_action('admin_menu', 'wp_forecast_admin');
454 -if ( function_exists("is_multisite") && is_multisite() && is_super_admin() )
455 - add_action('admin_menu', 'wpmu_forecast_admin');
456 -
457 532 // Run our code later in case this loads prior to any required plugins.
458 -add_action('plugins_loaded', 'widget_wp_forecast_init');
533 +add_action( 'plugins_loaded', 'widget_wp_forecast_init' );
459 534
460 -pdebug(1,"End of MAIN");
461 -?>
535 +// add virtual page filter.
536 +add_action( 'init', 'wpf_filter_url' );