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