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
wp-forecast / func-openweathermap-v2.php

func-openweathermap-v2.php in wp-forecast 9.6, at func-openweathermap-v2.php

348 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /** This file is part of the wp-forecast plugin for WordPress
3 *
4 * Copyright 2024 Hans Matzen (email : webmaster at tuxlog dot de)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * @package wp-forecast
21 */
22
23 if ( ! function_exists( 'openweathermap_get_weather_v2' ) ) {
24 /**
25 * Get the data from OpenWeatherMap.
26 *
27 * @param strin $baseuri the Base URI for OWM.
28 * @param string $apikey the apikey from OWM.
29 * @param string $lat the latitude of the location.
30 * @param string $lon the longitude of the location.
31 * @param string $metric the units to us.
32 */
33 function openweathermap_get_weather_v2( $baseuri, $apikey, $lat, $lon, $metric ) {
34 $data = array();
35
36 if ( '1' == $metric ) {
37 $metric = 'metric';
38 } else {
39 $metric = 'imperial';
40 }
41
42 // check parms.
43 if ( trim( $apikey ) == '' || trim( $lat ) == '' || trim( $lon ) == '' ) {
44 return array();
45 }
46 $url1 = $baseuri . '/weather?lat=' . $lat . '&lon=' . $lon . '&appid=' . $apikey . '&units=' . $metric . '&lang=en';
47 $url2 = $baseuri . '/forecast?lat=' . $lat . '&lon=' . $lon . '&appid=' . $apikey . '&units=' . $metric . '&lang=en';
48
49 // Open the file and decode it.
50 $file1 = wp_remote_get( $url1 );
51 if ( ! is_wp_error( $file1 ) ) {
52 $data1 = json_decode( $file1['body'], true );
53 }
54
55 $file2 = wp_remote_get( $url2 );
56 if ( ! is_wp_error( $file2 ) ) {
57 $data2 = json_decode( $file2['body'], true );
58 }
59
60 $data['current'] = $data1;
61 $data['forecast'] = $data2;
62
63 return $data;
64 }
65 }
66
67
68 if ( ! function_exists( 'openweathermap_get_data_v2' ) ) {
69 /**
70 * Extract the weather data from json object returned from OWM.
71 *
72 * @param array $weather_array the array to store the weather.
73 * @param array $wpf_vars the parameters of the weather widget.
74 */
75 function openweathermap_get_data_v2( $weather_array, $wpf_vars ) {
76 $w = array();
77
78 if ( '1' == $wpf_vars['metric'] ) {
79 $w['un_temp'] = 'C';
80 $w['un_dist'] = 'km';
81 $w['un_speed'] = 'm/s';
82 $w['un_pres'] = 'mb';
83 $w['un_prec'] = 'mm';
84 } else {
85 $w['un_temp'] = 'F';
86 $w['un_dist'] = 'mi';
87 $w['un_speed'] = 'mph';
88 $w['un_pres'] = 'mb';
89 $w['un_prec'] = 'in';
90 }
91
92 if ( ! isset( $weather_array['current'] ) || ! isset( $weather_array['forecast'] ) ) {
93 $w['failure'] = 'No OpenWeathermap data available. ';
94 if ( isset( $weather_array['message'] ) ) {
95 $w['failure'] .= $weather_array['message'];
96 }
97 return $w;
98 }
99
100 $w['lat'] = $weather_array['current']['coord']['lat'];
101 $w['lon'] = $weather_array['current']['coord']['lon'];
102 $w['time'] = $weather_array['current']['dt'];
103 $w['timezone'] = $weather_array['current']['timezone'];
104 $tz_prefix = '+';
105 if ( $w['timezone'] < 0 ) {
106 $tz_prefix = '-';
107 }
108 $mtz = new DateTimeZone( $tz_prefix . $w['timezone'] / 36 );
109
110 // current conditions.
111 $w['pressure'] = $weather_array['current']['main']['pressure'];
112 $w['temperature'] = round( $weather_array['current']['main']['temp'], 0 );
113 $w['realfeel'] = round( $weather_array['current']['main']['feels_like'], 0 );
114 $w['humidity'] = $weather_array['current']['main']['humidity'];
115 $w['weathertext'] = $weather_array['current']['weather'][0]['description'];
116 $w['weathericon'] = $weather_array['current']['weather'][0]['icon'];
117 $w['weatherid'] = $weather_array['current']['weather'][0]['id'];
118 $w['wgusts'] = $weather_array['current']['wind']['speed'];
119 $w['windspeed'] = $weather_array['current']['wind']['speed'];
120 $w['winddirection'] = $weather_array['current']['wind']['deg'];
121 $w['uvindex'] = -1;
122
123 // map precipitation values.
124 // init vars.
125 $w['precipProbability'] = 0;
126 $w['precipIntensity'] = 0;
127 $w['precipType'] = '';
128 // if it rains add rain volume and set precipitation type to rain.
129 if ( isset( $weather_array['current']['rain'] ) ) {
130 $w['precipIntensity'] += $weather_array['current']['rain']['1h'];
131 $w['precipType'] = 'Rain';
132 }
133 // if it snows add snow volume and set precipitation type to snow.
134 if ( isset( $weather_array['current']['snow'] ) ) {
135 $w['precipIntensity'] += $weather_array['current']['snow']['1h'];
136 $w['precipType'] = 'Snow';
137 }
138 // convert mm to inches for compatibility reasons with accuweather.
139 $w['precipIntensity'] = $w['precipIntensity'] / 2.54 / 10;
140
141 if ( $w['precipIntensity'] > 0 ) {
142 $w['precipProbability'] = 100;
143 }
144
145 // sunset sunrise.
146 $sr = new DateTime();
147 $sr->setTimezone( $mtz );
148 $sr->setTimestamp( $weather_array['current']['sys']['sunrise'] );
149 $w['sunrise'] = $sr->format( get_option( 'time_format' ) );
150
151 $ss = new DateTime();
152 $ss->setTimezone( $mtz );
153 $ss->setTimestamp( $weather_array['current']['sys']['sunset'] );
154 $w['sunset'] = $ss->format( get_option( 'time_format' ) );
155
156 // forecast.
157 $j = 0;
158 foreach ( $weather_array['forecast']['list'] as $ff ) {
159
160 if ( substr( $ff['dt_txt'], 11 ) == '00:00:00' ) {
161 ++$j;
162 }
163 $odt = new DateTime();
164 $odt->setTimezone( $mtz );
165
166 if ( substr( $ff['dt_txt'], 11 ) == '00:00:00' ) {
167
168 $w[ 'fc_nt_short_' . $j ] = $ff['weather'][0]['description'];
169 $w[ 'fc_nt_icon_' . $j ] = $ff['weather'][0]['icon'];
170 $w[ 'fc_nt_iconcode_' . $j ] = openweathermap_map_icon( $ff['weather'][0]['icon'], true, $wpf_vars['fonticon'] );
171 $w[ 'fc_nt_id_' . $j ] = $ff['weather'][0]['id'];
172 $w[ 'fc_nt_htemp_' . $j ] = round( $ff['main']['temp_max'], 0 );
173 $w[ 'fc_nt_ltemp_' . $j ] = round( $ff['main']['temp_min'], 0 );
174 $w[ 'fc_nt_windspeed_' . $j ] = $ff['wind']['speed'];
175 $w[ 'fc_nt_winddir_' . $j ] = $ff['wind']['deg'];
176 $w[ 'fc_nt_wgusts_' . $j ] = $ff['wind']['gust'];
177 $w[ 'fc_nt_maxuv_' . $j ] = -1;
178 }
179
180 if ( substr( $ff['dt_txt'], 11 ) == '12:00:00' ) {
181 $w[ 'fc_obsdate_' . $j ] = $ff['dt'] + $odt->getOffset();
182 $w[ 'fc_dt_short_' . $j ] = $ff['weather'][0]['description'];
183 $w[ 'fc_dt_icon_' . $j ] = $ff['weather'][0]['icon'];
184 $w[ 'fc_dt_iconcode_' . $j ] = openweathermap_map_icon( $ff['weather'][0]['icon'], false, $wpf_vars['fonticon'] );
185 $w[ 'fc_dt_id_' . $j ] = $ff['weather'][0]['id'];
186 $w[ 'fc_dt_htemp_' . $j ] = round( $ff['main']['temp_max'], 0 );
187 $w[ 'fc_dt_ltemp_' . $j ] = round( $ff['main']['temp_min'], 0 );
188 $w[ 'fc_dt_windspeed_' . $j ] = $ff['wind']['speed'];
189 $w[ 'fc_dt_winddir_' . $j ] = $ff['wind']['deg'];
190 $w[ 'fc_dt_wgusts_' . $j ] = $ff['wind']['gust'];
191 $w[ 'fc_dt_maxuv_' . $j ] = -1;
192
193 // map precipitation values.
194 // init vars.
195 $w[ 'fc_dt_precipProbability' . $j ] = $ff['pop'] * 100;
196 $w[ 'fc_dt_precipIntensity' . $j ] = 0;
197 $w[ 'fc_dt_precipType' . $j ] = '';
198 // if it rains add rain volume and set precipitation type to rain.
199 if ( isset( $ff['rain'] ) ) {
200 $w[ 'fc_dt_precipIntensity' . $j ] += $ff['rain']['3h'];
201 $w[ 'fc_dt_precipType' . $j ] = 'Rain';
202 }
203 // if it snows add snow volume and set precipitation type to snow.
204 if ( isset( $ff['snow'] ) ) {
205 $w[ 'fc_dt_precipIntensity' . $j ] += $ff['snow']['3h'];
206 $w[ 'fc_dt_precipType' . $j ] = 'Snow';
207 }
208
209 // convert mm to inches for compatibility reasons with accuweather.
210 $w[ 'fc_dt_precipIntensity' . $j ] = $w[ 'fc_dt_precipIntensity' . $j ] / 2.54 / 10;
211 }
212 }
213
214 // fill failure anyway.
215 $w['failure'] = ( isset( $w['failure'] ) ? $w['failure'] : '' );
216
217 return $w;
218 }
219 }
220
221 if ( ! function_exists( 'openweathermap_forecast_data_v2' ) ) {
222 /**
223 * Return the weather data for the cache from OWM
224 *
225 * @param string $wpfcid the Widget ID.
226 * @param string $language_override the iso code of the language to use.
227 */
228 function openweathermap_forecast_data_v2( $wpfcid = 'A', $language_override = null ) {
229
230 $wpf_vars = get_wpf_opts( $wpfcid );
231 if ( ! empty( $language_override ) ) {
232 $wpf_vars['wpf_language'] = $language_override;
233 }
234
235 $w = maybe_unserialize( wpf_get_option( 'wp-forecast-cache' . $wpfcid ) );
236
237 // get translations.
238 global $wpf_lang_dict;
239 $wpf_lang = array();
240 $langfile = WP_PLUGIN_DIR . '/wp-forecast/widgetlang/wp-forecast-' . strtolower( str_replace( '_', '-', $wpf_vars['wpf_language'] ) ) . '.php';
241 if ( file_exists( $langfile ) ) {
242 include $langfile;
243 }
244 $wpf_lang_dict[ $wpf_vars['wpf_language'] ] = $wpf_lang;
245
246 // --------------------------------------------------------------
247 // calc values for current conditions.
248 if ( isset( $w['failure'] ) && '' != $w['failure'] ) {
249 return array( 'failure' => $w['failure'] );
250 }
251
252 $w['servicelink'] = 'https://openweathermap.org/weathermap?basemap=map&cities=true&layer=temperature&lat=' . $w['lat'] . '&lon=' . $w['lon'] . '&zoom=5';
253 $w['copyright'] = '<a href="https://openweathermap.org">&copy; ' . gmdate( 'Y' ) . ' Powered by OpenWeather</a>';
254
255 // next line is for compatibility.
256 $w['acculink'] = $w['servicelink'];
257 $w['location'] = $wpf_vars['locname'];
258 $w['locname'] = $w['location'];
259
260 // handle empty timezone setting.
261 if ( ! isset( $w['timezone'] ) ) {
262 $w['timezone'] = get_option( 'timezone_string' );
263 $tz = new DateTimeZone( $w['timezone'] );
264 } else {
265 $tz_prefix = '+';
266 if ( $w['timezone'] < 0 ) {
267 $tz_prefix = '-';
268 }
269 $tz = new DateTimeZone( $tz_prefix . $w['timezone'] / 36 );
270 }
271 $w['gmtdiff'] = $tz->getOffset( new DateTime() );
272
273 $ct = current_time( 'U' );
274 $ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset.
275
276 $w['blogdate'] = date_i18n( $wpf_vars['fc_date_format'], $ct );
277 $w['blogtime'] = date_i18n( $wpf_vars['fc_time_format'], $ct );
278
279 // get date/time from openweathermap.
280 $ct = $w['time'] + $w['gmtdiff'];
281 $w['accudate'] = date_i18n( $wpf_vars['fc_date_format'], $ct );
282 $w['accutime'] = date_i18n( $wpf_vars['fc_time_format'], $ct );
283
284 $ico = openweathermap_map_icon( $w['weatherid'], false, $wpf_vars['fonticon'] );
285 $iconfile = find_icon( $ico );
286 $w['icon'] = 'icons/' . $iconfile;
287 $w['iconcode'] = $ico;
288 $w['shorttext'] = wpf__( openweathermap_wcode2text( $w['weatherid'] ), $wpf_vars['wpf_language'] );
289
290 $w['temperature'] = $w['temperature'] . '&deg;' . $w['un_temp'];
291 $w['realfeel'] = $w['realfeel'] . '&deg;' . $w['un_temp'];
292 $w['humidity'] = round( $w['humidity'], 0 );
293
294 // workaround different pressure values returned by accuweather.
295 $press = round( $w['pressure'], 0 );
296 if ( strlen( $press ) == 3 && substr( $press, 0, 1 ) == '1' ) {
297 $press = $press * 10;
298 }
299 $w['pressure'] = $press . ' ' . $w['un_pres'];
300 $w['humidity'] = round( $w['humidity'], 0 );
301 $w['windspeed'] = windstr( $wpf_vars['metric'], $w['windspeed'], $wpf_vars['windunit'] );
302 $w['winddir'] = translate_winddir_degree( $w['winddirection'], $wpf_vars['wpf_language'] );
303 $w['winddir_orig'] = str_replace( 'O', 'E', $w['winddir'] );
304 $w['windgusts'] = windstr( $wpf_vars['metric'], $w['wgusts'], $wpf_vars['windunit'] );
305
306 // calc values for forecast.
307 for ( $i = 1; $i < 6; $i++ ) {
308 if ( ! isset( $w[ 'fc_obsdate_' . $i ] ) ) {
309 continue;
310 }
311 // daytime forecast.
312 $w[ 'fc_obsdate_' . $i ] = date_i18n( $wpf_vars['fc_date_format'], $w[ 'fc_obsdate_' . $i ] );
313
314 $ico = openweathermap_map_icon( $w[ 'fc_dt_id_' . $i ], false, $wpf_vars['fonticon'] );
315 $iconfile = find_icon( $ico );
316 $w[ 'fc_dt_icon_' . $i ] = 'icons/' . $iconfile;
317 $w[ 'fc_dt_iconcode_' . $i ] = $ico;
318 $w[ 'fc_dt_desc_' . $i ] = wpf__( openweathermap_wcode2text( $w[ 'fc_dt_id_' . $i ] ), $wpf_vars['wpf_language'] );
319 $w[ 'fc_dt_htemp_' . $i ] = $w[ 'fc_dt_htemp_' . $i ] . '&deg;' . $w['un_temp'];
320 $wstr = windstr( $wpf_vars['metric'], $w[ 'fc_dt_windspeed_' . $i ], $wpf_vars['windunit'] );
321 $w[ 'fc_dt_windspeed_' . $i ] = $wstr;
322 $w[ 'fc_dt_winddir_' . $i ] = translate_winddir_degree( $w[ 'fc_dt_winddir_' . $i ], $wpf_vars['wpf_language'] );
323 $w[ 'fc_dt_winddir_orig_' . $i ] = str_replace( 'O', 'E', $w[ 'fc_dt_winddir_' . $i ] );
324 $w[ 'fc_dt_wgusts_' . $i ] = windstr( $wpf_vars['metric'], $w[ 'fc_dt_wgusts_' . $i ], $wpf_vars['windunit'] );
325 $w[ 'fc_dt_maxuv_' . $i ] = $w[ 'fc_dt_maxuv_' . $i ];
326
327 // nighttime forecast.
328 $ico = openweathermap_map_icon( $w[ 'fc_nt_id_' . $i ], true, $wpf_vars['fonticon'] );
329 $iconfile = find_icon( $ico );
330 $w[ 'fc_nt_icon_' . $i ] = 'icons/' . $iconfile;
331 $w[ 'fc_nt_iconcode_' . $i ] = $ico;
332 $w[ 'fc_nt_desc_' . $i ] = wpf__( openweathermap_wcode2text( $w[ 'fc_nt_id_' . $i ] ), $wpf_vars['wpf_language'] );
333 $w[ 'fc_nt_ltemp_' . $i ] = $w[ 'fc_nt_ltemp_' . $i ] . '&deg;' . $w['un_temp'];
334 $wstr = windstr( $wpf_vars['metric'], $w[ 'fc_nt_windspeed_' . $i ], $wpf_vars['windunit'] );
335 $w[ 'fc_nt_windspeed_' . $i ] = $wstr;
336 $w[ 'fc_nt_winddir_' . $i ] = translate_winddir_degree( $w[ 'fc_nt_winddir_' . $i ], $wpf_vars['wpf_language'] );
337 $w[ 'fc_nt_winddir_orig_' . $i ] = str_replace( 'O', 'E', $w[ 'fc_nt_winddir_' . $i ] );
338 $w[ 'fc_nt_wgusts_' . $i ] = windstr( $wpf_vars['metric'], $w[ 'fc_nt_wgusts_' . $i ], $wpf_vars['windunit'] );
339 $w[ 'fc_nt_maxuv_' . $i ] = $w[ 'fc_nt_maxuv_' . $i ];
340 }
341
342 // add hook for possible individual changes.
343 $w = apply_filters( 'wp-forecast-openweathermap-data', $w );
344
345 return $w;
346 }
347 }
348