PluginProbe
Event Post / 6.1.2
Event Post v6.1.2
6.1.2 6.1.1 6.1.0 6.0.1 6.0.0 5.12.0 trunk 3.9 4.0 4.2 4.3 4.4 4.5 5.0 5.1 5.10.0 5.10.1 5.10.2 5.10.3 5.10.4 5.11.0 5.11.1 5.2 5.5 5.6 All 47 releases
← All changes | inc/openweathermap.php +114 -83 5.5 → 6.1.2 View file →
@@ -1,11 +1,19 @@
1 1 <?php
2 2 /**
3 - *
3 + * Weather support
4 + *
4 5 * @package event-post
6 + * @version 6.1.2
7 + * @since 4.3.0
5 8 */
6 -$EventPostWeather = new EventPostWeather();
7 9
10 +namespace EventPost;
11 +
12 +if ( ! defined( 'ABSPATH' ) ) exit;
13 +
14 +$EventPostWeather = new Weather();
15 +
8 16 /**
9 17 * Provides weather support thanks to OpenWeatherMap
10 18 * http://openweathermap.org
11 19 *
@@ -14,11 +22,10 @@
14 22 *
15 23 *
16 24 * Get an API key
17 25 * http://openweathermap.org/appid#get
18 - *
19 26 */
20 -class EventPostWeather {
27 +class Weather {
21 28
22 29 var $META_WEATHER;
23 30
24 31 var $api_key;
@@ -36,9 +43,9 @@
36 43
37 44 /**
38 45 * PHP4 constructor
39 46 */
40 - function EventPostWeather() {
47 + function Weather() {
41 48 $this->__construct();
42 49 }
43 50
44 51 /**
@@ -43,9 +50,9 @@
43 50
44 51 /**
45 52 * Only for localization
46 53 *
47 - * available values:
54 + * Available values:
48 55 *
49 56 * - clear sky
50 57 * - few clouds
51 58 * - scattered clouds
@@ -69,9 +76,10 @@
69 76
70 77 }
71 78
72 79 /**
73 - *
80 + * Get settings
81 + *
74 82 * @param array reference &$ep_settings
75 83 * @param boolean reference &$reg_settings
76 84 */
77 85 function get_settings(&$ep_settings, &$reg_settings) {
@@ -89,27 +97,29 @@
89 97 }
90 98 }
91 99
92 100 /**
93 - *
101 + * Settings form
102 + *
94 103 * @param type $ep_settings
95 104 */
96 105 function settings_form($ep_settings) {
97 106 ?>
98 - <h2><?php _e('Weather', 'event-post'); ?></h2>
99 - <p class="description"><?php printf(__('Provided thanks to %s', 'event-post'), '<a href="http://openweathermap.org" target="_blank">openweathermap.org</a>'); ?></p>
107 + <h2><?php esc_html_e('Weather', 'event-post'); ?></h2>
108 + <?php // Translators: %s is a link to openweathermap.org ?>
109 + <p class="description"><?php printf(esc_html(__('Provided thanks to %s', 'event-post')), '<a href="http://openweathermap.org" target="_blank">openweathermap.org</a>'); ?></p>
100 110 <table class="form-table" id="eventpost-settings-table-weather">
101 111 <tbody>
102 112 <tr>
103 113 <th>
104 - <?php _e('Enable weather', 'event-post') ?>
114 + <?php esc_html_e('Enable weather', 'event-post') ?>
105 115 </th>
106 116 <td>
107 117 <label for="weather_enabled">
108 118 <input type="checkbox" name="ep_settings[weather_enabled]" id="weather_enabled" <?php if ($ep_settings['weather_enabled'] == '1') {
109 - echo'checked';
119 + echo esc_attr('checked');
110 120 } ?> value="1">
111 - <?php _e('Enable weather feature', 'event-post') ?>
121 + <?php esc_html_e('Enable weather feature', 'event-post') ?>
112 122 </label>
113 123 </td>
114 124 </tr>
115 125 <tr>
@@ -114,32 +124,33 @@
114 124 </tr>
115 125 <tr>
116 126 <th>
117 127 <label for="weather_api_key">
118 - <?php _e('API key', 'event-post') ?>
128 + <?php esc_html_e('API key', 'event-post') ?>
119 129 </label>
120 130 </th>
121 131 <td>
122 - <input type="text" name="ep_settings[weather_api_key]" id="weather_api_key" value="<?php echo $ep_settings['weather_api_key']; ?>" size="40">
123 - <p class="description"><?php printf(__('Get a free API key at: %s', 'event-post'), '<a href="http://openweathermap.org/appid#get" target="_blank">openweathermap.org/appid#get</a>'); ?></p>
132 + <input type="text" name="ep_settings[weather_api_key]" id="weather_api_key" value="<?php echo esc_attr($ep_settings['weather_api_key']); ?>" size="40">
133 + <?php // Translators: %s is a link to openweathermap.org/appid#get ?>
134 + <p class="description"><?php printf(esc_html(__('Get a free API key at: %s', 'event-post')), '<a href="http://openweathermap.org/appid#get" target="_blank">openweathermap.org/appid#get</a>'); ?></p>
124 135 </td>
125 136 </tr>
126 137 <tr>
127 138 <th>
128 139 <label for="weather_units">
129 - <?php _e('Units', 'event-post') ?>
140 + <?php esc_html_e('Units', 'event-post') ?>
130 141 </label>
131 142 </th>
132 143 <td>
133 144 <select name="ep_settings[weather_units]" id="weather_units">
134 145 <option value="standard" <?php selected($ep_settings['weather_units'], 'standard', true);?>>
135 - <?php _e('Standard (Fahrenheit)', 'event-post') ?>
146 + <?php esc_html_e('Standard (Fahrenheit)', 'event-post') ?>
136 147 </option>
137 148 <option value="metric" <?php selected($ep_settings['weather_units'], 'metric', true);?>>
138 - <?php _e('Metric (Celsius)', 'event-post') ?>
149 + <?php esc_html_e('Metric (Celsius)', 'event-post') ?>
139 150 </option>
140 151 <option value="imperial" <?php selected($ep_settings['weather_units'], 'imperial', true);?>>
141 - <?php _e('Imperial (Kelvin)', 'event-post') ?>
152 + <?php esc_html_e('Imperial (Kelvin)', 'event-post') ?>
142 153 </option>
143 154 </select>
144 155 </td>
145 156 </tr>
@@ -148,10 +159,12 @@
148 159 <?php
149 160 }
150 161
151 162 /**
152 - *
163 + * Initialization of weather support
164 + *
153 165 * @param object $EP
166 + *
154 167 * @return void
155 168 */
156 169 function init($EP) {
157 170 // Ensure OpenWeatherMap is required and available.
@@ -181,10 +194,12 @@
181 194
182 195 }
183 196
184 197 /**
185 - *
198 + * Alters parameters
199 + *
186 200 * @param array $params
201 + *
187 202 * @return array
188 203 */
189 204 function params($params = array()) {
190 205 $params['weather'] = true;
@@ -191,10 +206,12 @@
191 206 return $params;
192 207 }
193 208
194 209 /**
195 - *
210 + * Alters an event object
211 + *
196 212 * @param WP_Post $event
213 + *
197 214 * @return WP_Post
198 215 */
199 216 function retreive($event) {
200 217 $event->weather = get_post_meta($event->ID, $this->META_WEATHER, true);
@@ -201,10 +218,12 @@
201 218 return $event;
202 219 }
203 220
204 221 /**
205 - *
222 + * Alters schema entities
223 + *
206 224 * @param array $attr
225 + *
207 226 * @return array
208 227 */
209 228 function scheme_entities($attr = array()) {
210 229 array_push($attr, '%event_weather%');
@@ -211,10 +230,12 @@
211 230 return $attr;
212 231 }
213 232
214 233 /**
215 - *
234 + * Alters schema values
235 + *
216 236 * @param array $values
237 + *
217 238 * @return array
218 239 */
219 240 function scheme_values($values = array(), $post = null) {
220 241 array_push($values, $this->get_weather($post));
@@ -221,10 +242,12 @@
221 242 return $values;
222 243 }
223 244
224 245 /**
225 - *
246 + * Alters default schema
247 + *
226 248 * @param array $schema
249 + *
227 250 * @return array
228 251 */
229 252 function default_shema($schema) {
230 253 $schema['item'] = str_replace('</%child%>', "%event_weather%\n</%child%>", $schema['item']);
@@ -234,18 +257,17 @@
234 257 function get_single($event_datas, $post = null) {
235 258 return $event_datas . $this->get_weather($post);
236 259 }
237 260
238 - /**
239 - * From here, methods intends to get datas
240 - */
261 + //From here, methods intends to get datas
241 262
242 263 function get_weather_icons($weather){
243 264 $string = '';
244 265 foreach((array) $weather as $item){
245 - $text = ucfirst(__(strtolower($item->description), 'event-post'));
266 + $text = ucfirst(__(strtolower($item->description), 'event-post')); // phpcs:ignore WordPress.WP.I18n
246 267 $string.='<span class="eventpost-weather-'.str_replace('-', '', strtolower($item->description)).' eventpost-weather-'.$item->icon.'">'
247 - . '<img src="'. plugins_url('../img/weather/'.$this->theme.'/'.$item->icon.'.png', __FILE__).'" alt="'.sprintf('%s icon', $text).'">'
268 + // Translators: %s is the weather description
269 + . '<img src="'. plugins_url('../img/weather/'.$this->theme.'/'.$item->icon.'.png', __FILE__).'" alt="'.sprintf('%s icon', $text).'">'
248 270 . '<em class="eventpost-weather-text">'.$text.'</em>'
249 271 . '</span>';
250 272 }
251 273 return $string;
@@ -250,10 +272,12 @@
250 272 }
251 273 return $string;
252 274 }
253 275 /**
254 - *
276 + * Get weather item
277 + *
255 278 * @param object $item
279 + *
256 280 * @return string
257 281 */
258 282 function get_weather_item($item){
259 283 $string = '';
@@ -265,10 +289,12 @@
265 289 return $string;
266 290 }
267 291
268 292 /**
269 - *
293 + * Get weather
294 + *
270 295 * @param type $post
296 + *
271 297 * @return string
272 298 */
273 299 function get_weather($post = null, $echo=false) {
274 300 global $EventPost;
@@ -307,16 +333,18 @@
307 333 }
308 334 break;
309 335 }
310 336 if($echo){
311 - echo $string;
337 + echo wp_kses($string, EventPost()->kses_tags);
312 338 }
313 339 return $string;
314 340 }
315 341
316 342 /**
317 - *
343 + * Get weather datas
344 + *
318 345 * @param type $event
346 + *
319 347 * @return type
320 348 */
321 349 function get_weather_datas($event){
322 350 if (!$event->lat || !$event->long || !is_numeric($event->time_start) || !is_numeric($event->time_end)) {
@@ -359,10 +387,12 @@
359 387
360 388
361 389 /**
362 390 * Generates the URL to call the API
391 + *
363 392 * @param type $method
364 393 * @param type $params
394 + *
365 395 * @return type
366 396 */
367 397 function get_url($method = 'weather', $params = array()) {
368 398 $params['APPID']= $this->api_key;
@@ -370,26 +400,27 @@
370 400 return 'http://api.openweathermap.org/data/2.5/' . $method . '?' . http_build_query($params);
371 401 }
372 402
373 403 /**
374 - * ### current
404 + * Current weather
375 405 * http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&APPID=XXXX
376 406 *
377 - * return:
407 + * Return:
378 408 *
379 - {"coord":{"lon":139,"lat":35},
380 - "sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
381 - "weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
382 - "main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
383 - "wind":{"speed":7.31,"deg":187.002},
384 - "rain":{"3h":0},
385 - "clouds":{"all":92},
386 - "dt":1369824698,
387 - "id":1851632,
388 - "name":"Shuzenji",
389 - "cod":200}
409 + * {"coord":{"lon":139,"lat":35},
410 + * "sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
411 + * "weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
412 + * "main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
413 + * "wind":{"speed":7.31,"deg":187.002},
414 + * "rain":{"3h":0},
415 + * "clouds":{"all":92},
416 + * "dt":1369824698,
417 + * "id":1851632,
418 + * "name":"Shuzenji",
419 + * "cod":200}
390 420 *
391 421 * @param type $event
422 + *
392 423 * @return object
393 424 */
394 425 function get_current($event){
395 426 if (!$event->lat || !$event->long) {
@@ -405,38 +436,39 @@
405 436 ));
406 437 }
407 438
408 439 /**
409 - * ### forecast
440 + * Forecast
410 441 * api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&APPID=XXXX
411 442 *
412 - * return:
443 + * Return:
413 444 *
414 - {"city":{"id":1851632,"name":"Shuzenji",
415 - "coord":{"lon":138.933334,"lat":34.966671},
416 - "country":"JP",
417 - "cod":"200",
418 - "message":0.0045,
419 - "cnt":38,
420 - "list":[{
421 - "dt":1406106000,
422 - "main":{
423 - "temp":298.77,
424 - "temp_min":298.77,
425 - "temp_max":298.774,
426 - "pressure":1005.93,
427 - "sea_level":1018.18,
428 - "grnd_level":1005.93,
429 - "humidity":87
430 - "temp_kf":0.26},
431 - "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
432 - "clouds":{"all":88},
433 - "wind":{"speed":5.71,"deg":229.501},
434 - "sys":{"pod":"d"},
435 - "dt_txt":"2014-07-23 09:00:00"}
436 - ]}
445 + * {"city":{"id":1851632,"name":"Shuzenji",
446 + * "coord":{"lon":138.933334,"lat":34.966671},
447 + * "country":"JP",
448 + * "cod":"200",
449 + * "message":0.0045,
450 + * "cnt":38,
451 + * "list":[{
452 + * "dt":1406106000,
453 + * "main":{
454 + * "temp":298.77,
455 + * "temp_min":298.77,
456 + * "temp_max":298.774,
457 + * "pressure":1005.93,
458 + * "sea_level":1018.18,
459 + * "grnd_level":1005.93,
460 + * "humidity":87
461 + * "temp_kf":0.26},
462 + * "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
463 + * "clouds":{"all":88},
464 + * "wind":{"speed":5.71,"deg":229.501},
465 + * "sys":{"pod":"d"},
466 + * "dt_txt":"2014-07-23 09:00:00"}
467 + * ]}
437 468 *
438 469 * @param type $event
470 + *
439 471 * @return type
440 472 */
441 473 function get_forecast($event){
442 474 if (!$event->lat || !$event->long) {
@@ -461,21 +493,20 @@
461 493 * type type of the call, keep this parameter in the API call as 'hour'
462 494 * start start date (unix time, UTC time zone), e.g. start=1369728000
463 495 * end end date (unix time, UTC time zone), e.g. end=1369789200
464 496 * cnt amount of returned data (one per hour, can be used instead of 'end') *
465 - *
466 497 * return:
467 - {"message":"","cod":"200","type":"tick","station_id":39419,"cnt":30,
468 - "list":[
469 - {"dt":1345291920,
470 - "main":{"temp":291.55,"humidity":95,"pressure":1009.3},
471 - "wind":{"speed":0,"gust":0.3},
472 - "rain":{"1h":0.6,"today":2.7},
473 - "calc":{"dewpoint":17.6} }
474 - ]}
498 + * {"message":"","cod":"200","type":"tick","station_id":39419,"cnt":30,
499 + * "list":[
500 + * {"dt":1345291920,
501 + * "main":{"temp":291.55,"humidity":95,"pressure":1009.3},
502 + * "wind":{"speed":0,"gust":0.3},
503 + * "rain":{"1h":0.6,"today":2.7},
504 + * "calc":{"dewpoint":17.6} }
505 + * ]}
475 506 *
476 - *
477 507 * @param type $event
508 + *
478 509 * @return type
479 510 */
480 511 function get_history($event) {
481 512 if (!$event->start || !$event->end || !$event->lat || !$event->long) {